Improvements to www_tree and move over last 10

This commit is contained in:
Andrew Pamment 2018-10-21 20:11:30 +10:00
parent fc590d9608
commit 71b465eba2
3 changed files with 117 additions and 52 deletions

View File

@ -9,7 +9,6 @@
extern struct bbs_config conf;
char *www_blog() {
//stralloc page = EMPTY_STRALLOC;
struct ptr_vector entries = blog_load();
struct www_tag *page;
struct www_tag *cur_tag;

View File

@ -4,15 +4,22 @@
#include <stdlib.h>
#include <string.h>
#include "www_tree.h"
#include "bbs.h"
extern struct bbs_config conf;
char *www_last10() {
size_t n = 0;
stralloc page = EMPTY_STRALLOC;
//stralloc page = EMPTY_STRALLOC;
struct last10_callers callers[10];
char last10_path[PATH_MAX];
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;
snprintf(last10_path, PATH_MAX, "%s/last10v2.dat", conf.bbs_path);
@ -24,37 +31,77 @@ char *www_last10() {
fclose(fptr);
}
stralloc_copys(&page, "<div class=\"content-header\"><h2>Last 10 Callers</h2></div>\n");
stralloc_cats(&page, "<div class=\"div-table\">\n");
page = www_tag_new(NULL, "");
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "content-header");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("h2", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "Last 10 Callers");
www_tag_add_child(child_tag, child_child_tag);
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "div-table");
www_tag_add_child(page, cur_tag);
for (size_t i = 0; i < n; ++i) {
struct tm called;
char buffer[32];
stralloc_cats(&page, "<div class=\"last10-row\"><div class=\"last10-name\">");
stralloc_cats(&page, callers[i].name);
stralloc_cats(&page, "</div><div class=\"last10-location\">");
stralloc_cats(&page, callers[i].location);
stralloc_cats(&page, "</div>");
stralloc_cats(&page, "<div class=\"last10-date\">");
child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_tag, "class", "last10-row");
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "last10-name");
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new(NULL, callers[i].name);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "last10-location");
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new(NULL, callers[i].location);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "last10-date");
www_tag_add_child(child_tag, child_child_tag);
localtime_r(&callers[i].time, &called);
if (conf.date_style == 1)
strftime(buffer, sizeof buffer, "%H:%M %m-%d-%y", &called);
else
strftime(buffer, sizeof buffer, "%H:%M %d-%m-%y", &called);
stralloc_cats(&page, buffer);
stralloc_cats(&page, "</div>\n");
if (callers[i].calls == 1) {
stralloc_cats(&page, "<div class=\"last10-new\"><img src=\"");
stralloc_cats(&page, conf.www_url);
stralloc_cats(&page, "static/newuser.png\" /></div>");
}
stralloc_cats(&page, "</div>\n");
}
stralloc_cats(&page, "</div>\n");
stralloc_0(&page);
return page.s;
child_child_child_tag = www_tag_new(NULL, buffer);
www_tag_add_child(child_child_tag, child_child_child_tag);
if (callers[i].calls == 1) {
child_child_tag = www_tag_new("div", NULL);
www_tag_add_attrib(child_child_tag, "class", "last10-new");
www_tag_add_child(child_tag, child_child_tag);
stralloc url = EMPTY_STRALLOC;
stralloc_copys(&url, conf.www_url);
stralloc_cats(&url, "static/newuser.png");
stralloc_0(&url);
child_child_child_tag = www_tag_new("img", NULL);
www_tag_add_attrib(child_child_child_tag, "src", url.s);
free(url.s);
www_tag_add_child(child_child_tag, child_child_child_tag);
}
}
return www_tag_unwravel(page);
}
#endif

View File

@ -59,9 +59,10 @@ void www_tag_add_child(struct www_tag *tag, struct www_tag *child) {
char *www_tag_unwravel(struct www_tag *tag) {
stralloc thedata = EMPTY_STRALLOC;
int children = tag->children.len;
while (tag->children.len > 0) {
struct www_tag *child = ptr_vector_del(&tag->children, 0);
if (child->children.len > 0) {
if (child->tag != NULL) {
stralloc_append1(&thedata, '<');
stralloc_cats(&thedata, child->tag);
@ -90,13 +91,31 @@ char *www_tag_unwravel(struct www_tag *tag) {
ptr_vector_apply(&child->values, free);
destroy_ptr_vector(&child->values);
}
} else {
if (child->tag != NULL) {
stralloc_append1(&thedata, '<');
stralloc_cats(&thedata, child->tag);
for (int i = 0; i < child->attribs.len; i++) {
stralloc_append1(&thedata, ' ');
stralloc_cats(&thedata, (char *)ptr_vector_get(&child->attribs, i));
stralloc_append1(&thedata, '=');
stralloc_append1(&thedata, '\"');
stralloc_cats(&thedata, (char *)ptr_vector_get(&child->values, i));
stralloc_append1(&thedata, '\"');
}
stralloc_cats(&thedata, " />");
ptr_vector_apply(&child->attribs, free);
destroy_ptr_vector(&child->attribs);
ptr_vector_apply(&child->values, free);
destroy_ptr_vector(&child->values);
} else {
stralloc_cats(&thedata, child->data);
}
}
destroy_ptr_vector(&child->children);
}
if (tag->data != NULL) {
stralloc_cats(&thedata, tag->data);
}
stralloc_0(&thedata);
return thedata.s;