Merge pull request #33 from sharelatex/pr/28

wordcount
This commit is contained in:
Henry Oswald
2015-09-09 14:03:20 +01:00
9 changed files with 789 additions and 3 deletions

View File

@@ -66,3 +66,13 @@ module.exports = CompileController =
res.send JSON.stringify {
code: codePositions
}
wordcount: (req, res, next = (error) ->) ->
file = req.query.file || "main.tex"
project_id = req.params.project_id
CompileManager.wordcount project_id, file, (error, result) ->
return next(error) if error?
res.send JSON.stringify {
texcount: result
}

View File

@@ -7,6 +7,8 @@ Path = require "path"
logger = require "logger-sharelatex"
Metrics = require "./Metrics"
child_process = require "child_process"
CommandRunner = require(Settings.clsi?.commandRunner or "./CommandRunner")
fs = require("fs")
module.exports = CompileManager =
doCompile: (request, callback = (error, outputFiles) ->) ->
@@ -107,4 +109,47 @@ module.exports = CompileManager =
line: parseInt(line, 10)
column: parseInt(column, 10)
}
return results
return results
wordcount: (project_id, file_name, callback = (error, pdfPositions) ->) ->
logger.log project_id:project_id, file_name:file_name, "running wordcount"
file_path = "$COMPILE_DIR/" + file_name
command = [ "texcount", '-inc', file_path, "-out=" + file_path + ".wc"]
directory = Path.join(Settings.path.compilesDir, project_id)
timeout = 10 * 1000
CommandRunner.run project_id, command, directory, timeout, (error) ->
return callback(error) if error?
stdout = fs.readFileSync(directory + "/" + file_name + ".wc", "utf-8")
callback null, CompileManager._parseWordcountFromOutput(stdout)
_parseWordcountFromOutput: (output) ->
results = {
encode: ""
textWords: 0
headWords: 0
outside: 0
headers: 0
elements: 0
mathInline: 0
mathDisplay: 0
}
for line in output.split("\n")
[data, info] = line.split(":")
if data.indexOf("Encoding") > -1
results['encode'] = info.trim()
if data.indexOf("in text") > -1
results['textWords'] = parseInt(info, 10)
if data.indexOf("in head") > -1
results['headWords'] = parseInt(info, 10)
if data.indexOf("outside") > -1
results['outside'] = parseInt(info, 10)
if data.indexOf("of head") > -1
results['headers'] = parseInt(info, 10)
if data.indexOf("Number of floats/tables/figures") > -1
results['elements'] = parseInt(info, 10)
if data.indexOf("Number of math inlines") > -1
results['mathInline'] = parseInt(info, 10)
if data.indexOf("Number of math displayed") > -1
results['mathDisplay'] = parseInt(info, 10)
return results