-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaillight.c
495 lines (462 loc) · 24.1 KB
/
taillight.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "guppy.h"
#define ADD_KEY_VALUE_LITERAL(key_val_pair) \
do {\
if (has_namespace) gup_string_append_cstr_arena(&a, &taillight_rule, " ");\
gup_string_append_cstr_arena(&a, &taillight_rule, " ");\
gup_string_append_cstr_arena(&a, &taillight_rule, key_val_pair);\
if (is_important) gup_string_append_cstr_arena(&a, &taillight_rule, " !important");\
gup_string_append_cstr_arena(&a, &taillight_rule, ";\n");\
} while (0)
#define ADD_KEY_WITH_PARSED_VALUE(key) \
do {\
if (has_namespace) gup_string_append_cstr_arena(&a, &taillight_rule, " ");\
gup_string_append_cstr_arena(&a, &taillight_rule, " ");\
gup_string_append_cstr_arena(&a, &taillight_rule, key);\
gup_string_append_cstr_arena(&a, &taillight_rule, ":");\
for (int j = 0; j < values_and_units.count; j++) {\
gup_string_append_cstr_arena(&a, &taillight_rule, " ");\
gup_string_append_str_arena(&a, &taillight_rule, values_and_units.data[j]);\
}\
if (is_important) gup_string_append_cstr_arena(&a, &taillight_rule, " !important");\
gup_string_append_cstr_arena(&a, &taillight_rule, ";\n");\
} while (0)
// TODO: these global paths kinda bother me, but I put them in so I could do the watch mode.
// Specifically, the watch mode take a function pointer with no arguments, so I can't have
// them as arguments to run like I'd like to.
char *html_file_path;
char *css_file_path;
bool verbose_mode;
void run() {
GupArena a = gup_arena_create();
// Scrape the raw rules from the HTML
if (verbose_mode) {
printf("Parsing %s...\n", html_file_path);
}
GupString html = gup_file_read_arena(&a, html_file_path);
GupArrayString html_rules = gup_array_string_create_arena(&a);
for (int i = 0; i < html.count - 7; i++) {
GupString view = {
.data = &html.data[i],
.count = 7,
.capacity = 7
};
if (gup_string_eq_cstr(view, "class=\"")) {
i += 7;
int j = i;
while (html.data[j] != '"') {
j++;
}
view = (GupString) {
.data = &html.data[i],
.count = j - i,
.capacity = j - i
};
GupArrayString tokens = gup_string_split_arena(&a, view, ' ');
// TODO: would be nice to have a Set
for (int k = 0; k < tokens.count; k++) {
gup_array_string_append_arena(&a, &html_rules, tokens.data[k]);
}
}
}
if (verbose_mode) {
printf("Successfully parsed %s.\n", html_file_path);
}
if (verbose_mode) {
printf("Generating taillight rules...\n");
}
GupArrayString no_namespace_rules = gup_array_string_create_arena(&a);
GupArrayString mobile_namespace_rules = gup_array_string_create_arena(&a);
GupArrayString tablet_namespace_rules = gup_array_string_create_arena(&a);
GupArrayString computer_namespace_rules = gup_array_string_create_arena(&a);
GupArrayString ultrawide_namespace_rules = gup_array_string_create_arena(&a);
GupArrayString unknown_rules = gup_array_string_create_arena(&a);
// Generate the taillight rules from the parsed html rules
for (int i = 0; i < html_rules.count; i++) {
GupString html_rule = html_rules.data[i];
GupString taillight_rule = gup_string_create_arena(&a);
// Generate rule for this html rule
{
GupArrayString tokens = gup_string_split_arena(&a, html_rule, '-');
GupArrayString name_and_namespace_as_tokens = gup_string_split_arena(&a, tokens.data[0], ':');
// A "namespace" is a prefix for specifying the device type (e.g. `m:` for mobile, `uw:` for ultra-wide).
bool has_namespace = name_and_namespace_as_tokens.count == 2;
GupString abbreviated_name = has_namespace
? name_and_namespace_as_tokens.data[1]
: name_and_namespace_as_tokens.data[0];
bool is_important = false;
GupArrayString values_and_units = gup_array_string_create_arena(&a);
for (int j = 1; j < tokens.count; j++) {
GupString token = tokens.data[j];
GupString trimmed_token = gup_string_trim_char_arena(&a, token, '!');
gup_array_string_append_arena(&a, &values_and_units, trimmed_token);
}
GupString taillight_class = gup_string_create_arena(&a);
// TODO: this can probably be DRY'd up
for (int i = 0; i < html_rule.count; i++) {
switch (html_rule.data[i]) {
case ':': {
gup_string_append_cstr_arena(&a, &taillight_class, "\\:");
break;
}
case '!': {
gup_string_append_cstr_arena(&a, &taillight_class, "\\!");
is_important = true;
break;
}
case '%': {
gup_string_append_cstr_arena(&a, &taillight_class, "\\%");
break;
}
case '.': {
gup_string_append_cstr_arena(&a, &taillight_class, "\\.");
break;
}
case '#': {
gup_string_append_cstr_arena(&a, &taillight_class, "\\#");
break;
}
default: {
gup_string_append_arena(&a, &taillight_class, html_rule.data[i]);
break;
}
}
}
// These keywords are used by the media queries and don't need anything to be auto generated for them.
if (
gup_string_eq_cstr(html_rule, "mobile") ||
gup_string_eq_cstr(html_rule, "tablet") ||
gup_string_eq_cstr(html_rule, "computer") ||
gup_string_eq_cstr(html_rule, "ultrawide")
) {
continue;
}
// Add classname
if (has_namespace) gup_string_append_cstr_arena(&a, &taillight_rule, " ");
gup_string_append_arena(&a, &taillight_rule, '.');
gup_string_append_str_arena(&a, &taillight_rule, taillight_class);
gup_string_append_cstr_arena(&a, &taillight_rule, " {\n");
// Add properties
{
if (gup_string_eq_cstr(abbreviated_name, "absolute")) {
ADD_KEY_VALUE_LITERAL("position: absolute");
} else if (gup_string_eq_cstr(abbreviated_name, "alignStart")) {
ADD_KEY_VALUE_LITERAL("align-items: flex-start");
} else if (gup_string_eq_cstr(abbreviated_name, "alignEnd")) {
ADD_KEY_VALUE_LITERAL("align-items: flex-end");
} else if (gup_string_eq_cstr(abbreviated_name, "alignCenter")) {
ADD_KEY_VALUE_LITERAL("align-items: center");
} else if (gup_string_eq_cstr(abbreviated_name, "alignBaseline")) {
ADD_KEY_VALUE_LITERAL("align-items: baseline");
} else if (gup_string_eq_cstr(abbreviated_name, "alignStretch")) {
ADD_KEY_VALUE_LITERAL("align-items: stretch");
} else if (gup_string_eq_cstr(abbreviated_name, "b")) {
ADD_KEY_WITH_PARSED_VALUE("border");
} else if (gup_string_eq_cstr(abbreviated_name, "breakAll")) {
ADD_KEY_VALUE_LITERAL("word-break: break-all");
} else if (gup_string_eq_cstr(abbreviated_name, "breakWord")) {
ADD_KEY_VALUE_LITERAL("word-wrap: break-word");
} else if (gup_string_eq_cstr(abbreviated_name, "basis")) {
ADD_KEY_WITH_PARSED_VALUE("flex-basis");
} else if (gup_string_eq_cstr(abbreviated_name, "bb")) {
ADD_KEY_WITH_PARSED_VALUE("border-bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "bg")) {
if (gup_string_eq_cstr(values_and_units.data[0], "default")) {
ADD_KEY_VALUE_LITERAL("background-color: var(--background-color)");
} else {
ADD_KEY_WITH_PARSED_VALUE("background-color");
}
} else if (gup_string_eq_cstr(abbreviated_name, "bl")) {
ADD_KEY_WITH_PARSED_VALUE("border-left");
} else if (gup_string_eq_cstr(abbreviated_name, "br")) {
ADD_KEY_WITH_PARSED_VALUE("border-right");
} else if (gup_string_eq_cstr(abbreviated_name, "brad")) {
ADD_KEY_WITH_PARSED_VALUE("border-radius");
} else if (gup_string_eq_cstr(abbreviated_name, "bt")) {
ADD_KEY_WITH_PARSED_VALUE("border-top");
} else if (gup_string_eq_cstr(abbreviated_name, "bottom")) {
ADD_KEY_WITH_PARSED_VALUE("bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "bx")) {
ADD_KEY_WITH_PARSED_VALUE("border-left");
ADD_KEY_WITH_PARSED_VALUE("border-right");
} else if (gup_string_eq_cstr(abbreviated_name, "by")) {
ADD_KEY_WITH_PARSED_VALUE("border-top");
ADD_KEY_WITH_PARSED_VALUE("border-bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "c")) {
ADD_KEY_WITH_PARSED_VALUE("color");
} else if (gup_string_eq_cstr(abbreviated_name, "column")) {
ADD_KEY_VALUE_LITERAL("flex-direction: column");
} else if (gup_string_eq_cstr(abbreviated_name, "dot")) {
ADD_KEY_VALUE_LITERAL("list-style-type: disc");
ADD_KEY_VALUE_LITERAL("margin-left: 1.5rem");
} else if (gup_string_eq_cstr(abbreviated_name, "fixed")) {
ADD_KEY_VALUE_LITERAL("position: fixed");
} else if (gup_string_eq_cstr(abbreviated_name, "flex")) {
ADD_KEY_VALUE_LITERAL("display: flex");
} else if (gup_string_eq_cstr(abbreviated_name, "fs")) {
ADD_KEY_WITH_PARSED_VALUE("font-size");
} else if (gup_string_eq_cstr(abbreviated_name, "gap")) {
ADD_KEY_WITH_PARSED_VALUE("gap");
} else if (gup_string_eq_cstr(abbreviated_name, "grid")) {
ADD_KEY_VALUE_LITERAL("display: grid");
} else if (gup_string_eq_cstr(abbreviated_name, "gridCol")) {
if (has_namespace) gup_string_append_cstr_arena(&a, &taillight_rule, " ");
gup_string_append_cstr_arena(&a, &taillight_rule, " ");
gup_string_append_cstr_arena(&a, &taillight_rule, "grid-template-columns: ");
if (gup_string_eq_cstr(values_and_units.data[0], "1")) {
gup_string_append_cstr_arena(&a, &taillight_rule, "minmax(0, 1fr)");
} else {
gup_string_append_cstr_arena(&a, &taillight_rule, "repeat(");
gup_string_append_str_arena(&a, &taillight_rule, values_and_units.data[0]);
gup_string_append_cstr_arena(&a, &taillight_rule,", 1fr)");
}
if (is_important) gup_string_append_cstr_arena(&a, &taillight_rule, " !important");
gup_string_append_cstr_arena(&a, &taillight_rule, ";\n");
} else if (gup_string_eq_cstr(abbreviated_name, "grow")) {
ADD_KEY_WITH_PARSED_VALUE("flex-grow");
} else if (gup_string_eq_cstr(abbreviated_name, "h")) {
ADD_KEY_WITH_PARSED_VALUE("height");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyStart")) {
ADD_KEY_VALUE_LITERAL("justify-content: flex-start");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyEnd")) {
ADD_KEY_VALUE_LITERAL("justify-content: flex-end");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyCenter")) {
ADD_KEY_VALUE_LITERAL("justify-content: center");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyBetween")) {
ADD_KEY_VALUE_LITERAL("justify-content: space-between");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyAround")) {
ADD_KEY_VALUE_LITERAL("justify-content: space-around");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyEvenly")) {
ADD_KEY_VALUE_LITERAL("justify-content: space-evenly");
} else if (gup_string_eq_cstr(abbreviated_name, "justifyStretch")) {
ADD_KEY_VALUE_LITERAL("justify-content: stretch");
} else if (gup_string_eq_cstr(abbreviated_name, "left")) {
ADD_KEY_WITH_PARSED_VALUE("left");
} else if (gup_string_eq_cstr(abbreviated_name, "lightShadow")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 0 16px 0 rgba(255, 255, 255, 0.2)");
} else if (gup_string_eq_cstr(abbreviated_name, "lightShadowSmall")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 0 8px 0 rgba(255, 255, 255, 0.2)");
} else if (gup_string_eq_cstr(abbreviated_name, "m")) {
ADD_KEY_WITH_PARSED_VALUE("margin");
} else if (gup_string_eq_cstr(abbreviated_name, "maxh")) {
ADD_KEY_WITH_PARSED_VALUE("max-height");
} else if (gup_string_eq_cstr(abbreviated_name, "maxw")) {
ADD_KEY_WITH_PARSED_VALUE("max-width");
} else if (gup_string_eq_cstr(abbreviated_name, "mb")) {
ADD_KEY_WITH_PARSED_VALUE("margin-bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "minh")) {
ADD_KEY_WITH_PARSED_VALUE("min-height");
} else if (gup_string_eq_cstr(abbreviated_name, "minw")) {
ADD_KEY_WITH_PARSED_VALUE("min-width");
} else if (gup_string_eq_cstr(abbreviated_name, "ml")) {
ADD_KEY_WITH_PARSED_VALUE("margin-left");
} else if (gup_string_eq_cstr(abbreviated_name, "mr")) {
ADD_KEY_WITH_PARSED_VALUE("margin-right");
} else if (gup_string_eq_cstr(abbreviated_name, "mt")) {
ADD_KEY_WITH_PARSED_VALUE("margin-top");
} else if (gup_string_eq_cstr(abbreviated_name, "mx")) {
ADD_KEY_WITH_PARSED_VALUE("margin-left");
ADD_KEY_WITH_PARSED_VALUE("margin-right");
} else if (gup_string_eq_cstr(abbreviated_name, "my")) {
ADD_KEY_WITH_PARSED_VALUE("margin-top");
ADD_KEY_WITH_PARSED_VALUE("margin-bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "p")) {
ADD_KEY_WITH_PARSED_VALUE("padding");
} else if (gup_string_eq_cstr(abbreviated_name, "pb")) {
ADD_KEY_WITH_PARSED_VALUE("padding-bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "pl")) {
ADD_KEY_WITH_PARSED_VALUE("padding-left");
} else if (gup_string_eq_cstr(abbreviated_name, "pr")) {
ADD_KEY_WITH_PARSED_VALUE("padding-right");
} else if (gup_string_eq_cstr(abbreviated_name, "pt")) {
ADD_KEY_WITH_PARSED_VALUE("padding-top");
} else if (gup_string_eq_cstr(abbreviated_name, "px")) {
ADD_KEY_WITH_PARSED_VALUE("padding-left");
ADD_KEY_WITH_PARSED_VALUE("padding-right");
} else if (gup_string_eq_cstr(abbreviated_name, "py")) {
ADD_KEY_WITH_PARSED_VALUE("padding-top");
ADD_KEY_WITH_PARSED_VALUE("padding-bottom");
} else if (gup_string_eq_cstr(abbreviated_name, "right")) {
ADD_KEY_WITH_PARSED_VALUE("right");
} else if (gup_string_eq_cstr(abbreviated_name, "relative")) {
ADD_KEY_VALUE_LITERAL("position: relative");
} else if (gup_string_eq_cstr(abbreviated_name, "rounded")) {
ADD_KEY_VALUE_LITERAL("border-radius: 8px;");
} else if (gup_string_eq_cstr(abbreviated_name, "row")) {
ADD_KEY_VALUE_LITERAL("flex-direction: row");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowSm")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 1px 2px 0 rgb(0, 0, 0, 0.05)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadow")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 1px 3px 0 rgb(0, 0, 0, 0.1), 0 1px 2px -1px rgb(0, 0, 0, 0.1)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowMd")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 4px 6px -1px rgb(0, 0, 0, 0.1), 0 2px 4px -2px rgb(0, 0, 0, 0.1)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowLg")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 10px 15px -3px rgb(0, 0, 0, 0.1), 0 4px 6px -4px rgb(0, 0, 0, 0.1)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowXl")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 20px 25px -5px rgb(0, 0, 0, 0.1), 0 8px 10px -6px rgb(0, 0, 0, 0.1)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowXxl")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 25px 50px -12px rgb(0, 0, 0, 0.25)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowInner")) {
ADD_KEY_VALUE_LITERAL("box-shadow: inset 0 2px 4px 0 rgb(0, 0, 0, 0.05)");
} else if (gup_string_eq_cstr(abbreviated_name, "shadowNone")) {
ADD_KEY_VALUE_LITERAL("box-shadow: 0 0 #0000");
} else if (gup_string_eq_cstr(abbreviated_name, "shrink")) {
ADD_KEY_WITH_PARSED_VALUE("flex-shrink");
} else if (gup_string_eq_cstr(abbreviated_name, "sticky")) {
ADD_KEY_VALUE_LITERAL("position: sticky");
ADD_KEY_VALUE_LITERAL("top: 0");
ADD_KEY_VALUE_LITERAL("box-sizing: border-box");
} else if (gup_string_eq_cstr(abbreviated_name, "top")) {
ADD_KEY_WITH_PARSED_VALUE("top");
} else if (gup_string_eq_cstr(abbreviated_name, "underlineDots")) {
ADD_KEY_VALUE_LITERAL("text-decoration: underline dotted");
} else if (gup_string_eq_cstr(abbreviated_name, "w")) {
ADD_KEY_WITH_PARSED_VALUE("width");
} else if (gup_string_eq_cstr(abbreviated_name, "wrap")) {
ADD_KEY_VALUE_LITERAL("word-wrap: break-word");
} else if (gup_string_eq_cstr(abbreviated_name, "z")) {
ADD_KEY_WITH_PARSED_VALUE("z-index");
} else { // Parsed an unsupported token
// TODO: Set
if (!gup_array_string_contains(unknown_rules, html_rule)) {
gup_array_string_append_arena(&a, &unknown_rules, html_rule);
}
// If this rule is not supported by taillight, then we don't actually want to add it to the
continue;
}
}
if (has_namespace) gup_string_append_cstr_arena(&a, &taillight_rule, " ");
gup_string_append_cstr_arena(&a, &taillight_rule, "}");
}
// TODO: using Sets would be better instead of manually checking whether they're already contained, I'd guess
if (gup_string_starts_with_cstr(html_rule, "m:")) {
if (!gup_array_string_contains(mobile_namespace_rules, taillight_rule)) {
gup_array_string_append_arena(&a, &mobile_namespace_rules, taillight_rule);
}
} else if (gup_string_starts_with_cstr(html_rule, "t:")) {
if (!gup_array_string_contains(tablet_namespace_rules, taillight_rule)) {
gup_array_string_append_arena(&a, &tablet_namespace_rules, taillight_rule);
}
} else if (gup_string_starts_with_cstr(html_rule, "c:")) {
if (!gup_array_string_contains(computer_namespace_rules, taillight_rule)) {
gup_array_string_append_arena(&a, &computer_namespace_rules, taillight_rule);
}
} else if (gup_string_starts_with_cstr(html_rule, "uw:")) {
if (!gup_array_string_contains(ultrawide_namespace_rules, taillight_rule)) {
gup_array_string_append_arena(&a, &ultrawide_namespace_rules, taillight_rule);
}
} else if (!gup_array_string_contains(no_namespace_rules, taillight_rule)) {
gup_array_string_append_arena(&a, &no_namespace_rules, taillight_rule);
}
}
if (unknown_rules.count > 0 && verbose_mode) {
printf("WARNING: Taillight found the following class names but didn't recognize them. These are probably just custom classes you have, but you might want to check out this list just in case:\n");
gup_array_string_print(unknown_rules);
}
if (verbose_mode) {
printf("Successfully generated taillight rules.\n");
}
// Write the lines to the final css file
if (verbose_mode) {
printf("Writing taillight rules to %s...\n", css_file_path);
}
// NOTE: Writing this first line is actually a sort of hack to make sure we can append everything afterwards
// but still be overwriting any old file that might be there.
gup_file_write_cstr("/**\n * Stylesheet generated by taillight https://github.com/cbouwense/taillight.\n", css_file_path);
gup_file_append_line_cstr(" * You can edit this file, but I would not recommend it since it will be overwritten without warning the next time you run taillight.", css_file_path);
gup_file_append_line_cstr(" * If you want to add your own rules, I recommend you add them to a separate stylesheet (I use a 'custom.css' file for this).\n */", css_file_path);
gup_file_append_lines_arena(&a, no_namespace_rules, css_file_path);
gup_file_append_line_cstr("\n/* Mobile */", css_file_path);
gup_file_append_line_cstr("@media (max-width: 768px) {", css_file_path);
gup_file_append_line_cstr(" .mobile { display: inherit; }", css_file_path);
gup_file_append_line_cstr(" .tablet { display: none; }", css_file_path);
gup_file_append_line_cstr(" .computer { display: none; }", css_file_path);
gup_file_append_line_cstr(" .ultrawide { display: none; }", css_file_path);
gup_file_append_line_cstr(" :root { font-size: 14px; }", css_file_path);
gup_file_append_lines_arena(&a, mobile_namespace_rules, css_file_path);
gup_file_append_line_cstr("}", css_file_path);
gup_file_append_line_cstr("\n/* Tablet */", css_file_path);
gup_file_append_line_cstr("@media (min-width: 768px) and (max-width: 1024px) {", css_file_path);
gup_file_append_line_cstr(" .mobile { display: none; }", css_file_path);
gup_file_append_line_cstr(" .tablet { display: inherit; }", css_file_path);
gup_file_append_line_cstr(" .computer { display: none; }", css_file_path);
gup_file_append_line_cstr(" .ultrawide { display: none; }", css_file_path);
gup_file_append_line_cstr(" :root { font-size: 14px; }", css_file_path);
gup_file_append_lines_arena(&a, tablet_namespace_rules, css_file_path);
gup_file_append_line_cstr("}", css_file_path);
gup_file_append_line_cstr("\n/* Computer (desktop / laptop) */", css_file_path);
gup_file_append_line_cstr("@media (min-width: 1024px) and (max-width: 3840px) {", css_file_path);
gup_file_append_line_cstr(" .mobile { display: none; }", css_file_path);
gup_file_append_line_cstr(" .tablet { display: none; }", css_file_path);
gup_file_append_line_cstr(" .computer { display: inherit; }", css_file_path);
gup_file_append_line_cstr(" .ultrawide { display: none; }", css_file_path);
gup_file_append_line_cstr(" :root { font-size: 18px; }", css_file_path);
gup_file_append_lines_arena(&a, computer_namespace_rules, css_file_path);
gup_file_append_line_cstr("}", css_file_path);
gup_file_append_line_cstr("\n/* Ultrawide */", css_file_path);
gup_file_append_line_cstr("@media (min-width: 3840px) {", css_file_path);
gup_file_append_line_cstr(" .mobile { display: none; }", css_file_path);
gup_file_append_line_cstr(" .tablet { display: none; }", css_file_path);
gup_file_append_line_cstr(" .computer { display: none; }", css_file_path);
gup_file_append_line_cstr(" .ultrawide { display: inherit; }", css_file_path);
gup_file_append_line_cstr(" :root { font-size: 18px; }", css_file_path);
gup_file_append_lines_arena(&a, ultrawide_namespace_rules, css_file_path);
gup_file_append_line_cstr("}", css_file_path);
if (verbose_mode) {
printf("Successfully wrote taillight rules to %s.\n", css_file_path);
printf("All done, hope it looks good!\n");
}
gup_arena_destroy(&a);
}
int main(int argc, char **argv) {
bool watch_mode = false;
// Process arguments
{
for (int i = 1; i < argc; i++) {
GupString arg_view = (GupString) {
.data = argv[i],
.count = gup_cstr_length(argv[i]),
.capacity = gup_cstr_length(argv[i])
};
if (gup_string_ends_with_cstr(arg_view, ".html")) {
html_file_path = argv[i];
}
else if (gup_string_ends_with_cstr(arg_view, ".css")) {
css_file_path = argv[i];
}
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printf("Usage: %s [optional input.html] [optional output.css] [flags]\n", argv[0]);
printf("Flags:\n");
printf(" -h, --help Display this help message\n");
printf(" -v, --verbose Enable verbose output\n");
printf(" -w, --watch Enable watch mode, so taillight runs on every input html modification\n");
return 0;
}
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
verbose_mode = true;
}
else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--watch") == 0) {
watch_mode = true;
}
}
if (html_file_path == NULL || strcmp(html_file_path, "") == 0) {
html_file_path = "index.html";
}
if (css_file_path == NULL || strcmp(css_file_path, "") == 0) {
css_file_path = "taillight.css";
}
}
// Check to see if html file even exists
if (!gup_file_exists(html_file_path)) {
if (verbose_mode) {
printf("ERROR: unable to find input html file at \"%s\". Are you sure the pathing is correct?", html_file_path);
gup_print_cwd();
}
exit(1);
}
if (watch_mode) {
gup_file_watch(html_file_path, run);
} else {
run();
}
return 0;
}