-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinen.min.js
12 lines (12 loc) · 5.96 KB
/
linen.min.js
1
2
3
4
5
6
7
8
9
10
11
12
var linen=function(){function q(b){function c(a){a=function(a){var a=a.split("\n"),b=[],c;for(c in a){var e=/^(\*+|#+)(.*)/.exec(a[c]);if(e){types={"*":"ul","#":"ol"};var l=i(e[2]);b.push({type:types[e[1][0]],indent:e[1].length,content:l.content,attrs:l.attrs})}}return b}(a);return{type:a[0].type,extended:!1,attrs:[],content:a}}function d(a){var a=a.split("\n"),b=[],c,e;for(e in a){var g=a[e].replace(/^\|/,"").replace(/\|$/,""),d=i(g[0]);g[0]=d.content;if(e==0)c=d.attrs;b.push(g.split("|"))}return{type:"table",
extended:!1,attrs:c,content:b}}function a(a){function b(a){var c=["h1","h2","h3","h4","h5","h6","pre","bq","fn","p","bc","pre","table","ul","ol"],e;for(e in c)if(c[e]==a)return!0;return!1}var e="",f=0,g=a[0];g=="f"&&a[f+1]=="n"&&(e="fn",f++);if((g=="*"||g=="#")&&/^\s+/.exec(i(a.slice(1)).content))return c(a);if(g=="|")return d(a);g=="b"&&(e="b"+a[++f]);g=="h"&&(e="h"+a[++f]);g=="p"&&(a.slice(f,f+3)=="pre"?(f+=2,e="pre"):e="p");f++;f=i(a.slice(f,a.length));(g=/^(\.+ )/.exec(f.content))&&b(e)?(f.type=
e,f.content=f.content.replace(/^(\.+ )/,""),f.extended=g[1]==".. ",f.noBlock=!1):(f.type="p",f.content=a,f.extended=!1,f.noBlock=!0);return f}var b=b.split(/\n\n+/),e=[],n;for(n in b){var o=a(b[n]);o&&e.push(o)}return e}function i(b){for(var c=[],d=b.match(/^<\S+[^<>]*>/)!==null,a=0;a<b.length;a++){var e=b[a];if(e==">")c.push(">");else if(e=="=")c.push("=");else if(e=="<"&&!d)b[a+1]==">"?(a++,c.push("<>")):c.push("<");else if(e=="("){for(e=a;b[a++]!==")"&&a<b.length;);c.push(b.slice(e,a--))}else if(e==
"["){for(e=a;b[a++]!=="]"&&a<b.length;);c.push(b.slice(e,a--))}else if(e=="{"){for(e=a;b[a++]!=="}"&&a<b.length;);c.push(b.slice(e,a--))}else break}return{attrs:c,content:b.slice(a,b.length)}}function p(b){function c(a,b,c){var c=typeof c=="undefined"?"":" "+c,b=i(b),d=k(b.attrs);return"<"+a+h({attrs:d})+c+">"+b.content+"</"+a+">"}function d(a,b){typeof b=="undefined"&&(b=1);return a.slice(b,a.length-b)}return b.replace(/(\s)"(.*?)"([^:])/g,"$1“$2”$3").replace(/"([^"]+)":(http\S+)/g,function(a,
b,d){return d[d.length-1]=="."?c("a",b,'href="'+d.slice(0,d.length-1)+'"')+".":c("a",b,'href="'+d+'"')}).replace(/!\b([^! \n]+)\b!:(http\S+)/g,function(a,b,d){return c("a"," "+c("img","",'src="'+b+'"'),'href="'+d+'"')}).replace(/!([^! \n]+)!/g,function(a){return c("img","",'src="'+d(a)+'"')}).replace(/--/g,"—").replace(/\n/g,"<br/>").replace(/(\b)'([^']*)'(\b)/g,"$1‘$2’$3").replace(/'/g,"’").replace(/ - /g," – ").replace(/\.\.\./g,"…").replace(/\(r\)/g,"®").replace(/\(tm\)/g,
"™").replace(/\(c\)/g,"©").replace(/([A-Z]{2,})\(([^)]+)\)/g,'<span class="caps"><acronym title="$2">$1</acronym></span>').replace(/([A-Z]{2,})/g,function(a){return c("span",a+" ",'class="caps"')}).replace(/\?{2}([^\?]+)\?{2}/g,function(a){return c("cite",d(a))}).replace(/%([^%]+)%/g,function(a){return c("span",d(a))}).replace(/(@[^@]+@)/g,function(a){return c("code",d(a))}).replace(/\*([^\*]+)\*/g,function(a){return c("strong",d(a))}).replace(/\*\*([^\*]+)\*\*/g,function(a){return c("b",
d(a))}).replace(/\b_([^_]+)_\b/g,function(a){return c("em",d(a))}).replace(/\b__([^_]+)__\b/g,function(a){return c("i",d(a))}).replace(/\s\+([^\+]+)\+\s/g,function(a){return" "+c("ins",d(a,2))+" "}).replace(/\s-([^-]+)-\s/g,function(a){return" "+c("del",d(a,2))+" "}).replace(/\^([^\^]+)\^/g,function(a){return c("sup",d(a))}).replace(/~([^~]+)~/g,function(a){return c("sub",d(a))})}function r(b){function c(a){for(var b=a.content,c=1,d=[],j=d,m=[],f=0;f<b.length;f++){var g=b[f];g.content=p(g.content);
g.attrs=k(g.attrs);if(g.indent>c)for(var h=0;h<g.indent-c;h++){m.push(j);var i=[];j.push(i);j=i}if(g.indent<c)for(h=0;h<c-g.indent;h++)j=m.pop();j.push(g);c=g.indent}return{type:a.type,content:d,attrs:d[0].attrs,classes:"",id:"",lang:"",style:"",alignment:""}}var d=[],a;for(a in b)d.push(b[a].type=="ol"||b[a].type=="ul"?c(b[a]):b[a].type=="table"?{type:"table",attrs:k(b[a].attrs),content:b[a].content,classes:"",id:"",lang:"",style:"",alignment:""}:b[a].type=="bc"?{type:b[a].type,content:b[a].content.replace(/</g,
"<").replace(/>/g,">").replace(/"/g,"""),attrs:k(b[a].attrs)}:{type:b[a].type,content:p(b[a].content),attrs:k(b[a].attrs)});return d}function k(b){var c={classes:"",id:"",lang:"",style:"",alignment:""},d;for(d in b){var a=b[d];if(a[0]=="("){if(a=a.replace(/^\(/,"").replace(/\)$/,"").split("#"),c.classes=c.classes==""?a[0]:c.classes+" "+a[0],a.length>1&&c.id=="")c.id=a[1]}else if(a[0]=="[")c.lang=a.replace(/^\[/,"").replace(/\]$/,"");else if(a[0]=="{")c.style=a.replace(/^\{/,"").replace(/\}$/,
"");else if(c.alignment==""){if(a==">")c.alignment="right";if(a=="<")c.alignment="left";if(a=="=")c.alignment="center";if(a=="<>")c.alignment="justify"}}return c}function h(b){var c=" ",b=b.attrs;b.classes&&(c+='class="'+b.classes+'" ');b.id&&(c+='id="'+b.id+'" ');if(b.align)b.style="text-align: "+b.align+"; "+b.style;b.style&&(c+='style="'+b.style+'" ');b.lang&&(c+='lang="'+b.lang+'" ');return c.slice(0,c.length-1)}function s(b){function c(a){function b(a){var c="<table"+h(a)+">",d;for(d in a.content){var e=
a.content[d];c+="<tr>";for(var f in e)c+="<td>"+e[f]+"</td>";c+="</tr>"}c+="</table>";return c}function c(a){function b(a){var c="<"+a[0].type+h(a[0])+">",d;for(d in a){var e=a[d];e.type?(c+="<li>"+e.content,a[d+1]&&a[d+1].type==void 0&&(c+=b(a[++d])),c+="</li>"):c+=b(e)}c+="</"+a[0].type+">";return c}return b(a.content)}switch(a.type){case "h1":case "h2":case "h3":case "h4":case "h5":case "h6":return"<"+a.type+h(a)+">"+a.content+"</"+a.type+">";case "bq":return"<blockquote"+h(a)+"><p>"+a.content+
"</p></blockquote>";case "fn":return"<p"+h(a)+"><sup>"+a.number+"</sup>"+a.content+"</p>";case "p":return"<p"+h(a)+">"+a.content+"</p>";case "bc":return"<pre"+h(a)+"><code>"+a.content+"</code></pre>";case "pre":return"<pre"+h(a)+">"+a.content+"</pre>";case "table":return b(a);case "ul":case "ol":return c(a)}}ret="";for(var d in b)ret+=c(b[d])+"\n";return ret}return function(b){return s(r(q(b)))}}();if(typeof exports!="undefined")exports.linen=linen,exports.textilize=linen;