forked from commonmark/commonmark.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.js
More file actions
200 lines (174 loc) · 5.51 KB
/
xml.js
File metadata and controls
200 lines (174 loc) · 5.51 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
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
"use strict";
import Renderer from "./renderer.js";
import { escapeXml } from "../common.js";
var reXMLTag = /\<[^>]*\>/;
function toTagName(s) {
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
}
function XmlRenderer(options) {
options = options || {};
this.disableTags = 0;
this.lastOut = "\n";
this.indentLevel = 0;
this.indent = " ";
this.esc = options.esc || escapeXml;
// escape html with a custom function
// else use escapeXml
this.options = options;
}
function render(ast) {
this.buffer = "";
var attrs;
var tagname;
var walker = ast.walker();
var event, node, entering;
var container;
var selfClosing;
var nodetype;
var options = this.options;
if (options.time) {
console.time("rendering");
}
this.buffer += '<?xml version="1.0" encoding="UTF-8"?>\n';
this.buffer += '<!DOCTYPE document SYSTEM "CommonMark.dtd">\n';
while ((event = walker.next())) {
entering = event.entering;
node = event.node;
nodetype = node.type;
container = node.isContainer;
selfClosing =
nodetype === "thematic_break" ||
nodetype === "linebreak" ||
nodetype === "softbreak";
tagname = toTagName(nodetype);
if (entering) {
attrs = [];
switch (nodetype) {
case "document":
attrs.push(["xmlns", "http://commonmark.org/xml/1.0"]);
break;
case "list":
if (node.listType !== null) {
attrs.push(["type", node.listType.toLowerCase()]);
}
if (node.listStart !== null) {
attrs.push(["start", String(node.listStart)]);
}
if (node.listTight !== null) {
attrs.push([
"tight",
node.listTight ? "true" : "false"
]);
}
var delim = node.listDelimiter;
if (delim !== null) {
var delimword = "";
if (delim === ".") {
delimword = "period";
} else {
delimword = "paren";
}
attrs.push(["delimiter", delimword]);
}
break;
case "code_block":
if (node.info) {
attrs.push(["info", node.info]);
}
break;
case "heading":
attrs.push(["level", String(node.level)]);
break;
case "link":
case "image":
attrs.push(["destination", node.destination]);
attrs.push(["title", node.title]);
break;
case "custom_inline":
case "custom_block":
attrs.push(["on_enter", node.onEnter]);
attrs.push(["on_exit", node.onExit]);
break;
default:
break;
}
if (options.sourcepos) {
var pos = node.sourcepos;
if (pos) {
attrs.push([
"sourcepos",
String(pos[0][0]) +
":" +
String(pos[0][1]) +
"-" +
String(pos[1][0]) +
":" +
String(pos[1][1])
]);
}
}
this.cr();
this.out(this.tag(tagname, attrs, selfClosing));
if (container) {
this.indentLevel += 1;
} else if (!container && !selfClosing) {
var lit = node.literal;
if (lit) {
this.out(this.esc(lit));
}
this.out(this.tag("/" + tagname));
}
} else {
this.indentLevel -= 1;
this.cr();
this.out(this.tag("/" + tagname));
}
}
if (options.time) {
console.timeEnd("rendering");
}
this.buffer += "\n";
return this.buffer;
}
function out(s) {
if (this.disableTags > 0) {
this.buffer += s.replace(reXMLTag, "");
} else {
this.buffer += s;
}
this.lastOut = s;
}
function cr() {
if (this.lastOut !== "\n") {
this.buffer += "\n";
this.lastOut = "\n";
for (var i = this.indentLevel; i > 0; i--) {
this.buffer += this.indent;
}
}
}
// Helper function to produce an XML tag.
function tag(name, attrs, selfclosing) {
var result = "<" + name;
if (attrs && attrs.length > 0) {
var i = 0;
var attrib;
while ((attrib = attrs[i]) !== undefined) {
result += " " + attrib[0] + '="' + this.esc(attrib[1]) + '"';
i++;
}
}
if (selfclosing) {
result += " /";
}
result += ">";
return result;
}
// quick browser-compatible inheritance
XmlRenderer.prototype = Object.create(Renderer.prototype);
XmlRenderer.prototype.render = render;
XmlRenderer.prototype.out = out;
XmlRenderer.prototype.cr = cr;
XmlRenderer.prototype.tag = tag;
XmlRenderer.prototype.esc = escapeXml;
export default XmlRenderer;