From 3eb41f7ddc9083d7878f973b2952710b325d93f6 Mon Sep 17 00:00:00 2001 From: zswang Date: Thu, 30 Jul 2015 15:40:11 +0800 Subject: [PATCH] Merge branch 'cbml' fixed error --- index.js | 2 +- lib/jdists.js | 2 +- lib/scope.js | 2 +- package.json | 11 -- src/jdists.js | 213 ----------------------------- test/fixtures/relative.input.html | 11 -- test/fixtures/relative.output.html | 13 -- 7 files changed, 3 insertions(+), 251 deletions(-) delete mode 100644 test/fixtures/relative.input.html delete mode 100644 test/fixtures/relative.output.html diff --git a/index.js b/index.js index 4782b77..4908545 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib/jdists'); \ No newline at end of file +module.exports = require('./src/jdists'); \ No newline at end of file diff --git a/lib/jdists.js b/lib/jdists.js index 07fab6d..da7982d 100644 --- a/lib/jdists.js +++ b/lib/jdists.js @@ -4,7 +4,7 @@ * Code block processing tools * @author * zswang (http://weibo.com/zswang) - * @version 1.0.0 + * @version 0.9.0 * @date 2015-07-30 */ var fs = require('fs'); diff --git a/lib/scope.js b/lib/scope.js index 3087242..ee21076 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -4,7 +4,7 @@ * Code block processing tools * @author * zswang (http://weibo.com/zswang) - * @version 1.0.0 + * @version 0.9.0 * @date 2015-07-30 */ var colors = require('colors'); diff --git a/package.json b/package.json index d5b83c2..355d575 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,8 @@ { "name": "jdists", -<<<<<<< HEAD - "version": "0.7.7", - "description": "JS 区块进行预处理工具", - "main": "src/jdists.js", -======= "version": "0.9.0", "description": "Code block processing tools", "main": "index.js", ->>>>>>> cbml "bin": { "jdists": "cli.js" }, @@ -57,10 +51,6 @@ }, "scripts": { "test": "mocha -R spec", -<<<<<<< HEAD - "lint": "jshint src/**/*.js" - } -======= "lint": "jshint src/*.js src/**/*.js processor/*.js", "dist": "node cli.js src/jdists.js -o lib/jdists.js && node cli.js src/scope.js -o lib/scope.js " }, @@ -68,5 +58,4 @@ "lib", "processor" ] ->>>>>>> cbml } \ No newline at end of file diff --git a/src/jdists.js b/src/jdists.js index 8f64635..0af2eea 100644 --- a/src/jdists.js +++ b/src/jdists.js @@ -117,225 +117,13 @@ function build(filename, argv) { if (config.exclude instanceof Array) { excludeList = excludeList.concat(config.exclude); } -<<<<<<< HEAD - options = options || {}; - - if (/\.(png|jpeg|jpg|mp3|ogg|gif|eot|ttf|woff)$/.test(filename) || - (options.isBinary && options.isBinary(filename))) { // 已知二进制文件 - blocks[[filename, '']].isBinary = true; - return; - } - - var dirname = path.dirname(filename); - - var readBlock = function(all, fl, tag, attrText, fr, content, end, pos) { - var attrs = getAttrs(tag, attrText, dirname); - - if (attrs.trigger && - !common.intersection(options.triggerList, attrs.trigger)) { - return all; -======= if (config.processors) { for (var encoding in config.processors) { registerProcessor(encoding, config.processors[encoding]); ->>>>>>> cbml } } -<<<<<<< HEAD - blocks[[filename, '']].content = content; - - return buildBlock(blocks[[filename, '']].content, readBlock); - }; - - /** - * 替换文件内容 - * @param {string} filename 文件名,绝对路径 - * @param {Object} options 配置项 - * @return 返回替换后的内容 - */ - var replaceFile = function(filename, options) { - if (!blocks[[filename, '']]) { - return ''; - } - - var dirname = path.dirname(filename); - - var readBlock = function(all, fl, tag, attrText, fr, content, end) { - - var attrs = getAttrs(tag, attrText, dirname); - - if (attrs.trigger && - !common.intersection(options.triggerList, attrs.trigger)) { - return all; - } - - if (options.removeList.indexOf(tag) >= 0) { - return ''; - } - - switch (tag) { - case 'replace': - case 'include': - var isBinary = false; - var blockfile = ''; - var blockname = ''; - - if (variants[attrs.import]) { // 文件在变量中出现 - content = variants[attrs.import]; - } - else if (attrs.block || attrs.file) { - blockfile = attrs['@filename'] || getAttrOrValue(attrs.file, filename); // 默认当前文件名 - blockname = getAttrOrValue(attrs.block, ''); // 默认全部文件 - - var key = [blockfile, blockname].join(); - var block = blocks[key]; - - if (!block) { // 没有发现预加载的块 - loadFile(blockfile, options); // 预处理,文件 - block = blocks[key]; - if (!block) { - return; - } - } - - if (!block.completed) { - if (chain.indexOf(key) >= 0) { // 出现循环引用 - throw new Error('Circular reference block.'); - } - chain.push(key); - - if (block.isFile) { - if (block.isBinary) { // 二进制文件 - block.content = fs.readFileSync(block.filename); - } - else { - block.content = replaceFile(block.filename, options); - } - } - else { - block.nodes.sort(function(a, b) { // 保证代码顺序 - return a.pos - b.pos; - }); - block.content = block.nodes.map(function(node) { - if (!node.completed) { - node.content = buildBlock(node.content, readBlock, true); - if (node.attrs.type === 'comment') { - if (/^\s*\s*$/, '$1'); - } - else { - node.content = node.content.replace(/^\s*\/\*([^]*)\*\/\s*$/, '$1'); - } - } - node.completed = true; - } - return node.content; - }).join('\n'); - } - block.completed = true; - chain.pop(); // 移除引用链 - } - content = block.content; - isBinary = block.isBinary; - } - - var trim = getAttrOrValue(attrs.trim, ''); - if (/^(true|before)$/.test(trim)) { // 编码前,清理空白字符 - content = content.trim(); - } - - var processor; - var encoding = getAttrOrValue(attrs.encoding, ''); - if (/^[\w-_]+$/.test(encoding)) { // 正常编码前 - processor = processors[encoding]; - } - else if (encoding) { // 编码器来至变量 - var module = { - exports: {} - }; - new Function('require', 'module', 'exports', encoding)( - require, module, module.exports - ); - processor = module.exports; - } - if (typeof processor === 'function') { // 编码处理器 - if (!isBinary) { // 非二进制文件再次编译 - content = buildBlock(content, readBlock, true); - } - content = processor({ - content: content, // 内容 - attrs: attrs, // 属性 - dirname: blockfile ? path.dirname(blockfile) : dirname, // 当前内容所在目录 - blockfile: blockfile, // 块文件名 - blockname: blockname, // 块名 - options: options, // 选项 - tag: tag, // 标签 - buildBlock: buildBlock, // 编译一个块 - readBlock: readBlock, // 读取模块的函数 - getValue: getValue, // 获取变量的函数 - setValue: setValue, // 设置变量 - getAttrOrValue: getAttrOrValue, // 获取属性或者是变量 - filename: filename, // 输入文件 - jdists: exports // jdists 本身 - }); - } - - if (attrs.trim === 'after') { // 编码前,清理空白字符 - content = content.trim(); - } - - if (attrs.slice) { - var params = attrs.slice.split(','); - content = content.slice(params[0], params[1]); - } - content = buildBlock(content, readBlock, true); - - if (attrs.export) { - if (/^#[\w-_]+$/.test(attrs.export)) { // 保存到虚拟文件中 - variants[attrs.export] = content; - } - else { - if (attrs['@export']) { - forceDirSync(path.dirname(attrs['@export'])); - fs.writeFileSync(attrs['@export'], content); - } - } - return ''; - } - return content; - case 'remove': // 必然移除的 - return ''; - } - - return fl + tag + attrText + fr + buildBlock(content, readBlock, true) + end; - }; - - return buildBlock(blocks[[filename, '']].content, readBlock, true); - }; - - var buildFile = function(filename, options) { - options = options || {}; - options.remove = options.remove || 'debug,test'; - options.trigger = options.trigger || 'release'; - options.triggerList = String(options.trigger).split(','); - options.removeList = String(options.remove).split(','); - options.clean = typeof options.clean === 'undefined' ? true : options.clean; - - if (options.nocache) { - blocks = {}; - } - - chain = []; // 引用链 - filename = path.resolve('', filename); // 使用绝对文件路径 - - loadFile(filename, options); // 预处理,文件 - var result = replaceFile(filename, options); - chain = []; // 引用链 - if (options.clean) { // 清理空白字符 - result = clean(result); -======= if (config.tags) { for (var name in config.tags) { var item = config.tags[name]; @@ -343,7 +131,6 @@ function build(filename, argv) { tags[name] = item; } } ->>>>>>> cbml } } diff --git a/test/fixtures/relative.input.html b/test/fixtures/relative.input.html deleted file mode 100644 index 4937b5a..0000000 --- a/test/fixtures/relative.input.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/test/fixtures/relative.output.html b/test/fixtures/relative.output.html deleted file mode 100644 index 855e6ff..0000000 --- a/test/fixtures/relative.output.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - -
- - - - - \ No newline at end of file