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
| #include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUF_SIZE 256
#define MAX_FILES 1024
#define MAX_FILE_SIZE 65536
#define RSS_SIZE 5
#define DOMAIN "mcksp.com"
struct Conf {
char filename[BUF_SIZE];
char title[BUF_SIZE];
char date[BUF_SIZE];
char article[MAX_FILE_SIZE];
};
void header(FILE* f, char* title, char* date) {
fprintf(f, "\
<!doctype html>\n\
<!--title:%s-->\n\
<!--date:%s-->\n\
<html lang='en'>\n\
<head>\n\
<meta charset='UTF-8'>\n\
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n\
<meta name='author' content='Maciej Spychala' />\n\
<title>%s</title>\n\
<link type='text/css' rel='stylesheet' href='/assets/style.css'>\n\
</head>\n\
<body>\n\
<header>\n\
<a id='title' href='/'>mcksp</a></span>\n\
<div id='navbar'>\n\
<a href='/blog'>blog</a>\n\
<a href='/tech'>tech</a>\n\
<a href='https://git.mcksp.com'>git</a>\n\
<a href='/about'>about</a>\n\
<a href='/rss.xml'>rss</a>\n\
</div>\n\
</header>\n",
title,
date,
title
);
}
void footer(FILE* f) {
fputs("\
<footer>\n\
made with <a href='https://git.mcksp.com/brcl'>brcl</a>\n\
</footer>\n\
</body>\n\
</html>\n",
f);
}
void listItem(FILE* f, struct Conf* conf) {
fprintf(f,
"<div class='item'>"
"<span class='date'>%s</span><a href='/%s'>%s</a>"
"</div>\n",
conf->date, conf->filename, conf->title
);
}
enum HtmlPart{Header, Body, Footer};
void rssHeader(FILE* f) {
fprintf(f,
"<rss version='2.0'>\n\
<channel><title>%s</title><link>https://%s</link><description>%s</description>\n",
DOMAIN, DOMAIN, DOMAIN);
}
void html_encode(FILE *f, const char *s) {
while(*s) {
switch(*s) {
case '<': fputs("<", f); break;
case '>': fputs(">", f); break;
case '\'': fputs("'", f); break;
case '&': fputs("&", f); break;
case '"': fputs(""", f); break;
default: fputc(*s, f); break;
}
s++;
}
}
void rssDate(char* dst, char* str) {
struct tm date = {};
int year, month, day;
if (sscanf(str, "%d/%d/%d", &year, &month, &day) != 3) {
fprintf(stderr, "couldn't parse date %s\n", str);
exit(1);
}
date.tm_year = year - 1900;
date.tm_mon = month - 1;
date.tm_mday = day;
date.tm_hour = 12;
time_t timestamp = mktime(&date);
gmtime_r(×tamp, &date); // get correct weekday without strptime
strftime(dst, BUF_SIZE, "%a, %d %b %Y 12:00:00 +0000", &date);
}
void rssItem(FILE* f, struct Conf* conf) {
char dateBuf[BUF_SIZE];
rssDate(dateBuf, conf->date);
fprintf(f, "\
<item>\n\
<guid>https://%s/%s</guid>\n\
<title>%s</title>\n\
<link>https://%s/%s</link>\n\
<description>",
DOMAIN, conf->filename, conf->title, DOMAIN, conf->filename);
html_encode(f, conf->article);
fprintf(f, "\
</description>\n\
<pubDate>%s</pubDate>\n\
</item>",
dateBuf);
}
void rssFooter(FILE* f) {
fprintf(f, "</channel></rss>\n");
}
void generateRss(struct Conf* rss[]) {
FILE *file = fopen("rss.xml", "w");
if (file == NULL) {
fprintf(stderr, "couldn't open rss.xml");
exit(1);
}
rssHeader(file);
for (int i = 0; i < RSS_SIZE; i++) {
if (rss[i]->title[0] != '\0') {
rssItem(file, rss[i]);
}
}
rssFooter(file);
}
int compare(const void* _a, const void* _b) {
struct Conf* a = *(struct Conf**) _a;
struct Conf* b = *(struct Conf**) _b;
if (!a && !b) { return 0; }
if (!a) { return 1; }
if (!b) { return -1; }
return strcmp(b->date, a->date);
}
void getVar(char* line, struct Conf* conf) {
char key[BUF_SIZE], value[BUF_SIZE];
char* comment = strstr(line, "<!--");
if (!comment) {
return;
}
if (sscanf(comment + 4, "%[^:]:%[^-]", key, value) != 2) {
return;
}
if (strncmp(key, "title", 5) == 0) {
strncpy(conf->title, value, BUF_SIZE);
} else if (strncmp(key, "date", 4) == 0) {
strncpy(conf->date, value, BUF_SIZE);
}
}
void filePath(char* dest, char* dir, char* filename) {
if (strncmp(dir, ".", 1) == 0) {
strncpy(dest, filename, BUF_SIZE);
} else if (dir[strlen(dir) - 1] == '/') {
snprintf(dest, BUF_SIZE, "%s%s", dir, filename);
} else {
snprintf(dest, BUF_SIZE, "%s/%s", dir, filename);
}
}
void saveArticle(struct Conf* conf) {
FILE* file = fopen(conf->filename, "w");
if (file == NULL) {
fprintf(stderr, "couldn't open file %s\n", conf->filename);
exit(1);
}
header(file, conf->title, conf->date);
fputs(conf->article, file);
footer(file);
fclose(file);
}
struct Conf* read_article(char* dir_name, char* filename) {
struct Conf* conf = calloc(1, sizeof(struct Conf));
filePath(conf->filename, dir_name, filename);
FILE *file = fopen(conf->filename, "r");
if (file == NULL) {
fprintf(stderr, "couldn't open file %s\n", conf->filename);
exit(1);
}
char line[BUF_SIZE];
enum HtmlPart part = Header;
while (fgets(line, BUF_SIZE, file)) {
switch (part) {
case Header:
getVar(line, conf);
if (strstr(line, "</header>")) {
part = Body;
}
break;
case Body:
if (strstr(line, "<footer>")) {
part = Footer;
break;
}
strncat(
conf->article,
line,
MAX_FILE_SIZE - strlen(conf->article) - 1
);
break;
case Footer:
break;
}
}
fclose(file);
return conf;
}
void handleDir(char* dir_name, bool indexable, struct Conf** rss) {
DIR *dir;
struct dirent *dirEntry;
dir = opendir(dir_name);
if (dir == NULL) {
fprintf(stderr, "couldn't open directory\n");
exit(1);
}
int file_index = 0;
struct Conf* confs[MAX_FILES] = {};
while ((dirEntry = readdir(dir)) != NULL && file_index < MAX_FILES) {
char *extension = strrchr(dirEntry->d_name, '.');
if (extension == NULL || strcmp(extension, ".html") != 0) {
continue;
}
if (indexable && strcmp(dirEntry->d_name, "index.html") == 0) {
continue;
}
confs[file_index] = read_article(dir_name, dirEntry->d_name);
saveArticle(confs[file_index]);
file_index++;
}
if (indexable) {
char index_path[BUF_SIZE];
filePath(index_path, dir_name, "index.html");
qsort(confs, file_index, sizeof(confs[0]), compare);
FILE* file = fopen(index_path, "w");
if (file == NULL) {
fprintf(stderr, "couldn't open file %s\n", index_path);
exit(1);
}
header(file, dir_name, "");
for (int i = 0; i < file_index; i++) {
listItem(file, confs[i]);
}
footer(file);
for (int i = 0; i < file_index && i < RSS_SIZE; i++) {
*rss[RSS_SIZE + i] = *confs[i];
}
qsort(rss, RSS_SIZE * 2, sizeof(rss[0]), compare);
}
for (int i = 0; i < MAX_FILES; i++) {
if (confs[i] != NULL) {
free(confs[i]);
}
}
closedir(dir);
}
void create_new_article(int argc, char *argv[]) {
char title[BUF_SIZE] = "";
if (argc == 3) {
strncpy(title, argv[2], BUF_SIZE - 1);
} else {
for (int i = 3; i < argc; i++) {
strncat(title, argv[i], BUF_SIZE - strlen(title) - 1);
if (i < argc - 1) {
strncat(title, " ", BUF_SIZE - strlen(title) - 1);
}
}
}
time_t t = time(NULL);
struct tm tm = *localtime(&t);
struct Conf new_file;
char filename[BUF_SIZE];
if (strrchr(argv[2], '.') == NULL) {
snprintf(filename, BUF_SIZE, "%s.html", argv[2]);
} else {
strncpy(filename, argv[2], BUF_SIZE - 1);
}
filePath(new_file.filename, argv[1], filename);
strncpy(new_file.title, title, BUF_SIZE - 1);
snprintf(new_file.date, BUF_SIZE, "%d/%02d/%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
strncpy(new_file.article, "TODO: write something\n", MAX_FILE_SIZE - 1);
saveArticle(&new_file);
printf("created new file: %s\n", new_file.filename);
}
void generate_blog() {
struct Conf* rss[RSS_SIZE * 2] = {};
for (int i = 0; i < RSS_SIZE * 2; i++) {
rss[i] = calloc(1, sizeof(struct Conf));
}
handleDir(".", false, NULL);
handleDir("blog", true, rss);
handleDir("tech", true, rss);
generateRss(rss);
for (int i = 0; i < RSS_SIZE * 2; i++) {
if (rss[i] != NULL) {
free(rss[i]);
}
}
}
int main(int argc, char *argv[]) {
if (argc > 1) {
if (argc == 2) {
printf("usage: brcl <dir> <filename> [title...]\n");
return 0;
}
create_new_article(argc, argv);
}
generate_blog();
return 0;
}
|