Wiki Parser Rajesh

by epinapala

HTML

<textarea id="wikitext" cols="50" rows="10">
* OL 1
* OL 2
** OL 2.1
** OL 2.2

* OL 3
* OL 4  

# UL 1
# UL 2
## UL 2.1
## UL 2.2

# UL 3
# UL 4 
* bullet1
bullet1cont

kkk

# iii
# ddd
lll

* bullet2
bullet2 cont
</textarea>
<br>
<input id="btnsave" type="button" value="Preview HTML" />
<br>
<hr>
<div id="html"></div>

JavaScript

document.getElementById("btnsave").addEventListener("click", parseWiki, false);



/**
 * This is a JS function to convert yioop wiki markup to
 * html.
 * @param {String} wiki_text tobe parsed as HTML
 * @returns {String} parsed html
 */
function displayParsedContent(wiki_text)
{
    var html = wiki_text;
    
    //note that line breaks from a text area are sent
    //as \r\n , so make sure we clean them up to replace
    //all \r\n with \n
    html = html.replace(/\r\n/g, "\n");
    html = parseLists2(html);
    //Regex replace for headings
    html = html.replace(/(?:^|\n)([=]+)(.*)\1/g,
            function (match, contents, t) {
                return '<h'
                        + contents.length + '>'
                        + t
                        + '</h'
                        + contents.length
                        + '>';
            });

    //Regex replace for Bold characters
    html = html.replace(/'''(.*?)'''/g, function (match, contents) {
        return '<strong>' + contents + '</strong>';
    });

    //Regex replace for Italic characters
    html = html.replace(/''(.*?)''/g, function (match, contents) {
        return '<em>' + contents + '</em>';
    });

    

    //Regex replace normal links
    html = html.replace(/[^\[](http[^\[\s]*)/g, function(m, l) {
        // normal link
        return '<a href="' + l + '">' + l + '</a>';
    });
    //Regex replace for external links
    html = html.replace(/[\[](http.*)[!\]]/g, function(m, l) {
        // external link
        var p = l.replace(/[\[\]]/g, '').split(/ /);
        var link = p.shift();
        return '<a href="' + link + '">' + (p.length ? p.join(' ') :
            link) + '</a>';
    });
    //Regex replace for headings
    html = html.replace(/(?:^|\n)([=]+)(.*)\1/g,
        function(match, contents, t) {
            return '<h' + contents.length + '>' + t + '</h' + contents.length +
                '>';
        });
    //Regex replace for Bold characters
    html =...