PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mt-static/js/common/Template.js

http://github.com/movabletype/movabletype
JavaScript | 410 lines | 237 code | 98 blank | 75 comment | 97 complexity | dd1d42ec32f564a9ee44e0ca25248eb6 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. /*
  2. Template - Copyright 2005 Six Apart
  3. $Id: Template.js 204 2007-05-30 19:50:59Z ddavis $
  4. Copyright (c) 2005, Six Apart Ltd.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. * Neither the name of "Six Apart" nor the names of its
  16. contributors may be used to endorse or promote products derived from
  17. this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /* core template object */
  31. Template = new Class( Object, {
  32. beginToken: "[#",
  33. endToken: "#]",
  34. init: function( source ) {
  35. if( source )
  36. this.compile( source );
  37. },
  38. compile: function( source ) {
  39. var statements = [
  40. "context.open();",
  41. "with( context.vars ) { "
  42. ];
  43. var start = 0, end = -this.endToken.length;
  44. while( start < source.length ) {
  45. end += this.endToken.length;
  46. /* plaintext */
  47. start = source.indexOf( this.beginToken, end );
  48. if( start < 0 )
  49. start = source.length;
  50. if( start > end )
  51. statements.push( "context.write( ", '"' + source.substring( end, start ).escapeJS() + '"', " );" );
  52. start += this.beginToken.length;
  53. // code
  54. if( start >= source.length )
  55. break;
  56. end = source.indexOf( this.endToken, start );
  57. if( end < 0 )
  58. throw "Template parsing error: Unable to find matching end token (" + this.endToken + ").";
  59. var length = ( end - start );
  60. /* empty tag */
  61. if( length <= 0 )
  62. continue;
  63. /* comment */
  64. else if( length >= 4 &&
  65. source.charAt( start ) == "-" &&
  66. source.charAt( start + 1 ) == "-" &&
  67. source.charAt( end - 1 ) == "-" &&
  68. source.charAt( end - 2 ) == "-" )
  69. continue;
  70. /* write */
  71. else if( source.charAt( start ) == "=" )
  72. statements.push( "context.write( ", source.substring( start + 1, end ), " );" );
  73. else if( source.charAt( start ) == "*" ) {
  74. // commands that effect flow
  75. var cmd = source.substring( start + 1, end ).match( /^\s*(\w+)/ );
  76. if ( cmd ) {
  77. cmd = cmd[ 1 ];
  78. switch ( cmd ) {
  79. case "return":
  80. statements.push( "return context.close();" );
  81. }
  82. }
  83. /* filters */
  84. } else if( source.charAt( start ) == "|" ) {
  85. start += 1;
  86. /* find the first whitespace */
  87. var afterfilters = source.substring( start, end ).search(/\s/);
  88. var filters = [];
  89. var params = [];
  90. if (afterfilters > 0) {
  91. /* pipes or commas must seperate filters
  92. * split the string, reverse and rejoin to reverse it
  93. */
  94. filters = source.substring( start,start + afterfilters ).replace(/(\w+)(\(([^\)]+)\))?/g,"$1|$3").split( "|" );
  95. afterfilters += 1; /* data starts after whitespace and filter list */
  96. } else {
  97. /* default to escapeHTML */
  98. filters = [ "h", "" ];
  99. }
  100. var cmds = [];
  101. var params = [];
  102. for ( var j = 0; j < filters.length; j++ ) {
  103. if (j % 2)
  104. params.push( filters[ j ] );
  105. else
  106. cmds.push( filters[ j ] );
  107. }
  108. /* we have to do them in reverse order */
  109. filters = cmds.reverse();
  110. /* start with our original filter number */
  111. var numfilters = filters.length;
  112. /* add the text between [#| #] */
  113. filters.push( source.substring( start + afterfilters, end ) );
  114. /* adjust each filter into a function call */
  115. /* H|substr(-1,1)|u */
  116. /* eg. u( substr( H( name ), -1, 1 ) ) */
  117. for ( var i = 0; i < numfilters; i++ ) {
  118. filters[ i ] = " context.f." + filters[ i ] + "( ";
  119. filters.push( ", context" );
  120. if ( params[ i ] != "" )
  121. filters.push( ", [" + params[ i ] + "]" );
  122. filters.push( " )" );
  123. }
  124. /* rewrite command params */
  125. filters = filters.join( "" );
  126. statements.push( "context.write( " + filters + " );");
  127. }
  128. /* evaluate */
  129. else
  130. statements.push( source.substring( start, end ) );
  131. }
  132. statements.push( "} return context.close();" );
  133. this.process = new Function( "context", statements.join( "\n" ) );
  134. },
  135. process: function( context ) {
  136. return "";
  137. },
  138. /* deprecated */
  139. exec: function( context ) {
  140. log( "Template::exec() method has been deprecated. Please use process() instead or " +
  141. "the new static Template.process( name[, vars[, templates]] ) method." );
  142. return this.process( context );
  143. }
  144. } );
  145. /* static members */
  146. extend( Template, {
  147. templates: {},
  148. process: function( name, vars, templates ) {
  149. var context = new Template.Context( vars, templates );
  150. return context.include( name );
  151. }
  152. } );
  153. /* context object */
  154. Template.Context = new Class( Object, {
  155. init: function( vars, templates ) {
  156. this.vars = vars || {};
  157. this.templates = templates || Template.templates;
  158. this.stack = [];
  159. this.out = [];
  160. this.f = Template.Filter;
  161. },
  162. include: function( name ) {
  163. if ( !this.templates.hasOwnProperty( name ) ) {
  164. log.error( "Template name " + name + " does not exist!" );
  165. return;
  166. }
  167. if ( typeof this.templates[ name ] == "string" )
  168. this.templates[ name ] = new Template( this.templates[ name ] );
  169. try {
  170. return this.templates[ name ].process( this );
  171. } catch( e ) {
  172. var error = "Error while processing template:" + name + " - " + e.message;
  173. log.error( error );
  174. throw error;
  175. }
  176. },
  177. write: function() {
  178. this.out.push.apply( this.out, arguments );
  179. },
  180. writeln: function() {
  181. this.write.apply( this, arguments );
  182. this.write( "\n" );
  183. },
  184. clear: function() {
  185. this.out.length = 0;
  186. },
  187. exit: function() {
  188. return this.getOutput();
  189. },
  190. getOutput: function() {
  191. return this.out.join( "" );
  192. },
  193. open: function() {
  194. this.stack.push( this.out );
  195. this.out = [];
  196. },
  197. close: function() {
  198. var result = this.getOutput();
  199. this.out = this.stack.pop() || [];
  200. return result;
  201. }
  202. } );
  203. /* filters */
  204. Template.Filter = {
  205. /* interpolate */
  206. i: function( string, context ) {
  207. if ( ( typeof string != "string" ) && string && string.toString )
  208. string = string.toString();
  209. return string.interpolate( context.vars );
  210. },
  211. /* escapeHTML */
  212. h: function( string, context ) {
  213. if ( ( typeof string != "string" ) && string && string.toString )
  214. string = string.toString();
  215. return ( typeof string == "string" )
  216. ? string.encodeHTML() : "".encodeHTML( string );
  217. },
  218. /* unescapeHTML */
  219. H: function( string, context ) {
  220. if ( ( typeof string != "string" ) && string && string.toString )
  221. string = string.toString();
  222. return ( typeof string == "string" )
  223. ? string.decodeHTML() : "".encodeHTML( string );
  224. },
  225. /* decodeURI */
  226. U: function( string, context ) {
  227. return decodeURI( string );
  228. },
  229. /* escapeURI */
  230. u: function( string, context ) {
  231. return encodeURI( string ).replace( /\//g, "%2F" );
  232. },
  233. /* lowercase */
  234. lc: function( string, context ) {
  235. if ( ( typeof string != "string" ) && string && string.toString )
  236. string = string.toString();
  237. return ( typeof string == "string" )
  238. ? string.toLowerCase() : "".toLowerCase( string );
  239. },
  240. /* uppercase */
  241. uc: function( string, context ) {
  242. if ( ( typeof string != "string" ) && string && string.toString )
  243. string = string.toString();
  244. return ( typeof string == "string" )
  245. ? string.toUpperCase() : "".toUpperCase( string );
  246. },
  247. /* substr */
  248. substr: function( string, context, params ) {
  249. if( !params )
  250. throw "Template Filter Error: substr() requires at least one parameter";
  251. /* allow negative offset */
  252. if( params[ 0 ] < 0 )
  253. params[ 0 ] = string.length + params[ 0 ];
  254. return String.substr.apply( string, params );
  255. },
  256. /* removes whitepace before and after */
  257. ws: function( string, context ) {
  258. if ( ( typeof string != "string" ) && string && string.toString )
  259. string = string.toString();
  260. return ( typeof string == "string" )
  261. ? string.replace( /^\s+/g, "" ).replace( /\s+$/g, "" ) : string;
  262. },
  263. /* trims to length and adds elipsis */
  264. trim: function( string, context, params ) {
  265. if ( !params )
  266. throw "Template Filter Error: trim() requires at least one parameter";
  267. if ( ( typeof string != "string" ) && string && string.toString )
  268. string = string.toString();
  269. if ( ( typeof string == "string" ) && string.length > params[ 0 ] ) {
  270. string = string.substr( 0, params[ 0 ] );
  271. /* don't trunc on a word */
  272. var newstr = string.replace( /\w+$/, "" );
  273. return ( ( newstr == "" ) ? string : newstr ) + "\u2026";
  274. } else
  275. return string;
  276. },
  277. /* returns YYYY-MM-DD from an iso string like: 1995-02-05T13:00:00.000-08:00 */
  278. date: function( string, context ) {
  279. if ( ( typeof string != "string" ) && string && string.toString )
  280. string = string.toString();
  281. var date = Date.fromISOString( string );
  282. return ( date ) ? date.toISODateString() : "";
  283. },
  284. localeDate: function( string, context ) {
  285. if ( ( typeof string != "string" ) && string && string.toString )
  286. string = string.toString();
  287. var date = Date.fromISOString( string );
  288. return ( date ) ? date.toLocaleString() : "";
  289. },
  290. /* remove html tags */
  291. rt: function( string, context ) {
  292. if ( ( typeof string != "string" ) && string && string.toString )
  293. string = string.toString();
  294. return ( typeof string == "string" )
  295. ? string.replace( /<\/?[^>]+>/gi, "" ) : string;
  296. },
  297. rp: function( string, params ) {
  298. if ( ( typeof string != "string" ) && string && string.toString )
  299. string = string.toString();
  300. if ( ( typeof string == "string" ) && params.length == 2 ) {
  301. return string.replace( params[ 0 ], params[ 1 ] );
  302. } else
  303. return string;
  304. }
  305. };