PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/jsdoc_toolkit-1.3.3/app/JsPlate.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 130 lines | 107 code | 6 blank | 17 comment | 1 complexity | 678930aa44744eb9ccd1bde037ec6243 MD5 | raw file
  1. /**
  2. * @fileOverview
  3. * @name JsPlate
  4. * @author Michael Mathews micmath@gmail.com
  5. * @url $HeadURL: http://jsdoc-toolkit.googlecode.com/svn/tags/jsdoc_toolkit-1.3.3/app/JsPlate.js $
  6. * @revision $Id: JsPlate.js 213 2007-08-22 10:21:50Z micmath $
  7. * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
  8. * (See the accompanying README file for full details.)
  9. */
  10. /**
  11. * @class A lightweight template engine for JavaScript.
  12. * @constructor
  13. * @author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
  14. * @param {string} template
  15. */
  16. JsPlate = function(template) {
  17. this.template = IO.readFile(template);
  18. this.code = "";
  19. this.parse();
  20. }
  21. /** Converts a template into evalable code. */
  22. JsPlate.prototype.parse = function() {
  23. this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
  24. this.code = "var output=``"+this.template;
  25. this.code = this.code.replace(
  26. /<for each="(.+?)" in="(.+?)"(?: sortby="(.+?)")?>/g,
  27. function (match, eachName, inName, sortby) {
  28. if (!sortby) sortby = "asis";
  29. return "``; var $"+eachName+"_keys = "+sortby+"("+inName+"); for(var $"+eachName+"_i = 0; $"+eachName+"_i < $"+eachName+"_keys.length; $"+eachName+"_i++) { var $"+eachName+"_last = ($"+eachName+"_i == $"+eachName+"_keys.length-1); var $"+eachName+"_key = $"+eachName+"_keys[$"+eachName+"_i]; var "+eachName+" = "+inName+"[$"+eachName+"_key]; output+=``";
  30. }
  31. );
  32. this.code = this.code.replace(/<if test="(.+?)">/g, "``; if ($1) { output+=``");
  33. this.code = this.code.replace(/<\/(if|for)>/g, "``; }; output+=``");
  34. this.code = this.code.replace(
  35. /\{\+\s*([\s\S]+?)\s*\+\}/gi,
  36. function (match, code) {
  37. code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
  38. code = code.replace(/(\r?\n)/g, " ");
  39. return "``+"+code+"+``";
  40. }
  41. );
  42. this.code = this.code.replace(
  43. /\{!\s*([\s\S]+?)\s*!\}/gi,
  44. function (match, code) {
  45. code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
  46. code = code.replace(/(\r?\n)/g, " ");
  47. return "``; "+code+"; output+=``";
  48. }
  49. );
  50. this.code = this.code+"``;";
  51. this.code = this.code.replace(/(\r?\n)/g, "\\n");
  52. this.code = this.code.replace(/"/g, "\\\"");
  53. this.code = this.code.replace(/``/g, "\"");
  54. }
  55. /**
  56. * @private
  57. */
  58. JsPlate.prototype.toCode = function() {
  59. return this.code;
  60. }
  61. /**
  62. * @private
  63. * @static
  64. * @memberOf JsPlate
  65. */
  66. JsPlate.keys = function(obj) {
  67. var keys = [];
  68. if (obj.constructor.toString().indexOf("Array") > -1) {
  69. for (var i = 0; i < obj.length; i++) keys.push(i);
  70. }
  71. else {
  72. for (var i in obj) { keys.push(i); }
  73. }
  74. return keys.sort();
  75. };
  76. /**
  77. * @private
  78. * @static
  79. * @memberOf JsPlate
  80. */
  81. JsPlate.values = function(obj) {
  82. var values = [];
  83. if (obj.constructor.toString().indexOf("Array") > -1) {
  84. for (var i = 0; i < obj.length; i++) values.push(obj[i]);
  85. }
  86. else {
  87. for (var i in obj) { values.push(obj[i]); }
  88. }
  89. return values.sort();
  90. };
  91. /**
  92. * @private
  93. * @static
  94. * @memberOf JsPlate
  95. */
  96. JsPlate.asis = function(obj) {
  97. var keys = [];
  98. if (obj.constructor.toString().indexOf("Array") > -1) {
  99. for (var i = 0; i < obj.length; i++) keys.push(i);
  100. }
  101. else {
  102. for (var i in obj) { keys.push(i); }
  103. }
  104. return keys;
  105. };
  106. /**
  107. * Return the output. This must be called after parse()
  108. * @param {object} data What shall represent the "data" in your template.
  109. * @return {string}
  110. */
  111. JsPlate.prototype.process = function(data) {
  112. var keys = JsPlate.keys;
  113. var values = JsPlate.values;
  114. var asis = JsPlate.asis;
  115. eval(this.code);
  116. return output;
  117. //print(this.code)
  118. }