Просмотр исходного кода

Merge pull request #75 from dfelder/master

compile current file if top level document #42
Henry Oswald 12 лет назад
Родитель
Сommit
22c467c231

+ 8 - 3
services/web/app/coffee/Features/Compile/ClsiManager.coffee

@@ -8,8 +8,8 @@ logger = require "logger-sharelatex"
 url = require("url")
 url = require("url")
 
 
 module.exports = ClsiManager =
 module.exports = ClsiManager =
-	sendRequest: (project_id, callback = (error, success) ->) ->
-		ClsiManager._buildRequest project_id, (error, req) ->
+	sendRequest: (project_id, settingsOverride = {}, callback = (error, success) ->) ->
+		ClsiManager._buildRequest project_id, settingsOverride, (error, req) ->
 			return callback(error) if error?
 			return callback(error) if error?
 			logger.log project_id: project_id, "sending compile to CLSI"
 			logger.log project_id: project_id, "sending compile to CLSI"
 			ClsiManager._postToClsi project_id, req, (error, response) ->
 			ClsiManager._postToClsi project_id, req, (error, response) ->
@@ -52,7 +52,7 @@ module.exports = ClsiManager =
 		return outputFiles
 		return outputFiles
 
 
 	VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
 	VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
-	_buildRequest: (project_id, callback = (error, request) ->) ->
+	_buildRequest: (project_id, settingsOverride={}, callback = (error, request) ->) ->
 		Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
 		Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
 			return callback(error) if error?
 			return callback(error) if error?
 			return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
 			return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
@@ -67,6 +67,7 @@ module.exports = ClsiManager =
 
 
 					resources = []
 					resources = []
 					rootResourcePath = null
 					rootResourcePath = null
