forked from commonmark/commonmark.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
75 lines (65 loc) · 1.43 KB
/
renderer.js
File metadata and controls
75 lines (65 loc) · 1.43 KB
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
"use strict";
function Renderer() {}
/**
* Walks the AST and calls member methods for each Node type.
*
* @param ast {Node} The root of the abstract syntax tree.
*/
function render(ast) {
var walker = ast.walker(),
event,
type;
this.buffer = "";
this.lastOut = "\n";
while ((event = walker.next())) {
type = event.node.type;
if (this[type]) {
this[type](event.node, event.entering);
}
}
return this.buffer;
}
/**
* Concatenate a literal string to the buffer.
*
* @param str {String} The string to concatenate.
*/
function lit(str) {
this.buffer += str;
this.lastOut = str;
}
/**
* Output a newline to the buffer.
*/
function cr() {
if (this.lastOut !== "\n") {
this.lit("\n");
}
}
/**
* Concatenate a string to the buffer possibly escaping the content.
*
* Concrete renderer implementations should override this method.
*
* @param str {String} The string to concatenate.
*/
function out(str) {
this.lit(str);
}
/**
* Escape a string for the target renderer.
*
* Abstract function that should be implemented by concrete
* renderer implementations.
*
* @param str {String} The string to escape.
*/
function esc(str) {
return str;
}
Renderer.prototype.render = render;
Renderer.prototype.out = out;
Renderer.prototype.lit = lit;
Renderer.prototype.cr = cr;
Renderer.prototype.esc = esc;
export default Renderer;