Add in a synctex end point

This commit is contained in:
James Allen
2014-04-08 15:18:56 +01:00
parent c0464aca9a
commit 9fc0ef2885
19 changed files with 5760 additions and 5 deletions

View File

@@ -0,0 +1,38 @@
Client = require "./helpers/Client"
request = require "request"
require("chai").should()
expect = require("chai").expect
describe "Syncing", ->
before (done) ->
@request =
resources: [
path: "main.tex"
content: '''
\\documentclass{article}
\\begin{document}
Hello world
\\end{document}
'''
]
@project_id = Client.randomId()
Client.compile @project_id, @request, (@error, @res, @body) => done()
describe "from code to pdf", ->
it "should return the correct location", (done) ->
Client.syncFromCode @project_id, "main.tex", 3, 5, (error, pdfPositions) ->
throw error if error?
expect(pdfPositions).to.deep.equal(
pdf: [ { page: 1, h: 133.77, v: 134.76, height: 6.92, width: 343.71 } ]
)
done()
describe "from pdf to code", ->
it "should return the correct location", (done) ->
Client.syncFromPdf @project_id, 1, 100, 200, (error, codePositions) ->
throw error if error?
expect(codePositions).to.deep.equal(
code: [ { file: 'main.tex', line: 3, column: -1 } ]
)
done()

View File

@@ -32,6 +32,29 @@ module.exports = Client =
app.use express.static(directory)
app.listen(port, host)
syncFromCode: (project_id, file, line, column, callback = (error, pdfPositions) ->) ->
request.get {
url: "#{@host}/project/#{project_id}/sync/code"
qs: {
file: file
line: line
column: column
}
}, (error, response, body) ->
return callback(error) if error?
callback null, JSON.parse(body)
syncFromPdf: (project_id, page, h, v, callback = (error, pdfPositions) ->) ->
request.get {
url: "#{@host}/project/#{project_id}/sync/pdf"
qs: {
page: page,
h: h, v: v
}
}, (error, response, body) ->
return callback(error) if error?
callback null, JSON.parse(body)
compileDirectory: (project_id, baseDirectory, directory, serverPort, callback = (error, res, body) ->) ->
resources = []
entities = fs.readdirSync("#{baseDirectory}/#{directory}")

View File

@@ -18,6 +18,7 @@ describe "CompileController", ->
@Settings.externalUrl = "http://www.example.com"
@req = {}
@res = {}
@next = sinon.stub()
describe "compile", ->
beforeEach ->
@@ -90,3 +91,61 @@ describe "CompileController", ->
)
.should.equal true
describe "syncFromCode", ->
beforeEach ->
@file = "main.tex"
@line = 42
@column = 5
@project_id = "mock-project-id"
@req.params =
project_id: @project_id
@req.query =
file: @file
line: @line.toString()
column: @column.toString()
@res.send = sinon.stub()
@CompileManager.syncFromCode = sinon.stub().callsArgWith(4, null, @pdfPositions = ["mock-positions"])
@CompileController.syncFromCode @req, @res, @next
it "should find the corresponding location in the PDF", ->
@CompileManager.syncFromCode
.calledWith(@project_id, @file, @line, @column)
.should.equal true
it "should return the positions", ->
@res.send
.calledWith(JSON.stringify
pdf: @pdfPositions
)
.should.equal true
describe "syncFromPdf", ->
beforeEach ->
@page = 5
@h = 100.23
@v = 45.67
@project_id = "mock-project-id"
@req.params =
project_id: @project_id
@req.query =
page: @page.toString()
h: @h.toString()
v: @v.toString()
@res.send = sinon.stub()
@CompileManager.syncFromPdf = sinon.stub().callsArgWith(4, null, @codePositions = ["mock-positions"])
@CompileController.syncFromPdf @req, @res, @next
it "should find the corresponding location in the code", ->
@CompileManager.syncFromPdf
.calledWith(@project_id, @page, @h, @v)
.should.equal true
it "should return the positions", ->
@res.send
.calledWith(JSON.stringify
code: @codePositions
)
.should.equal true

View File

@@ -4,6 +4,7 @@ require('chai').should()
modulePath = require('path').join __dirname, '../../../app/js/CompileManager'
tk = require("timekeeper")
EventEmitter = require("events").EventEmitter
Path = require "path"
describe "CompileManager", ->
beforeEach ->
@@ -102,3 +103,68 @@ describe "CompileManager", ->
.should.equal true
@callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id} failed: #{@error}"
describe "syncing", ->
beforeEach ->
@page = 1
@h = 42.23
@v = 87.56
@width = 100.01
@height = 234.56
@line = 5
@column = 3
@file_name = "main.tex"
@proc = new EventEmitter()
@proc.stdout = new EventEmitter()
@proc.stderr = new EventEmitter()
@child_process.spawn = sinon.stub().returns(@proc)
@Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}"
describe "syncFromCode", ->
beforeEach ->
@stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n"
@CompileManager.syncFromCode @project_id, @file_name, @line, @column, @callback
@proc.stdout.emit "data", @stdout
@proc.emit "close", 0
it "should execute the synctex binary", ->
bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
file_path = "#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}"
@child_process.spawn
.calledWith(bin_path, ["code", synctex_path, file_path, @line, @column])
.should.equal true
it "should call the callback with the parsed output", ->
@callback
.calledWith(null, [{
page: @page
h: @h
v: @v
height: @height
width: @width
}])
.should.equal true
describe "syncFromPdf", ->
beforeEach ->
@stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}\t#{@line}\t#{@column}\n"
@CompileManager.syncFromPdf @project_id, @page, @h, @v, @callback
@proc.stdout.emit "data", @stdout
@proc.emit "close", 0
it "should execute the synctex binary", ->
bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
@child_process.spawn
.calledWith(bin_path, ["pdf", synctex_path, @page, @h, @v])
.should.equal true
it "should call the callback with the parsed output", ->
@callback
.calledWith(null, [{
file: @file_name
line: @line
column: @column
}])
.should.equal true