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

Loading…
Cancel
Save