Skip to content
Snippets Groups Projects
Commit 94bd153b authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Add BB-code parser.

parent c756d4a4
No related branches found
No related tags found
No related merge requests found
let el = document.getElementsByClassName("nfo")[0].children[0]
let str = el.innerText
var r = /\[(\/)?(\w{0,5})(=([^\]]*))?\]/g
let result;
let stk = [];
let result_str = "";
let last_idx = 0;
let other;
while ((result = r.exec(str)) !== null) {
index = result.index
var [matched, end, tag, _, param] = result
if (! end) { /* if start tag */
o = Object()
o.tag = tag
o.str = ""
o.index = index + matched.length
o.param = param
if (stk.length === 0) {
result_str += str.substring(last_idx, index);
} else {
other = stk.last();
other.str += str.substring(other.index, index);
}
last_idx = o.index;
stk.push(o)
} else { /* if end tag */
parent = stk.pop()
if (parent.tag !== tag) {
alert("Non matching tags");
} else {
parent.str += str.substring(last_idx, index);
parent.str = parent.str.strip()
last_idx = index + matched.length;
var s;
switch (parent.tag) {
case "url":
if (typeof parent.param === "undefined") {
s = "<a href='" + parent.str;
} else {
s = "<a href='" + parent.param;
}
s += "'>" + parent.str + "</a>";
break;
case "img":
s = "<img src='" + parent.str + "'/>"
break;
default:
s = "<b>Unknown tag <pre>" + parent.str + "</pre></b>"
break;
}
if (stk.length === 0) {
result_str += s;
} else {
stk.last().str += s;
}
}
}
}
el.innerHTML = result_str
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment