diff --git a/.jdistsrc b/.jdistsrc index 10d05fa..2c65d82 100644 --- a/.jdistsrc +++ b/.jdistsrc @@ -6,6 +6,9 @@ }, "xor": { "encoding": "xor" + }, + "toggle": { + "encoding": "toggle" } }, "processors": { diff --git a/lib/jdists.js b/lib/jdists.js index 1f749fb..c9b31da 100644 --- a/lib/jdists.js +++ b/lib/jdists.js @@ -4,8 +4,8 @@ * Code block processing tools * @author * zswang (http://weibo.com/zswang) - * @version 2.0.0-alpha.6 - * @date 2016-10-28 + * @version 2.0.0 + * @date 2016-11-14 */ var fs = require('fs'); var path = require('path'); @@ -20,6 +20,7 @@ var defaultProcessors = { "jphps": require('../processor/processor-jphps'), "quoted": require('../processor/processor-quoted'), "regex": require('../processor/processor-regex'), + "toggle": require('../processor/processor-toggle'), }; var defaultTags = { jdists: { diff --git a/lib/scope.js b/lib/scope.js index 7b18720..da1ac18 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -4,8 +4,8 @@ * Code block processing tools * @author * zswang (http://weibo.com/zswang) - * @version 2.0.0-alpha.6 - * @date 2016-10-28 + * @version 2.0.0 + * @date 2016-11-14 */ var colors = require('colors'); var util = require('util'); diff --git a/package.json b/package.json index 713b6c0..1bb2566 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jdists", - "version": "2.0.0-alpha.6", + "version": "2.0.0", "description": "Code block processing tools", "main": "lib/jdists.js", "bin": { @@ -59,6 +59,7 @@ "_update_version": "jdists version.jdists", "_dist": "jdists src/jdists.js -o lib/jdists.js && jdists src/scope.js -o lib/scope.js", "dist": "npm run _update_version && npm run _dist && npm run test", + "dist2": "npm run _dist && npm run test", "test": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -R spec", "mocha": "mocha", "lint": "jshint src/*.js src/**/*.js processor/*.js processor-extend/*.js" diff --git a/processor/processor-toggle.js b/processor/processor-toggle.js new file mode 100644 index 0000000..09c24d6 --- /dev/null +++ b/processor/processor-toggle.js @@ -0,0 +1,41 @@ +/** + * 注释块切换 + * + * @param {string} content 文本内容 + * @param {Object} attrs 属性 + * @param {Object} scope 作用域 + * @param {Object} node 当前节点 + */ +module.exports = function processor(content, attr, scope, node) { + if (!content || node.type != 'block') { + return content; + } + + var lang = { + 'c': { + prefix: '/*<', + suffix: '>*/', + }, + 'pascal': { + prefix: '(*<', + suffix: '>*)', + }, + 'lua': { + prefix: '--[[<', + suffix: '>]]', + }, + 'python': { + prefix: "'''<", + suffix: ">'''", + }, + 'xml': { + prefix: "", + }, + }[node.language]; + + if (node.comment) { + return node.prefix.slice(0, -1) + lang.suffix + node.content + lang.prefix + node.suffix.slice(1); + } + return node.prefix.slice(0, -lang.suffix.length) + '>' + node.content + '<' + node.suffix.slice(lang.prefix.length); +}; \ No newline at end of file diff --git a/src/jdists.js b/src/jdists.js index 3674a68..f754c1d 100644 --- a/src/jdists.js +++ b/src/jdists.js @@ -94,6 +94,9 @@ function build(filename, argv, hook) { }, "xor": { "encoding": "xor" + }, + "toggle": { + "encoding": "toggle" } }, "processors": { diff --git a/test/fixtures/toggle.input.html b/test/fixtures/toggle.input.html new file mode 100644 index 0000000..c2ad1ff --- /dev/null +++ b/test/fixtures/toggle.input.html @@ -0,0 +1,31 @@ + + + +xml On + +/* +c Off +*/ +/**/ +c On +/**/ +(* +pascal Off +*) +(**) +pascal On +(**) +''' +python Off +''' +'''''' +python On +'''''' +--[[ +lua Off +]] +--[[]] +lua On +--[[]] \ No newline at end of file diff --git a/test/fixtures/toggle.output.html b/test/fixtures/toggle.output.html new file mode 100644 index 0000000..e126b3e --- /dev/null +++ b/test/fixtures/toggle.output.html @@ -0,0 +1,31 @@ + + +xml Off + + +/**/ +c Off +/**/ +/* +c On +*/ +(**) +pascal Off +(**) +(* +pascal On +*) +'''''' +python Off +'''''' +''' +python On +''' +--[[]] +lua Off +--[[]] +--[[ +lua On +]] \ No newline at end of file