-
Notifications
You must be signed in to change notification settings - Fork 4
/
text2obj.js
executable file
·157 lines (140 loc) · 3.65 KB
/
text2obj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/local/bin/node
/*
text2obj - Generate a javascript object or extract data
from a textual data structure
Copyright (C) 2012 Luc Deschenaux - https://github.com/luxigo/text2obj
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var DEBUG;
var stack;
var result;
if (module && module.id!='.') {
module.exports={
parse: text2obj
}
} else {
var standAlone=true;
var fs=require('fs');
if (process.argv[2]=='-') {
var input='';
process.stdin.resume();
process.stdin.setEncoding('ascii');
process.stdin.on('data', function(chunk){
input += chunk;
})
process.stdin.on('end', function(){
text2obj(input);
});
} else {
var input=fs.readFileSync(process.argv[2]);
text2obj(input.toString());
}
}
function text2obj(input) {
result={};
stack=[
{
tag: 'root',
col: -1
}
];
if (input.length) {
input=input.toString().split('\n');
while(input.length) parseLine(input);
if (standAlone) {
var util=require('util');
if (process.argv.length>3) {
var i=3;
while (true) {
var levels=process.argv[i].split('.');
var requested=result;
try {
levels.forEach(function(prop){
requested=requested[prop];
});
console.log(typeof(requested)=='object'?util.inspect(requested,false,null):requested);
} catch(e) {
console.log(e);
}
++i;
if (i>=process.argv.length) break;
}
} else {
console.log(typeof(result)=='object'?util.inspect(result,false,null):result);
}
process.exit(0);
}
}
return result;
}
function parseLine(input) {
var line=input.shift().trimRight();
if (!line.length) return;
var matches=line.match(/^( *)?([^:]+):(.*)?/);
if (DEBUG) console.log(matches);
var tag=matches[2].replace(/ /g,'');
var col=matches[1]?matches[1].length:0;
var curLevel=stack[stack.length-1];
if (DEBUG) console.log(curLevel,col);
while (col<=curLevel.col) {
stack.pop();
curLevel=stack[stack.length-1];
if (DEBUG) console.log(curLevel,col);
}
var value=matches[3];
if (value==undefined) {
var curLevelObj=getCurLevelObj();
if (curLevelObj[tag]==undefined) {
curLevelObj[tag]={};
} else {
if (!Array.isArray(curLevelObj[tag])) {
curLevelObj[tag]=[curLevelObj[tag]];
}
curLevelObj[tag].push({});
}
stack.push({
tag: tag,
col: col
});
} else {
var value=value.trim();
var curLevelObj=getCurLevelObj();
if (Array.isArray(curLevelObj)) {
curLevelObj=curLevelObj[curLevelObj.length-1];
}
switch(typeof(curLevelObj[tag])) {
case 'undefined':
curLevelObj[tag]=value;
break;
case 'object':
if (Array.isArray(curLevelObj[tag])) {
curLevelObj[tag][curLevelObj[tag].length-1][tag]=value;
console.log('=====',curLevelObj[tag][curLevelObj[tag].length-1]);
return;
}
// no break;
default:
curLevelObj[tag]=[curLevelObj[tag]];
curLevelObj[tag].push(value);
console.log('unhandled',tag,value);
break;
}
}
}
function getCurLevelObj() {
var obj;
stack.forEach(function(level,index) {
if (index==0) obj=result;
else obj=obj[level.tag];
});
return obj;
}