-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateHTML.js
197 lines (155 loc) · 5.53 KB
/
generateHTML.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
var fs = require('fs'),
parser = require('xml2json'),
_ = require('underscore'),
argv = require('minimist')(process.argv.slice(2)),
createElement = require('create-element');
var fileName = argv.file.split('/').pop().slice(0,-5);
fs.readFile(argv.file, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var json = parser.toJson(data); //returns a string containing the JSON structure by default
var jsobject = JSON.parse(json);
var html = "";
html += '<!DOCTYPE html><html><head>' +
'<title>' + fileName + '</title>' +
'<link rel="stylesheet" type="text/css" href="style.css">' +
'</head><body>';
var groupTest = function (value, groupTopPos, groupLeftPos) {
if (value.controlTypeID !== "__group__") {
return;
};
_.each(value.groupChildrenDescriptors.control, function (value, key, list) {
console.log(" " + value.controlTypeID);
if (value.controlTypeID === 'com.balsamiq.mockups::Title') {
html += createElement(
'h1', {
style: [
'top: ' + (groupTopPos + value.y) + 'px;',
'left: ' + (groupLeftPos + value.x) + 'px;',
'z-index: ' + value.zOrder + ';'
],
class: 'title'
},
decodeURI(value.controlProperties.text)
);
} else {
console.warn(
'WARN Group Item: ' +
'ID: ' + value.controlID + ', ' +
'Type: ' + value.controlTypeID + ', '
);
};
groupTest(value, groupTopPos, groupLeftPos);
});
};
/*
* Balsamiq Mockups places all controls in an absolute position.
* Any difference between measuredHeight/Width is whitespace at the top left of the view.
*/
var viewLeftPos = jsobject.mockup.measuredW - jsobject.mockup.mockupW - 1;
var viewTopPos = jsobject.mockup.measuredH - jsobject.mockup.mockupH - 1;
/* Now we iterate through the full list of controls */
_.each(jsobject.mockup.controls.control, function(value, key, list) {
var symbol = "";
var topPos;
var leftPos;
if (value.controlProperties) {
if (value.controlProperties.src) {
symbol = value.controlProperties.src;
};
};
topPos = value.y - viewTopPos;
leftPos = value.x - viewLeftPos;
if (value.controlTypeID === 'com.balsamiq.mockups::Button') {
html += createElement(
'button', {
style: [
'top: ' + topPos + 'px;',
'left: ' + leftPos + 'px;',
'z-index: ' + value.zOrder + ';'
],
class: 'button'
},
decodeURI(value.controlProperties.text)
);
} else if (value.controlTypeID === 'com.balsamiq.mockups::Title') {
html += createElement(
'h1', {
style: [
'top: ' + topPos + 'px;',
'left: ' + leftPos + 'px;',
'z-index: ' + value.zOrder + ';'
],
class: 'title'
},
decodeURI(value.controlProperties.text)
);
} else if (value.controlTypeID === 'com.balsamiq.mockups::Icon') {
html += createElement(
'div', {
style: [
'top: ' + topPos + 'px;',
'left: ' + leftPos + 'px;',
'z-index: ' + value.zOrder + ';',
'height: ' + value.measuredH + 'px;',
'width: ' + value.measuredW + 'px;'
],
class: 'icon'
},
decodeURI(value.controlProperties.icon)
);
} else if (value.controlTypeID === 'com.balsamiq.mockups::DataGrid') {
var dataGridContent = decodeURI(value.controlProperties.text).split('\n');
for (var i = dataGridContent.length - 1; i >= 0; i--) {
dataGridContent[i] = dataGridContent[i].replace(/%3A/g, ':');
dataGridContent[i] = dataGridContent[i].split('%2C');
};
html += '<table style="' +
'top: ' + topPos + 'px;' +
'left: ' + leftPos + 'px;' +
'z-index: ' + value.zOrder + ';' +
'line-height: ' + value.controlProperties.rowHeight + 'px;' +
'" class="table"><tbody>';
_.each(dataGridContent, function(dataGridContentRow, index) {
html += '<tr>';
_.each(dataGridContentRow, function(dataGridContentColumn) {
if (dataGridContentColumn === '[]') {
return html += '<td><input type="checkbox"></td>';
};
html += '<td>' + dataGridContentColumn + '</td>';
});
html += '</tr>';
});
html += '</tbody></table>';
// console.log(dataGridContent);
} else {
// Unknown element type
console.warn(
'WARN: ' +
'ID: ' + value.controlID + ', ' +
'Type: ' + value.controlTypeID + ', ' +
'Symbol: ' + symbol
);
html += createElement(
'div', {
style: [
'top: ' + topPos + 'px;',
'left: ' + leftPos + 'px;',
'height: ' + value.h + 'px; ',
'width: ' + value.w + 'px; ',
'z-index: ' + value.zOrder + ';'
],
class: 'unknown'
},
value.controlTypeID
);
};
groupTest(value, topPos, leftPos);
});
html += '</body></html>';
// Write json as output if required:
// var prettyjson = JSON.stringify(jsobject, null, 2);
// fs.writeFile('output/json/' + argv.file + '.json', prettyjson);
fs.writeFile('output/html/' + fileName + '.html', html);
});