Show full markdown link in top bar

main
cel 5 years ago
parent 042c8494bb
commit 0f03accaaa

@ -121,7 +121,7 @@ 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 *str) {
static int write_topbar(int fd, enum page page, const char *id, const char *title) {
int rc = write_buf(fd, "<form action=\"search\">");
if (page == PAGE_ALL) rc |= write_buf(fd, "<strong>");
@ -136,13 +136,16 @@ static int write_topbar(int fd, enum page page, const char *str) {
rc |= write_buf(fd, " <input name=q placeholder=Search");
if (page == PAGE_SEARCH) {
rc |= write_buf(fd, " value=\"");
rc |= write_html(fd, str, strlen(str));
rc |= write_html(fd, id, strlen(id));
rc |= write_buf(fd, "\"");
}
rc |= write_buf(fd, ">");
if (page == PAGE_ZET) {
rc |= dprintf(fd, " <strong><code><a href=\"%s\">§%s</a></code></strong>", str, str);
rc |= dprintf(fd, " <a href=\"%s\">"
"[<strong><code>§%s</code></strong>](", id, id);
rc |= write_html(fd, title, strlen(title));
rc |= write_buf(fd, ")</a>");
}
rc |= write_buf(fd, "</form>");
@ -180,24 +183,29 @@ static int dpi_respond_err(int fd, const char *fmt, ...) {
return rc;
}
static char *zet_get_title(const char *id, char *buf, size_t len) {
char path_buf[_POSIX_PATH_MAX];
ssize_t sz = snprintf(path_buf, sizeof(path_buf), "%s/%s", zet_dir, id);
if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return NULL;
int fd = open(path_buf, O_RDONLY);
if (fd < 0) return NULL;
static char *zet_get_title_fd(int fd, char *buf, size_t len) {
int rc = read_some(fd, (unsigned char *)buf, &len);
if (rc < 0) { close(fd); return NULL; }
if (rc < 0) return NULL;
buf[len] = '\0';
char *end = strchr(buf, '\n');
if (end != NULL) *end = '\0';
close(fd);
while (*buf == '#') buf++;
while (*buf == ' ') buf++;
if (!*buf) {
return buf;
}
static char *zet_get_title(const char *id, char *buf, size_t len) {
char path_buf[_POSIX_PATH_MAX];
ssize_t sz = snprintf(path_buf, sizeof(path_buf), "%s/%s", zet_dir, id);
if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return NULL;
int fd = open(path_buf, O_RDONLY);
if (fd < 0) return NULL;
buf = zet_get_title_fd(fd, buf, len);
if (buf != NULL && *buf == '\0') {
strncpy(buf, "§", sigil_size);
strncpy(buf + sigil_size, id, len);
}
close(fd);
return buf;
}
@ -325,7 +333,7 @@ static int dpi_serve_zet_index(int fd) {
"<meta charset=utf-8>"
"</head>"
"<body>");
rc |= write_topbar(fd, PAGE_ALL, NULL);
rc |= write_topbar(fd, PAGE_ALL, NULL, NULL);
rc |= write_buf(fd, "<ul>");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
@ -404,7 +412,7 @@ static int dpi_serve_zet_search(int fd, char *qs) {
"</head>"
"<body>"
);
rc |= write_topbar(fd, PAGE_SEARCH, query);
rc |= write_topbar(fd, PAGE_SEARCH, query, NULL);
rc |= write_buf(fd, "<ul>");
if (rc < 0) { warn("write"); close(fd); return 0; }
const char *id;
@ -458,7 +466,7 @@ static int dpi_serve_zet_new(int fd) {
"<meta charset=utf-8>"
"</head>"
"<body>");
write_topbar(fd, PAGE_NEW, NULL);
write_topbar(fd, PAGE_NEW, NULL, NULL);
dprintf(fd,
"<form method=post action=\"%snew\" enctype=\"multipart/form-data\">"
"<textarea name=text cols=64 style=\"width:50%%;height:80%%\"></textarea><br>"
@ -634,19 +642,22 @@ static int dpi_serve_zet(int fd, char *path) {
rc = lseek(note_fd, 0, SEEK_SET);
if (rc < 0) return dpi_respond_err(fd, "lseek");
char buf[128];
char *title = zet_get_title_fd(note_fd, buf, sizeof(buf));
rc = lseek(note_fd, 0, SEEK_SET);
if (rc < 0) return dpi_respond_err(fd, "lseek");
rc = dpi_send_header(fd, "text/html");
rc |= dprintf(fd,
"<!doctype html><head>"
"<title>");
char buf[128];
char *title = zet_get_title(id, buf, sizeof(buf));
if (title != NULL) rc |= write_html(fd, title, strlen(title));
rc |= dprintf(fd,
"</title>"
"<meta charset=utf-8>"
"</head><body>");
rc |= write_topbar(fd, PAGE_ZET, id);
rc |= write_topbar(fd, PAGE_ZET, id, title);
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%%\">"

Loading…
Cancel
Save