/*
@author: remy sharp / http://remysharp.com
@url: http://remysharp.com/2008/04/01/wiki-to-html-using-javascript/
@license: Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/
@version: 1.0
Can extend String or be used stand alone - just change the flag at the top of the script.
*/
(function () {
var extendString = true;
if (extendString) {
String.prototype.wiki2html = wiki2html;
String.prototype.iswiki = iswiki;
} else {
window.wiki2html = wiki2html;
window.iswiki = iswiki;
}
// utility function to check whether it's worth running through the wiki2html
function iswiki(s) {
if (extendString) {
s = this;
}
return !!(s.match(/^[\s{2} `#\*='{2}]/m));
}
// the regex beast...
function wiki2html(s) {
if (extendString) {
s = this;
}
// lists need to be done using a function to allow for recusive calls
function list(str) {
return str.replace(/(?:(?:(?:^|\n)[\*#].*)+)/g, function (m) { // (?=[\*#])
var type = m.match(/(^|\n)#/) ? 'OL' : 'UL';
// strip first layer of list
m = m.replace(/(^|\n)[\*#][ ]{0,1}/g, "$1");
m = list(m);
return '<' + type + '>
' + m.replace(/^\n/, '').split(/\n/).join('') + '' + type + '>';
});
}
return list(s
/* tables */
.replace(/\{\|([.\n\s\S]*?)\|\}/gim, function (m, l) {
var to_return = '';
var rows = l.split("|-");
if (!rows) {
return m;
}
// add the caption, if it exists
var caption = '';
var i = 0;
if (rows[0].match(/\|\+\s*?(.*?)\n/gim)) {
to_return += "FIXME: SOME CAPTION";
i++;
}
to_return += '';
var num_rows = rows.length;
while (i < num_rows) {
if (!rows[i].match(/[^\s\n]/)) {
i++;
continue;
}
rows[i] = rows[i].replace(/\|\|\s*(.*?)/gim, "\n|$1");
rows[i] = rows[i].split("\n|");
to_return += "" + rows[i].join(" | ") + " |
";
i++;
}
return to_return + '
';
})
/* BLOCK ELEMENTS */
.replace(/(?:^\s*|\n+\s*)([^\s\+\|#=\*<].+)(?:\s*\n+|$)/gm, function (m, l) {
if (l.match(/^\^+$/)) return l;
return "" + l + "
\n";
})
.replace(/(?:^|\n)[ ]{2}(.*)+/g, function (m, l) { // blockquotes
if (l.match(/^\s+$/)) return m;
return '' + l + '
';
})
.replace(/((?:^|\n)[ ]+.*)+/g, function (m) { // code
if (m.match(/^\s+$/)) return m;
return '' + m.replace(/(^|\n)[ ]+/g, "$1") + '
';
})
.replace(/^([=]+)(.*?)\1/mg, function (m, l, t) { // headings
return '' + t + '';
})
/* INLINE ELEMENTS */
.replace(/'''(.*?)'''/g, function (m, l) { // bold
return '' + l + '';
})
.replace(/''(.*?)''/g, function (m, l) { // italic
return '' + l + '';
})
.replace(/[^\[](http[^\[\s]*)/g, function (m, l) { // normal link
return '' + l + '';
})
.replace(/[\[](http.*)[!\]]/g, function (m, l) { // external link
var p = l.replace(/[\[\]]/g, '').split(/ /);
var link = p.shift();
return '' + (p.length ? p.join(' ') : link) + '';
})
.replace(/\[\[(.*?)\]\]/g, function (m, l) { // internal link or image
var p = l.split(/\|/);
var link = p.shift();
if (link.match(/^Image:(.*)/)) {
// no support for images - since it looks up the source from the wiki db :-(
return m;
} else {
return '' + (p.length ? p.join('|') : link) + '';
}
})
);
}
})();