Merge pull request #77 from sharelatex/bg-fix-tikzexternalize-II

fix tikzexternalize ii
This commit is contained in:
Brian Gough
2017-10-02 11:19:06 +01:00
committed by GitHub
5 changed files with 72 additions and 42 deletions

View File

@@ -62,7 +62,9 @@ module.exports = CompileManager =
callback() callback()
createTikzFileIfRequired = (callback) -> createTikzFileIfRequired = (callback) ->
if TikzManager.needsOutputFile(request.rootResourcePath, resourceList) TikzManager.checkMainFile compileDir, request.rootResourcePath, resourceList, (error, usesTikzExternalize) ->
return callback(error) if error?
if usesTikzExternalize
TikzManager.injectOutputFile compileDir, request.rootResourcePath, callback TikzManager.injectOutputFile compileDir, request.rootResourcePath, callback
else else
callback() callback()

View File

@@ -78,6 +78,8 @@ module.exports = ResourceWriter =
should_delete = true should_delete = true
if path.match(/^output\./) or path.match(/\.aux$/) or path.match(/^cache\//) # knitr cache if path.match(/^output\./) or path.match(/\.aux$/) or path.match(/^cache\//) # knitr cache
should_delete = false should_delete = false
if path.match(/^output-.*/) # Tikz cached figures
should_delete = false
if path == "output.pdf" or path == "output.dvi" or path == "output.log" or path == "output.xdv" if path == "output.pdf" or path == "output.dvi" or path == "output.log" or path == "output.xdv"
should_delete = true should_delete = true
if path == "output.tex" # created by TikzManager if present in output files if path == "output.tex" # created by TikzManager if present in output files

View File

@@ -1,6 +1,7 @@
fs = require "fs" fs = require "fs"
Path = require "path" Path = require "path"
ResourceWriter = require "./ResourceWriter" ResourceWriter = require "./ResourceWriter"
SafeReader = require "./SafeReader"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
# for \tikzexternalize to work the main file needs to match the # for \tikzexternalize to work the main file needs to match the
@@ -8,25 +9,21 @@ logger = require "logger-sharelatex"
# copy of the main file as 'output.tex'. # copy of the main file as 'output.tex'.
module.exports = TikzManager = module.exports = TikzManager =
needsOutputFile: (rootResourcePath, resources) ->
checkMainFile: (compileDir, mainFile, resources, callback = (error, usesTikzExternalize) ->) ->
# if there's already an output.tex file, we don't want to touch it # if there's already an output.tex file, we don't want to touch it
for resource in resources for resource in resources
if resource.path is "output.tex" if resource.path is "output.tex"
return false logger.log compileDir: compileDir, mainFile: mainFile, "output.tex already in resources"
return callback(null, false)
# if there's no output.tex, see if we are using tikz/pgf in the main file # if there's no output.tex, see if we are using tikz/pgf in the main file
for resource in resources ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
if resource.path is rootResourcePath return callback(error) if error?
return TikzManager._includesTikz (resource) SafeReader.readFile path, 65536, "utf8", (error, content) ->
# otherwise false return callback(error) if error?
return false usesTikzExternalize = content?.indexOf("\\tikzexternalize") >= 0
logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, "checked for tikzexternalize"
_includesTikz: (resource) -> callback null, usesTikzExternalize
# check if we are using tikz externalize
content = resource.content?.slice(0,65536)
if content?.indexOf("\\tikzexternalize") >= 0
return true
else
return false
injectOutputFile: (compileDir, mainFile, callback = (error) ->) -> injectOutputFile: (compileDir, mainFile, callback = (error) ->) ->
ResourceWriter.checkPath compileDir, mainFile, (error, path) -> ResourceWriter.checkPath compileDir, mainFile, (error, path) ->

View File

@@ -108,7 +108,7 @@ describe "CompileManager", ->
@OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files) @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
@OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files) @OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files)
@DraftModeManager.injectDraftMode = sinon.stub().callsArg(1) @DraftModeManager.injectDraftMode = sinon.stub().callsArg(1)
@TikzManager.needsOutputFile = sinon.stub().returns(false) @TikzManager.checkMainFile = sinon.stub().callsArg(3, false)
describe "normally", -> describe "normally", ->
beforeEach -> beforeEach ->

