DraftModeManager.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-useless-escape,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let DraftModeManager
  15. const fs = require('fs')
  16. const logger = require('logger-sharelatex')
  17. module.exports = DraftModeManager = {
  18. injectDraftMode(filename, callback) {
  19. if (callback == null) {
  20. callback = function (error) {}
  21. }
  22. return fs.readFile(filename, 'utf8', function (error, content) {
  23. if (error != null) {
  24. return callback(error)
  25. }
  26. // avoid adding draft mode more than once
  27. if (
  28. (content != null
  29. ? content.indexOf('\\documentclass[draft')
  30. : undefined) >= 0
  31. ) {
  32. return callback()
  33. }
  34. const modified_content = DraftModeManager._injectDraftOption(content)
  35. logger.log(
  36. {
  37. content: content.slice(0, 1024), // \documentclass is normally v near the top
  38. modified_content: modified_content.slice(0, 1024),
  39. filename
  40. },
  41. 'injected draft class'
  42. )
  43. return fs.writeFile(filename, modified_content, callback)
  44. })
  45. },
  46. _injectDraftOption(content) {
  47. return (
  48. content
  49. // With existing options (must be first, otherwise both are applied)
  50. .replace(/\\documentclass\[/g, '\\documentclass[draft,')
  51. // Without existing options
  52. .replace(/\\documentclass\{/g, '\\documentclass[draft]{')
  53. )
  54. }
  55. }