ProjectRootDocManager.coffee 847 B

123456789101112131415161718192021222324252627
  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. for line in doc.lines || []
  13. match = line.match /(.*)\\documentclass/ # no lookbehind in js regexp :(
  14. isRootDoc = Path.extname(path).match(/\.R?tex$/) and match and !match[1].match /%/
  15. if isRootDoc
  16. return cb(doc?._id)
  17. else
  18. return cb()
  19. async.parallel jobs, (root_doc_id)->
  20. if root_doc_id?
  21. ProjectEntityHandler.setRootDoc project_id, root_doc_id, callback
  22. else
  23. callback()