å é±æ¸ããã¨ã³ããªã¼ãåè«ãã¢ããããã©ããã¦ææ¸å¦çãXMLã¨é¢ä¿ããã®ã§ããï¼ãã®å 容ãå®éã«ç¢ºèªããããã®JavaScriptããã°ã©ã ãæ¸ãã¦ã¿ã¾ããã
3ã¤ã®é¢æ°ãå«ã¿ãå ¨é¨ã§12è¡ã®ã©ã¤ãã©ãªã§ãã
/* templ-process.js */ function processTemplate(templ, con) { var a = (templ.replace(/\}/g, '{')).split('{'); for (var i = 0; i < a.length; i++) if (i%2 == 1) a[i] = con(a[i]); // ã³ã³ããã¹ãconã¯é¢æ° return a.join(''); } function processContext(con1, con2) { return function (k) {return processTemplate(con1(k), con2);} } function contextFun(map) { return function (k) {return map[k];} }
- æ¬å¼§ï¼'{'ã¨'}'ï¼ã®ã¨ã¹ã±ã¼ãå¦çã¯ãµããã¾ãï¼ããããããï¼ã
- é¢æ°processTemplateã¯ããã³ãã¬ã¼ãå±éå¦çãè¡ãã¾ãã第1å¼æ°ã¯ãæ§æçã«æ£ãããã³ãã¬ã¼ãã»ããã¹ãã ã¨ä»®å®ãã¦ãã¾ãï¼ããã§ãªãã¨ãã¾ãåããªãï¼ã'{'ãå é ã«æ¥ã¦ãï¼å°ãªãã¨ãRhinoã§ã¯ï¼ããã§å¤§ä¸å¤«ãªããã§ãã
- ã³ã³ããã¹ãã¨ã¯ãæååå¼æ°ï¼ååããã¼ï¼ã1ã¤åãé¢æ°ã®ãã¨ã ã¨ãã¾ãã
- é¢æ°processContextã¯ã2ã¤ã®ã³ã³ããã¹ãï¼ã³ã³ããã¹ãã¯é¢æ°ã§ããï¼ï¼ãå¼æ°ã¨ãã¦ãã第1ã®ã³ã³ããã¹ãcon1ã®å¤ï¼å±éããã¹ãï¼ãã第2ã®ã³ã³ããã¹ãcon2ã§å±éããå¤ãè¿ãã³ã³ããã¹ãããè¿ãã¾ãã
- é¢æ°contextFunã¯ããããï¼JavaScriptãªãã¸ã§ã¯ãï¼ãã¼ã¿ã§ä¸ããããã³ã³ããã¹ãããé¢æ°ã¨ãã¦ã®ã³ã³ããã¹ãã«ç´ãã¾ããcontextFunã¯å¿ ãããå¿ è¦ãªãã®ã§ã¯ããã¾ããããããã°ä¾¿å©ã§ãã
[追è¨] ããã¯ä½ãã«ãææãã ã¨æãæ¹ã¯ãã¯ã¯ã¿ããã«ããPythonçãåèã«ãã¦ã¿ã¦ãã ããâhttp://return0.dyndns.org/d/2007/01/26ãhttp://return0.dyndns.org/d/2007/01/30 [/追è¨]
次ã¯ããã¹ãã®ã»ããã¢ããããããã®ã§ãã
/* templ-test.js */ var message = "{greeting}\n{body}\n--\n{sign}\n"; var condata1 = { greeting:"Hello, {person}.", body:"It's a {good-or-bad} News, ...", sign:"Hanako" }; var condata2 = { person:"Tonkichi", "good-or-bad":"Good" }; var confun1 = contextFun(condata1); var confun2 = contextFun(condata2);
JavaScriptã¤ã³ã¿ã¼ããªã¿Rhinoã§æ¥æ¬èªã®è¡¨ç¤ºããããããªãã®ã§ãä¾æãè±æ°åã§æ¸ãã¦ãã¾ãããã®Rhinoã§å®è¡ãã¦ã¿ãã¨ï¼
js> load("templ-process.js") js> load("templ-test.js") js> processTemplate(processTemplate(message, confun1), confun2) Hello, Tonkichi. It's a Good News, ... -- Hanako js> processTemplate(message, processContext(confun1, confun2)); Hello, Tonkichi. It's a Good News, ... -- Hanako js>
ããã¯ããåè«ãã¢ããããã©ããã¦ææ¸å¦çãXMLã¨é¢ä¿ããã®ã§ããï¼ãã®å±±å ´ã§ãããå¤æ®µéã®ãã³ãã¬ã¼ãå¦çãã«åºã¦ããçå¼ã確èªããä¾ã§ããåæ§ã®ãã¨ããã©ã¦ã¶ã§ããã«ã¯ãä¾ãã°æ¬¡ã®ãããªHTMLãã¡ã¤ã«ãæºåãã¦ãã ããã
<!-- templ-test.html --> <html> <head> <script src="templ-process.js" ></script> <script src="templ-test.js" ></script> <script> function test1() { alert( processTemplate(processTemplate(message, confun1), confun2) ); } function test2() { alert( processTemplate(message, processContext(confun1, confun2)) ); } </script> </head> <body> <h1>Template processing test</h1> <ol> <li><button onclick="test1();" >Test 1</button> <li><button onclick="test2();" >Test 2</button> </ol> </body> </html>
ãåè«ãã¢ããããã©ããã¦ææ¸å¦çãXMLã¨é¢ä¿ããã®ã§ããï¼ãã®ãã¢ããã«åãã£ã¦çªã£èµ°ãï¼ï¼ãã¨ãããããªãã¢ããã ãããã§èª¬æããããã³ãã¬ã¼ãã»ã¢ããã®extã¨unitï¼ãããããã¢ããã®æ¡å¼µã¨åä½ãä¸ããï¼ãã次ã®ããã«ç°¡åã§ãã
function ext(con) { return function (t) {return processTemplate(t, con);} } function unit(k) { return "{" + k + "}"; }
ãããã®ç´ æãããã°ãã¢ããæ³åãå ·ä½ä¾ã§å®é¨ã§ãã¾ãã
js> unit("foo") {foo} js> (ext(unit))("{foo}bar") {foo}bar js> (ext(confun1))(unit("greeting")) Hello, {person}. js> confun1("greeting") Hello, {person}. js>
ã¢ããæ³åã®3çªç®ãå ·ä½ä¾ã§ç¢ºèªããã®ã¯å°ãã ãé¢åã§ãããè¯ãç·´ç¿ã§ããããï¼åãããªãã£ãããã³ã³ãè¦ã¦ãã ãããï¼
åãã£ã¬ã³ã¸æ¯æ´ã»èãæ¹ã®ãã³ã
ãåè«ãã¢ããããã©ããã¦ææ¸å¦çãXMLã¨é¢ä¿ããã®ã§ããï¼ãã§ãæ«æããã¡ãªç®æã¯ãã¾ããã³ã³ããã¹ãããã¼ã¿ã¨èãããé¢æ°ã¨èãããããã¨ããã§ããããprocessTemplateãåãã£ã¦ããã¨ãã¦ãprocessContextãäºéãã«æ¸ãã¦ã¿ã¾ãã
// ã³ã³ããã¹ãããã¼ã¿ã®å ´å function processContext(condata1, condata2) { var result = {}; for (var key in condata1) { result[key] = processTemplate(condata1[key], condata2); } return result; }
// ã³ã³ããã¹ããé¢æ°ã®å ´å function processContext(confun1, confun2) { return function (key) {return processTemplate(confun1(key), confun2);} }
ãããã®å ´åãã第1ã³ã³ããã¹ãã®å±éããã¹ãï¼condata1[key]ã¾ãã¯confun1(key)ï¼ã第2ã³ã³ããã¹ãã«ããå±éãã¦ãã¾ãããã®å±éå¦çããã¤ã©ã®ã¿ã¤ãã³ã°ã§è¡ãããéã£ã¦ã¾ãããå±éå¦ççµæãä¿æï¼åå©ç¨ãããæ¨ã¦ã¦æ¯åããç´ãããéãã¾ããããæ¦å¿µã¬ãã«ã§èããã°åããã¨ãªãã§ãã
ããã¨ãã¢ããæ³åãext((ext(con2))ã»con1) = ext(con2)ã»ext(con1) ããåçªã§å¤©ä¸ããã¤ããã¡ã¤ã¨æããã§ããããextã®å®ç¾© (ext(con))(t) ï¼ï¼ processTemplate(t, con) ã«æ»ã£ã¦ããã»ããã©ã ãè¨ç®ã使ã£ã¦æ¸ãæããã¨ï¼
- processTemplate(t, processContext(con1, con2)) = processTemplate(processTemplate(t, con1), con2)
ããã«ã©ã ãè¨ç®ããã¦ï¼
processContext(processContext(con1, con2), con3) = processContext(λk.(processTemplate(con1(k), con2)), con3) = λj.processTemplate(λk.(processTemplate(con1(k), con2))(j), con3) = λj.processTemplate(processTemplate(con1(j), con2)), con3) ããã§ãt = con1(j) ã ã¨æã£ã¦å ã®çå¼ãé©ç¨ï¼å³è¾ºâ左辺ã¨å¤å½¢ï¼ = λj.processTemplate(con1(j), processContext(con2, con3)) = processContext(con1, processContext(con2, con3))
çµå±ãprocessContext(processContext(con1, con2), con3) = processContext(con1, processContext(con2, con3)) ãªã®ã§ãããprocessContext(X, Y)ã¨ããé¢æ°å¼ã³åºãå½¢å¼ã Xâ»Y ã¨ããäºé æ¼ç®å½¢å¼ã«ãã¦ã¿ãã¨ã
- (con1â»con2)â»con3 = con1â»(con2â»con3)
ããã£ã¦ãâ»ã«é¢ããçµåæ³åã§ããããããã¢ããæ³åã®3çªç®ã¯ãå®éä¸ã¯çµåæ³åãæ®éããç®ã«ããã¢ããæ³åã¯ãçµåæ³åã®ã¢ãã«ãªãããã«æåããä»çµã¾ãã¦ããå½¢ãªããããæ®ãã®2ã¤ã®æ³åã¯ãããããå·¦åä½æ³åã¨å³åä½æ³åã«ãªãããã«ä»çµã¾ãã¦ãããã¤ã¾ããããããæ®éã®ããã®ãããå½ããåã®ãä¸å¦çã§ãç¥ã£ã¦ããè¨ç®æ³åã«éããªãã®ã§ãããã¢ããæ³åã£ã¦ã®ã¯ã
ã¨ã¯ããããã®ã¹ãããªã¨ããäºå®ã«ãã©ãçãã«ã¯ãç´ã¨éçã§ã©ã ãè¨ç®ãå®è¡ã§ãããã¨ã¯å¿ è¦ã ãªããã£ã±ãã