PageRenderTime 47ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/lib/JSDOC/JsPlate.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 100 lines | 93 code | 4 blank | 3 comment | 1 complexity | c22cdf5c6ca1af460a458fcc7b22c824 MD5 | raw file
  1. /**
  2. @constructor
  3. */
  4. JSDOC.JsPlate = function(templateFile) {
  5. if (templateFile) this.template = IO.readFile(templateFile);
  6. this.templateFile = templateFile;
  7. this.code = "";
  8. this.parse();
  9. }
  10. JSDOC.JsPlate.prototype.parse = function() {
  11. this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
  12. this.code = "var output=``"+this.template;
  13. this.code = this.code.replace(
  14. /<for +each="(.+?)" +in="(.+?)" *>/gi,
  15. function (match, eachName, inName) {
  16. return "``;\rvar $"+eachName+"_keys = keys("+inName+");\rfor(var $"+eachName+"_i = 0; $"+eachName+"_i < $"+eachName+"_keys.length; $"+eachName+"_i++) {\rvar $"+eachName+"_last = ($"+eachName+"_i == $"+eachName+"_keys.length-1);\rvar $"+eachName+"_key = $"+eachName+"_keys[$"+eachName+"_i];\rvar "+eachName+" = "+inName+"[$"+eachName+"_key];\routput+=``";
  17. }
  18. );
  19. this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { output+=``");
  20. this.code = this.code.replace(/<elseif test="(.+?)"\s*\/>/g, "``;}\relse if ($1) { output+=``");
  21. this.code = this.code.replace(/<else\s*\/>/g, "``;}\relse { output+=``");
  22. this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
  23. this.code = this.code.replace(
  24. /\{\+\s*([\s\S]+?)\s*\+\}/gi,
  25. function (match, code) {
  26. code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
  27. code = code.replace(/(\r?\n)/g, " ");
  28. return "``+ ("+code+") +``";
  29. }
  30. );
  31. this.code = this.code.replace(
  32. /\{!\s*([\s\S]+?)\s*!\}/gi,
  33. function (match, code) {
  34. code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
  35. code = code.replace(/(\n)/g, " ");
  36. return "``; "+code+";\routput+=``";
  37. }
  38. );
  39. this.code = this.code+"``;";
  40. this.code = this.code.replace(/(\r?\n)/g, "\\n");
  41. this.code = this.code.replace(/"/g, "\\\"");
  42. this.code = this.code.replace(/``/g, "\"");
  43. }
  44. JSDOC.JsPlate.prototype.toCode = function() {
  45. return this.code;
  46. }
  47. JSDOC.JsPlate.keys = function(obj) {
  48. var keys = [];
  49. if (obj.constructor.toString().indexOf("Array") > -1) {
  50. for (var i = 0; i < obj.length; i++) {
  51. keys.push(i);
  52. }
  53. }
  54. else {
  55. for (var i in obj) {
  56. keys.push(i);
  57. }
  58. }
  59. return keys;
  60. };
  61. JSDOC.JsPlate.values = function(obj) {
  62. var values = [];
  63. if (obj.constructor.toString().indexOf("Array") > -1) {
  64. for (var i = 0; i < obj.length; i++) {
  65. values.push(obj[i]);
  66. }
  67. }
  68. else {
  69. for (var i in obj) {
  70. values.push(obj[i]);
  71. }
  72. }
  73. return values;
  74. };
  75. JSDOC.JsPlate.prototype.process = function(data) {
  76. var keys = JSDOC.JsPlate.keys;
  77. var values = JSDOC.JsPlate.values;
  78. try {
  79. eval(this.code);
  80. }
  81. catch (e) {
  82. print(">> There was an error evaluating the compiled code from template: "+this.templateFile);
  83. print(" The error was on line "+e.lineNumber+" "+e.name+": "+e.message);
  84. var lines = this.code.split("\r");
  85. if (e.lineNumber-2 >= 0) print("line "+(e.lineNumber-1)+": "+lines[e.lineNumber-2]);
  86. print("line "+e.lineNumber+": "+lines[e.lineNumber-1]);
  87. print("");
  88. }
  89. /*debug*///print(this.code);
  90. return output;
  91. }