Add in a synctex end point
This commit is contained in:
@@ -38,3 +38,27 @@ module.exports = CompileController =
|
||||
ProjectPersistenceManager.clearProject req.params.project_id, (error) ->
|
||||
return next(error) if error?
|
||||
res.send 204 # No content
|
||||
|
||||
syncFromCode: (req, res, next = (error) ->) ->
|
||||
file = req.query.file
|
||||
line = parseInt(req.query.line, 10)
|
||||
column = parseInt(req.query.column, 10)
|
||||
project_id = req.params.project_id
|
||||
|
||||
CompileManager.syncFromCode project_id, file, line, column, (error, pdfPositions) ->
|
||||
return next(error) if error?
|
||||
res.send JSON.stringify {
|
||||
pdf: pdfPositions
|
||||
}
|
||||
|
||||
syncFromPdf: (req, res, next = (error) ->) ->
|
||||
page = parseInt(req.query.page, 10)
|
||||
h = parseFloat(req.query.h)
|
||||
v = parseFloat(req.query.v)
|
||||
project_id = req.params.project_id
|
||||
|
||||
CompileManager.syncFromPdf project_id, page, h, v, (error, codePositions) ->
|
||||
return next(error) if error?
|
||||
res.send JSON.stringify {
|
||||
code: codePositions
|
||||
}
|
||||
|
||||
@@ -52,3 +52,66 @@ module.exports = CompileManager =
|
||||
return callback(null)
|
||||
else
|
||||
return callback(new Error("rm -r #{compileDir} failed: #{stderr}"))
|
||||
|
||||
syncFromCode: (project_id, file_name, line, column, callback = (error, pdfPositions) ->) ->
|
||||
# If LaTeX was run in a virtual environment, the file path that synctex expects
|
||||
# might not match the file path on the host. The .synctex.gz file however, will be accessed
|
||||
# wherever it is on the host.
|
||||
base_dir = Settings.path.synctexBaseDir(project_id)
|
||||
file_path = Path.join(base_dir, file_name)
|
||||
synctex_path = Path.join(Settings.path.compilesDir, project_id, "output.pdf")
|
||||
CompileManager._runSynctex ["code", synctex_path, file_path, line, column], (error, stdout) ->
|
||||
return callback(error) if error?
|
||||
logger.log project_id: project_id, file_name: file_name, line: line, column: column, stdout: stdout, "synctex code output"
|
||||
callback null, CompileManager._parseSynctexFromCodeOutput(stdout)
|
||||
|
||||
syncFromPdf: (project_id, page, h, v, callback = (error, filePositions) ->) ->
|
||||
base_dir = Settings.path.synctexBaseDir(project_id)
|
||||
synctex_path = Path.join(Settings.path.compilesDir, project_id, "output.pdf")
|
||||
CompileManager._runSynctex ["pdf", synctex_path, page, h, v], (error, stdout) ->
|
||||
return callback(error) if error?
|
||||
logger.log project_id: project_id, page: page, h: h, v:v, stdout: stdout, "synctex pdf output"
|
||||
callback null, CompileManager._parseSynctexFromPdfOutput(stdout, base_dir)
|
||||
|
||||
_runSynctex: (args, callback = (error, stdout) ->) ->
|
||||
bin_path = Path.resolve(__dirname + "/../../bin/synctex")
|
||||
proc = child_process.spawn bin_path, args
|
||||
proc.on "error", callback
|
||||
|
||||
stdout = ""
|
||||
proc.stdout.on "data", (chunk) -> stdout += chunk.toString()
|
||||
stderr = ""
|
||||
proc.stderr.on "data", (chunk) -> stderr += chunk.toString()
|
||||
|
||||
proc.on "close", (code) ->
|
||||
if code == 0
|
||||
return callback(null, stdout)
|
||||
else
|
||||
return callback(new Error("synctex failed: #{stderr}"))
|
||||
|
||||
_parseSynctexFromCodeOutput: (output) ->
|
||||
results = []
|
||||
for line in output.split("\n")
|
||||
[node, page, h, v, width, height] = line.split("\t")
|
||||
if node == "NODE"
|
||||
results.push {
|
||||
page: parseInt(page, 10)
|
||||
h: parseFloat(h)
|
||||
v: parseFloat(v)
|
||||
height: parseFloat(height)
|
||||
width: parseFloat(width)
|
||||
}
|
||||
return results
|
||||
|
||||
_parseSynctexFromPdfOutput: (output, base_dir) ->
|
||||
results = []
|
||||
for line in output.split("\n")
|
||||
[node, file_path, line, column] = line.split("\t")
|
||||
if node == "NODE"
|
||||
file = file_path.slice(base_dir.length + 1)
|
||||
results.push {
|
||||
file: file
|
||||
line: parseInt(line, 10)
|
||||
column: parseInt(column, 10)
|
||||
}
|
||||
return results
|
||||
@@ -33,25 +33,25 @@ module.exports = LatexRunner =
|
||||
|
||||
_pdflatexCommand: (mainFile) ->
|
||||
LatexRunner._latexmkBaseCommand.concat [
|
||||
"-pdf", "-e", "$pdflatex='pdflatex -interaction=batchmode %O %S'",
|
||||
"-pdf", "-e", "$pdflatex='pdflatex -synctex=1 -interaction=batchmode %O %S'",
|
||||
Path.join("$COMPILE_DIR", mainFile)
|
||||
]
|
||||
|
||||
_latexCommand: (mainFile) ->
|
||||
LatexRunner._latexmkBaseCommand.concat [
|
||||
"-pdfdvi", "-e", "$latex='latex -interaction=batchmode %O %S'",
|
||||
"-pdfdvi", "-e", "$latex='latex -synctex=1 -interaction=batchmode %O %S'",
|
||||
Path.join("$COMPILE_DIR", mainFile)
|
||||
]
|
||||
|
||||
_xelatexCommand: (mainFile) ->
|
||||
LatexRunner._latexmkBaseCommand.concat [
|
||||
"-xelatex", "-e", "$pdflatex='xelatex -interaction=batchmode %O %S'",
|
||||
"-xelatex", "-e", "$pdflatex='xelatex -synctex=1 -interaction=batchmode %O %S'",
|
||||
Path.join("$COMPILE_DIR", mainFile)
|
||||
]
|
||||
|
||||
_lualatexCommand: (mainFile) ->
|
||||
LatexRunner._latexmkBaseCommand.concat [
|
||||
"-pdf", "-e", "$pdflatex='lualatex -interaction=batchmode %O %S'",
|
||||
"-pdf", "-e", "$pdflatex='lualatex -synctex=1 -interaction=batchmode %O %S'",
|
||||
Path.join("$COMPILE_DIR", mainFile)
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user