markdown-maker 1.7.10 → 1.9.1

Sign up to get free protection for your applications and to get access to all the features.
package/build/parse.js DELETED
@@ -1,295 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var fs = require("fs"); /* for handling reading of files */
4
- var path = require("path"); /* for handling file paths */
5
- var Colors = require("colors.ts"); /* for adding colours to strings */
6
- Colors.enable();
7
- var marked = require("marked");
8
- var commands = require("./commands.js");
9
- var TargetType;
10
- (function (TargetType) {
11
- TargetType[TargetType["HTML"] = 0] = "HTML";
12
- TargetType[TargetType["MARKDOWN"] = 1] = "MARKDOWN";
13
- })(TargetType || (TargetType = {}));
14
- /* parse some md
15
- * recursively with extra options */
16
- var Parser = /** @class */ (function () {
17
- function Parser(filename, clargs, opts) {
18
- /* this.working_directory */
19
- this.file = filename;
20
- if (!opts)
21
- opts = {};
22
- /* the parent parser */
23
- this.parent = opts.parent;
24
- this.line_num = 0;
25
- this.wd = path.dirname(filename);
26
- /* finished blob */
27
- this.blobs = {};
28
- /* all options */
29
- this.opts = {
30
- defs: {},
31
- secs: [],
32
- args: [],
33
- depth: 0,
34
- verbose: false,
35
- debug: false,
36
- max_depth: 5,
37
- use_underscore: false,
38
- toc_level: 3,
39
- allow_undef: false,
40
- html: false,
41
- targetType: undefined,
42
- only_warn: false,
43
- parent: undefined,
44
- isFileCallback: function (f) {
45
- if (!fs.existsSync(f))
46
- return false;
47
- return fs.readFileSync(f, "utf-8") + "\n";
48
- },
49
- };
50
- if (!clargs) {
51
- clargs = {};
52
- }
53
- /* append all commandline arguments to this */
54
- Object.assign(this.opts, clargs);
55
- Object.assign(this.opts, opts);
56
- this.raw = this.opts.isFileCallback(filename) || filename;
57
- }
58
- /**
59
- * parse wrapper for handling
60
- * preprocessing, parsing and postprocess
61
- **/
62
- Parser.prototype.parse = function () {
63
- if (this.opts.verbose || this.opts.debug) {
64
- console.log(Colors.colors("magenta", "parsing " + this.file + ": depth=" + this.opts.depth));
65
- }
66
- if (this.opts.debug) {
67
- console.log("Parsing options:");
68
- console.log(this.opts);
69
- }
70
- /* reset sections for beginning parse */
71
- if (this.opts.depth === 0)
72
- this.opts.secs = [];
73
- var __blob;
74
- /* apply preproccessing to raw file */
75
- __blob = this.preprocess(this.raw);
76
- /* main parser instance call */
77
- __blob = this.mainparse(__blob);
78
- /**
79
- * apply postprocessing after */
80
- __blob = this.postprocess(__blob);
81
- return __blob;
82
- };
83
- Parser.prototype.mainparse = function (blob) {
84
- var _this = this;
85
- if (this.opts.verbose || this.opts.debug) {
86
- console.debug(("beginning mainparse of '" + this.file + "'").blue);
87
- }
88
- var __blob = "";
89
- /* main parser instance loop */
90
- blob.split("\n").forEach(function (line, lnum) {
91
- _this.line_num = lnum;
92
- /* if line looks like a title */
93
- var titleMatch = line.trim().match(/^(#+) (.+)$/);
94
- if (titleMatch) {
95
- if (_this.opts.verbose || _this.opts.debug)
96
- console.log("found toc element: " + line);
97
- /* implement toc level */
98
- var level = titleMatch[1].length;
99
- /**
100
- * parse elements of title
101
- * such as variables */
102
- if (level <= _this.opts.toc_level) {
103
- var title = titleMatch[2]
104
- .trim()
105
- .split(" ")
106
- .map(function (s) {
107
- return s.startsWith(Parser.TOKEN) ? _this.parseToken(s) : s;
108
- })
109
- .join("_");
110
- _this.opts.secs.push({ level: level, title: title });
111
- if (_this.opts.debug) {
112
- console.log("updated sections:", { level: level, title: title });
113
- }
114
- }
115
- }
116
- var __line_tokens = [];
117
- /* split line into tokens */
118
- line.split(" ").forEach(function (token) {
119
- /* if token is not #md token,
120
- * just add it and continue */
121
- if (token.startsWith(Parser.TOKEN)) {
122
- token = _this.parseToken(token);
123
- }
124
- __line_tokens.push(token);
125
- });
126
- /* put line back properly */
127
- __blob += __line_tokens.join(" ") + "\n";
128
- });
129
- return __blob;
130
- };
131
- Parser.prototype.parseToken = function (token) {
132
- /* iterate over all commands,
133
- * and if command is valid, execute it */
134
- if (this.opts.verbose || this.opts.debug)
135
- console.log("found mdtoken: " + token);
136
- for (var i = 0; i < commands.parse.length; i++) {
137
- var command = commands.parse[i];
138
- if (command.valid(token, this)) {
139
- return command.act(token, this);
140
- }
141
- }
142
- /* check if the command is for later */
143
- for (var i = 0; i < commands.postparse.length; i++) {
144
- var command = commands.postparse[i];
145
- if (command.valid(token, this)) {
146
- return token;
147
- }
148
- }
149
- throw new SyntaxError("Unknown token: " + token);
150
- };
151
- Parser.prototype.preprocess = function (blob) {
152
- var _this = this;
153
- if (this.opts.verbose || this.opts.debug) {
154
- console.debug(("beginning preprocess of '" + this.file + "'").blue);
155
- }
156
- var __blob = "";
157
- var lines = blob.split("\n");
158
- lines.forEach(function (line) {
159
- var __line_tokens = [];
160
- line.split(" ").forEach(function (token) {
161
- for (var _i = 0, _a = commands.preparse; _i < _a.length; _i++) {
162
- var command = _a[_i];
163
- if (command.valid(token, _this)) {
164
- token = command.act(token, _this);
165
- }
166
- }
167
- __line_tokens.push(token);
168
- });
169
- __blob += __line_tokens.join(" ") + "\n";
170
- });
171
- return __blob;
172
- };
173
- Parser.prototype.postprocess = function (blob) {
174
- var _this = this;
175
- if (this.opts.verbose || this.opts.debug) {
176
- console.debug(("beginning postprocess of '" + this.file + "'").blue);
177
- }
178
- var __blob = "";
179
- var lines = blob.split("\n");
180
- lines.forEach(function (line) {
181
- var __line_tokens = [];
182
- line.split(" ").forEach(function (token) {
183
- // only look
184
- for (var _i = 0, _a = commands.postparse; _i < _a.length; _i++) {
185
- var command = _a[_i];
186
- if (command.valid(token, _this)) {
187
- token = command.act(token, _this);
188
- }
189
- }
190
- __line_tokens.push(token);
191
- });
192
- __blob += __line_tokens.join(" ") + "\n";
193
- });
194
- /* remove double empty lines */
195
- __blob = this.remove_double_blank_lines(__blob);
196
- return __blob;
197
- };
198
- Parser.prototype.titleId = function (title) {
199
- var sep = this.opts.use_underscore ? "_" : "-";
200
- title = title
201
- .toLowerCase()
202
- .replace(/[^\w\s]+/g, "")
203
- .replace(/[\s_]+/g, sep);
204
- return title;
205
- };
206
- Parser.prototype.gen_toc = function () {
207
- var _this = this;
208
- var __blob = [];
209
- var tabSize = 2;
210
- var beg = "* ";
211
- var hor = " ".repeat(tabSize);
212
- this.opts.secs.forEach(function (sec) {
213
- var link = _this.titleId(sec.title);
214
- var title = sec.title.replace(/_/g, " ");
215
- var __line = hor.repeat(Math.max(sec.level - 1, 0)) +
216
- beg +
217
- ("[" + title + "](#" + link + ")");
218
- __blob.push(__line);
219
- });
220
- return __blob.join("\n");
221
- };
222
- Parser.prototype.remove_double_blank_lines = function (blob) {
223
- /* replace all triple newlines, and EOF by double newline */
224
- blob = blob.replace(/(\r\n|\n){3,}/g, "\n\n");
225
- return blob;
226
- };
227
- /* output the parsed document to bundle */
228
- Parser.prototype.to = function (bundleName, cb) {
229
- var dir = path.dirname(bundleName);
230
- var called = false;
231
- if (!cb)
232
- cb = function () { };
233
- if (!fs.existsSync(dir)) {
234
- fs.mkdirSync(dir);
235
- }
236
- this.get(TargetType.MARKDOWN, function (blob) {
237
- fs.writeFile(bundleName, blob, function () {
238
- if (!called)
239
- cb(bundleName);
240
- called = true;
241
- });
242
- });
243
- if (this.opts.html) {
244
- var htmlFileName_1 = bundleName.replace(".md", ".html");
245
- fs.writeFile(htmlFileName_1, this.html(), function () {
246
- if (!called)
247
- cb(htmlFileName_1);
248
- called = true;
249
- });
250
- }
251
- };
252
- Parser.prototype.html = function () {
253
- var htmlFormatted = marked(this.get(TargetType.HTML));
254
- return htmlFormatted;
255
- };
256
- Parser.prototype.get = function (targetType, callback) {
257
- if (this.blobs[targetType]) {
258
- if (callback) {
259
- callback(this.blobs[targetType]);
260
- }
261
- return this.blobs[targetType];
262
- }
263
- else {
264
- try {
265
- this.opts.targetType = targetType;
266
- var blob = this.parse();
267
- this.opts.targetType = undefined;
268
- if (callback)
269
- callback(blob);
270
- return blob;
271
- }
272
- catch (error) {
273
- var traceback = "";
274
- var p = this;
275
- do {
276
- traceback += ("\n...on line " + (p.line_num + 1) + " in " + p.file).grey(15);
277
- if (p.parent)
278
- p = p.parent;
279
- } while (p.parent);
280
- error.message += traceback;
281
- /* only interested in stacktrace, when debugging */
282
- if (!this.opts.debug)
283
- error.stack = "";
284
- if (this.opts.only_warn)
285
- console.error(error);
286
- else
287
- throw error;
288
- }
289
- }
290
- };
291
- Parser.TOKEN = "#md";
292
- return Parser;
293
- }());
294
- module.exports = Parser;
295
- exports.default = Parser;