From 71b465eba27996d3091aa6a478a5cf04244328a7 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 21 Oct 2018 20:11:30 +1000 Subject: [PATCH] Improvements to www_tree and move over last 10 --- src/www_blog.c | 1 - src/www_last10.c | 89 ++++++++++++++++++++++++++++++++++++------------ src/www_tree.c | 79 ++++++++++++++++++++++++++---------------- 3 files changed, 117 insertions(+), 52 deletions(-) diff --git a/src/www_blog.c b/src/www_blog.c index 2b1ccba..f2c6c9d 100644 --- a/src/www_blog.c +++ b/src/www_blog.c @@ -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; diff --git a/src/www_last10.c b/src/www_last10.c index 3e666cc..75a448b 100644 --- a/src/www_last10.c +++ b/src/www_last10.c @@ -4,15 +4,22 @@ #include #include +#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, "

Last 10 Callers

\n"); - stralloc_cats(&page, "
\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, "
"); - stralloc_cats(&page, callers[i].name); - stralloc_cats(&page, "
"); - stralloc_cats(&page, callers[i].location); - stralloc_cats(&page, "
"); - stralloc_cats(&page, "
"); + 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, "
\n"); - if (callers[i].calls == 1) { - stralloc_cats(&page, "
"); - } - stralloc_cats(&page, "
\n"); - } - stralloc_cats(&page, "
\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 diff --git a/src/www_tree.c b/src/www_tree.c index 01f7d5a..eee4da2 100644 --- a/src/www_tree.c +++ b/src/www_tree.c @@ -59,44 +59,63 @@ 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->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, '\"'); + if (child->children.len > 0) { + 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_append1(&thedata, '>'); } + char *data = www_tag_unwravel(child); + stralloc_cats(&thedata, data); + free(data); - - stralloc_append1(&thedata, '>'); - } - char *data = www_tag_unwravel(child); - stralloc_cats(&thedata, data); - free(data); - - if (child->tag != NULL) { - stralloc_cats(&thedata, "tag); - stralloc_append1(&thedata, '>'); - ptr_vector_apply(&child->attribs, free); - destroy_ptr_vector(&child->attribs); - ptr_vector_apply(&child->values, free); - destroy_ptr_vector(&child->values); + if (child->tag != NULL) { + stralloc_cats(&thedata, "tag); + stralloc_append1(&thedata, '>'); + ptr_vector_apply(&child->attribs, free); + destroy_ptr_vector(&child->attribs); + 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;