2014-02-12 05:23:40 -05:00
|
|
|
ProjectEntityHandler = require "./ProjectEntityHandler"
|
|
|
|
Path = require "path"
|
2014-11-26 12:19:21 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ProjectRootDocManager =
|
2014-10-15 09:11:02 -04:00
|
|
|
setRootDocAutomatically: (project_id, callback = (error) ->) ->
|
2014-11-30 20:07:03 -05:00
|
|
|
|
2014-10-15 09:11:02 -04:00
|
|
|
ProjectEntityHandler.getAllDocs project_id, (error, docs) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2014-11-26 12:19:21 -05:00
|
|
|
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
root_doc_id = null
|
2014-11-26 12:19:21 -05:00
|
|
|
jobs = _.map docs, (doc, path)->
|
|
|
|
return (cb)->
|
2014-11-30 20:07:03 -05:00
|
|
|
rootDocId = null
|
2014-11-26 12:19:21 -05:00
|
|
|
for line in doc.lines || []
|
|
|
|
match = line.match /(.*)\\documentclass/ # no lookbehind in js regexp :(
|
|
|
|
isRootDoc = Path.extname(path).match(/\.R?tex$/) and match and !match[1].match /%/
|
|
|
|
if isRootDoc
|
2014-11-30 20:07:03 -05:00
|
|
|
rootDocId = doc?._id
|
|
|
|
cb(rootDocId)
|
|
|
|
|
2014-11-26 16:53:57 -05:00
|
|
|
async.series jobs, (root_doc_id)->
|
2014-11-26 12:19:21 -05:00
|
|
|
if root_doc_id?
|
|
|
|
ProjectEntityHandler.setRootDoc project_id, root_doc_id, callback
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-12 05:23:40 -05:00
|
|
|
|