-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatexParser.js
65 lines (55 loc) · 2.13 KB
/
latexParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use strict";
/**
*@desc Provides interface for latexMl-Script, to create html output.
*/
var config = require('./config.js');
var cp = require('child_process');
var fs = require('fs-extra');
var async = require('async');
/**
* @desc generates a html file out of an xml file
* @param inPath path of the file, that shall be parsed
* @param outPath path, where to save the new file
*/
var xml2html = function (inPath, outPath, callback) {
var jquerypath = __dirname + '/public/vendor/jquery/dist/jquery.js';
var waypointspath = __dirname + '/public/vendor/waypoints/lib/jquery.waypoints.min.js';
var jsIncludePath = __dirname + '/paperInclude.js';
var cmd = 'latexmlpost --dest=' + outPath +' --format=html4'+' --javascript='+ jquerypath +
' --javascript=' + waypointspath + ' --javascript="' + jsIncludePath + '" "' + inPath + '"';
cp.exec(cmd, function(err, stdout, stderr) {
if(err) return callback(err);
callback(null);
});
};
/**
* @desc generate a xml file out of the .tex input file
* @param inPath path of the file, that shall be parsed
* @param outPath path, where to save the new file
*/
var latex2xml = function (inPath, outPath, callback) {
var cmd = 'latexml --dest=' + outPath + ' "' + inPath + '"';
cp.exec(cmd, function(err, stdout, stderr) {
if(err) return callback(err);
callback(null);
});
};
/**
* @desc converts a given Latex document to HTML
* @param paperID ID of the associated paper in the DB
* @param texPath filename of the .tex file, located in ./uploads
* @param callback node style callback
*/
exports.latex2html = function (paperID, texPath, callback) {
var xmlPath = config.dataDir.papers + '/' + paperID + '/' + paperID + '.xml';
var htmlPath = config.dataDir.papers + '/' + paperID + '/html/' + paperID + '.html';
console.log("starting conversion of %s to HTML!", paperID);
async.series([
async.apply(latex2xml, texPath, xmlPath), // convert tex -> xml
async.apply(xml2html, xmlPath, htmlPath), // convert xml -> html
async.apply(fs.remove, xmlPath) // remove temporary xml file
], function(err, results) {
if (err) return callback(err);
callback(null);
});
};