Refactor topbar generation

main
cel 5 years ago
parent 82a4e3d9da
commit 19ac6f7d78

@ -6,6 +6,9 @@ SUNDOWN_SRC=$(wildcard sundown/*.c)
OBJ=dpi.o zet.dpi.o io.o $(SUNDOWN_SRC:c=o)
CFLAGS=-Wall -Wextra -pedantic
all-reload: all
pkill $(BIN)
all: $(BIN)
$(BIN): $(OBJ)

@ -22,14 +22,6 @@
#include "sundown/buffer.h"
#include "sundown/houdini.h"
#define NAV_ALL "<a href=\".\">All</a>"
#define NAV_NEW "<a href=\"new\">New</a>"
#define STRONG(html) "<strong>" html "</strong>"
#define TOPBAR_NAV NAV_ALL " " NAV_NEW " "
#define TOPBAR_NAV_ALL STRONG(NAV_ALL) " " NAV_NEW " "
#define TOPBAR_NAV_NEW NAV_ALL " " STRONG(NAV_NEW) " "
static const size_t sigil_size = sizeof("§")-1;
static const int id_length = 16;
@ -60,6 +52,12 @@ struct zet_search {
char *id;
};
enum page {
PAGE_NEW,
PAGE_ALL,
PAGE_ZET,
};
static int count_lines_in(const char *str) {
int count = 0;
char c;
@ -116,6 +114,25 @@ static int write_html(int fd, const char *buf, size_t len) {
return 0;
}
static int write_topbar(int fd, enum page page, const char *id) {
int rc = write_buf(fd, "<div>");
if (page == PAGE_ALL) rc |= write_buf(fd, "<strong>");
rc |= write_buf(fd, "<a href=\".\">All</a>");
if (page == PAGE_ALL) rc |= write_buf(fd, "</strong>");
rc |= write_buf(fd, " ");
if (page == PAGE_NEW) rc |= write_buf(fd, "<strong>");
rc |= write_buf(fd, "<a href=\"new\">New</a>");
if (page == PAGE_NEW) rc |= write_buf(fd, "</strong>");
if (page == PAGE_ZET) {
rc |= dprintf(fd, " <strong><code><a href=\"%s\">§%s</a></code></strong>", id, id);
}
rc |= write_buf(fd, "</div>");
return rc;
}
static int passthrough_note_html(int fd, int note_fd) {
int rc;
do {
@ -266,10 +283,10 @@ static int dpi_serve_zet_index(int fd) {
"<title>Notes</title>"
"<meta charset=utf-8>"
"</head>"
"<body>"
"<div>" TOPBAR_NAV_ALL "</div>"
"<ul>"
);
"<body>");
rc |= write_topbar(fd, PAGE_ALL, NULL);
rc |= write_buf(fd, "<ul>");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
while (1) {
rc = zet_search_next(&zs, &id);
@ -320,8 +337,9 @@ static int dpi_serve_zet_new(int fd) {
"<title>New Note</title>"
"<meta charset=utf-8>"
"</head>"
"<body>"
"<div>" TOPBAR_NAV_NEW "</div>"
"<body>");
write_topbar(fd, PAGE_NEW, NULL);
dprintf(fd,
"<form method=post action=\"%snew\" enctype=\"multipart/form-data\">"
"<textarea name=text cols=64 style=\"width:50%%;height:80%%\"></textarea><br>"
"<input type=submit value=\"Save\">"
@ -466,12 +484,13 @@ static int dpi_serve_zet(int fd, char *path) {
"<!doctype html><head>"
"<title>§%s</title>"
"<meta charset=utf-8>"
"</head><body>"
"<div>" TOPBAR_NAV " <strong><code><a href=\"%s\">§%s</a></code></strong></div>"
"</head><body>", id);
rc |= write_topbar(fd, PAGE_ZET, id);
rc |= dprintf(fd,
"<table style=\"width:100%%; height:100%%\"><tr>"
"<td style=\"width:50%%\"><form method=post action=\"%s%s\" enctype=\"multipart/form-data\" style=\"height:100%%\">"
"<textarea name=text cols=64 rows=%d style=\"width:100%%\">"
, id, id, id, server_url, id, rows);
, server_url, id, rows);
rc = passthrough_note_html(fd, note_fd);
if (rc < 0) { warnx("passthrough_note_html"); close(fd); return 0; }
rc = dprintf(fd, "</textarea><br>"

Loading…
Cancel
Save