View File

@@ -7,34 +7,63 @@ describe 'TikzManager', ->
beforeEach -> beforeEach ->
@TikzManager = SandboxedModule.require modulePath, requires: @TikzManager = SandboxedModule.require modulePath, requires:
"./ResourceWriter": @ResourceWriter = {} "./ResourceWriter": @ResourceWriter = {}
"./SafeReader": @SafeReader = {}
"fs": @fs = {} "fs": @fs = {}
"logger-sharelatex": @logger = {log: () ->} "logger-sharelatex": @logger = {log: () ->}
describe "needsOutputFile", -> describe "checkMainFile", ->
it "should return true if there is a \\tikzexternalize", -> beforeEach ->
@TikzManager.needsOutputFile("main.tex", [ @compileDir = "compile-dir"
{ path: 'foo.tex' }, @mainFile = "main.tex"
{ path: 'main.tex', content:'foo \\usepackage{tikz} \\tikzexternalize' } @callback = sinon.stub()
]).should.equal true
it "should return false if there is no \\tikzexternalize", -> describe "if there is already an output.tex file in the resources", ->
@TikzManager.needsOutputFile("main.tex", [ beforeEach ->
{ path: 'foo.tex' }, @resources = [{path:"main.tex"},{path:"output.tex"}]
{ path: 'main.tex', content:'foo \\usepackage{tikz}' } @TikzManager.checkMainFile @compileDir, @mainFile, @resources, @callback
]).should.equal false
it "should return false if there is already an output.tex file", -> it "should call the callback with false ", ->
@TikzManager.needsOutputFile("main.tex", [ @callback.calledWithExactly(null, false)
{ path: 'foo.tex' }, .should.equal true
{ path: 'main.tex', content:'foo \\usepackage{tikz} \\tikzexternalize' },
{ path: 'output.tex' }
]).should.equal false
it "should return false if the file has no content", -> describe "if there is no output.tex file in the resources", ->
@TikzManager.needsOutputFile("main.tex", [ beforeEach ->
{ path: 'foo.tex' }, @resources = [{path:"main.tex"}]
{ path: 'main.tex' } @ResourceWriter.checkPath = sinon.stub()
]).should.equal false .withArgs(@compileDir, @mainFile)
.callsArgWith(2, null, "#{@compileDir}/#{@mainFile}")
describe "and the main file contains tikzexternalize", ->
beforeEach ->
@SafeReader.readFile = sinon.stub()
.withArgs("#{@compileDir}/#{@mainFile}")
.callsArgWith(3, null, "hello \\tikzexternalize")
@TikzManager.checkMainFile @compileDir, @mainFile, @resources, @callback
it "should look at the file on disk", ->
@SafeReader.readFile
.calledWith("#{@compileDir}/#{@mainFile}")
.should.equal true
it "should call the callback with true ", ->
@callback.calledWithExactly(null, true)
.should.equal true
describe "and the main file does not contain tikzexternalize", ->
beforeEach ->
@SafeReader.readFile = sinon.stub()
.withArgs("#{@compileDir}/#{@mainFile}")
.callsArgWith(3, null, "hello")
@TikzManager.checkMainFile @compileDir, @mainFile, @resources, @callback
it "should look at the file on disk", ->
@SafeReader.readFile
.calledWith("#{@compileDir}/#{@mainFile}")
.should.equal true
it "should call the callback with false", ->
@callback.calledWithExactly(null, false)
.should.equal true
describe "injectOutputFile", -> describe "injectOutputFile", ->
beforeEach -> beforeEach ->