1 // -----------------------------------------------------------------------
2 // Copyright (c) 2008, Stone Steps Inc.
4 // http://www.stonesteps.ca/legal/bsd-license/
6 // This is a BBCode parser written in JavaScript. The parser is intended
7 // to demonstrate how to parse text containing BBCode tags in one pass
8 // using regular expressions.
10 // The parser may be used as a backend component in ASP or in the browser,
11 // after the text containing BBCode tags has been served to the client.
13 // Following BBCode expressions are recognized:
18 // [s]strike-through[/s]
19 // [samp]sample[/samp]
21 // [color=red]red[/color]
22 // [color=#FF0000]red[/color]
23 // [size=1.2]1.2em[/size]
25 // [url]http://blogs.stonesteps.ca/showpost.asp?pid=33[/url]
26 // [url=http://blogs.stonesteps.ca/showpost.asp?pid=33][b]BBCode[/b] Parser[/url]
28 // [q=http://blogs.stonesteps.ca/showpost.asp?pid=33]inline quote[/q]
29 // [q]inline quote[/q]
30 // [blockquote=http://blogs.stonesteps.ca/showpost.asp?pid=33]block quote[/blockquote]
31 // [blockquote]block quote[/blockquote]
36 // print("done");[/code]
38 // text containing [noparse] [brackets][/noparse]
40 // -----------------------------------------------------------------------
41 var opentags; // open tag stack
42 var crlf2br = true; // convert CRLF to <br>?
43 var noparse = false; // ignore BBCode tags?
44 var urlstart = -1; // beginning of the URL if zero or greater (ignored if -1)
46 // aceptable BBcode tags, optionally prefixed with a slash
47 var tagname_re = /^\/?(?:b|i|u|pre|samp|code|colou?r|size|noparse|url|s|q|blockquote)$/;
49 // color names or hex color
50 var color_re = /^(:?black|silver|gray|white|maroon|red|purple|fuchsia|green|lime|olive|yellow|navy|blue|teal|aqua|#(?:[0-9a-f]{3})?[0-9a-f]{3})$/i;
53 var number_re = /^[\\.0-9]{1,8}$/i;
55 // reserved, unreserved, escaped and alpha-numeric [RFC2396]
56 var uri_re = /^[-;\/\?:@&=\+\$,_\.!~\*'\(\)%0-9a-z]{1,512}$/i;
58 // main regular expression: CRLF, [tag=option], [tag] or [/tag]
59 var postfmt_re = /([\r\n])|(?:\[([a-z]{1,16})(?:=([^\x00-\x1F"'\(\)<>\[\]]{1,256}))?\])|(?:\[\/([a-z]{1,16})\])/ig;
62 function taginfo_t(bbtag, etag)
68 // check if it's a valid BBCode tag
69 function isValidTag(str)
71 if(!str || !str.length)
74 return tagname_re.test(str);
79 // m2 - the tag of the [tag=option] expression
80 // m3 - the option of the [tag=option] expression
81 // m4 - the end tag of the [/tag] expression
83 function textToHtmlCB(mstr, m1, m2, m3, m4, offset, string)
104 // if in the noparse state, just echo the tag
106 return "[" + m2 + "]";
108 // ignore any tags if there's an open option-less [url] tag
109 if(opentags.length && opentags[opentags.length-1].bbtag == "url" && urlstart >= 0)
110 return "[" + m2 + "]";
114 opentags.push(new taginfo_t(m2, "</code></pre>"));
116 return "<pre><code>";
119 opentags.push(new taginfo_t(m2, "</pre>"));
125 if(!m3 || !color_re.test(m3))
127 opentags.push(new taginfo_t(m2, "</span>"));
128 return "<span style=\"color: " + m3 + "\">";
131 if(!m3 || !number_re.test(m3))
133 opentags.push(new taginfo_t(m2, "</span>"));
134 return "<span style=\"font-size: " + Math.min(Math.max(m3, 0.7), 3) + "em\">";
137 opentags.push(new taginfo_t(m2, "</span>"));
138 return "<span style=\"text-decoration: line-through\">";
145 opentags.push(new taginfo_t(m2, "</a>"));
147 // check if there's a valid option
148 if(m3 && uri_re.test(m3)) {
149 // if there is, output a complete start anchor tag
151 return "<a href=\"" + m3 + "\">";
154 // otherwise, remember the URL offset
155 urlstart = mstr.length + offset;
157 // and treat the text following [url] as a URL
162 opentags.push(new taginfo_t(m2, "</" + m2 + ">"));
163 return m3 && m3.length && uri_re.test(m3) ? "<" + m2 + " cite=\"" + m3 + "\">" : "<" + m2 + ">";
166 opentags.push(new taginfo_t(m2, "</span>"));
167 return "<span style=\"font-weight: bold\">";
170 opentags.push(new taginfo_t(m2, "</span>"));
171 return "<span style=\"font-style:italic\">";
174 opentags.push(new taginfo_t(m2, "</span>"));
175 return "<span style=\"text-decoration: underline\">";
178 // [samp]don't need special processing
179 opentags.push(new taginfo_t(m2, "</" + m2 + ">"));
180 return "<" + m2 + ">";
190 // if it's the closing noparse tag, flip the noparse state
191 if(m4 == "noparse") {
196 // otherwise just output the original text
197 return "[/" + m4 + "]";
200 // highlight mismatched end tags
201 if(!opentags.length || opentags[opentags.length-1].bbtag != m4)
202 return "<span style=\"color: red\">[/" + m4 + "]</span>";
205 // if there was no option, use the content of the [url] tag
207 return "\">" + string.substr(urlstart, offset-urlstart) + opentags.pop().etag;
209 // otherwise just close the tag
210 return opentags.pop().etag;
212 else if(m4 == "code" || m4 == "pre")
215 // other tags require no special processing, just output the end tag
216 return opentags.pop().etag;
223 // post must be HTML-encoded
225 function parseBBCode(post)
227 var result, endtags, tag;
229 // convert CRLF to <br> by default
232 // create a new array for open tags
233 if(opentags == null || opentags.length)
234 opentags = new Array(0);
236 // run the text through main regular expression matcher
237 result = post.replace(postfmt_re, textToHtmlCB);
239 // reset noparse, if it was unbalanced
243 // if there are any unbalanced tags, make sure to close them
244 if(opentags.length) {
245 endtags = new String();
247 // if there's an open [url] at the top, close it
248 if(opentags[opentags.length-1].bbtag == "url") {
250 endtags += "\">" + post.substr(urlstart, post.length-urlstart) + "</a>";
253 // close remaining open tags
254 while(opentags.length)
255 endtags += opentags.pop().etag;
258 return endtags ? result + endtags : result;