ProjectRootDocManager.coffee 859 B

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