Skip to content

Commit

Permalink
Add "toggle" processer.
Browse files Browse the repository at this point in the history
Merge branch 'master' into 2.0.0-alpha

# Conflicts:
#	lib/jdists.js
#	lib/scope.js
#	package.json
  • Loading branch information
zswang committed Nov 14, 2016
2 parents f2b6130 + 00998cf commit a9c1128
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .jdistsrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
},
"xor": {
"encoding": "xor"
},
"toggle": {
"encoding": "toggle"
}
},
"processors": {
Expand Down
5 changes: 3 additions & 2 deletions lib/jdists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions processor/processor-toggle.js
Original file line number Diff line number Diff line change
@@ -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: "<!--",
suffix: "-->",
},
}[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);
};
3 changes: 3 additions & 0 deletions src/jdists.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ function build(filename, argv, hook) {
},
"xor": {
"encoding": "xor"
},
"toggle": {
"encoding": "toggle"
}
},
"processors": {
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/toggle.input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--jdists encoding="toggle" /-->
<!--jdists encoding="toggle">
xml Off
</jdists-->
<!--jdists encoding="toggle"-->
xml On
<!--/jdists-->
/*<jdists encoding="toggle">
c Off
</jdists>*/
/*<jdists encoding="toggle">*/
c On
/*</jdists>*/
(*<jdists encoding="toggle">
pascal Off
</jdists>*)
(*<jdists encoding="toggle">*)
pascal On
(*</jdists>*)
'''<jdists encoding="toggle">
python Off
</jdists>'''
'''<jdists encoding="toggle">'''
python On
'''</jdists>'''
--[[<jdists encoding="toggle">
lua Off
</jdists>]]
--[[<jdists encoding="toggle">]]
lua On
--[[</jdists>]]
31 changes: 31 additions & 0 deletions test/fixtures/toggle.output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

<!--jdists encoding="toggle"-->
xml Off
<!--/jdists-->
<!--jdists encoding="toggle">
xml On
</jdists-->
/*<jdists encoding="toggle">*/
c Off
/*</jdists>*/
/*<jdists encoding="toggle">
c On
</jdists>*/
(*<jdists encoding="toggle">*)
pascal Off
(*</jdists>*)
(*<jdists encoding="toggle">
pascal On
</jdists>*)
'''<jdists encoding="toggle">'''
python Off
'''</jdists>'''
'''<jdists encoding="toggle">
python On
</jdists>'''
--[[<jdists encoding="toggle">]]
lua Off
--[[</jdists>]]
--[[<jdists encoding="toggle">
lua On
</jdists>]]

0 comments on commit a9c1128

Please sign in to comment.