1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var Ware = require('ware');
|
8 | var extend = require('extend.js');
|
9 | var parser = require('./lib/parse.js');
|
10 | var stringifier = require('./lib/stringify.js');
|
11 | var File = require('./lib/file.js');
|
12 | var utilities = require('./lib/utilities.js');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var Parser = parser.Parser;
|
19 | var parseProto = Parser.prototype;
|
20 | var Compiler = stringifier.Compiler;
|
21 | var compileProto = Compiler.prototype;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function fail(exception) {
|
34 | if (exception) {
|
35 | throw exception;
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function constructParser() {
|
45 | var customProto;
|
46 | var expressions;
|
47 | var key;
|
48 |
|
49 | |
50 |
|
51 |
|
52 | function CustomProto() {}
|
53 |
|
54 | CustomProto.prototype = parseProto;
|
55 |
|
56 | customProto = new CustomProto();
|
57 |
|
58 | |
59 |
|
60 |
|
61 | function CustomParser() {
|
62 | Parser.apply(this, arguments);
|
63 | }
|
64 |
|
65 | CustomParser.prototype = customProto;
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | customProto.blockTokenizers = extend({}, parseProto.blockTokenizers);
|
73 | customProto.blockMethods = extend([], parseProto.blockMethods);
|
74 | customProto.inlineTokenizers = extend({}, parseProto.inlineTokenizers);
|
75 | customProto.inlineMethods = extend([], parseProto.inlineMethods);
|
76 |
|
77 | expressions = parseProto.expressions;
|
78 | customProto.expressions = {};
|
79 |
|
80 | for (key in expressions) {
|
81 | customProto.expressions[key] = extend({}, expressions[key]);
|
82 | }
|
83 |
|
84 | return CustomParser;
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function constructCompiler() {
|
93 | var customProto;
|
94 |
|
95 | |
96 |
|
97 |
|
98 | function CustomProto() {}
|
99 |
|
100 | CustomProto.prototype = compileProto;
|
101 |
|
102 | customProto = new CustomProto();
|
103 |
|
104 | |
105 |
|
106 |
|
107 | function CustomCompiler() {
|
108 | Compiler.apply(this, arguments);
|
109 | }
|
110 |
|
111 | CustomCompiler.prototype = customProto;
|
112 |
|
113 | return CustomCompiler;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function MDAST() {
|
122 | var self = this;
|
123 |
|
124 | if (!(self instanceof MDAST)) {
|
125 | return new MDAST();
|
126 | }
|
127 |
|
128 | self.ware = new Ware();
|
129 | self.attachers = [];
|
130 |
|
131 | self.Parser = constructParser();
|
132 | self.Compiler = constructCompiler();
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function use(attach, options, fileSet) {
|
145 | var self = this;
|
146 | var index;
|
147 | var transformer;
|
148 |
|
149 | if (!(self instanceof MDAST)) {
|
150 | self = new MDAST();
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 | if ('length' in attach && typeof attach !== 'function') {
|
158 | index = -1;
|
159 |
|
160 | while (attach[++index]) {
|
161 | self.use(attach[index], options, fileSet);
|
162 | }
|
163 |
|
164 | return self;
|
165 | }
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 | if (self.attachers.indexOf(attach) === -1) {
|
172 | transformer = attach(self, options, fileSet);
|
173 |
|
174 | self.attachers.push(attach);
|
175 |
|
176 | if (transformer) {
|
177 | self.ware.use(transformer);
|
178 | }
|
179 | }
|
180 |
|
181 | return self;
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | function run(ast, file, done) {
|
193 | var self = this;
|
194 |
|
195 | if (typeof file === 'function') {
|
196 | done = file;
|
197 | file = null;
|
198 | }
|
199 |
|
200 | file = new File(file);
|
201 |
|
202 | done = typeof done === 'function' ? done : fail;
|
203 |
|
204 | if (typeof ast !== 'object' && typeof ast.type !== 'string') {
|
205 | utilities.raise(ast, 'ast');
|
206 | }
|
207 |
|
208 | |
209 |
|
210 |
|
211 |
|
212 | if (self.ware) {
|
213 | self.ware.run(ast, file, done);
|
214 | } else {
|
215 | done(null, ast, file);
|
216 | }
|
217 |
|
218 | return ast;
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | function parse(value, options) {
|
225 | return parser.call(this, new File(value), options);
|
226 | }
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | function stringify(ast, file, options) {
|
232 | if (options === null || options === undefined) {
|
233 | options = file;
|
234 | file = null;
|
235 | }
|
236 |
|
237 | return stringifier.call(this, ast, new File(file), options);
|
238 | }
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | function process(value, options, done) {
|
249 | var file = new File(value);
|
250 | var self = this instanceof MDAST ? this : new MDAST();
|
251 | var result = null;
|
252 | var ast;
|
253 |
|
254 | if (typeof options === 'function') {
|
255 | done = options;
|
256 | options = null;
|
257 | }
|
258 |
|
259 | if (!options) {
|
260 | options = {};
|
261 | }
|
262 |
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 | function callback(exception) {
|
269 | if (exception) {
|
270 | (done || fail)(exception);
|
271 | } else {
|
272 | result = self.stringify(ast, file, options);
|
273 |
|
274 | if (done) {
|
275 | done(null, result, file);
|
276 | }
|
277 | }
|
278 | }
|
279 |
|
280 | ast = self.parse(file, options);
|
281 | file.ast = ast;
|
282 | self.run(ast, file, callback);
|
283 |
|
284 | return result;
|
285 | }
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | var proto = MDAST.prototype;
|
292 |
|
293 | proto.use = use;
|
294 | proto.parse = parse;
|
295 | proto.run = run;
|
296 | proto.stringify = stringify;
|
297 | proto.process = process;
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | MDAST.use = use;
|
304 | MDAST.parse = parse;
|
305 | MDAST.run = run;
|
306 | MDAST.stringify = stringify;
|
307 | MDAST.process = process;
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | module.exports = MDAST;
|