diff --git a/js/md.js b/js/md.js new file mode 100644 index 0000000..96893a6 --- /dev/null +++ b/js/md.js @@ -0,0 +1,127 @@ +/* jshint browser: true, devel: true */ + +/** + * preg_replace (from PHP) in JavaScript! + * + * This is basically a pattern replace. You can use a regex pattern to search and + * another for the replace. For more information see the PHP docs on the original + * function (http://php.net/manual/en/function.preg-replace.php), and for more on + * JavaScript flavour regex visit http://www.regular-expressions.info/javascript.html + * + * NOTE: Unlike the PHP version, this function only deals with string inputs. No arrays. + * + * @author William Duyck + * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License 2.0 + * + * @param {String} pattern The pattern to search for. + * @param {String} replace The string to replace. + * @param {String} subject The string to search and replace. + * @param {Integer} limit The maximum possible replacements. + * @return {String} If matches are found, the new subject will be returned. + */ +var preg_replace=function(a,b,c,d){void 0===d&&(d=-1);var e=a.substr(a.lastIndexOf(a[0])+1),f=a.substr(1,a.lastIndexOf(a[0])-1),g=RegExp(f,e),i=[],j=0,k=0,l=c,m=[];if(-1===d){do m=g.exec(c),null!==m&&i.push(m);while(null!==m&&-1!==e.indexOf("g"))}else i.push(g.exec(c));for(j=i.length-1;j>-1;j--){for(m=b,k=i[j].length;k>-1;k--)m=m.replace("${"+k+"}",i[j][k]).replace("$"+k,i[j][k]).replace("\\"+k,i[j][k]);l=l.replace(i[j][0],m)}return l}; + +/** + * Basic Markdown Parser + * + * This function parses a small subset of the Markdown language as defined by + * [John Gruber](http://daringfireball.net/projects/markdown). It's very basic + * and needs to be refactored a little, and there are plans to add more support + * for the rest of the language in the near future. + * + * This implimentation is based loosely on + * [slimdown.php](https://gist.github.com/jbroadway/2836900) by Johnny Broadway. + * + * @version 0.1 + * @author William Duyck + * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License 2.0 + * + * @param {String} str A Markdown string to be converted to HTML. + * @return {String} The HTML for the given Markdown. + */ +var markdown_parser = function(str){ + + var rules = [ + // headers + ['/(#+)(.*)/g', function(chars, header){ + var level = chars.length; + return ''+header.trim()+''; + }], + // images + ['/\\!\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '\"\\1\"'], + // link + ['/\\[([^\\[]+)\\]\\(([^\\(]+)\\)/g', '\\1'], + // bold + ['/(\\*\\*|__)(.*?)\\1/g', '\\2'], + // emphasis + ['/(\\*|_)(.*?)\\1/g', '\\2'], + // strike + ['/(\\~\\~)(.*?)\\1/g', '\\2'], + // quote + ['/\\:\\"(.*?)\\"\\:/g', '\\1'], + // unordered list + ['/\\n\\*(.*)/g', function(item){ + return ''; + }], + // ordered list + ['/\\n[0-9]+\\.(.*)/g', function(item){ + return '
    \n
  1. '+item.trim()+'
  2. \n
'; + }], + // blockquote + ['/\\n\\>(.*)/g', function(str){ + return '
'+str.trim()+'
'; + }], + // paragraphs + ['/\\n[^\\n]+\\n/g', function(line){ + line = line.trim(); + if(line[0] === '<'){ + return line; + } + return '\n

'+line+'

\n'; + }] + ], fixes = [ + ['/<\\/ul>\n