Mercurial > pub > ImplabJs
diff src/implab/text/format-compile.js @ 34:27e8e9e38e07 default tip
Слияние
author | nickolay |
---|---|
date | Wed, 05 Jun 2019 20:44:15 +0300 |
parents | 8af8e840dd49 1dc2fd263b90 |
children |
line wrap: on
line diff
--- a/src/implab/text/format-compile.js Wed Jun 05 17:44:17 2019 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -define( - [], - function() { - var map = { - "\\{" : "&curlopen;", - "\\}" : "&curlclose;", - "&" : "&", - "\\:" : ":" - }; - - var rev = { - curlopen : "{", - curlclose : "}", - amp : "&", - colon : ":" - }; - - var espaceString = function(s) { - if (!s) - return s; - return "'" + s.replace(/('|\\)/g, "\\$1") + "'"; - }; - - var encode = function(s) { - if (!s) - return s; - return s.replace(/\\{|\\}|&|\\:/g, function(m) { - return map[m] || m; - }); - }; - - var decode = function(s) { - if (!s) - return s; - return s.replace(/&(\w+);/g, function(m, $1) { - return rev[$1] || m; - }); - }; - - var subst = function(s) { - var i = s.indexOf(":"), name, pattern; - if (i >= 0) { - name = s.substr(0, i); - pattern = s.substr(i + 1); - } else { - name = s; - } - - if (pattern) - return [ - espaceString(decode(name)), - espaceString(decode(pattern)) ]; - else - return [ espaceString(decode(name)) ]; - }; - - var compile = function(str) { - if (!str) - return function() {}; - - var chunks = encode(str).split("{"), chunk; - - var code = [ "var result=[];" ]; - - for (var i = 0; i < chunks.length; i++) { - chunk = chunks[i]; - - if (i === 0) { - if (chunk) - code.push("result.push(" + espaceString(decode(chunk)) + - ");"); - } else { - var len = chunk.indexOf("}"); - if (len < 0) - throw new Error("Unbalanced substitution #" + i); - - code.push("result.push(subst(" + - subst(chunk.substr(0, len)).join(",") + "));"); - if (chunk.length > len + 1) - code.push("result.push(" + - espaceString(decode(chunk.substr(len + 1))) + ");"); - } - } - - code.push("return result.join('');"); - - /* jshint -W054 */ - return new Function("subst", code.join("\n")); - }; - - var cache = {}; - - return function(template) { - var compiled = cache[template]; - if (!compiled) { - compiled = compile(template); - cache[template] = compiled; - } - return compiled; - }; - }); \ No newline at end of file