1 /**
   2  * @class JsPlateクラスはテンプレートファイルにシンボルオブジェクトなどのデータを適用し、整形されたドキュメントを生成します。 
   3  * @param {String} templateFile テンプレートファイルのパス
   4 */
   5 JSDOC.JsPlate = function(templateFile) {
   6     if (templateFile) this.template = IO.readFile(templateFile);
   7     
   8     /**
   9      * テンプレートファイルのパス
  10      * @type String
  11      */
  12     this.templateFile = templateFile;
  13     
  14     this.code = "";
  15     this.parse();
  16 }
  17 
  18 /** @ignore */
  19 JSDOC.JsPlate.prototype.parse = function() {
  20     this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
  21     this.code = "var output=\u001e"+this.template;
  22 
  23     this.code = this.code.replace(
  24         /<for +each="(.+?)" +in="(.+?)" *>/gi, 
  25         function (match, eachName, inName) {
  26             return "\u001e;\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+=\u001e";
  27         }
  28     );    
  29     this.code = this.code.replace(/<if test="(.+?)">/g, "\u001e;\rif ($1) { output+=\u001e");
  30     this.code = this.code.replace(/<elseif test="(.+?)"\s*\/>/g, "\u001e;}\relse if ($1) { output+=\u001e");
  31     this.code = this.code.replace(/<else\s*\/>/g, "\u001e;}\relse { output+=\u001e");
  32     this.code = this.code.replace(/<\/(if|for)>/g, "\u001e;\r};\routput+=\u001e");
  33     this.code = this.code.replace(
  34         /\{\+\s*([\s\S]+?)\s*\+\}/gi,
  35         function (match, code) {
  36             code = code.replace(/"/g, "\u001e"); // prevent qoute-escaping of inline code
  37             code = code.replace(/(\r?\n)/g, " ");
  38             return "\u001e+ ("+code+") +\u001e";
  39         }
  40     );
  41     this.code = this.code.replace(
  42         /\{!\s*([\s\S]+?)\s*!\}/gi,
  43         function (match, code) {
  44             code = code.replace(/"/g, "\u001e"); // prevent qoute-escaping of inline code
  45             code = code.replace(/(\n)/g, " ");
  46             return "\u001e; "+code+";\routput+=\u001e";
  47         }
  48     );
  49     this.code = this.code+"\u001e;";
  50 
  51     this.code = this.code.replace(/(\r?\n)/g, "\\n");
  52     this.code = this.code.replace(/"/g, "\\\"");
  53     this.code = this.code.replace(/\u001e/g, "\"");
  54 }
  55 
  56 /**
  57  * パース済みのテンプレートコードを返します。
  58  * @return {String} パース済みのテンプレートコード
  59  */
  60 JSDOC.JsPlate.prototype.toCode = function() {
  61     return this.code;
  62 }
  63 
  64 /**
  65  * 配列のキー(インデックス)またはオブジェクトのプロパティ名の配列を返します。
  66  * @param {*[] | Object} obj 処理対象の配列またはオブジェクト
  67  * @return {String[]} 配列のキー(インデックス)またはオブジェクトのプロパティ名の配列
  68  */
  69 JSDOC.JsPlate.keys = function(obj) {
  70     var keys = [];
  71     if (obj.constructor.toString().indexOf("Array") > -1) {
  72         for (var i = 0; i < obj.length; i++) {
  73             keys.push(i);
  74         }
  75     }
  76     else {
  77         for (var i in obj) {
  78             keys.push(i);
  79         }
  80     }
  81     return keys;
  82 };
  83 
  84 /**
  85  * 配列の要素またはオブジェクトのプロパティ値の配列を返します。
  86  * @param {*[] | Object} obj 処理対象の配列またはオブジェクト
  87  * @return {*[]} 配列の要素またはオブジェクトのプロパティ値の配列
  88  */
  89 JSDOC.JsPlate.values = function(obj) {
  90     var values = [];
  91     if (obj.constructor.toString().indexOf("Array") > -1) {
  92         for (var i = 0; i < obj.length; i++) {
  93             values.push(obj[i]);
  94         }
  95     }
  96     else {
  97         for (var i in obj) {
  98             values.push(obj[i]);
  99         }
 100     }
 101     return values;
 102 };
 103 
 104 /**
 105  * テンプレートにデータを渡し、生成されたドキュメント文字列を返します。
 106  * @param {Object} data テンプレートに渡されるデータ
 107  * @param {Boolean} [compact=false] trueとすると、テンプレートから改行と空白を削除して出力します。
 108  * @return {String} 生成されたドキュメントコンテンツ 
 109  */
 110 JSDOC.JsPlate.prototype.process = function(data, compact) {
 111     var keys = JSDOC.JsPlate.keys;
 112     var values = JSDOC.JsPlate.values;
 113     
 114     try {
 115         eval(this.code);
 116     }
 117     catch (e) {
 118         print(">> There was an error evaluating the compiled code from template: "+this.templateFile);
 119         print("   The error was on line "+e.lineNumber+" "+e.name+": "+e.message);
 120         var lines = this.code.split("\r");
 121         if (e.lineNumber-2 >= 0) print("line "+(e.lineNumber-1)+": "+lines[e.lineNumber-2]);
 122         print("line "+e.lineNumber+": "+lines[e.lineNumber-1]);
 123         print("");
 124     }
 125     
 126     if (compact) { // patch by mcbain.asm
 127          // Remove lines that contain only space-characters, usually left by lines in the template
 128          // which originally only contained JSPlate tags or code. This makes it easier to write
 129          // non-tricky templates which still put out nice code (not bloated with extra lines).
 130          // Lines purposely left blank (just a line ending) are left alone.
 131          output = output.replace(/\s+?(\r?)\n/g, "$1\n");
 132      }
 133      
 134     /*debug*///print(this.code);
 135     return output;
 136 }