+					rootResourcePathOverride = null
 
 
 					for path, doc of docs
 					for path, doc of docs
 						path = path.replace(/^\//, "") # Remove leading /
 						path = path.replace(/^\//, "") # Remove leading /
@@ -75,6 +76,10 @@ module.exports = ClsiManager =
 							content: doc.lines.join("\n")
 							content: doc.lines.join("\n")
 						if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
 						if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
 							rootResourcePath = path
 							rootResourcePath = path
+						if settingsOverride.rootDoc_id? and doc._id.toString() == settingsOverride.rootDoc_id.toString()
+							rootResourcePathOverride = path
+
+					rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
 
 
 					for path, file of files
 					for path, file of files
 						path = path.replace(/^\//, "") # Remove leading /
 						path = path.replace(/^\//, "") # Remove leading /

+ 3 - 1
services/web/app/coffee/Features/Compile/CompileController.coffee

@@ -11,9 +11,11 @@ module.exports = CompileController =
 	compile: (req, res, next = (error) ->) ->
 	compile: (req, res, next = (error) ->) ->
 		project_id = req.params.Project_id
 		project_id = req.params.Project_id
 		isAutoCompile = !!req.query?.auto_compile
 		isAutoCompile = !!req.query?.auto_compile
+		settingsOverride = req.body?.settingsOverride ? {};
+		logger.log "root doc overriden" if settingsOverride.rootDoc_id?
 		AuthenticationController.getLoggedInUserId req, (error, user_id) ->
 		AuthenticationController.getLoggedInUserId req, (error, user_id) ->
 			return next(error) if error?
 			return next(error) if error?
-			CompileManager.compile project_id, user_id, { isAutoCompile }, (error, status, outputFiles) ->
+			CompileManager.compile project_id, user_id, { isAutoCompile, settingsOverride }, (error, status, outputFiles) ->
 				return next(error) if error?
 				return next(error) if error?
 				res.contentType("application/json")
 				res.contentType("application/json")
 				res.send 200, JSON.stringify {
 				res.send 200, JSON.stringify {

+ 2 - 2
services/web/app/coffee/Features/Compile/CompileManager.coffee

@@ -25,12 +25,12 @@ module.exports = CompileManager =
 				return callback(error) if error?
 				return callback(error) if error?
 				if recentlyCompiled
 				if recentlyCompiled
 					return callback new Error("project was recently compiled so not continuing")
 					return callback new Error("project was recently compiled so not continuing")
-
+				
 				CompileManager._ensureRootDocumentIsSet project_id, (error) ->
 				CompileManager._ensureRootDocumentIsSet project_id, (error) ->
 					return callback(error) if error?
 					return callback(error) if error?
 					DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
 					DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
 						return callback(error) if error?
 						return callback(error) if error?
-						ClsiManager.sendRequest project_id, (error, status, outputFiles) ->
+						ClsiManager.sendRequest project_id, opt.settingsOverride, (error, status, outputFiles) ->
 							return callback(error) if error?
 							return callback(error) if error?
 							logger.log files: outputFiles, "output files"
 							logger.log files: outputFiles, "output files"
 							callback(null, status, outputFiles)
 							callback(null, status, outputFiles)

+ 6 - 1
services/web/public/coffee/pdf/CompiledView.coffee

@@ -204,7 +204,12 @@ define [
 
 
 		recompilePdf: () ->
 		recompilePdf: () ->
 			@options.manager.trigger "compile:pdf"
 			@options.manager.trigger "compile:pdf"
-			@options.manager.refreshPdf()
+			rootDocOverride_id = null
+			for line in @ide.editor.getLines()
+				match = line.match /(.*)\\documentclass/
+				if match and !match[1].match /%/
+					rootDocOverride_id = @ide.editor.getCurrentDocId()
+			@options.manager.refreshPdf {rootDocOverride_id}
 
 
 		toggleFlatViewButton: () -> @$("#flatViewButton").button("toggle")
 		toggleFlatViewButton: () -> @$("#flatViewButton").button("toggle")
 		toggleSplitViewButton: () -> @$("#splitViewButton").button("toggle")
 		toggleSplitViewButton: () -> @$("#splitViewButton").button("toggle")

+ 6 - 3
services/web/public/coffee/pdf/PdfManager.coffee

@@ -143,7 +143,7 @@ define [
 				@view.onCompiling()
 				@view.onCompiling()
 				@syncButtonsView?.hide()
 				@syncButtonsView?.hide()
 				@compiling = true
 				@compiling = true
-				@_doCompile opts.isAutoCompile, (error, status, outputFiles) =>
+				@_doCompile opts, (error, status, outputFiles) =>
 					@compiling = false
 					@compiling = false
 					doneCompiling()
 					doneCompiling()
 
 
@@ -172,16 +172,19 @@ define [
 					if outputFiles?
 					if outputFiles?
 						@view.showOutputFileDownloadLinks(outputFiles)
 						@view.showOutputFileDownloadLinks(outputFiles)
 
 
-		_doCompile: (isAutoCompile, callback = (error, status, outputFiles) ->) ->
+		_doCompile: (opts, callback = (error, status, outputFiles) ->) ->
 			url = "/project/#{@ide.project_id}/compile"
 			url = "/project/#{@ide.project_id}/compile"
-			if isAutoCompile
+			if opts.isAutoCompile
 				url += "?auto_compile=true"
 				url += "?auto_compile=true"
 			$.ajax(
 			$.ajax(
 				url: url
 				url: url
 				type: "POST"
 				type: "POST"
 				headers:
 				headers:
 					"X-CSRF-Token": window.csrfToken
 					"X-CSRF-Token": window.csrfToken
+				contentType: "application/json; charset=utf-8"
 				dataType: 'json'
 				dataType: 'json'
+				data: JSON.stringify settingsOverride: 
+					rootDoc_id: opts.rootDocOverride_id ? null
 				success: (body, status, response) ->
 				success: (body, status, response) ->
 					callback null, body.status, body.outputFiles
 					callback null, body.status, body.outputFiles
 				error: (error) ->
 				error: (error) ->

+ 28 - 6
services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee

@@ -24,7 +24,7 @@ describe "ClsiManager", ->
 
 
 	describe "sendRequest", ->
 	describe "sendRequest", ->
 		beforeEach ->
 		beforeEach ->
-			@ClsiManager._buildRequest = sinon.stub().callsArgWith(1, null, @request = "mock-request")
+			@ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @request = "mock-request")
 
 
 		describe "with a successful compile", ->
 		describe "with a successful compile", ->
 			beforeEach ->
 			beforeEach ->
@@ -39,7 +39,7 @@ describe "ClsiManager", ->
 							type: "log"
 							type: "log"
 						}]
 						}]
 				})
 				})
-				@ClsiManager.sendRequest @project_id, @callback
+				@ClsiManager.sendRequest @project_id, {}, @callback
 
 
 			it "should build the request", ->
 			it "should build the request", ->
 				@ClsiManager._buildRequest
 				@ClsiManager._buildRequest
@@ -67,7 +67,7 @@ describe "ClsiManager", ->
 					compile:
 					compile:
 						status: @status = "failure"
 						status: @status = "failure"
 				})
 				})
