Skip to content

Commit 1aa3f74

Browse files
committed
modify xml reader
1 parent 2c64749 commit 1aa3f74

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

bin/xml2

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
const xml = require('..');
4+
5+
const [ name, fn ] = process.argv.slice(2);
6+
7+
const d = [];
8+
process.stdin
9+
.on('data', d.push.bind(d))
10+
.on('end', async () => {
11+
let data = await xml.read(d.join(''));
12+
if(name){
13+
if(name === '-e' && fn){
14+
data = eval(`(${fn})(data)`);
15+
}else{
16+
data = data[name];
17+
}
18+
}
19+
try{
20+
console.log(data);
21+
process.exit(0);
22+
}catch(e){
23+
console.error(e);
24+
process.exit(-1);
25+
}
26+
})

example/demo.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
<import src="item.wxml" />
2-
<view class="question">
3-
<view class="question-title">
4-
<text class="question-id">{{ question.I }}</text>
5-
<text>{{ question.Q }}</text>
6-
</view>
7-
<view wx:if="{{ question.P }}">
8-
<image class="question-image" mode="aspectFit" src="https://lsong.org/~lsong/HAM/img/{{question.P}}" />
9-
</view>
10-
<view class="question-options">
11-
<view bindtap="onTapOption" wx:for="{{ question.options }}" wx:for-item="option" wx:key="{{ question.I }}" data-option="{{ option }}" class="question-option question-option-{{index}} question-option-{{ option.style }}">
12-
{{ option.text }}
13-
</view>
14-
</view>
15-
</view>

example/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,5 @@ const printer = require('../printer');
66
const traverse = require('../traverse');
77

88
xml.readFile(__dirname + '/demo.xml').then(obj => {
9-
traverse(obj, node => {
10-
if(node.name === 'image'){
11-
node.attributes.src = 'abc';
12-
}
13-
});
14-
console.log(xml.serialize(obj));
9+
console.log(obj);
1510
});

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const EventEmitter = require('events');
88

99
class XML extends EventEmitter {
1010
static read(str){
11-
return new Promise(done => reader(done)(str));
11+
return new Promise(done => reader(done)(`<root>${str}</root>`))
12+
.then(root => root.children);
1213
}
1314
static async readFile(filename, options = {}){
1415
const readFile = promisify(fs.readFile);
1516
const source = await readFile(filename, 'utf8');
16-
const ast = await XML.read(`<root>${source}</root>`, options);
17-
return ast.children;
17+
return XML.read(source, options);
1818
}
1919
static serialize(ast, options){
2020
return printer(ast, options);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "xml2",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "simple xml reader and parser",
55
"main": "index.js",
66
"browser": "browser.js",
77
"scripts": {
88
"build": "rollup -c"
99
},
10+
"bin": "./bin/xml2",
1011
"repository": {
1112
"type": "git",
1213
"url": "git+https://github.com/song940/xmljs.git"

parser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const ACTIONS = {
2020
quote: 'action-quote',
2121
slash: 'action-slash',
2222
char: 'action-char',
23-
bang: 'action-bang'
23+
bang: 'action-bang',
24+
question: 'action-question'
2425
};
2526

2627
const TYPES = {
@@ -44,6 +45,7 @@ const charToAction = {
4445
'=': ACTIONS.equal,
4546
'/': ACTIONS.slash,
4647
'!': ACTIONS.bang,
48+
'?': ACTIONS.question,
4749
};
4850

4951
const noop = () => {};
@@ -155,6 +157,10 @@ module.exports = emit => {
155157
state = STATES.DATA;
156158
},
157159
[ACTIONS.space]: noop,
160+
[ACTIONS.question]: () => {
161+
isClosing = true;
162+
state = STATES.TAG_END;
163+
},
158164
[ACTIONS.slash]: () => {
159165
isClosing = true;
160166
state = STATES.TAG_END;

0 commit comments

Comments
 (0)