commit 4ec3e7274683b05253250323ea359be0da46d311
parent d20805b85cb58696ff83292c9798ba2c39ce193a
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Wed, 20 Mar 2024 20:32:49 -0700
Add tk_startpage to insert common elements
The tk_startpage function inserts a navigational header into every
page.
Diffstat:
3 files changed, 65 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,8 +6,8 @@ prefix = /var/www/htdocs/${name}.primus.lan
pages = pages/index.c pages/login.c pages/logout.c \
pages/account.c pages/main.c pages/export.c pages/archive.c
-hdrsrc = page.c html.c user.c stmt.c key.c times.c
-src = ${hdrsrc} ${pages} pages/util.c
+hdrsrc = page.c html.c user.c stmt.c key.c times.c pages/util.c pages/common.c
+src = ${hdrsrc} ${pages}
hdr = ${hdrsrc:.c=.h} util.h pages/util.h pages/pages.h
obj = ${src:.c=.o}
diff --git a/pages/common.c b/pages/common.c
@@ -0,0 +1,59 @@
+#include <ctype.h>
+#include <string.h>
+
+#include "common.h"
+#include "pages.h"
+
+enum kcgi_err
+tk_header(struct pagedata *pd)
+{
+ static int unauth_pages[] = { PageIndex, PageLogin, -1 };
+ static int auth_pages[] = {
+ PageIndex, PageMain, PageArchive, PageAccount, PageLogout, -1
+ };
+ static char *pagenames[PageMax] = {
+ [PageIndex] = "Home"
+ };
+
+ enum kcgi_err status;
+ int *pages;
+ unsigned int i;
+ char namebuf[32];
+ char *url;
+
+ if ((status = khtml_elem(&pd->html, KELEM_NAV)) != KCGI_OK ||
+ (status = khtml_elem(&pd->html, KELEM_UL)) != KCGI_OK)
+ return status;
+
+ if (pd->user)
+ pages = auth_pages;
+ else
+ pages = unauth_pages;
+
+ for (i = 0; pages[i] >= 0; ++i) {
+ url = pd->pages[pages[i]];
+ if (pagenames[i]) {
+ strcpy(namebuf, pagenames[i]);
+ } else {
+ strcpy(namebuf, url);
+ namebuf[0] = toupper(namebuf[0]);
+ }
+ if ((status = khtml_elem(&pd->html, KELEM_LI)) != KCGI_OK ||
+ (status = htmllink(pd, url, namebuf)) != KCGI_OK ||
+ (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
+ return status;
+ }
+
+ return khtml_closeelem(&pd->html, 1);
+}
+
+enum kcgi_err
+tk_startpage(struct pagedata *pd, struct pagetemplate *t, enum khttp code)
+{
+ enum kcgi_err status;
+
+ if ((status = startpage(pd, t, code)) != KCGI_OK ||
+ (status = tk_header(pd)) != KCGI_OK)
+ return status;
+ return KCGI_OK;
+}
diff --git a/pages/common.h b/pages/common.h
@@ -10,3 +10,7 @@
#include "../user.h"
#include "../key.h"
#include "util.h"
+
+enum kcgi_err htmlheader(struct pagedata *pd);
+enum kcgi_err tk_startpage(struct pagedata *pd, struct pagetemplate *t,
+ enum khttp code);