ProjectRootDocManager.coffee 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. ProjectEntityHandler = require "./ProjectEntityHandler"
  2. Path = require "path"
  3. async = require("async")
  4. _ = require("underscore")
  5. module.exports = ProjectRootDocManager =
  6. setRootDocAutomatically: (project_id, callback = (error) ->) ->
  7. ProjectEntityHandler.getAllDocs project_id, (error, docs) ->
  8. return callback(error) if error?
  9. root_doc_id = null
  10. jobs = _.map docs, (doc, path)->
  11. return (cb)->
  12. rootDocId = null
  13. for line in doc.lines || []
  14. # We've had problems with this regex locking up CPU.
  15. # Previously /.*\\documentclass/ would totally lock up on lines of 500kb (data text files :()
  16. # This regex will only look from the start of the line, including whitespace so will return quickly
  17. # regardless of line length.
  18. match = line.match /^\s*\\documentclass/
  19. isRootDoc = Path.extname(path).match(/\.R?tex$/) and match
  20. if isRootDoc
  21. rootDocId = doc?._id
  22. cb(rootDocId)
  23. async.series jobs, (root_doc_id)->
  24. if root_doc_id?
  25. ProjectEntityHandler.setRootDoc project_id, root_doc_id, callback
  26. else
  27. callback()