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-05 04:26:13 -04:00
|
|
|
LabelsHandler.extractLabelsFromProjectDocs docs, (err, projectLabels) ->
|
2017-06-02 10:12:59 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
|
|
|
callback(null, projectLabels)
|
|
|
|
|
2017-06-05 04:26:13 -04:00
|
|
|
getLabelsForDoc: (projectId, docId, callback=(err, docLabels)->) ->
|
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-05 09:48:50 -04:00
|
|
|
ProjectEntityHandler.getDoc projectId, docId, (err, lines, rev) ->
|
2017-06-05 04:26:13 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2017-06-05 09:48:50 -04:00
|
|
|
LabelsHandler.extractLabelsFromDoc lines, (err, docLabels) ->
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
|
|
|
callback(null, docLabels)
|
2017-06-05 04:26:13 -04:00
|
|
|
|
|
|
|
extractLabelsFromDoc: (lines, callback=(err, docLabels)->) ->
|
|
|
|
docLabels = []
|
|
|
|
for line in lines
|
|
|
|
re = LabelsHandler.labelCaptureRegex()
|
|
|
|
while (labelMatch = re.exec(line))
|
|
|
|
if labelMatch[1]
|
|
|
|
docLabels.push(labelMatch[1])
|
|
|
|
callback(null, docLabels)
|
|
|
|
|
|
|
|
extractLabelsFromProjectDocs: (docs, callback=(err, projectLabels)->) ->
|
2017-06-02 10:12:59 -04:00
|
|
|
projectLabels = {} # docId => List[Label]
|
2017-06-05 04:26:13 -04:00
|
|
|
for _docPath, doc of docs
|
2017-06-02 10:12:59 -04:00
|
|
|
docLabels = []
|
|
|
|
for line in doc.lines
|
|
|
|
re = LabelsHandler.labelCaptureRegex()
|
|
|
|
while (labelMatch = re.exec(line))
|
|
|
|
if labelMatch[1]
|
|
|
|
docLabels.push(labelMatch[1])
|
|
|
|
projectLabels[doc._id] = docLabels
|
|
|
|
callback(null, projectLabels)
|