This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttl-pug.min.js
4 lines (4 loc) · 3.28 KB
/
ttl-pug.min.js
1
2
3
4
// ttl-pug.js 1.0
// Contributors - Indong Yoo, Hanah Choi
// (c) 2015-2018 Marpple. MIT Licensed.
var TAB,TAB_SIZE,REG1,REG2,REG3,REG4,REG5,REG6;export function tab(c){TAB_SIZE=c,TAB="( {"+c+"}|\\t)";var d=TAB+"+";REG1=new RegExp("^"+d),REG2={},REG3=new RegExp("^("+d+")(\\[.*?\\]|\\{.*?\\}|\\S)+\\.(?!\\S)"),REG4={},REG5=new RegExp("\\n("+d+"[\\s\\S]*)"),REG6=new RegExp("^"+d+"\\|")}tab(2);export default function pug(c,...d){for(var e=c.reduce((l,m,n)=>`${l}{{${n-1}}}${m}`).split("\n").filter(l=>l),f=[],g=number_of_tab(e[0]),h=0,j=0;j<e.length;j++){for(;number_of_tab(e[j])-g<f.length&&(h=0,0!=f.length);)e[j-1]+=end_tag(f.pop());var k=e[j];if(!h){e[j]=line(e[j],f),k.match(REG3)&&(h=number_of_tab(RegExp.$1)+1);continue}e[j]=e[j].replace(REG4[h]||(REG4[h]=new RegExp("("+TAB+"{"+h+"})","g")),"\n"),e[j]!==(e[j]=e[j].replace(REG5,"\n"))&&(e=push_in(e,j+1,RegExp.$1))}for(;f.length;)e[e.length-1]+=end_tag(f.pop());return d.reduce((l,m,n)=>l.replace("{{"+n+"}}",array_to_str(m)),e.join(""))}const array_to_str=c=>"function"==typeof c?c():Array.isArray(c)?c.join(""):c;export function html(c){return c.reduce((d,e,f)=>d+array_to_str(arguments[f])+e)}function number_of_tab(c){var d=c.match(REG1)[0],e=(d.match(/\t/g)||[]).length,f=d.replace(/\t/g,"").length;return f/TAB_SIZE+e}function line(c,d){return c=c.replace(REG6,"\n").replace(/^[ \t]*/,""),c.match(/^[\[.#\w\-]/)?c.replace(/^(\[.*\]|\{.*?\}|\S)+ ?/,function(e){return start_tag(e,d)}):c}function push_in(c,d,e){var f=c.splice(d);return c.push(e),c.concat(f)}function start_tag(c,d,e,f,g){return e="",f=c.match(/^\w+/),f=f&&"d"!=f?"sp"==f?"span":f:"div","input"!=f&&"br"!=f&&d.push(f),c=c.replace(/\[(.*)\]/,function(h,j){return(e+=" "+j)&&""}),(g=map(c.match(/\.(\{\{.*?\}\}|[\w\-]+)/g),function(h){return h.slice(1)}).join(" "))&&e==(e=e.replace(/class\s*=\s*((\").*?\"|(\{.*?\}|\S)+)/,function(h,j,k){return" class=\""+g+" "+(k?j.slice(1,-1):j)+"\""}))&&(e=" class=\""+g+"\""+e),e=["",...map(c.match(/#(\{\{.*?\}\}|[\w\-]+)/g),function(h){return h.slice(1)})].join(" id=")+e,"<"+f+e+" >"}function end_tag(c){return"</"+c+">"}var escapeMap={"&":"&","<":"<",">":">","\"":""","'":"'","`":"`"},unescapeMap={"&":"&","<":"<",">":">",""":"\"","'":"'","`":"`"},createEscaper=function(c){var d=function(h){return c[h]},e="(?:"+Object.keys(c).join("|")+")",f=RegExp(e),g=RegExp(e,"g");return function(h){return h=null==h?"":""+h,f.test(h)?h.replace(g,d):h}};const escape=createEscaper(escapeMap),unescape=createEscaper(unescapeMap);var ObjectValues=Object.values||function(c){return Object.keys(c).map(d=>c[d])};export function values(c){return c?Array.isArray(c)?c:"function"==typeof c[Symbol.iterator]?[...(c.values?c.values():c[Symbol.iterator]())]:typeof c.length=='number'?Array.prototype.slice.call(c):ObjectValues(c):[]}export function map(c,d){return values(c).map(d)}export function scat(c,d=e=>e){return values(c).reduce((e,f)=>`${e}${d(f)}`,"")}export{escape,unescape};export function el(c){var d,e;return /^<(tr|th|td).*><\/(tr|th|td)>$/.test(c)?(e=document.createElement("table"),e.innerHTML=c,e=e.firstChild,"tr"!=RegExp.$1&&(e=e.firstChild)):(e=document.createElement("div"),e.innerHTML=c),1==(d=map(e.children,f=>f)).length?d[0]:d}export function elpug(){return el(pug(...arguments))}export function elhtml(){return el(html(...arguments))}