var path = require('path'),
	fs = require('fs'),
	util = require('../util.js'),
	
	includeReg = /<% *include +([^ ]+?) *%>/ig,
	loopReg = /<% *loop +(\d+)(?: +: +(\w+))? *%>/ig,
	endLoopReg = /<% *endLoop *%>/ig,
	blockReg = /<% *block *([\w ]+) *%>/ig,
	endBlockReg = /<% *endBlock *%>/ig,

	includeRel = {};


function createArray(len) {
	var arr = [];
	for (i = 0; i < len; i++) arr.push(i);
		
	return arr;
}


module.exports = function(cont, file, options, parseHTML){
	var fileRoot = path.dirname(file)+'/',
		appConf = options.appConf;

	var rel = includeRel[file];		// 如果没有值，则表明是入口HTML（解析顺序决定这一判断）
	
	cont = cont.replace(includeReg, function(str, inFile) {
			inFile = util.parsePath(fileRoot+ inFile);

			var rel2 = includeRel[inFile];
			if (!rel2) {
				rel2 = {};
				includeRel[inFile] = rel2;
			}

			rel2[file] = rel;
			
			if (!appConf.watchFiles[inFile]) {
				appConf.addPageReloadWatch(inFile, function(){
					delete appConf.cacheTpl[inFile];

					util.eachObject(rel2, function(i, v){
						delete appConf.cacheTpl[i];
						if (v) {
							util.eachObject(v, arguments.callee);		// 如果关联了 include文件，那么继续遍历
						} else {
							delete appConf.cache[i];					// 如果关联的是入口HTML，那么删除入口HTML的缓存
						}
					});
				});
			}
			return parseHTML(fs.readFileSync(inFile).toString(), inFile, options)+'\n';
		})
		
		.replace(loopReg, function(str, len, paramName){
			return '<% ['+createArray(len).join(',')+'].forEach(function('+(paramName || '')+'){ %>';
		}).replace(endLoopReg, function(){
			return '<% }); %>';
		})

		.replace(blockReg, function(str, blocks){
			return '<% if (!block || block.test("'+blocks+'")) { %>';
		}).replace(endBlockReg, function(){
			return '<% } %>';
		});

	return cont;
};
@@ -73,16 +73,51 @@
 l.js'),%0A
+%09notice = require('../notice.js'),%0A
 %09%0A%09inclu
@@ -739,17 +739,49 @@
 nFile);%0A
-%0A
+%09%09%09if (fs.existsSync(inFile)) %7B%0A%09
 %09%09%09var r
@@ -806,16 +806,17 @@
 nFile%5D;%0A
+%09
 %09%09%09if (!
@@ -827,16 +827,17 @@
 ) %7B%0A%09%09%09%09
+%09
 rel2 = %7B
@@ -839,16 +839,17 @@
 2 = %7B%7D;%0A
+%09
 %09%09%09%09incl
@@ -874,19 +874,21 @@
 el2;%0A%09%09%09
+%09
 %7D%0A%0A
+%09
 %09%09%09rel2%5B
@@ -903,17 +903,19 @@
 rel;%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09if (!
@@ -940,24 +940,25 @@
 %5BinFile%5D) %7B%0A
+%09
 %09%09%09%09appConf.
@@ -997,24 +997,25 @@
 ion()%7B%0A%09%09%09%09%09
+%09
 delete appCo
@@ -1036,16 +1036,17 @@
 File%5D;%0A%0A
+%09
 %09%09%09%09%09uti
@@ -1078,32 +1078,33 @@
 on(i, v)%7B%0A%09%09%09%09%09%09
+%09
 delete appConf.c
@@ -1117,24 +1117,25 @@
 l%5Bi%5D;%0A%09%09%09%09%09%09
+%09
 if (v) %7B%0A%09%09%09
@@ -1127,24 +1127,25 @@
 %09%09%09if (v) %7B%0A
+%09
 %09%09%09%09%09%09%09util.
@@ -1210,16 +1210,17 @@
 %E5%8E%86%0A%09%09%09%09%09%09
+%09
 %7D else %7B
@@ -1220,16 +1220,17 @@
  else %7B%0A
+%09
 %09%09%09%09%09%09%09d
@@ -1293,17 +1293,28 @@
 %E5%AD%98%0A%09%09%09%09%09%09
-%7D
+%09%7D%0A%09%09%09%09%09%09%7D);
 %0A%09%09%09%09%09%7D)
@@ -1324,16 +1324,10 @@
 %09%09%09%7D
-);%0A%09%09%09%7D
 %0A
+%09
 %09%09%09r
@@ -1401,16 +1401,111 @@
 )+'%5Cn';%0A
+%09%09%09%7D else %7B%0A%09%09%09%09notice.warn('HTML', 'include file is not exists', inFile);%0A%09%09%09%09return '';%0A%09%09%09%7D%0A
 %09%09%7D)%0A%09%09%0A

修正htmlTpl中，由于include文件不存在，导致的文件监听及读取错误