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
|
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ENTRY_DIR "entries"
#define MAX_ENTRIES 1024
static char *entries[MAX_ENTRIES];
static int num_entries;
static void load_entries()
{
DIR *d = opendir(ENTRY_DIR);
if (!d) exit(1);
struct dirent *de;
while ((de = readdir(d))) {
if (de->d_name[0] == '.') continue;
if (num_entries == MAX_ENTRIES) exit(1);
size_t entry_size = strlen(de->d_name) + 1;
char *entry = malloc(entry_size);
memcpy(entry, de->d_name, entry_size);
entries[num_entries++] = entry;
}
closedir(d);
}
static void print_entry(char *entry)
{
size_t len = strlen(entry) + 1;
char *path = malloc(sizeof(ENTRY_DIR) + len);
memcpy(path, ENTRY_DIR"/", sizeof(ENTRY_DIR));
memcpy(path + sizeof(ENTRY_DIR), entry, len);
FILE *f = fopen(path, "r");
if (!f) return;
char date[11];
long time = atol(entry);
strftime(date, sizeof(date), "%Y/%m/%d", localtime(&time));
printf("<div class='entry'><p><span class='date'>%s</span> ", date);
for (int c = getc(f), nl = 0; c != EOF; c = getc(f)) {
if (c == '\n') {
++nl;
continue;
}
if (nl > 0) {
printf(nl == 1 ? "<br>" : "<p></p>");
nl = 0;
}
putchar(c);
}
fclose(f);
printf("</p></div>\n");
}
static int entry_cmp(const void *a, const void *b)
{
return atoi(*(const char **)b) - atoi(*(const char **)a);
}
int main()
{
load_entries();
qsort(entries, num_entries, sizeof(char *), entry_cmp);
printf(
"<!DOCTYPE html>\n"
"<html lang='en'>\n"
"<head>\n"
"<meta charset='utf-8'>\n"
"<meta name='viewport' content='width=device-width, initial-scale=1'>\n"
"<link href='https://fonts.googleapis.com/css?family=Source+Serif+Pro:400,700' rel='stylesheet'>\n"
"<link href='style.css' rel='stylesheet'>\n"
"<title>david's journal</title>\n"
"</head>\n"
"<body>\n"
"<h1>david's journal</h1>\n");
for (int i = 0; i < num_entries; ++i) {
print_entry(entries[i]);
free(entries[i]);
}
printf(
"</body>\n"
"</html>\n");
return 0;
}
|