Skip to content

Commit

Permalink
Merge branch 'cbml'
Browse files Browse the repository at this point in the history
fixed error
  • Loading branch information
zswang committed Jul 30, 2015
1 parent 13a4fc2 commit 3eb41f7
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 251 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/jdists');
module.exports = require('./src/jdists');
2 changes: 1 addition & 1 deletion lib/jdists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 0 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down Expand Up @@ -57,16 +51,11 @@
},
"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 "
},
"files": [
"lib",
"processor"
]
>>>>>>> cbml
}
213 changes: 0 additions & 213 deletions src/jdists.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,233 +117,20 @@ 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*</.test(node.content)) {
node.content = node.content.replace(/^\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];
if (item) {
tags[name] = item;
}
}
>>>>>>> cbml
}
}

Expand Down
11 changes: 0 additions & 11 deletions test/fixtures/relative.input.html

This file was deleted.

13 changes: 0 additions & 13 deletions test/fixtures/relative.output.html

This file was deleted.

0 comments on commit 3eb41f7

Please sign in to comment.