-
Notifications
You must be signed in to change notification settings - Fork 2
/
JOSM_preset_gen.js
220 lines (200 loc) · 4.84 KB
/
JOSM_preset_gen.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
JOSM_preset_gen.js
Yet Another Preset Generator for JOSM
for Node.js v6 or later
Author: Maripo GODA
*/
let fs = require('fs');
let builder = require('xmlbuilder');
function read (file, onSuccess, onError) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
onError(err);
} else {
onSuccess(data);
}
});
}
class Group {
constructor (values) {
this.values = values;
}
appendNodeTo (parentNode) {
let groupName = this.values[0]
let groupNameKo = null;
if (arguments[1]) {
groupNameKo = this.values[arguments[1]];
return parentNode.ele("group",{"name":groupName, "ko.name":groupNameKo});
} else {
return parentNode.ele("group",{"name":groupName});
}
}
}
class Preset {
constructor (values) {
this.values = values;
}
appendNodeTo (parentNode, conf) {
let columns = conf.columns;
let presetName = this.values[0];
let presetNameKo = null;
let itemNode;
if (arguments[2]) {
presetNameKo = this.values[arguments[2]];
itemNode = parentNode.ele("item", {"name":presetName, "ko.name":presetNameKo});
} else {
itemNode = parentNode.ele("item", {"name":presetName});
}
if (conf.presetHeader) {
conf.presetHeader(itemNode, this.values);
}
for (let i=0; i<Math.min(this.values.length, columns.length); i++) {
columns[i].append(itemNode, this.values[i]);
}
if (conf.presetFooter) {
conf.presetFooter(itemNode, this.values);
}
return itemNode;
}
}
const PATH_TYPE_ERROR = 0;
const PATH_TYPE_ITEM = 1;
const PATH_TYPE_GROUP = 2;
function parseHierarchy (str) {
let depth = 0;
str = "" + str;
for (let i=0; i<str.length; i++) {
let ch = (str).charAt(i);
if (ch=='-') {
return {type:PATH_TYPE_ITEM, depth:depth};
}
if (ch=='+') {
return {type:PATH_TYPE_GROUP, depth:depth};
}
depth++;
}
return {type:PATH_TYPE_ERROR}
}
function generateXML (content, conf) {
let lines = content.split("\n");
let xml = builder.create("presets");
xml.att("xmlns","http://josm.openstreetmap.de/tagging-preset-1.0");
if (conf.preset_attrs) {
for (let key in conf.preset_attrs) {
xml.att(key, conf.preset_attrs[key]);
}
}
if (conf.add_chunks) {
conf.add_chunks(xml);
}
// Container of all trees
let group;
if (conf.name) {
if (conf.nameKo) {
group = xml.ele("group", {"name":conf.name, "ko.name":conf.nameKo});
} else {
group = xml.ele("group", {"name":conf.name});
}
} else {
group = xml;
}
let breadcrumb = [group];
let depth = 0;
for (let i=1; i<lines.length; i++) {
let values = lines[i].replace("\r","").replace("\n","").split("\t");
let pathStr = values.splice(0, 1);
let path = parseHierarchy(pathStr);
if (path.type==PATH_TYPE_ITEM) {
breadcrumb[path.depth+1] = new Preset(values).appendNodeTo(breadcrumb[path.depth], conf, 11);
}
else {
breadcrumb[path.depth+1] = new Group(values).appendNodeTo(breadcrumb[path.depth], 11);
}
}
return xml;
}
exports.generatePresets = function (conf) {
let inFile = null;
let outFile = null;
process.argv.forEach(function (value, index, array) {
if (index == 2)
inFile = value;
if (index == 3)
outFile = value;
});
if (!inFile || !outFile) {
console.log("Usage: node JOSM_tree_preset_gen.js <in_file> <out_file>");
return;
}
read(inFile, function (content) {
let xml = generateXML(content, conf);
fs.writeFileSync(outFile, xml.end({pretty:true}));
},
function(err){
console.log("Error " + err);
}
);
}
exports.FieldRootAttr = class FieldRootAttr {
constructor (key) {
this.key = key;
}
append (node, value) {
if (value && value.length) {
node.att(this.key, value);
}
}
};
// Simple Key-value pair
exports.FieldText = class FieldText {
constructor (key) {
this.key = key;
}
append (node, value) {
if (value && value.length > 0) {
node.ele("key", {"key":this.key, "value":value});
}
}
}
// Key-value field (defined by dropdown options)
exports.FieldAnnotatedText = class FieldAnnotatedText {
constructor (key) {
this.key = key;
}
append (node, value) {
value = value.replace(/ \(.*\)/, "");
if (value && value.length > 0) {
node.ele("key", {"key":this.key, "value":value});
}
}
}
// Dropdown menu
exports.FieldCombo = class FieldCombo {
constructor (key, label, values) {
this.key = key;
this.label = label;
this.values = values;
if (arguments[3]) {
this.labelKo = arguments[3];
}
}
append (node, value) {
if (value == "yes") {
if (this.labelKo) {
node.ele("combo", {"key":this.key, "text":this.label, "ko.text":this.labelKo, "values":this.values.join(",")});
} else {
node.ele("combo", {"key":this.key, "text":this.label, "values":this.values.join(",")});
}
}
}
}
// TODO
exports.FieldLink = class FieldLink {
constructor () {
}
append (node, value) {
if (value && value.length > 0) {
node.ele("link", {"href":"https://www.wikidata.org/wiki/" + value});
}
}
}