2017-06-02 10:12:59 -04:00
|
|
|
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
|
2017-06-05 09:48:50 -04:00
|
|
|
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
2017-06-02 10:12:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = LabelsHandler =
|
|
|
|
|
|
|
|
labelCaptureRegex: () ->
|
|
|
|
/\\label\{([^\}\n\\]{0,80})\}/g
|
|
|
|
|
|
|
|
getAllLabelsForProject: (projectId, callback=(err, projectLabels)->) ->
|
|
|
|
ProjectEntityHandler.getAllDocs projectId, (err, docs) ->
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
2017-06-13 06:10:21 -04:00
|
|
|
projectLabels = LabelsHandler.extractLabelsFromProjectDocs docs
|
|
|
|
callback(null, projectLabels)
|
2017-06-02 10:12:59 -04:00
|
|
|
|
2017-06-05 04:26:13 -04:00
|
|
|
getLabelsForDoc: (projectId, docId, callback=(err, docLabels)->) ->
|
2017-06-12 08:09:13 -04:00
|
|
|
# Flush doc first, because this action is often performed while
|
|
|
|
# a document is being edited by the client. By contrast,
|
|
|
|
# `getAllLabelsForProject` is called only when a project/editor is loaded
|
2017-06-05 09:48:50 -04:00
|
|
|
DocumentUpdaterHandler.flushDocToMongo projectId, docId, (err) ->
|
2017-06-05 04:26:13 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2017-06-08 05:25:39 -04:00
|
|
|
ProjectEntityHandler.getDoc projectId, docId, (err, lines) ->
|
2017-06-05 04:26:13 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2017-06-13 06:10:21 -04:00
|
|
|
docLabels = LabelsHandler.extractLabelsFromDoc lines
|
|
|
|
callback(null, docLabels)
|
2017-06-05 04:26:13 -04:00
|
|
|
|
2017-06-13 06:10:21 -04:00
|
|
|
extractLabelsFromDoc: (lines) ->
|
2017-06-05 04:26:13 -04:00
|
|
|
docLabels = []
|
|
|
|
for line in lines
|
|
|
|
re = LabelsHandler.labelCaptureRegex()
|
|
|
|
while (labelMatch = re.exec(line))
|
|
|
|
if labelMatch[1]
|
|
|
|
docLabels.push(labelMatch[1])
|
2017-06-13 06:10:21 -04:00
|
|
|
return docLabels
|
2017-06-05 04:26:13 -04:00
|
|
|
|
2017-06-13 06:10:21 -04:00
|
|
|
extractLabelsFromProjectDocs: (projectDocs) ->
|
2017-06-02 10:12:59 -04:00
|
|
|
projectLabels = {} # docId => List[Label]
|
2017-06-13 06:10:21 -04:00
|
|
|
for _path, doc of projectDocs
|
|
|
|
projectLabels[doc._id] = LabelsHandler.extractLabelsFromDoc(doc.lines)
|
|
|
|
return projectLabels
|