From 0f03accaaa1b29538b3ee3796bed92486aa1b43c Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 2 Jun 2020 16:57:46 -0400 Subject: [PATCH] Show full markdown link in top bar --- zet.dpi.c | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/zet.dpi.c b/zet.dpi.c index 86a1bc7..fd139b5 100644 --- a/zet.dpi.c +++ b/zet.dpi.c @@ -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, "
"); if (page == PAGE_ALL) rc |= write_buf(fd, ""); @@ -136,13 +136,16 @@ static int write_topbar(int fd, enum page page, const char *str) { rc |= write_buf(fd, " "); if (page == PAGE_ZET) { - rc |= dprintf(fd, " §%s", str, str); + rc |= dprintf(fd, " " + "[§%s](", id, id); + rc |= write_html(fd, title, strlen(title)); + rc |= write_buf(fd, ")"); } rc |= write_buf(fd, ""); @@ -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) { "" "" ""); - rc |= write_topbar(fd, PAGE_ALL, NULL); + rc |= write_topbar(fd, PAGE_ALL, NULL, NULL); rc |= write_buf(fd, "
    "); 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) { "" "" ); - rc |= write_topbar(fd, PAGE_SEARCH, query); + rc |= write_topbar(fd, PAGE_SEARCH, query, NULL); rc |= write_buf(fd, "
      "); if (rc < 0) { warn("write"); close(fd); return 0; } const char *id; @@ -458,7 +466,7 @@ static int dpi_serve_zet_new(int fd) { "" "" ""); - write_topbar(fd, PAGE_NEW, NULL); + write_topbar(fd, PAGE_NEW, NULL, NULL); dprintf(fd, "
      " "
      " @@ -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, "" ""); - 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, "" "" ""); - rc |= write_topbar(fd, PAGE_ZET, id); + rc |= write_topbar(fd, PAGE_ZET, id, title); rc |= dprintf(fd, "" "
      "