This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/src/www_blog.c
2018-10-21 20:11:30 +10:00

117 lines
3.2 KiB
C

#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;
}
}
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