summaryrefslogtreecommitdiff
path: root/src/www_blog.c
blob: c2c55a90d60a73b39e8f8b82ac89083a15fb5a62 (plain)
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
#if defined(ENABLE_WWW)

#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "www_tree.h"
#include "bbs.h"

extern struct bbs_config conf;

char *www_blog() {
	struct ptr_vector entries = blog_load();
	struct www_tag *page;
	struct www_tag *cur_tag;
	struct www_tag *child_tag;
	struct www_tag *child_child_tag;
	struct www_tag *child_child_child_tag;

	page = www_tag_new(NULL, "");
	cur_tag = www_tag_new("div", NULL);
	www_tag_add_attrib(cur_tag, "class", "content-header");
	child_tag = www_tag_new("h2", NULL);
	www_tag_add_child(cur_tag, child_tag);
	www_tag_add_child(child_tag, www_tag_new(NULL, "System Blog"));
	www_tag_add_child(page, cur_tag);

	if (ptr_vector_len(&entries) == 0) {
		cur_tag = www_tag_new("p", NULL);
		www_tag_add_child(cur_tag, www_tag_new(NULL, "No Entries"));
		www_tag_add_child(page, cur_tag);


		return www_tag_unwravel(page);
	}
	for (size_t i = 0; i < ptr_vector_len(&entries); i++) {
		struct blog_entry_t *entry = ptr_vector_get(&entries, i);
		struct tm entry_time;
		int hour;
		char timebuf[16];
		char datebuf[24];

		localtime_r(&entry->date, &entry_time);
		hour = entry_time.tm_hour;
		strftime(timebuf, sizeof timebuf, "%l:%M", &entry_time);
		strftime(datebuf, sizeof datebuf, " %a, %e %b %Y", &entry_time);

		cur_tag = www_tag_new("div", NULL);
		www_tag_add_attrib(cur_tag, "class", "blog-header");
		www_tag_add_child(page, cur_tag);

		child_tag = www_tag_new("div", NULL);
		www_tag_add_attrib(child_tag, "class", "blog-title");
		www_tag_add_child(cur_tag, child_tag);

		child_child_tag = www_tag_new("h3", NULL);
		www_tag_add_child(child_tag, child_child_tag);

		child_child_child_tag = www_tag_new(NULL, entry->subject);
		www_tag_add_child(child_child_tag, child_child_child_tag);

		child_tag = www_tag_new("div", NULL);
		www_tag_add_attrib(child_tag, "class", "blog-date");
		www_tag_add_child(cur_tag, child_tag);

		child_child_tag = www_tag_new(NULL, timebuf);
		www_tag_add_child(child_tag, child_child_tag);

		child_child_tag = www_tag_new(NULL, hour >= 12 ? "pm" : "am");
		www_tag_add_child(child_tag, child_child_tag);

		child_child_tag = www_tag_new(NULL, datebuf);
		www_tag_add_child(child_tag, child_child_tag);

		child_tag = www_tag_new("div", NULL);
		www_tag_add_attrib(child_tag, "class", "blog-author");
		www_tag_add_child(cur_tag, child_tag);

		child_child_tag = www_tag_new(NULL, "by ");
		www_tag_add_child(child_tag, child_child_tag);

		child_child_tag = www_tag_new(NULL, entry->author);
		www_tag_add_child(child_tag, child_child_tag);

		cur_tag = www_tag_new("div", NULL);
		www_tag_add_attrib(cur_tag, "class", "blog-entry");
		www_tag_add_child(page, cur_tag);

		child_tag = www_tag_new("p", NULL);
		www_tag_add_child(cur_tag, child_tag);

		stralloc blog_body = EMPTY_STRALLOC;

		for (char *p = entry->body; *p != '\0'; ++p) {
			if (*p != '\r') {
				stralloc_append1(&blog_body, *p);
				continue;
			}
			if (p[1] != '\0' && p[1] != '\r') {
				stralloc_append1(&blog_body, ' ');
				continue;
			} else if (p[1] != '\0') {
				if (blog_body.s != NULL) {
					child_child_tag = www_tag_new(NULL, blog_body.s);
					free(blog_body.s);
					www_tag_add_child(child_tag, child_child_tag);	
					blog_body = EMPTY_STRALLOC;
					child_tag = www_tag_new("p", NULL);
					www_tag_add_child(cur_tag, child_tag);
				}
				++p;				
			}
		}

		if (blog_body.s != NULL) {
			child_child_tag = www_tag_new(NULL, blog_body.s);
			free(blog_body.s);
			www_tag_add_child(child_tag, child_child_tag);
		}
	}
	ptr_vector_apply(&entries, free);
	destroy_ptr_vector(&entries);

	return www_tag_unwravel(page);
}

#endif