UNPKG

tipograph

Version:

A little javascript library and command line tool that makes your written content more typographically correct.

6 lines (4 loc) 8.01 kB
/** * tipograph v0.7.4 | Copyright (c) 2022 Petr Nevyhoštěný */ var tipograph=function(){"use strict";function e(e,n){var t=/<[/!]?[a-z][a-z0-9-_]*|<!--/gi;t.lastIndex=n;var o=null;if(null!==(o=t.exec(e))){var s=o[0].toLowerCase(),u=o.index;if("\x3c!--"===s){var i=function(e,r){var n=/-->/g;n.lastIndex=r;var t=null;return null!==(t=n.exec(e))?t.index+3:e.length}(e,t.lastIndex);return[u,i]}if(-1!=["<pre","<code","<style","<script"].indexOf(s)){var a=new RegExp(s[0]+"/"+s.slice(1),"gi");return a.lastIndex=t.lastIndex,null!==(o=a.exec(e))?[u,r(e,a.lastIndex)]:[u,e.length]}return[u,r(e,t.lastIndex)]}return null}function r(e,r){for(var n="initial",t=!1,o=r;o<e.length;o++)if("initial"===n){if(">"===e[o])return o+1;'"'===e[o]?n="double":"'"===e[o]&&(n="single")}else"double"===n?'"'!==e[o]||t?t="\\"===e[o]:n="initial":"single"===n&&("'"!==e[o]||t?t="\\"===e[o]:n="initial");return e.length}var n=Object.freeze({__proto__:null,html:function(){return function(r){for(var n=null,t=0,o=[];null!==(n=e(r,t));)o.push({transform:!0,content:r.slice(t,n[0])}),o.push({transform:!1,content:r.slice(n[0],n[1])}),t=n[1];return o.push({transform:!0,content:r.slice(t)}),o}},plain:function(){return function(e){return[{transform:!0,content:e}]}}}),t="“",o="”",s="‘",u="’",i="„",a="«",l="»",f=Object.freeze({__proto__:null,DOUBLE_OPEN_UP:t,DOUBLE_CLOSE_UP:o,SINGLE_OPEN_UP:s,SINGLE_CLOSE_UP:u,DOUBLE_OPEN_DOWN:i,SINGLE_OPEN_DOWN:"‚",DOUBLE_LEFT:a,SINGLE_LEFT:"‹",DOUBLE_RIGHT:l,SINGLE_RIGHT:"›",DOUBLE_LEFT_SPACE:"« ",SINGLE_LEFT_SPACE:"‹ ",DOUBLE_SPACE_RIGHT:" »",SINGLE_SPACE_RIGHT:" ›",DOUBLE_TOP_CORNER:"「",SINGLE_TOP_CORNER:"『",DOUBLE_BOTTOM_CORNER:"」",SINGLE_BOTTOM_CORNER:"』"}),g={quotes:[["「","」"],["『","』"]],dash:"en",rules:[]},c={quotes:[[i,t],["‚",s]],dash:"en",rules:[[/(\s|^)([KkSsVvZzOoUuAI])(?:\s+)(\S)/g,"$1$2 $3"]]},h={quotes:[[l,a],[i,t]],dash:"en",rules:[]},p={quotes:[[t,o],[s,u]],dash:"en",rules:[]},d={quotes:[[o,o],[u,u]],dash:"en",rules:[]},_={quotes:[["« "," »"],[t,o]],dash:"en",rules:[]},E={quotes:[[i,t],["‚",s]],dash:"en",rules:[]},v={quotes:[[a,l],[t,o]],dash:"en",rules:[]},O={quotes:[["「","」"],["『","』"]],dash:"en",rules:[]},m={quotes:[[a,l],[u,u]],dash:"en",rules:[]},$={quotes:[[i,o],[a,l]],dash:"en",rules:[]},q={quotes:[[t,o],[s,u]],dash:"en",rules:[]},w={quotes:[[a,l],[i,t]],dash:"en",rules:[]},b={quotes:[[a,l],[t,o]],dash:"en",rules:[]},y={quotes:[[o,o],[u,u]],dash:"en",rules:[]},L={quotes:[[a,l],["‹","›"]],dash:"en",rules:[]},S=Object.freeze({__proto__:null,chinese:g,czech:c,danish:h,english:p,finnish:d,french:_,german:E,italian:v,japanese:O,norwegian:m,polish:$,portuguese:q,russian:w,spanish:b,swedish:y,swiss:L});var j=Object.freeze({__proto__:null,hyphens:function(e,r){var n=[/\u0020+-\u0020+/g," – "],t=r.dash||e.dash,o=n;switch(t){case"en":o=n;break;case"em":o=[/\u0020+-\u0020+/g," — "];break;default:console.warn("invalid option `dash: "+t+"`")}return[[/\u0020*---(\r?\n|$)/g," —$1"],[/\u0020*---\u0020*/g," — "],[/--/g,"–"],[/(\d)-(\d)/g,"$1–$2"],o]},language:function(e){return e.rules},math:function(){return[[/(\d\s)-(\s\d)/g,"$1−$2"],[/\+-/g,"±"],[/-(\d)/g,"−$1"],[/(\d\s)[x*](\s\d)/g,"$1×$2"],[/(\d\s)\/(\s\d)/g,"$1÷$2"],[/!=/g,"≠"],[/<=/g,"≤"],[/>=/g,"≥"]]},quotes:function(e){var r=e.quotes[0][0],n=e.quotes[0][1],t=e.quotes[1][0],o=e.quotes[1][1];return[[/(\s|\(|^|\u200B),,([^"']*)(\S)(?:"|'')/g,"$1„$2$3"+n],[/(\s|\(|^|\u200B),(?!\s)([^']*)(\S)'/g,"$1‚$2$3"+o],[/([a-z])'([a-z])/gi,"$1’$2"],[/(\s|\u200B)'(\d{2})/g,"$1’$2"],[/(\s|\(|^|\u200B)"(?!\s)([^"]*)(\S)"/g,"$1"+r+"$2$3"+n],[/(\s|\(|^|\u200B)&quot;(?!\s)((?!&quot;).*)(\S)&quot;/g,"$1"+r+"$2$3"+n],[/(\s|\(|^|\u200B)'(?!\s)([^']*)(\S)'/g,"$1"+t+"$2$3"+o],[/(\d)"/g,"$1″"],[/(\d)'/g,"$1′"]]},spaces:function(){return[[/ {2,}/g," "],[/(\u00B6|\u00A7|\u00A9|\u2122|\u00AE) /g,"$1 "]]},symbols:function(){return[[/(\s|^)\((?:c|C)\)\s?/g,"$1© "],[/\((?:tm|TM)\)\s?/g,"™ "],[/\((?:r|R)\)\s?/g,"® "],[/([^.]|^)\.\.\.([^.]|$)/g,"$1…$2"],[/<-/g,"←"],[/&lt;-/g,"←"],[/->/g,"→"],[/-&gt;/g,"→"],[/\?!+/g,"⁈"],[/!\?+/g,"⁉"],[/\?{2,}/g,"⁇"],[/!{2,}/g,"!"]]}}),x=Object.freeze({__proto__:null,html:{" ":"&nbsp;","§":"&sect;","©":"&copy;","®":"&reg;","±":"&plusmn;","¶":"&para;","™":"&trade;","–":"&ndash;","—":"&mdash;","…":"&hellip;","←":"&larr;","→":"&rarr;","−":"&minus;","×":"&times;","≠":"&ne;","«":"&laquo;","»":"&raquo;","‘":"&lsquo;","’":"&rsquo;","‚":"&sbquo;","′":"&prime;","″":"&Prime;","“":"&ldquo;","”":"&rdquo;","„":"&bdquo;"},latex:{" ":"~","±":"\\(\\pm\\)","–":"--","—":"---","…":"\\textellipsis","←":"\\(\\leftarrow\\)","→":"\\(\\rightarrow\\)","−":"\\(-\\)","×":"\\(\\times\\)","≠":"\\(\\neq\\)","«":"\\guillemotleft","»":"\\guillemotright","‘":"`","’":"'","′":"\\('\\)","″":"\\(''\\)","“":"``","”":"''"}});function I(e){for(var r=0,n=1;n<e.length;n++)e[n]<e[r]&&(r=n);return r}function B(e,r){var n=e.length,t=r.length,o=function(e,r){for(var n=new Array(e),t=0;t<e;t++)n[t]=new Array(r);return n}(n+1,t+1),s=[];o[0][0]=0;for(var u=0;u<t;u++)o[0][u+1]=o[0][u]+3;for(var i=0;i<n;i++)o[i+1][0]=o[i][0]+1;for(var a=0;a<n;a++)for(var l=0;l<t;l++)if(e[a]===r[l])o[a+1][l+1]=o[a][l],s.push([a,l]);else{var f=[o[a][l+1]+1,o[a][l]+3,o[a+1][l]+3],g=I(f);o[a+1][l+1]=f[g]}if(0===s.length)return null;var c=I(s.map((function(e){return o[e[0]+1][e[1]+1]})));return s[c]}var N={format:"plain",language:"english",presets:Object.keys(j),options:{}};function P(e){var r=function(e){if("string"==typeof e){if(void 0!==n[e])return n[e]();throw new Error("Unsupported format: "+e+". Choose one from "+Object.keys(n).join(", ")+" or pass a function.")}if("function"==typeof e)return e();throw new Error("Format option must be either string or function.")}((e=Object.assign({},N,e)).format),t=function(e){if("string"==typeof e){if(void 0!==S[e])return S[e];throw new Error("Unsupported language: "+e+". Choose one from "+Object.keys(S).join(", ")+" or pass a object.")}if("object"==typeof e)return void 0===e.quotes&&(e.quotes=p.quotes),void 0===e.dash&&(e.dash=p.dash),void 0===e.rules&&(e.rules=p.rules),e;throw new Error("Language option must be either string or object.")}(e.language),o=function(e,r,n){if(Array.isArray(e))return e.map((function(e){if("string"==typeof e&&void 0===j[e])throw new Error("Unsupported preset: "+e+". Choose one from "+Object.keys(j).join(", ")+".");return"function"==typeof e?e(r,n):j[e](r,n)}));throw new Error("Presets option must be either array containing a preset name or preset definition.")}(e.presets,t,e.options),s=function(e){if(void 0!==x[e])return x[e];if(void 0===e)return null;throw"string"==typeof e?new Error("Unsupported postprocessing format: "+e+". Choose one from "+Object.keys(x).join(", ")+" or pass a object."):new Error("Post option must be string corresponding to an available postprocessing format.")}(e.post);return function(e,n){if("string"!=typeof e)throw new Error("Only strings are supported as input.");for(var t=r(e),u=[],i="",a=0;a<t.length;a++)t[a].transform?i+=t[a].content:(i+="​<tipograph["+u.length+"]>​",u.push(t[a].content));for(var l=0;l<o.length;l++)for(var f=0;f<o[l].length;f++){var g=o[l][f];i=i.replace(g[0],g[1])}if(i=i.replace(/\u200B<tipograph\[(\d+)\]>\u200B/g,(function(e,r){return u[r]})),null!==s){for(var c="",h=0;h<i.length;h++)"string"==typeof s[i[h]]?c+=s[i[h]]:c+=i[h];i=c}if("function"==typeof n){var p=function(e,r){e+="\0",r+="\0";for(var n=[],t=0,o=0;t<e.length&&o<r.length;)if(e[t]==r[o])t++,o++;else{var s,u=10;do{s=B(e.slice(t,t+u),r.slice(o,o+u)),u*=2}while(null===s);n.push([[t,t+s[0]],[o,o+s[1]]]),t+=s[0]+1,o+=s[1]+1}return n}(e,i);return n(i,p)}return i}}return P.extend=function(e){var r=Object.keys(j);if(Array.isArray(e))return r.concat(e);if(void 0!==e)throw new Error("Presets can be extended only with an array of custom presets");return r},P.presets=P.extend,P.quotes=f,P.languages=S,P}();