Skip to content

Commit e5dd8b4

Browse files
authored
Merge pull request commonmark#114 from tmpfs/renderer-tidy
Renderer tidy
2 parents fa0ac09 + 872ec55 commit e5dd8b4

File tree

3 files changed

+160
-146
lines changed

3 files changed

+160
-146
lines changed

lib/render/html.js

+89-91
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,35 @@
22

33
var Renderer = require('./renderer');
44

5-
var esc = require('../common').escapeXml;
6-
75
var reUnsafeProtocol = /^javascript:|vbscript:|file:|data:/i;
86
var reSafeDataProtocol = /^data:image\/(?:png|gif|jpeg|webp)/i;
97

108
var potentiallyUnsafe = function(url) {
11-
return reUnsafeProtocol.test(url) &&
12-
!reSafeDataProtocol.test(url);
9+
return reUnsafeProtocol.test(url) &&
10+
!reSafeDataProtocol.test(url);
1311
};
1412

1513
// Helper function to produce an HTML tag.
1614
function tag(name, attrs, selfclosing) {
17-
if (this.disableTags > 0) {
18-
return;
19-
}
20-
this.buffer += ('<' + name);
21-
if (attrs && attrs.length > 0) {
22-
var i = 0;
23-
var attrib;
24-
while ((attrib = attrs[i]) !== undefined) {
25-
this.buffer += (' ' + attrib[0] + '="' + attrib[1] + '"');
26-
i++;
27-
}
28-
}
29-
if (selfclosing) {
30-
this.buffer += ' /';
15+
if (this.disableTags > 0) {
16+
return;
17+
}
18+
this.buffer += ('<' + name);
19+
if (attrs && attrs.length > 0) {
20+
var i = 0;
21+
var attrib;
22+
while ((attrib = attrs[i]) !== undefined) {
23+
this.buffer += (' ' + attrib[0] + '="' + attrib[1] + '"');
24+
i++;
3125
}
32-
this.buffer += '>';
33-
this.lastOut = '>';
26+
}
27+
if (selfclosing) {
28+
this.buffer += ' /';
29+
}
30+
this.buffer += '>';
31+
this.lastOut = '>';
3432
}
3533

36-
3734
function HtmlRenderer(options) {
3835
options = options || {};
3936
// by default, soft breaks are rendered as newlines in HTML
@@ -64,38 +61,37 @@ function linebreak() {
6461
function link(node, entering) {
6562
var attrs = this.attrs(node);
6663
if (entering) {
67-
if (!(this.options.safe && potentiallyUnsafe(node.destination))) {
68-
attrs.push(['href', esc(node.destination, true)]);
69-
}
70-
if (node.title) {
71-
attrs.push(['title', esc(node.title, true)]);
72-
}
73-
this.tag('a', attrs);
64+
if (!(this.options.safe && potentiallyUnsafe(node.destination))) {
65+
attrs.push(['href', this.esc(node.destination, true)]);
66+
}
67+
if (node.title) {
68+
attrs.push(['title', this.esc(node.title, true)]);
69+
}
70+
this.tag('a', attrs);
7471
} else {
75-
this.tag('/a');
72+
this.tag('/a');
7673
}
7774
}
7875

7976
function image(node, entering) {
8077
if (entering) {
81-
if (this.disableTags === 0) {
82-
if (this.options.safe &&
83-
potentiallyUnsafe(node.destination)) {
84-
this.lit('<img src="" alt="');
85-
} else {
86-
this.lit('<img src="' + esc(node.destination, true) +
87-
'" alt="');
88-
}
78+
if (this.disableTags === 0) {
79+
if (this.options.safe && potentiallyUnsafe(node.destination)) {
80+
this.lit('<img src="" alt="');
81+
} else {
82+
this.lit('<img src="' + this.esc(node.destination, true) +
83+
'" alt="');
8984
}
90-
this.disableTags += 1;
85+
}
86+
this.disableTags += 1;
9187
} else {
92-
this.disableTags -= 1;
93-
if (this.disableTags === 0) {
94-
if (node.title) {
95-
this.lit('" title="' + esc(node.title, true));
96-
}
97-
this.lit('" />');
88+
this.disableTags -= 1;
89+
if (this.disableTags === 0) {
90+
if (node.title) {
91+
this.lit('" title="' + this.esc(node.title, true));
9892
}
93+
this.lit('" />');
94+
}
9995
}
10096
}
10197

@@ -111,29 +107,29 @@ function paragraph(node, entering) {
111107
var grandparent = node.parent.parent
112108
, attrs = this.attrs(node);
113109
if (grandparent !== null &&
114-
grandparent.type === 'list') {
115-
if (grandparent.listTight) {
116-
return;
117-
}
110+
grandparent.type === 'list') {
111+
if (grandparent.listTight) {
112+
return;
113+
}
118114
}
119115
if (entering) {
120-
this.cr();
121-
this.tag('p', attrs);
116+
this.cr();
117+
this.tag('p', attrs);
122118
} else {
123-
this.tag('/p');
124-
this.cr();
119+
this.tag('/p');
120+
this.cr();
125121
}
126122
}
127123

128124
function heading(node, entering) {
129125
var tagname = 'h' + node.level
130126
, attrs = this.attrs(node);
131127
if (entering) {
132-
this.cr();
133-
this.tag(tagname, attrs);
128+
this.cr();
129+
this.tag(tagname, attrs);
134130
} else {
135-
this.tag('/' + tagname);
136-
this.cr();
131+
this.tag('/' + tagname);
132+
this.cr();
137133
}
138134
}
139135

@@ -147,7 +143,7 @@ function code_block(node) {
147143
var info_words = node.info ? node.info.split(/\s+/) : []
148144
, attrs = this.attrs(node);
149145
if (info_words.length > 0 && info_words[0].length > 0) {
150-
attrs.push(['class', 'language-' + esc(info_words[0], true)]);
146+
attrs.push(['class', 'language-' + this.esc(info_words[0], true)]);
151147
}
152148
this.cr();
153149
this.tag('pre');
@@ -168,13 +164,13 @@ function thematic_break(node) {
168164
function block_quote(node, entering) {
169165
var attrs = this.attrs(node);
170166
if (entering) {
171-
this.cr();
172-
this.tag('blockquote', attrs);
173-
this.cr();
167+
this.cr();
168+
this.tag('blockquote', attrs);
169+
this.cr();
174170
} else {
175-
this.cr();
176-
this.tag('/blockquote');
177-
this.cr();
171+
this.cr();
172+
this.tag('/blockquote');
173+
this.cr();
178174
}
179175
}
180176

@@ -183,81 +179,81 @@ function list(node, entering) {
183179
, attrs = this.attrs(node);
184180

185181
if (entering) {
186-
var start = node.listStart;
187-
if (start !== null && start !== 1) {
188-
attrs.push(['start', start.toString()]);
189-
}
190-
this.cr();
191-
this.tag(tagname, attrs);
192-
this.cr();
182+
var start = node.listStart;
183+
if (start !== null && start !== 1) {
184+
attrs.push(['start', start.toString()]);
185+
}
186+
this.cr();
187+
this.tag(tagname, attrs);
188+
this.cr();
193189
} else {
194-
this.cr();
195-
this.tag('/' + tagname);
196-
this.cr();
190+
this.cr();
191+
this.tag('/' + tagname);
192+
this.cr();
197193
}
198194
}
199195

200196
function item(node, entering) {
201197
var attrs = this.attrs(node);
202198
if (entering) {
203-
this.tag('li', attrs);
199+
this.tag('li', attrs);
204200
} else {
205-
this.tag('/li');
206-
this.cr();
201+
this.tag('/li');
202+
this.cr();
207203
}
208204
}
209205

210206
function html_inline(node) {
211207
if (this.options.safe) {
212-
this.lit('<!-- raw HTML omitted -->');
208+
this.lit('<!-- raw HTML omitted -->');
213209
} else {
214-
this.lit(node.literal);
210+
this.lit(node.literal);
215211
}
216212
}
217213

218214
function html_block(node) {
219215
this.cr();
220216
if (this.options.safe) {
221-
this.lit('<!-- raw HTML omitted -->');
217+
this.lit('<!-- raw HTML omitted -->');
222218
} else {
223-
this.lit(node.literal);
219+
this.lit(node.literal);
224220
}
225221
this.cr();
226222
}
227223

228224
function custom_inline(node, entering) {
229225
if (entering && node.onEnter) {
230-
this.lit(node.onEnter);
226+
this.lit(node.onEnter);
231227
} else if (!entering && node.onExit) {
232-
this.lit(node.onExit);
228+
this.lit(node.onExit);
233229
}
234230
}
235231

236232
function custom_block(node, entering) {
237233
this.cr();
238234
if (entering && node.onEnter) {
239-
this.lit(node.onEnter);
235+
this.lit(node.onEnter);
240236
} else if (!entering && node.onExit) {
241-
this.lit(node.onExit);
237+
this.lit(node.onExit);
242238
}
243239
this.cr();
244240
}
245241

246242
/* Helper methods */
247243

248244
function out(s) {
249-
this.lit(esc(s, false));
245+
this.lit(this.esc(s, false));
250246
}
251247

252248
function attrs (node) {
253249
var att = [];
254250
if (this.options.sourcepos) {
255-
var pos = node.sourcepos;
256-
if (pos) {
257-
att.push(['data-sourcepos', String(pos[0][0]) + ':' +
258-
String(pos[0][1]) + '-' + String(pos[1][0]) + ':' +
259-
String(pos[1][1])]);
260-
}
251+
var pos = node.sourcepos;
252+
if (pos) {
253+
att.push(['data-sourcepos', String(pos[0][0]) + ':' +
254+
String(pos[0][1]) + '-' + String(pos[1][0]) + ':' +
255+
String(pos[1][1])]);
256+
}
261257
}
262258
return att;
263259
}
@@ -285,6 +281,8 @@ HtmlRenderer.prototype.item = item;
285281
HtmlRenderer.prototype.custom_inline = custom_inline;
286282
HtmlRenderer.prototype.custom_block = custom_block;
287283

284+
HtmlRenderer.prototype.esc = require('../common').escapeXml;
285+
288286
HtmlRenderer.prototype.out = out;
289287
HtmlRenderer.prototype.tag = tag;
290288
HtmlRenderer.prototype.attrs = attrs;

lib/render/renderer.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ function lit(str) {
3434
this.lastOut = str;
3535
}
3636

37+
/**
38+
* Output a newline to the buffer.
39+
*/
3740
function cr() {
38-
if (this.lastOut !== '\n') {
39-
this.lit('\n');
40-
}
41+
if (this.lastOut !== '\n') {
42+
this.lit('\n');
43+
}
4144
}
4245

4346
/**
@@ -51,9 +54,22 @@ function out(str) {
5154
this.lit(str);
5255
}
5356

57+
/**
58+
* Escape a string for the target renderer.
59+
*
60+
* Abstract function that should be implemented by concrete
61+
* renderer implementations.
62+
*
63+
* @param str {String} The string to escape.
64+
*/
65+
function esc(str) {
66+
return str;
67+
}
68+
5469
Renderer.prototype.render = render;
5570
Renderer.prototype.out = out;
5671
Renderer.prototype.lit = lit;
5772
Renderer.prototype.cr = cr;
73+
Renderer.prototype.esc = esc;
5874

5975
module.exports = Renderer;

0 commit comments

Comments
 (0)