2017-03-16 12:55:53 -04:00
|
|
|
fs = require "fs"
|
|
|
|
Path = require "path"
|
2017-03-21 07:29:37 -04:00
|
|
|
ResourceWriter = require "./ResourceWriter"
|
2017-09-29 12:00:53 -04:00
|
|
|
SafeReader = require "./SafeReader"
|
2017-03-16 12:55:53 -04:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
# for \tikzexternalize to work the main file needs to match the
|
|
|
|
# jobname. Since we set the -jobname to output, we have to create a
|
|
|
|
# copy of the main file as 'output.tex'.
|
|
|
|
|
|
|
|
module.exports = TikzManager =
|
2017-09-29 12:00:53 -04:00
|
|
|
|
|
|
|
checkMainFile: (compileDir, mainFile, resources, callback = (error, usesTikzExternalize) ->) ->
|
2017-03-16 12:55:53 -04:00
|
|
|
# if there's already an output.tex file, we don't want to touch it
|
|
|
|
for resource in resources
|
|
|
|
if resource.path is "output.tex"
|
2017-09-29 12:00:53 -04:00
|
|
|
logger.log compileDir: compileDir, mainFile: mainFile, "output.tex already in resources"
|
|
|
|
return callback(null, false)
|
2017-03-16 12:55:53 -04:00
|
|
|
# if there's no output.tex, see if we are using tikz/pgf in the main file
|
2017-09-29 12:00:53 -04:00
|
|
|
ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
SafeReader.readFile path, 65536, "utf8", (error, content) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
usesTikzExternalize = content?.indexOf("\\tikzexternalize") >= 0
|
|
|
|
logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, "checked for tikzexternalize"
|
|
|
|
callback null, usesTikzExternalize
|
2017-03-16 12:55:53 -04:00
|
|
|
|
|
|
|
injectOutputFile: (compileDir, mainFile, callback = (error) ->) ->
|
2017-03-21 07:29:37 -04:00
|
|
|
ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
|
2017-03-16 12:55:53 -04:00
|
|
|
return callback(error) if error?
|
2017-03-21 07:29:37 -04:00
|
|
|
fs.readFile path, "utf8", (error, content) ->
|
|
|
|
return callback(error) if error?
|
2017-10-02 10:45:09 -04:00
|
|
|
logger.log compileDir: compileDir, mainFile: mainFile, "copied file to output.tex for tikz"
|
2017-03-21 07:29:37 -04:00
|
|
|
# use wx flag to ensure that output file does not already exist
|
|
|
|
fs.writeFile Path.join(compileDir, "output.tex"), content, {flag:'wx'}, callback
|