curlic.js (3012B)
1 const templates = {} 2 templates['b'] = _.template('<b><%= text %></b>') 3 templates['i'] = _.template('<i><%= text %></i>') 4 templates['code'] = _.template('<code><%= text %></code>') 5 templates['t'] = _.template('<span><%= text %></span>') 6 templates['alr'] = _.template('<span class="alert"><%= text %></span>') 7 8 String.prototype.replace_all = function(search, replacement){ return `${this}`.split(search).join(replacement); }; 9 String.prototype.capitalize = function(){ return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase(); } 10 String.prototype.to_url = function(){ return this.toLowerCase().replace(/ /g,"+").replace(/[^0-9a-z\+\:\-\.\/]/gi,"").trim(); } 11 String.prototype.to_path = function(){ return this.toLowerCase().replace(/\+/g,".").replace(/ /g,".").replace(/[^0-9a-z\.\-]/gi,"").trim(); } 12 String.prototype.to_entities = function(){ return this.replace(/[\u00A0-\u9999<>\&]/gim, function(i) { return `&#${i.charCodeAt(0)}`; }); } 13 String.prototype.to_rss = function(){ return this.replace(/\</g,"<").replace(/\>/g,">") } 14 String.prototype.count = function(c) { var r = 0, i = 0; for(i;i<this.length;i++)if(this[i]==c) r++; return r; } 15 16 function Curlic(text = "",origin = null) 17 { 18 this.text = `${text}`; 19 this.origin = origin; 20 21 var runes = { 22 "*":{tag:"b"}, 23 "!":{tag:"alr"}, 24 ".":{tag:"i"}, 25 "#":{tag:"code"}, 26 "$":{tag:"t",fn:eval}, 27 } 28 29 function wrap(s,c,r) 30 { 31 s = s.replace(c,'').replace(c,''); 32 33 if(r.fn){ 34 s = s.replace(s,r.fn(s)); 35 } 36 37 return templates[r.tag]({text: he.encode(s, {useNamedReferences: true})}) //`<${r.tag}>${s}</${r.tag}>` 38 } 39 40 function link(s,t) 41 { 42 var target = t.replace("(","").replace(")","") 43 var external = target.indexOf("//") > -1 44 var name = s.replace(`(${target})`,"") 45 var href = !external ? `#${target.to_url()}` : target 46 var click = !external ? `` : '' 47 var view = external ? '_blank' : '_self' 48 var className = external ? 'external' : 'local' 49 50 return `<a href='${href}' title='${target}' class='${className}' target='${view}'>${name ? name : target}</a>` 51 } 52 53 function evaluate(s,t) 54 { 55 try{ 56 return `${eval(t.substr(1,t.length-2))}` 57 } 58 catch(err){ 59 console.warn("Cannot eval",t); return t 60 } 61 } 62 63 function parse(s) 64 { 65 // Eval 66 if(s.match(/\[.*\]/g)){ 67 s = evaluate(s,s.match(/\[.*\]/g)[0]) 68 } 69 70 // Wrap 71 for(var ch in runes){ 72 var rune = runes[ch]; 73 if(s.count(ch) < 2){ continue; } 74 s = wrap(s,ch,rune) 75 } 76 77 // Link 78 if(s.match(/\(.*\)/g)){ 79 s = link(s,s.match(/\(.*\)/g)[0]) 80 } 81 return s 82 } 83 84 this.extract = function() 85 { 86 return this.text.match(/[^{\}]+(?=})/g); 87 } 88 89 this.toString = function() 90 { 91 var matches = this.extract(); 92 if(!matches){ return this.text; } 93 94 matches.forEach(el => { 95 this.text = this.text.replace(`{${el}}`,parse(el)) 96 }) 97 return this.text 98 } 99 } 100 101 String.prototype.to_curlic = function(origin){ return `${new Curlic(this,origin)}`; }