/
shpaml.js
382 lines (316 loc) · 9.77 KB
/
shpaml.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
var SHPAML = (function () {
"use strict";
function syntax(regex, f) {
f.regex = regex;
return f;
}
// version number tracks to Python implementation
// in terms of passing test suite
var version = '0.99b',
//strings
PASS_SYNTAX = 'PASS',
FLUSH_LEFT_SYNTAX = '|| ',
FLUSH_LEFT_EMPTY_LINE = '||',
//regex
TAG_WHITESPACE_ATTRS = /(\S+)([ \t]*)(.*)/,
CLASS_OR_ID = /([.#])((?:[^ \t\.#]|\.\.)+)/g,
TAG_AND_REST = /((?:[^ \t\.#]|\.\.)+)(.*)/,
DIV_SHORTCUT = /^(?:#|(?:\.(?!\.)))/,
COMMENT_SYNTAX = /^::comment$/,
//syntax
INDENT = syntax(
/^([ \t]*)(.*)/,
function (m) {
var prefix, line;
prefix = m[1];
line = m[2];
line = rstrip(line);
if (line === '') {
prefix = '';
}
return [prefix, line];
}
),
RAW_HTML = syntax(
/^([<{]\S.*)/,
function (m) { return rstrip(m[1]); }
),
TEXT = syntax(
/^\| (.*)/,
function (m) { return rstrip(m[1]); }
),
OUTER_CLOSING_TAG = syntax(
/(.*?) > (.*)/,
function (m) { return enclose_tag(m[1], convert_line(m[2])); }
),
TEXT_ENCLOSING_TAG = syntax(
/(.*?) \| (.*)/,
function (m) { return enclose_tag(m[1], m[2]); }
),
SELF_CLOSING_TAG = syntax(
/^> (.*)/,
function (m) { return '<' + apply_jquery(strip(m[1]))[0] + ' />'; }
),
RAW_TEXT = syntax(
/^(.*)/,
function (m) { return rstrip(m[1]); }
),
LINE_METHODS = [
RAW_HTML,
TEXT,
OUTER_CLOSING_TAG,
TEXT_ENCLOSING_TAG,
SELF_CLOSING_TAG,
RAW_TEXT
];
// end of variable declarations
function strip(s) {
return s.replace(/^\s+|\s+$/g, '');
}
function rstrip(s) {
return s.replace(/\s+$/, '');
}
function startswith(haystack, neadle) {
return haystack.indexOf(neadle) === 0;
}
function rematch(regex, s) {
return regex.exec(s);
}
function convert_text(in_body) {
// You can call convert_text directly to convert shpaml markup
// to HTML markup.
return convert_shpaml_tree(in_body);
}
var quotedText = "(?:(?:'(?:\\\\'|[^'])*')" + '|(?:"(?:\\\\"|[^"])*"))';
var AUTO_QUOTE = new RegExp('((?:^|[ \\t]+)[^ \\t=]+=)(' + quotedText + '|[^ \\t]+)', 'g');
function AUTO_QUOTE_ATTRIBUTES(attrs) {
return attrs.replace(AUTO_QUOTE, function(s, $1, $2){
if ($2[0] === '"' || $2[0] === "'") {
return $1 + $2;
}
return $1 + '"' + $2 + '"'
});
}
function convert_shpaml_tree(in_body) {
return indent(in_body,
html_block_tag, //branch_method
convert_line, //leaf_method
PASS_SYNTAX, //pass_syntax
FLUSH_LEFT_SYNTAX, //flush_left_syntax
FLUSH_LEFT_EMPTY_LINE, //flush_left_empty_line
find_indentation //indentation_method
);
}
function html_block_tag(output, block, recurse) {
var tags, tag, prefix;
function append(ss) {
output.push(ss);
}
tags = block[0];
prefix = tags[0];
tag = tags[1];
//enclosed_block = recurse(block.slice(1));
if (rematch(RAW_HTML.regex, tag)) {
append(prefix + tag);
recurse(block.slice(1));
}
else if (rematch(COMMENT_SYNTAX, tag)) {
// do nothing
}
else {
tags = apply_jquery_sugar(tag);
append(prefix + tags[0]);
recurse(block.slice(1));
append(prefix + tags[1]);
}
}
function convert_line(line) {
var m, i, prefix, method;
line = find_indentation(strip(line)); //???
prefix = line[0];
line = line[1];
for (i = 0; i < LINE_METHODS.length; i++) {
method = LINE_METHODS[i];
m = rematch(method.regex, line);
if (m) {
return prefix + method(m);
}
}
return '';
}
function apply_jquery_sugar(markup) {
var tags;
if (rematch(DIV_SHORTCUT, markup)) {
markup = 'div' + markup;
}
tags = apply_jquery(markup);
return ['<' + tags[0] + '>', '</' + tags[1] + '>'];
}
function apply_jquery(markup) {
var a, m, tag, rest, ids, whitespace, attrs, classes, start_tag;
m = rematch(TAG_WHITESPACE_ATTRS, markup);
tag = m[1];
whitespace = m[2];
attrs = m[3];
a = tag_and_rest(tag);
tag = a[0];
rest = a[1];
a = ids_and_classes(rest);
ids = a[0];
classes = a[1];
attrs = AUTO_QUOTE_ATTRIBUTES(attrs);
if (classes) {
attrs += ' class="' + classes + '"';
}
if (ids) {
attrs += ' id="' + ids + '"';
}
start_tag = tag + whitespace + attrs;
return [start_tag, tag];
}
function ids_and_classes(rest) {
if (!rest) return ['', ''];
var ids = [], classes=[];
rest.replace(
CLASS_OR_ID,
function(s, $type, $name) {
$type === '#' ? ids.push($name) : classes.push($name)
}
);
return [fixdots(ids.join(' ')), fixdots(classes.join(' '))]
}
function fixdots(s) {
s = new String(s); // for rhino
return s.replace(/\.\./g, '.');
}
function tag_and_rest(tag) {
var m = rematch(TAG_AND_REST, tag);
if (m) {
return [fixdots(m[1]), m[2]];
}
else {
return [fixdots(tag), null];
}
}
function enclose_tag(tag, text) {
var a = apply_jquery_sugar(tag);
return a[0] + text + a[1];
}
function find_indentation(line) {
return INDENT(rematch(INDENT.regex, line));
}
function get_indented_block(prefix_lines) {
var a, i, prefix, new_prefix, line;
a = prefix_lines[0];
prefix = a[0];
line = a[1];
i = 1;
while (i < prefix_lines.length) {
a = prefix_lines[i];
new_prefix = a[0];
line = a[1];
if (line && (new_prefix.length <= prefix.length)) {
break;
}
i += 1;
}
while ((i > 1) && (strip(prefix_lines[i - 1][1]) === '')) {
i -= 1;
}
return i;
}
function indent(text,
branch_method,
leaf_method,
pass_syntax,
flush_left_syntax,
flush_left_empty_line,
indentation_method,
get_block //= get_indented_block
) {
var lines,
output = [];
if (!get_block) { get_block = get_indented_block; }
text = rstrip(text);
lines = text.split('\n');
lines = indent_lines(
lines,
output,
branch_method,
leaf_method,
pass_syntax,
flush_left_syntax,
flush_left_empty_line,
indentation_method,
get_block
);
text = output.join('\n') + '\n';
return text;
}
function indent_lines(
lines,
output,
branch_method,
leaf_method,
pass_syntax,
flush_left_syntax,
flush_left_empty_line,
indentation_method,
get_block
) {
var i;
function append(ss) {
output.push(ss);
}
function recurse(prefix_lines) {
var a, i, prefix, line, block, block_size;
while (prefix_lines.length) {
line = prefix_lines[0];
prefix = line[0];
line = line[1];
if (line === '') {
prefix_lines.shift();
append('');
}
else {
block_size = get_block(prefix_lines);
if (block_size === 1) {
prefix_lines.shift();
if (line === pass_syntax) {
//pass
}
else if (startswith(line, flush_left_syntax)) {
append(line.substring(flush_left_syntax.length));
}
else if (startswith(line, flush_left_empty_line)) {
append('');
}
else {
append(prefix + leaf_method(line));
}
}
else {
block = prefix_lines.slice(0, block_size);
prefix_lines = prefix_lines.slice(block_size);
branch_method(output, block, recurse);
}
}
}
}
for (i = 0; i < lines.length; i++ ) {
lines[i] = indentation_method(lines[i]);
}
recurse(lines);
}
return {
convert_text: convert_text,
indent: indent,
find_indentation: find_indentation,
PASS_SYNTAX: PASS_SYNTAX,
FLUSH_LEFT_SYNTAX: FLUSH_LEFT_SYNTAX,
FLUSH_LEFT_EMPTY_LINE: FLUSH_LEFT_EMPTY_LINE,
version: version
};
})();
if (typeof exports !== "undefined")
exports.SHPAML = SHPAML;