-
Notifications
You must be signed in to change notification settings - Fork 81
/
widget_example.js
232 lines (210 loc) · 7.58 KB
/
widget_example.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
221
222
223
224
225
226
227
228
229
230
231
232
/**
@module plugins/widget_example
@author Tobias Bräutigam
*/
'use strict';
var logger = require('jsdoc/util/logger');
var fs = require('fs');
var path = require('path');
var xsd = require('libxml-xsd');
var libxmljs = require('libxml-xsd').libxmljs;
var ini = require('ini');
var execSync = require('child_process').execSync;
var branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
var config = ini.parse(fs.readFileSync(path.join(".doc", "config.ini"), 'utf-8'));
var manifest = JSON.parse(fs.readFileSync('./package.json'));
var version = (branch == "develop") ? config.DEFAULT['develop-version-mapping'] : manifest.version;
var configParts = {
start : '<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" lib_version="8" design="%%%DESIGN%%%" xsi:noNamespaceSchemaLocation="../visu_config.xsd">',
meta : '<meta/>',
content_start: '<page name="Example">',
content_end: '</page>',
end : '</pages>'
};
var createDir = function(dir) {
try {
fs.statSync(dir);
} catch(e) {
var create = [dir];
var parts = dir.split(path.sep);
parts.pop();
var parentDir = parts.join(path.sep);
var exists = false;
while(!exists && parentDir) {
try {
fs.statSync(parentDir);
exists = true;
} catch(e) {
create.unshift(parentDir);
parts = parentDir.split(path.sep);
parts.pop();
parentDir = parts.join(path.sep);
}
}
create.forEach(function(newDir) {
fs.mkdirSync(newDir, "0744");
});
}
};
var cacheDir = path.join("cache", "widget_examples", "jsdoc");
var screenshotDir = path.join(config.api.target.replace("<version>", version), "examples");
createDir(cacheDir);
var schemaString = fs.readFileSync(path.join("src", "visu_config.xsd"), "utf-8");
var schema = xsd.parse(schemaString);
function getCaptionString(globalCaption, screenshots) {
var res = globalCaption;
for (var i=0; i < screenshots.length; i++) {
var divStyle = "margin-top: 5px; float:left; width: 50%; text-align: center;";
if (i % 2 == 0) {
divStyle += " clear: left";
}
res += '<div style="'+divStyle+'">'+
'<img id="'+screenshots[i].name+'" src="examples/'+screenshots[i].name+'.png"';
if (screenshots[i].caption) {
res += ' alt="'+screenshots[i].caption+'" title="'+screenshots[i].caption+' ">';
res += '<label style="margin-left: 10px; clear: left" for="' + screenshots[i].name + '">' + screenshots[i].caption + '</label>';
} else {
res += ' "/>';
}
res += '</div>';
}
if (res) {
return '<caption>' + res + '</caption>\n';
} else {
return '\n';
}
}
function traverseElements(rootNode, visuConfigParts) {
var res = {
metaNode: null,
configNode: null
};
rootNode.childNodes().forEach(function(child) {
if (child.type() == "element") {
if (child.name() == "meta") {
// this needs to to placed in the configs meta part
res.metaNode = child;
visuConfigParts.meta = child.toString(true);
}
else {
// must be the example code
res.configNode = child;
return;
}
}
});
return res;
}
exports.handlers = {
newDoclet: function(e) {
var tags = e.doclet.tags;
// any user-defined tags in this doclet?
if (typeof tags !== 'undefined') {
// only interested in the @source tags
tags = tags.filter(function ($) {
return $.title === 'widget_example';
});
e.doclet.examples = e.doclet.examples || [];
tags.forEach(function(tag, index) {
var jsdocExample, configExample;
var name = e.doclet.name.replace(/\//g,"_")+index;
var filename = path.join(cacheDir, name+".xml");
var xml = libxmljs.parseXmlString('<?xml version="1.0" encoding="UTF-8"?><root>'+tag.value+"</root>");
var firstChild = xml.root().childNodes()[0];
// clone the configParts
var visuConfigParts = JSON.parse(JSON.stringify(configParts));
var design = "metal";
var settings = {
selector: ".widget_container",
screenshots: [],
screenshotDir: screenshotDir
};
if (firstChild.name() == "settings") {
// read meta settings
var shotIndex = 0;
var globalCaption = null;
if (firstChild.attr("design")) {
design = firstChild.attr("design").value();
}
if (firstChild.attr("selector")) {
settings.selector = firstChild.attr("selector").value();
}
firstChild.childNodes().forEach(function(node) {
if (node.name() == "screenshot") {
var shot = {
name: node.attr("name").value() || name + shotIndex,
data: []};
node.childNodes().forEach(function (node) {
if (node.name() == "caption") {
shot.caption = node.text();
} else if (node.name() == "data") {
shot.data.push({
'address': node.attr("address").value(),
'value': node.text()
});
}
});
settings.screenshots.push(shot);
shotIndex++;
} else if (node.name() == "caption") {
// global caption
globalCaption = node;
}
});
// no screenshots defined, add a default one
if (settings.screenshots.length == 0) {
settings.screenshots.push({
name: name + shotIndex
});
}
} else if (firstChild.name() == "caption") {
globalCaption = firstChild;
firstChild.remove();
settings.screenshots = [{ name: name }];
} else {
settings.screenshots = [{ name: name }];
}
// replace the design value in the config
visuConfigParts.start = visuConfigParts.start.replace("%%%DESIGN%%%", design);
// get the next non-text node
var configNodes = traverseElements(xml.root(), visuConfigParts);
configExample = configNodes.configNode.toString(true);
jsdocExample = configExample;
if (configNodes.metaNode) {
// add meta settings to the example
jsdocExample = "...\n"+visuConfigParts.meta+"\n...\n"+jsdocExample;
}
var globalCaptionString = "";
if (globalCaption) {
// add caption for further example processing
globalCaptionString = globalCaption.text();
} else if (settings.screenshots.length == 1 && settings.screenshots[0].caption) {
// use the caption of the only screenshot we have as global caption for the example
globalCaptionString = settings.screenshots[0].caption;
}
jsdocExample = getCaptionString(globalCaptionString, settings.screenshots) +" {@lang xml} "+ jsdocExample;
e.doclet.examples.push(jsdocExample);
// build the real config source
var configSource = visuConfigParts.start +
visuConfigParts.meta +
visuConfigParts.content_start +
configExample +
visuConfigParts.content_end +
configParts.end;
// validate the configSource snippet against the visu_config.xsd file
var validationErrors = schema.validate(configSource);
if (validationErrors) {
logger.error(validationErrors);
logger.error(configSource);
return
}
// example is a valid configuration
fs.writeFile(filename, JSON.stringify(settings)+"\n"+configSource, function(err) {
if (err) {
logger.error(err);
}
});
});
}
}
};