-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcassets-parser.js
303 lines (266 loc) · 8.77 KB
/
xcassets-parser.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Parses the Asset.xcassets asset folder in same directory
* Generates a html file with the colors and images displayed
* Also some helping variables for Sass
*/
var globalflattxt = "";
var globallistindex = "";
var globalflatSassNormal = "";
var globalflatSass = "";
var header = `
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'>
<title>Assets xcassets parser</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" rel="stylesheet">
<style type='text/css'>
body {
margin: 0;
padding: 0;
font-size: 12pt;
font-family: Arial, sans-serif;
}
.header-logo {
font-size: 18pt;
}
.header {
padding: 1vh 1vw;
}
.conf-container {
padding: 1vh 1vw;
background: #e8e8e8;
}
.json-pre {
white-space: pre-wrap;
background: #f1f1f1;
}
.colorbox--white {
display: inline-block;
padding: 5px;
background: white;
border: 1px solid grey;
border-radius: 2px;
}
.colorbox--black {
display: inline-block;
padding: 5px;
background: black;
border: 1px solid grey;
border-radius: 2px;
}
.colorblock {
display: inline-block;
padding: 5px;
}
img {
max-width: 14vw;
}
</style>
`;
var prebody = `
<header class="header">
<div class="header-logo">
Assets
</div>
</header>
`;
//===================================================
// Assets
function allKeys(object) {
return Object.keys(object).reduce((keys, key) =>
keys.concat(key,
typeof object[key] === 'object' ? allKeys(object[key]) : []
),
[]
);
}
function hasP(obj, param)
{
if(param)
{
if(obj.hasOwnProperty(param))
{
return obj[param];
}
else
{
return false;
}
}
else
{
return false;
}
}
function isHex(h)
{
var a = parseInt(h,16);
if(h == 'undefined')
{
return false;
}
return (a.toString(16) === h.toLowerCase())
}
function colorConv(rawColor)
{
if(isHex(rawColor))
{
rawColor = parseInt(rawColor, 16);
return Math.round(rawColor / 255, 3);
}
else
{
if(rawColor == "0.000")
{
return 0;
}
return Math.round((255) * rawColor, 3);
}
}
var parseContentsJson = function(content, dirName)
{
var valuName = dirName.split("/");
let mash = "";
try {
if(content)
{
if(hasP(content, "colors"))
{
content.colors.forEach(function(item)
{
var rawAppearenceLast = "normal";
if(appearances = hasP(item, "appearances"))
{
if(appearances.length != 0)
{
appearances.forEach(function(ap) {
mash += `Appearances: ` + ap.appearance + `, ` + ap.value + `<br/>`;
rawAppearenceLast = ap.value;
});
}
}
if(color = hasP(item, "color"))
{
if(hasP(color.components, "white"))
{
let tmp = `rgba(`
+ colorConv(color.components.white) + `,`
+ colorConv(color.components.white) + `,`
+ colorConv(color.components.white) + `,`
+ color.components.alpha + `)`;
mash += `<span class='colorbox--black'><span class='colorblock' style='background-color: ` + tmp + `'>` + item.idiom + ` ` + tmp + `</span></span>`;
mash += `<span class='colorbox--white'><span class='colorblock' style='background-color: ` + tmp + `'>` + item.idiom + ` ` + tmp + `</span></span><br/>`;
if(rawAppearenceLast == "normal")
{
globalflatSassNormal += "$" + valuName[2].replace('.', '').replace('colorset', '') + "" + ": " + tmp + ";\n";
}
else {
globalflatSass += "$" + valuName[2].replace('.', '').replace('colorset', '') + "" + ": " + tmp + ";\n";
}
}
else
{
let tmp = `rgba(`
+ colorConv(color.components.red) + `,`
+ colorConv(color.components.green) + `,`
+ colorConv(color.components.blue) + `,`
+ color.components.alpha + `)`;
mash += `<span class='colorbox--black'><span class='colorblock' style='background-color: ` + tmp + `'>` + item.idiom + ` ` + tmp + `</span></span>`;
mash += `<span class='colorbox--white'><span class='colorblock' style='background-color: ` + tmp + `'>` + item.idiom + ` ` + tmp + `</span></span><br/>`;
if(rawAppearenceLast == "normal")
{
globalflatSassNormal += "$" + valuName[2].replace('.', '').replace('colorset', '') + "" + ": " + tmp + ";\n";
}
else {
globalflatSass += "$" + valuName[2].replace('.', '').replace('colorset', '') + "" + ": " + tmp + ";\n";
}
}
}
console.log(mash);
})
}
}
} catch(err) {
console.log("Error parsing:", err);
}
return mash;
}
// List all files
var walkSync = function(dir, filelist)
{
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file)
{
if (fs.statSync(dir + '/' + file).isDirectory())
{
globalflattxt += '\t\t<li class="list--headline"><a id="tag_' + file + '"></a><h3>' + file.replace('-', ' ') + '</h3></li>\n';
globallistindex += '\t\t<li class="list--hline"><a href="#tag_' + file + '"><span>' + file.replace('-', ' ') + '</span></a></li>\n';
filelist.push(walkSync(dir + '/' + file, []));
}
else if (file.indexOf(".json") > -1)
{
filelist.push(dir+'/'+file);
let filePath = dir+'/'+file;
let fileContent = {};
try
{
let rawdata = fs.readFileSync(filePath);
fileContent = JSON.parse(rawdata);
fileContentParsed = parseContentsJson(fileContent, dir);
}
catch(err)
{
console.log("error parse: ", err);
}
globalflattxt += `\t\t<li><span class="list-item-filename">` + file + `</span><br/>
` + fileContentParsed + `
<pre class='json-pre'>` + JSON.stringify(fileContent) + `</pre></li>\n`;
}
else if (file == ".DS_Store")
{
}
else
{
filelist.push(dir+'/'+file);
globalflattxt += '\t\t<li><img src="'+dir+'/'+file+'" > <br/><span class="list-item-filename">'+file + '</span></li>\n';
}
});
return filelist;
};
function buildHtml() {
var body = '';
var list = walkSync('./Assets.xcassets', []);
body += `
<div class='conf-container'>
</div>
<div class='conf-container'>
`
body += "\t<ul class='list-index'>\n";
body += globallistindex;
body += "\t</ul>\n";
body += "\t<ul class='list-assets'>\n";
body += globalflattxt;
body += "\t</ul>\n";
body += "\t<hr/>\n<h3>// Normal</h3><br/><pre>\n";
body += globalflatSassNormal;
body += "\t</pre>\n";
body += "\t<hr/>\n<h3>// Dark</h3><br/><pre>\n";
body += globalflatSass;
body += "\t</pre>\n";
body += `
</div>
<div class='conf-container'>
</div>
`
return '<!DOCTYPE html>'
+ '<html><head>' + header + '</head><body>' + prebody + body + '</body></html>';
};
var fs = require('fs');
var fileName = './index.html';
var stream = fs.createWriteStream(fileName);
stream.once('open', function(fd) {
var html = buildHtml();
stream.end(html);
});