-				@ClsiManager.sendRequest @project_id, @callback
+				@ClsiManager.sendRequest @project_id, {}, @callback
 			
 			
 			it "should call the callback with a failure statue", ->
 			it "should call the callback with a failure statue", ->
 				@callback.calledWith(null, @status).should.equal true
 				@callback.calledWith(null, @status).should.equal true
@@ -121,7 +121,7 @@ describe "ClsiManager", ->
 
 
 		describe "with a valid project", ->
 		describe "with a valid project", ->
 			beforeEach (done) ->
 			beforeEach (done) ->
-				@ClsiManager._buildRequest @project_id, (error, request) =>
+				@ClsiManager._buildRequest @project_id, null, (error, request) =>
 					@request = request
 					@request = request
 					done()
 					done()
 
 
@@ -159,10 +159,32 @@ describe "ClsiManager", ->
 						}]
 						}]
 				)
 				)
 
 
+
+		describe "when root doc override is valid", ->
+			beforeEach (done) ->
+				@ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
+					@request = request
+					done()
+
+			it "should change root path", ->
+				@request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
+
+
+		describe "when root doc override is invalid", ->
+			beforeEach (done) ->
+				@ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
+					@request = request
+					done()
+
+			it "should fallback to default root doc", ->
+				@request.compile.rootResourcePath.should.equal "main.tex"
+
+
+
 		describe "when the project has an invalid compiler", ->
 		describe "when the project has an invalid compiler", ->
 			beforeEach (done) ->
 			beforeEach (done) ->
 				@project.compiler = "context"
 				@project.compiler = "context"
-				@ClsiManager._buildRequest @project, (error, request) =>
+				@ClsiManager._buildRequest @project, null, (error, request) =>
 					@request = request
 					@request = request
 					done()
 					done()
 
 
@@ -172,7 +194,7 @@ describe "ClsiManager", ->
 		describe "when there is no valid root document", ->
 		describe "when there is no valid root document", ->
 			beforeEach (done) ->
 			beforeEach (done) ->
 				@project.rootDoc_id = "not-valid"
 				@project.rootDoc_id = "not-valid"
-				@ClsiManager._buildRequest @project, (@error, @request) =>
+				@ClsiManager._buildRequest @project, null, (@error, @request) =>
 					done()
 					done()
 			
 			
 			it "should return an error", ->
 			it "should return an error", ->

+ 2 - 2
services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee

@@ -44,7 +44,7 @@ describe "CompileController", ->
 
 
 			it "should do the compile without the auto compile flag", ->
 			it "should do the compile without the auto compile flag", ->
 				@CompileManager.compile
 				@CompileManager.compile
-					.calledWith(@project_id, @user_id, { isAutoCompile: false })
+					.calledWith(@project_id, @user_id, { isAutoCompile: false, settingsOverride:{} })
 					.should.equal true
 					.should.equal true
 
 
 			it "should set the content-type of the response to application/json", ->
 			it "should set the content-type of the response to application/json", ->
@@ -71,7 +71,7 @@ describe "CompileController", ->
 
 
 			it "should do the compile with the auto compile flag", ->
 			it "should do the compile with the auto compile flag", ->
 				@CompileManager.compile
 				@CompileManager.compile
-					.calledWith(@project_id, @user_id, { isAutoCompile: true })
+					.calledWith(@project_id, @user_id, { isAutoCompile: true, settingsOverride:{} })
 					.should.equal true
 					.should.equal true
 
 
 	describe "downloadPdf", ->
 	describe "downloadPdf", ->

+ 1 - 1
services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee

@@ -36,7 +36,7 @@ describe "CompileManager", ->
 			@CompileManager._checkIfRecentlyCompiled = sinon.stub().callsArgWith(2, null, false)
 			@CompileManager._checkIfRecentlyCompiled = sinon.stub().callsArgWith(2, null, false)
 			@CompileManager._ensureRootDocumentIsSet = sinon.stub().callsArgWith(1, null)
 			@CompileManager._ensureRootDocumentIsSet = sinon.stub().callsArgWith(1, null)
 			@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
 			@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
-			@ClsiManager.sendRequest = sinon.stub().callsArgWith(1, null, @status = "mock-status")
+			@ClsiManager.sendRequest = sinon.stub().callsArgWith(2, null, @status = "mock-status")
 
 
 		describe "succesfully", ->
 		describe "succesfully", ->
 			beforeEach ->
 			beforeEach ->