/
asciidoctor-extensions-api.js
425 lines (375 loc) · 11.1 KB
/
asciidoctor-extensions-api.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Extensions API
/**
* @private
*/
var toBlock = function (block) {
// arity is a mandatory field
block.$$arity = block.length;
return block;
};
var registerExtension = function (registry, type, processor, name) {
if (typeof processor === 'function') {
return Opal.send(registry, type, name && [name], toBlock(processor));
} else {
return registry['$' + type](processor, name);
}
};
/**
* @namespace
* @description
* Extensions provide a way to participate in the parsing and converting
* phases of the AsciiDoc processor or extend the AsciiDoc syntax.
*
* The various extensions participate in AsciiDoc processing as follows:
*
* 1. After the source lines are normalized, {{@link Extensions/Preprocessor}}s modify or replace
* the source lines before parsing begins. {{@link Extensions/IncludeProcessor}}s are used to
* process include directives for targets which they claim to handle.
* 2. The Parser parses the block-level content into an abstract syntax tree.
* Custom blocks and block macros are processed by associated {{@link Extensions/BlockProcessor}}s
* and {{@link Extensions/BlockMacroProcessor}}s, respectively.
* 3. {{@link Extensions/TreeProcessor}}s are run on the abstract syntax tree.
* 4. Conversion of the document begins, at which point inline markup is processed
* and converted. Custom inline macros are processed by associated {InlineMacroProcessor}s.
* 5. {{@link Extensions/Postprocessor}}s modify or replace the converted document.
* 6. The output is written to the output stream.
*
* Extensions may be registered globally using the {Extensions.register} method
* or added to a custom {Registry} instance and passed as an option to a single
* Asciidoctor processor.
*
* @example
* Opal.Asciidoctor.Extensions.register(function () {
* this.block(function () {
* var self = this;
* self.named('shout');
* self.onContext('paragraph');
* self.process(function (parent, reader) {
* var lines = reader.getLines().map(function (l) { return l.toUpperCase(); });
* return self.createBlock(parent, 'paragraph', lines);
* });
* });
* });
*/
var Extensions = Opal.const_get_qualified(Opal.Asciidoctor, 'Extensions');
// Alias
Opal.Asciidoctor.Extensions = Extensions;
/**
* Create a new {@link Extensions/Registry}.
* @param {string} name
* @param {function} block
* @memberof Extensions
* @returns {Extensions/Registry} - returns a {@link Extensions/Registry}
*/
Extensions.create = function (name, block) {
if (typeof name === 'function' && typeof block === 'undefined') {
return Opal.send(this, 'build_registry', null, toBlock(name));
} else if (typeof block === 'function') {
return Opal.send(this, 'build_registry', [name], toBlock(block));
} else {
return this.$build_registry();
}
};
/**
* @memberof Extensions
*/
Extensions.register = function (name, block) {
if (typeof name === 'function' && typeof block === 'undefined') {
return Opal.send(this, 'register', null, toBlock(name));
} else {
return Opal.send(this, 'register', [name], toBlock(block));
}
};
/**
* Get statically-registerd extension groups.
* @memberof Extensions
*/
Extensions.getGroups = function () {
return fromHash(this.$groups());
};
/**
* Unregister all statically-registered extension groups.
* @memberof Extensions
*/
Extensions.unregisterAll = function () {
this.$unregister_all();
};
/**
* Unregister the specified statically-registered extension groups.
*
* NOTE Opal cannot delete an entry from a Hash that is indexed by symbol, so
* we have to resort to using low-level operations in this method.
*
* @memberof Extensions
*/
Extensions.unregister = function () {
var names = Array.prototype.concat.apply([], arguments);
var groups = this.$groups();
var groupNameIdx = {};
for (var i = 0, groupSymbolNames = groups.$$keys; i < groupSymbolNames.length; i++) {
var groupSymbolName = groupSymbolNames[i];
groupNameIdx[groupSymbolName.toString()] = groupSymbolName;
}
for (var j = 0; j < names.length; j++) {
var groupStringName = names[j];
if (groupStringName in groupNameIdx) Opal.hash_delete(groups, groupNameIdx[groupStringName]);
}
};
/**
* @namespace
* @module Extensions/Registry
*/
var Registry = Extensions.Registry;
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.getGroups = Extensions.getGroups;
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.unregisterAll = function () {
this.groups = Opal.hash();
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.unregister = Extensions.unregister;
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.block = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'block', processor, name);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.inlineMacro = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'inline_macro', processor, name);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.includeProcessor = function (processor) {
return registerExtension(this, 'include_processor', processor);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.blockMacro = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'block_macro', processor, name);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.treeProcessor = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'tree_processor', processor, name);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.postprocessor = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'postprocessor', processor, name);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.preprocessor = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'preprocessor', processor, name);
};
/**
* @memberof Extensions/Registry
*/
Registry.$$proto.docinfoProcessor = function (name, processor) {
if (arguments.length === 1) {
processor = name;
name = null;
}
return registerExtension(this, 'docinfo_processor', processor, name);
};
/**
* @namespace
* @module Extensions/Processor
*/
var Processor = Extensions.Processor;
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.process = function (block) {
return Opal.send(this, 'process', null, toBlock(block));
};
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.named = function (name) {
return this.$named(name);
};
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.createBlock = function (parent, context, source, attrs, opts) {
return this.$create_block(parent, context, source, toHash(attrs), toHash(opts));
};
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.createImageBlock = function (parent, attrs, opts) {
return this.$create_image_block(parent, toHash(attrs), toHash(opts));
};
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.createInline = function (parent, context, text, opts) {
return this.$create_inline(parent, context, text, toHash(opts));
};
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.parseContent = function (parent, content, attrs) {
return this.$parse_content(parent, content, attrs);
};
/**
* @memberof Extensions/Processor
*/
Processor.$$proto.positionalAttributes = function (value) {
return this.$positional_attrs(value);
};
/**
* @namespace
* @module Extensions/BlockProcessor
*/
var BlockProcessor = Extensions.BlockProcessor;
/**
* @memberof Extensions/BlockProcessor
*/
BlockProcessor.$$proto.onContext = function (context) {
return this.$on_context(context);
};
/**
* @memberof Extensions/BlockProcessor
*/
BlockProcessor.$$proto.onContexts = function () {
return this.$on_contexts(Array.prototype.slice.call(arguments));
};
/**
* @namespace
* @module Extensions/BlockMacroProcessor
*/
// eslint-disable-next-line no-unused-vars
var BlockMacroProcessor = Extensions.BlockMacroProcessor;
/**
* @namespace
* @module Extensions/IncludeProcessor
*/
var IncludeProcessor = Extensions.IncludeProcessor;
/**
* @memberof Extensions/IncludeProcessor
*/
IncludeProcessor.$$proto.handles = function (block) {
return Opal.send(this, 'handles?', null, toBlock(block));
};
/**
* @namespace
* @module Extensions/TreeProcessor
*/
// eslint-disable-next-line no-unused-vars
var TreeProcessor = Extensions.TreeProcessor;
/**
* @namespace
* @module Extensions/Postprocessor
*/
// eslint-disable-next-line no-unused-vars
var Postprocessor = Extensions.Postprocessor;
/**
* @namespace
* @module Extensions/Preprocessor
*/
// eslint-disable-next-line no-unused-vars
var Preprocessor = Extensions.Preprocessor;
/**
* @namespace
* @module Extensions/DocinfoProcessor
*/
var DocinfoProcessor = Extensions.DocinfoProcessor;
/**
* @memberof Extensions/DocinfoProcessor
*/
DocinfoProcessor.$$proto.atLocation = function (value) {
this.$at_location(value);
};
// Converter API
/**
* @namespace
* @module Converter
*/
var Converter = Opal.const_get_qualified(Opal.Asciidoctor, 'Converter');
// Alias
Opal.Asciidoctor.Converter = Converter;
/**
* Convert the specified node.
*
* @param {AbstractNode} node - the AbstractNode to convert
* @param {string} transform - an optional String transform that hints at
* which transformation should be applied to this node.
* @param {Object} opts - a JSON of options that provide additional hints about
* how to convert the node (default: {})
* @returns the {Object} result of the conversion, typically a {string}.
* @memberof Converter
*/
Converter.$$proto.convert = function (node, transform, opts) {
return this.$convert(node, transform, toHash(opts));
};
// The built-in converter doesn't include Converter, so we have to force it
Converter.BuiltIn.$$proto.convert = Converter.$$proto.convert;
// Converter Factory API
/**
* @namespace
* @module Converter/Factory
*/
var ConverterFactory = Opal.Asciidoctor.Converter.Factory;
/**
* Retrieves the singleton instance of the converter factory.
*
* @param {boolean} initialize - instantiate the singleton if it has not yet
* been instantiated. If this value is false and the singleton has not yet been
* instantiated, this method returns a fresh instance.
* @returns {Converter/Factory} an instance of the converter factory.
* @memberof Converter/Factory
*/
ConverterFactory.getDefault = function (initialize) {
return this.$default(initialize);
};
/**
* Create an instance of the converter bound to the specified backend.
*
* @param {string} backend - look for a converter bound to this keyword.
* @param {Object} opts - a JSON of options to pass to the converter (default: {})
* @returns {Converter} - a converter instance for converting nodes in an Asciidoctor AST.
* @memberof Converter/Factory
*/
ConverterFactory.$$proto.create = function (backend, opts) {
return this.$create(backend, toHash(opts));
};