Henrique Dias 11 лет назад
Родитель
Сommit
59e87a8729

+ 1 - 0
services/clsi/app.coffee

@@ -36,6 +36,7 @@ app.delete "/project/:project_id", CompileController.clearCache
 
 app.get  "/project/:project_id/sync/code", CompileController.syncFromCode
 app.get  "/project/:project_id/sync/pdf", CompileController.syncFromPdf
+app.get  "/project/:project_id/wordcount", CompileController.wordcount
 
 ForbidSymlinks = require "./app/js/StaticServerForbidSymlinks"
 

+ 10 - 0
services/clsi/app/coffee/CompileController.coffee

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

+ 49 - 1
services/clsi/app/coffee/CompileManager.coffee

@@ -107,4 +107,52 @@ module.exports = CompileManager =
 					line: parseInt(line, 10)
 					column: parseInt(column, 10)
 				}
-		return results
+		return results
+
+	_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("float") > -1
+				results['elements'] = parseInt(info, 10)
+			if data.indexOf("inlines") > -1
+				results['mathInline'] = parseInt(info, 10)
+			if data.indexOf("displayed") > -1
+				results['mathDisplay'] = parseInt(info, 10)
+
+		return results
+
+	wordcount: (project_id, file_name, callback = (error, pdfPositions) ->) ->
+		base_dir = Settings.path.synctexBaseDir(project_id)
+		file_path = base_dir + "/" + file_name
+		logger.log project_id: project_id, file_name: file_name, "try wordcount"
+		CompileManager._runWordcount [file_path], (error, stdout) ->
+			return callback(error) if error?
+			logger.log project_id: project_id, file_name: file_name, stdout: stdout, "wordcount output"
+			callback null, CompileManager._parseWordcountFromOutput(stdout)
+
+	_runWordcount: (args, callback = (error, stdout) ->) ->
+		bin_path = Path.resolve("texcount")
+		seconds = 1000
+		child_process.execFile "texcount", args, timeout: 10 * seconds, (error, stdout, stderr) ->
+			return callback(error) if error?
+			callback(null, stdout)

+ 17 - 0
services/clsi/test/acceptance/coffee/SynctexTests.coffee

@@ -36,3 +36,20 @@ describe "Syncing", ->
 				)
 				done()
 
+	describe "wordcount file", ->
+		it "should return wordcount info", (done) ->
+			Client.wordcount @project_id, "main.tex", (error, result) ->
+				throw error if error?
+				expect(result).to.deep.equal(
+					texcount: { 
+						encode: "ascii"
+						textWords: 2
+						headWords: 0
+						outside: 0
+						headers: 0
+						elements: 0
+						mathInline: 0
+						mathDisplay: 0
+					}
+				)
+				done()

+ 9 - 0
services/clsi/test/acceptance/coffee/helpers/Client.coffee

@@ -90,3 +90,12 @@ module.exports = Client =
 
 			@compile project_id, req, callback
 
+	wordcount: (project_id, file, callback = (error, pdfPositions) ->) ->
+		request.get {
+			url: "#{@host}/project/#{project_id}/wordcount"
+			qs: {
+				file: file
+			}
+		}, (error, response, body) ->
+			return callback(error) if error?
+			callback null, JSON.parse(body)