-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathshortcode-parser.js
More file actions
155 lines (120 loc) · 4.01 KB
/
Copy pathshortcode-parser.js
File metadata and controls
155 lines (120 loc) · 4.01 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
/* lib/shortcode-parser.js */
var fs = require('fs');
var util = require('util');
var shortcodes = {};
var SHORTCODE_ATTRS = /(\s+([a-z0-9\-_]+|([a-z0-9\-_]+)\s*=\s*([a-z0-9\-_]+|\d+\.\d+|'[^']*'|"[^"]*")))*/.toString().slice(1,-1);
var SHORTCODE_SLASH = /\s*\/?\s*/.toString().slice(1,-1);
var SHORTCODE_OPEN = /\[\s*%s/.toString().slice(1,-1);
var SHORTCODE_RIGHT_BRACKET = '\\]';
var SHORTCODE_CLOSE = /\[\s*\/\s*%s\s*\]/.toString().slice(1,-1);
var SHORTCODE_CONTENT = /(.|\n|)*?/.toString().slice(1,-1);
var SHORTCODE_SPACE = /\s*/.toString().slice(1,-1);
function typecast(val) {
val = val.trim().replace(/(^['"]|['"]$)/g, '');
if (/^\d+$/.test(val)) {
return parseInt(val, 10);
} else if (/^\d+\.\d+$/.test(val)) {
return parseFloat(val);
} else if (/^(true|false)$/.test(val)) {
return (val === 'true');
} else if (/^undefined$/.test(val)) {
return undefined;
} else if (/^null$/i.test(val)) {
return null;
} else {
return val;
}
}
function closeTagString(name) {
return /^[^a-z0-9]/.test(name) ? util.format('[%s]?%s', name[0].replace('$', '\\$'), name.slice(1)) : name;
}
function parseShortcode(name, buf, inline) {
var regex, match, data = {}, attr = {};
if (inline) {
regex = new RegExp('^' + util.format(SHORTCODE_OPEN, name)
+ SHORTCODE_ATTRS
+ SHORTCODE_SPACE
+ SHORTCODE_SLASH
+ SHORTCODE_RIGHT_BRACKET, 'i');
} else {
regex = new RegExp('^' + util.format(SHORTCODE_OPEN, name)
+ SHORTCODE_ATTRS
+ SHORTCODE_SPACE
+ SHORTCODE_RIGHT_BRACKET, 'i');
}
while ((match = buf.match(regex)) !== null) {
var key = match[3] || match[2];
var val = match[4] || match[3];
var pattern = match[1];
if (pattern) {
var idx = buf.lastIndexOf(pattern);
attr[key] = (val !== undefined) ? typecast(val) : true;
buf = buf.slice(0, idx) + buf.slice(idx + pattern.length);
} else {
break;
}
}
attr = Object.keys(attr).reverse().reduce(function(prev, current) {
prev[current] = attr[current]; return prev;
}, {});
buf = buf.replace(regex, '').replace(new RegExp(util.format(SHORTCODE_CLOSE, closeTagString(name))), '');
return {
attr: attr,
content: inline ? buf : buf.replace(/(^\n|\n$)/g, '')
}
}
module.exports = {
_shortcodes: shortcodes,
add: function (name, callback) {
if (typeof name == 'object') {
var ob = name;
for (var m in ob) { // Adding methods from instance and prototype
if (ob[m] instanceof Function) {
shortcodes[m] = ob[m];
}
}
} else {
shortcodes[name] = callback;
}
},
remove: function(name) {
delete shortcodes[name];
},
parse: function(buf, extra, context) {
context = context || shortcodes;
extra = extra || {};
for (var name in context) {
// Allow absence of first char if not alpha numeric. E.g. [#shortcode]...[/shortcode]
var regex = {
wrapper: new RegExp(util.format(SHORTCODE_OPEN, name)
+ SHORTCODE_ATTRS
+ SHORTCODE_RIGHT_BRACKET
+ SHORTCODE_CONTENT
+ util.format(SHORTCODE_CLOSE, closeTagString(name)), 'gi'),
inline: new RegExp(util.format(SHORTCODE_OPEN, name)
+ SHORTCODE_ATTRS
+ SHORTCODE_SLASH
+ SHORTCODE_RIGHT_BRACKET, 'gi')
}
var matches = buf.match(regex.wrapper);
if (matches) {
for (var m,data,i=0,len=matches.length; i < len; i++) {
m = matches[i];
data = parseShortcode(name, m);
buf = buf.replace(m, context[name].call(null, data.content, data.attr, extra));
}
}
matches = buf.match(regex.inline);
if (matches) {
while((m = matches.shift()) !== undefined) {
data = parseShortcode(name, m, true);
buf = buf.replace(m, context[name].call(null, data.content, data.attr, extra));
}
}
}
return buf;
},
parseInContext: function(buf, context, data) {
return this.parse(buf, data, context);
}
}