2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-useless-escape,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let DraftModeManager
|
|
|
|
const fs = require('fs')
|
|
|
|
const logger = require('logger-sharelatex')
|
2016-02-02 09:26:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
module.exports = DraftModeManager = {
|
|
|
|
injectDraftMode(filename, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return fs.readFile(filename, 'utf8', function (error, content) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
// avoid adding draft mode more than once
|
|
|
|
if (
|
|
|
|
(content != null
|
|
|
|
? content.indexOf('\\documentclass[draft')
|
|
|
|
: undefined) >= 0
|
|
|
|
) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const modified_content = DraftModeManager._injectDraftOption(content)
|
|
|
|
logger.log(
|
|
|
|
{
|
|
|
|
content: content.slice(0, 1024), // \documentclass is normally v near the top
|
|
|
|
modified_content: modified_content.slice(0, 1024),
|
2021-07-13 07:04:48 -04:00
|
|
|
filename,
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
|
|
|
'injected draft class'
|
|
|
|
)
|
|
|
|
return fs.writeFile(filename, modified_content, callback)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_injectDraftOption(content) {
|
|
|
|
return (
|
|
|
|
content
|
|
|
|
// With existing options (must be first, otherwise both are applied)
|
|
|
|
.replace(/\\documentclass\[/g, '\\documentclass[draft,')
|
|
|
|
// Without existing options
|
|
|
|
.replace(/\\documentclass\{/g, '\\documentclass[draft]{')
|
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|