-
Notifications
You must be signed in to change notification settings - Fork 1
/
latex.js
149 lines (126 loc) · 4.63 KB
/
latex.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var fs = require('fs')
, util = require('util')
, sh = require('sync-exec')
, exec = require('child_process').exec
, prompt = require("prompt")
/* CONSTANTS */
, CONFIG_FILENAME = ".config.json";
prompt.message = "mkdoc";
prompt.start();
/* Latex class */
var Latex = function (type) {
function init() {
fs.readdir(process.cwd(), function (err, files) {
if (err) { throw err; }
if (files.length > 0) {
console.log("[ERROR] directory is not empty.");
return;
}
var src_template = util.format("%s/templates/%s.tex", __dirname, type);
var template = util.format("%s/main.tex", process.cwd());
fs.readFile(src_template, function (err, data) {
if (err) { throw err; }
fs.writeFile(template, data, function() {
if (err) { throw err; }
});
});
// create content.md file
var content = util.format("%s/content.md", process.cwd());
fs.writeFile(content, "your text here", function (err) {
if (err) { throw err; }
});
// config file
var config = {
"type": type,
"chapters": false
};
var config_filename = util.format("%s/%s", process.cwd(), CONFIG_FILENAME);
fs.writeFile(config_filename, JSON.stringify(config, undefined, ' '), function(err) {
if (err) { throw err; }
console.log("[ INFO] done.");
});
});
}
function view() {
var config_filename = util.format("%s/%s", process.cwd(), CONFIG_FILENAME);
fs.readFile(config_filename, function (err, data) {
if (err) { throw err; }
var config = JSON.parse(data);
if (! config.chapters) {
config.chapters = false;
}
var cmd = _safeListMarkdowFiles();
exec(cmd, {cwd: process.cwd()}, function(err, stdout /* , stderr */) {
if (err) { throw err; }
_compileBibtex();
var files = stdout.trim().split("\n");
for (var index in files) {
var basename = files[index].substr(0, files[index].length - 3); // path.basename(files[index], '.md');
sh(util.format(
"pandoc %s -t %s %s.md -o %s.tex",
(config.chapters ? "--chapters" : ""), config.type, basename, basename));
}
sh("pdflatex -shell-escape -interaction=nonstopmode main.tex");
sh("xdg-open main.pdf");
console.log("[ INFO] done.");
});
});
}
function cleanup() {
// list all auxiliary files
var ls = "ls | grep -E '\\.(aux|log|nav|out|snm|toc)$'";
exec(ls, {cwd: process.cwd()}, function(err, stdout /* , stderr */) {
if (err) { throw err; }
// show files to be deleted
console.log(stdout);
var args = stdout.trim().split("\n");
prompt.get({
properties: {
opt: {
message: "are you sure? y/[n]"
}
}
}, function(err, result) {
// default option: no
var opt = result.opt || "n";
if (opt === "y") {
// TODO: check to see if any of the args have spaces in filename!
var rm = util.format("cd %s && rm %s", process.cwd(), args.join(' '));
sh(rm);
}
});
});
}
/* private functions */
/**
* compile any existing bibtex files
*/
function _compileBibtex() {
var cmd = util.format("ls %s/*.bib", process.cwd());
var code = sh(cmd); // exists any bibtex file?
if (code === 0) {
sh("bibtex main");
}
}
/**
* Get a ls command that's safe from dir not found error
*/
function _safeListMarkdowFiles() {
var cmd = "ls *.md";
var result = sh("ls chapters | grep -E '\\.(md)$' | wc -l");
var count = parseInt(result.stdout);
if (count > 0) {
cmd += " chapters/*.md";
}
return cmd;
}
/* end of private functions */
/* public functions */
return {
init: init,
view: view,
cleanup: cleanup
};
};
module.exports = new Latex('latex');
exports.Latex = module.exports.Latex = Latex;