forked from codeholic/jscreole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creole.min.js
6 lines (6 loc) · 7.22 KB
/
creole.min.js
1
2
3
4
5
6
/*!
* Copyright (c) 2011 Ivan Fomichev
* http://github.com/codeholic/jscreole
* Licensed under MIT license, see http://www.opensource.org/licenses/mit-license.php
*/
var creole=function(a){var e={};e._link="[^\\]|~\\n]*(?:(?:\\](?!\\])|~.)[^\\]|~\\n]*)*";e._linkText="[^\\]~\\n]*(?:(?:\\](?!\\])|~.)[^\\]~\\n]*)*";e._uriPrefix="\\b(?:(?:https?|ftp)://|mailto:)";e._uri=e._uriPrefix+e._link;e._rawUri=e._uriPrefix+"\\S*[^\\s!\"',.:;?]";e._interwikiLink="[\\w.]+:"+e._link;e._img="\\{\\{((?!\\{)[^|}\\n]*(?:}(?!})[^|}\\n]*)*)"+(a&&a.strict?"":"(?:")+"\\|([^}~\\n]*((}(?!})|~.)[^}~\\n]*)*)"+(a&&a.strict?"":")?")+"}}";var d=function(f,g){if(g instanceof Function){return g(f)}g=g instanceof Array?g:[g];if(typeof g[1]=="undefined"){g[1]=""}return g[0]+f+g[1]};var c={_hr:{_tag:"hr",_regex:/(^|\n)\s*----\s*(\n|$)/},_br:{_tag:"br",_regex:/\\\\/},_preBlock:{_tag:"pre",_capture:1,_regex:/\{\{\{\n((.*\n)*?)\}\}\}(\n|$)/,_replaceRegex:/^ ([ \t]*\}\}\})/gm,_replaceString:"$1"},_tt:{_tag:"tt",_regex:/\{\{\{(.*?\}\}\}+)/,_capture:1,_replaceRegex:/\}\}\}$/,_replaceString:""},_ulist:{_tag:"ul",_capture:0,_regex:/(^|\n)([ \t]*\*[^*#].*(\n|$)([ \t]*[^\s*#({{{)].*(\n|$))*([ \t]*[*#]{2}.*(\n|$))*)+/},_olist:{_tag:"ol",_capture:0,_regex:/(^|\n)([ \t]*#[^*#].*(\n|$)([ \t]*[^\s*#({{{)].*(\n|$))*([ \t]*[*#]{2}.*(\n|$))*)+/},_li:{_tag:"li",_capture:0,_regex:/[ \t]*([*#]).+(\n[ \t]*[^*#\s].*)*(\n[ \t]*[*#]{2}.+)*/,_replaceRegex:/(^|\n)[ \t]*[*#]/g,_replaceString:"$1"},_table:{_tag:"table",_capture:0,_regex:/(^|\n)(\|.*?[ \t]*(\n|$))+/},_tr:{_tag:"tr",_capture:2,_regex:/(^|\n)(\|.*?)\|?[ \t]*(\n|$)/},_th:{_tag:"th",_regex:/\|+=([^|]*)/,_capture:1},_td:{_tag:"td",_capture:1,_regex:"\\|+([^|~\\[{]*((~(.|(?=\\n)|$)|\\[\\["+e._link+"(\\|"+e._linkText+")?\\]\\]"+(a&&a.strict?"":"|"+e._img)+"|[\\[{])[^|~]*)*)"},_singleLine:{_regex:/.+/,_capture:0},_paragraph:{_tag:"p",_capture:0,_regex:/(^|\n)([ \t]*\S.*(\n|$))+/},_text:{_capture:0,_regex:/(^|\n)([ \t]*[^\s].*(\n|$))+/},_strong:{_tag:"strong",_capture:1,_regex:/\*\*([^*~]*((\*(?!\*)|~(.|(?=\n)|$))[^*~]*)*)(\*\*|\n|$)/},_em:{_tag:"em",_capture:1,_regex:"\\/\\/(((?!"+e._uriPrefix+")[^\\/~])*(("+e._rawUri+"|\\/(?!\\/)|~(.|(?=\\n)|$))((?!"+e._uriPrefix+")[^\\/~])*)*)(\\/\\/|\\n|$)"},_img:{_regex:e._img,_build:function(j,h,g){var f=document.createElement("img");f.src=h[1];f.alt=h[2]===undefined?(g&&g.defaultImageText?g.defaultImageText:""):h[2].replace(/~(.)/g,"$1");j.appendChild(f)}},_namedUri:{_regex:"\\[\\[("+e._uri+")\\|("+e._linkText+")\\]\\]",_build:function(j,h,f){var g=document.createElement("a");g.href=h[1];if(f&&f.isPlainUri){g.appendChild(document.createTextNode(h[2]))}else{this._apply(g,h[2],f)}j.appendChild(g)}},_namedLink:{_regex:"\\[\\[("+e._link+")\\|("+e._linkText+")\\]\\]",_build:function(j,h,f){var g=document.createElement("a");g.href=f&&f.linkFormat?d(h[1].replace(/~(.)/g,"$1"),f.linkFormat):h[1].replace(/~(.)/g,"$1");this._apply(g,h[2],f);j.appendChild(g)}},_unnamedUri:{_regex:"\\[\\[("+e._uri+")\\]\\]"},_unnamedLink:{_regex:"\\[\\[("+e._link+")\\]\\]"},_unnamedInterwikiLink:{_regex:"\\[\\[("+e._interwikiLink+")\\]\\]"},_rawUri:{_regex:"("+e._rawUri+")"},_escapedSequence:{_regex:"~("+e._rawUri+"|.)",_capture:1,_tag:"span",_attrs:{"class":"escaped"}},_escapedSymbol:{_regex:/~(.)/,_capture:1,_tag:"span",_attrs:{"class":"escaped"}}};c._unnamedUri._build=c._rawUri._build=function(h,g,f){if(!f){f={}}f.isPlainUri=true;c._namedUri._build.call(this,h,Array(g[0],g[1],g[1]),f)};c._unnamedLink._build=function(h,g,f){c._namedLink._build.call(this,h,Array(g[0],g[1],g[1]),f)};c._namedInterwikiLink={_regex:"\\[\\[("+e._interwikiLink+")\\|("+e._linkText+")\\]\\]",_build:function(l,k,h){var j=document.createElement("a");var g,n;if(h&&h.interwiki){g=k[1].match(/(.*?):(.*)/);n=h.interwiki[g[1]]}if(typeof n=="undefined"){if(!c._namedLink._apply){c._namedLink=new this.constructor(c._namedLink)}return c._namedLink._build.call(c._namedLink,l,k,h)}j.href=d(g[2].replace(/~(.)/g,"$1"),n);this._apply(j,k[2],h);l.appendChild(j)}};c._unnamedInterwikiLink._build=function(h,g,f){c._namedInterwikiLink._build.call(this,h,Array(g[0],g[1],g[1]),f)};c._namedUri._children=c._unnamedUri._children=c._rawUri._children=c._namedLink._children=c._unnamedLink._children=c._namedInterwikiLink._children=c._unnamedInterwikiLink._children=[c._escapedSymbol,c._img];for(var b=1;b<=6;b++){c["h"+b]={_tag:"h"+b,_capture:2,_regex:"(^|\\n)[ \\t]*={"+b+"}[ \\t]*([^\\n=][^~]*?(~(.|(?=\\n)|$))*)[ \\t]*=*\\s*(\\n|$)"}}c._ulist._children=c._olist._children=[c._li];c._li._children=[c._ulist,c._olist,c._preBlock];c._li._fallback=c._text;c._table._children=[c._tr];c._tr._children=[c._th,c._td];c._td._children=[c._singleLine];c._th._children=[c._singleLine];c.h1._children=c.h2._children=c.h3._children=c.h4._children=c.h5._children=c.h6._children=c._singleLine._children=c._paragraph._children=c._text._children=c._strong._children=c._em._children=[c._escapedSequence,c._strong,c._em,c._br,c._rawUri,c._namedUri,c._namedInterwikiLink,c._namedLink,c._unnamedUri,c._unnamedInterwikiLink,c._unnamedLink,c._tt,c._img];c._root={_children:[c.h1,c.h2,c.h3,c.h4,c.h5,c.h6,c._hr,c._ulist,c._olist,c._preBlock,c._table],_fallback:{_children:[c._paragraph]}};creole._base.call(this,c,a)};creole._base=function(a,b){if(!arguments.length){return}this._grammar=a;this._grammar._root=new this._ruleConstructor(this._grammar._root);this._options=b};creole._rule=function(b){if(!arguments.length){return}for(var a in b){this[a]=b[a]}if(!this._children){this._children=[]}};creole._base.prototype={_ruleConstructor:null,_grammar:null,_options:null,parse:function(b,c,a){if(a){for(i in this._options){if(typeof a[i]=="undefined"){a[i]=this._options[i]}}}else{a=this._options}c=c.replace(/\r\n?/g,"\n");this._grammar._root._apply(b,c,a);if(a&&a.forIE){b.innerHTML=b.innerHTML.replace(/\r?\n/g,"\r\n")}}};creole._base.prototype.constructor=creole._base;creole._base.prototype._ruleConstructor=creole._rule;creole._rule.prototype={_match:function(b,a){return b.match(this._regex)},_build:function(d,c,a){var e;if(this._capture!==null){e=c[this._capture]}var f;if(this._tag){f=document.createElement(this._tag);d.appendChild(f)}else{f=d}if(e){if(this._replaceRegex){e=e.replace(this._replaceRegex,this._replaceString)}this._apply(f,e,a)}if(this._attrs){for(var b in this._attrs){f.setAttribute(b,this._attrs[b]);if(a&&a.forIE&&b=="class"){f.className=this._attrs[b]}}}return this},_apply:function(a,c,k){var f=""+c;var e=[];if(!this._fallback._apply){this._fallback=new this.constructor(this._fallback)}while(true){var b=false;var j=false;for(var d=0;d<this._children.length;d++){if(typeof e[d]=="undefined"){if(!this._children[d]._match){this._children[d]=new this.constructor(this._children[d])}e[d]=this._children[d]._match(f,k)}if(e[d]&&(!b||b.index>e[d].index)){b=e[d];j=this._children[d];if(b.index==0){break}}}var h=b?b.index:f.length;if(h>0){this._fallback._apply(a,f.substring(0,h),k)}if(!b){break}if(!j._build){j=new this.constructor(j)}j._build(a,b,k);var g=b.index+b[0].length;f=f.substring(g);for(var d=0;d<this._children.length;d++){if(e[d]){if(e[d].index>=g){e[d].index-=g}else{e[d]=void 0}}}}return this},_fallback:{_apply:function(b,c,a){if(a&&a.forIE){c=c.replace(/\n/g," \r")}b.appendChild(document.createTextNode(c))}}};creole._rule.prototype.constructor=creole._rule;creole.prototype=new creole._base();creole.prototype.constructor=creole;