wikid-cgi ist ein C99-CGI-Programm (HTML 4.01): Liste der Wikis, Seiten eines Wikis, eine Seite, Suche -- aus dem Datenverzeichnis von wikid ($WIKIDIR, sonst ~/.local/share/wikid). wikid-http, packaging/wikid-web.service, tests/test-http.sh; Makefile baut und installiert sie, README beschreibt sie.
720 lines
18 KiB
C
720 lines
18 KiB
C
/*
|
|
* wikid-cgi — C99 CGI binary that serves wikid data as HTML 4.01.
|
|
*
|
|
* Usage: placed in a CGI bin directory (e.g. /usr/lib/cgi-bin/), invoked
|
|
* by the web server. Reads the wiki data directory and serves:
|
|
*
|
|
* /wikid/ — list of all wikis
|
|
* /wikid/<slug>/ — pages in a wiki
|
|
* /wikid/<slug>/<id> — one page
|
|
* /wikid/search?q=… — search results
|
|
*
|
|
* Data source: $WIKIDIR (same as wikid). Falls back to ~/.local/share/wikid.
|
|
*
|
|
* Build:
|
|
* cc -std=c99 -O2 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L \
|
|
* -D_DEFAULT_SOURCE -o wikid-cgi wikid-cgi.c
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <ctype.h>
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <limits.h>
|
|
#include <math.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define MAX_TEXT 16384
|
|
#define MAX_SLUG 256
|
|
#define MAX_TITLE 512
|
|
#define MAX_PATH 1024
|
|
|
|
/* ── string buffer (append-only) ──────────────────────────────────── */
|
|
|
|
typedef struct {
|
|
char *p;
|
|
size_t len;
|
|
size_t cap;
|
|
} cgi_buf;
|
|
|
|
static void cgi_buf_init(cgi_buf *b)
|
|
{
|
|
b->p = NULL;
|
|
b->len = 0;
|
|
b->cap = 0;
|
|
}
|
|
|
|
static void cgi_buf_free(cgi_buf *b)
|
|
{
|
|
free(b->p);
|
|
cgi_buf_init(b);
|
|
}
|
|
|
|
static int cgi_buf_grow(cgi_buf *b, size_t need)
|
|
{
|
|
size_t newcap = b->cap ? b->cap * 2 : 256;
|
|
while (newcap < b->len + need)
|
|
newcap *= 2;
|
|
{
|
|
char *q = realloc(b->p, newcap);
|
|
if (!q)
|
|
return -1;
|
|
b->p = q;
|
|
b->cap = newcap;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int cgi_buf_add(cgi_buf *b, const char *data, size_t n)
|
|
{
|
|
if (n == 0)
|
|
return 0;
|
|
if (cgi_buf_grow(b, n) != 0)
|
|
return -1;
|
|
memcpy(b->p + b->len, data, n);
|
|
b->len += n;
|
|
return 0;
|
|
}
|
|
|
|
static int cgi_buf_puts(cgi_buf *b, const char *s)
|
|
{
|
|
return cgi_buf_add(b, s, strlen(s));
|
|
}
|
|
|
|
static int cgi_buf_printf(cgi_buf *b, const char *fmt, ...)
|
|
{
|
|
char tmp[4096];
|
|
va_list ap;
|
|
int n;
|
|
|
|
va_start(ap, fmt);
|
|
n = vsnprintf(tmp, sizeof(tmp), fmt, ap);
|
|
va_end(ap);
|
|
if (n < 0)
|
|
return -1;
|
|
if (n == 0)
|
|
return 0;
|
|
if (cgi_buf_grow(b, (size_t)n) != 0)
|
|
return -1;
|
|
memcpy(b->p + b->len, tmp, (size_t)n);
|
|
b->len += (size_t)n;
|
|
return 0;
|
|
}
|
|
|
|
/* ── small io helpers ─────────────────────────────────────────────── */
|
|
|
|
static char *read_file(const char *path)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
cgi_buf b;
|
|
char tmp[4096];
|
|
size_t n;
|
|
|
|
if (!f)
|
|
return NULL;
|
|
cgi_buf_init(&b);
|
|
while ((n = fread(tmp, 1, sizeof(tmp), f)) > 0)
|
|
cgi_buf_add(&b, tmp, n);
|
|
fclose(f);
|
|
if (!b.p)
|
|
cgi_buf_add(&b, "", 0);
|
|
return b.p;
|
|
}
|
|
|
|
static char *json_str(const char *json, const char *key)
|
|
{
|
|
char pat[512];
|
|
const char *p, *v;
|
|
size_t klen;
|
|
|
|
if (!json || !key)
|
|
return NULL;
|
|
klen = strlen(key);
|
|
if (klen == 0 || klen > 250)
|
|
return NULL;
|
|
snprintf(pat, sizeof(pat), "\"%s\":", key);
|
|
p = strstr(json, pat);
|
|
if (!p)
|
|
return NULL;
|
|
p += klen + 2;
|
|
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
|
|
p++;
|
|
if (*p != '"')
|
|
return NULL;
|
|
p++;
|
|
v = p;
|
|
while (*p && *p != '"')
|
|
p++;
|
|
if (!*p)
|
|
return NULL;
|
|
return strndup(v, (size_t)(p - v));
|
|
}
|
|
|
|
static long json_num(const char *json, const char *key, long def)
|
|
{
|
|
const char *p;
|
|
char pat[512];
|
|
size_t klen;
|
|
|
|
if (!json || !key)
|
|
return def;
|
|
klen = strlen(key);
|
|
if (klen == 0 || klen > 250)
|
|
return def;
|
|
snprintf(pat, sizeof(pat), "\"%s\":", key);
|
|
p = strstr(json, pat);
|
|
if (!p)
|
|
return def;
|
|
p += klen + 2;
|
|
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
|
|
p++;
|
|
if (*p == '-' || (*p >= '0' && *p <= '9')) {
|
|
long val = strtol(p, (char **)&p, 10);
|
|
return val;
|
|
}
|
|
return def;
|
|
}
|
|
|
|
/* ── wiki root ────────────────────────────────────────────────────── */
|
|
|
|
static char *wiki_root(void)
|
|
{
|
|
const char *env;
|
|
char *result;
|
|
|
|
env = getenv("WIKIDIR");
|
|
if (!env || !*env)
|
|
env = getenv("WIKID_HOME");
|
|
if (env && *env) {
|
|
result = strdup(env);
|
|
return result;
|
|
}
|
|
|
|
{
|
|
const char *home = getenv("HOME");
|
|
if (!home)
|
|
home = "/tmp";
|
|
{
|
|
const char *xdg = getenv("XDG_DATA_HOME");
|
|
if (xdg && *xdg)
|
|
result = malloc(strlen(xdg) + 9);
|
|
else
|
|
result = malloc(strlen(home) + 22);
|
|
if (xdg && *xdg)
|
|
sprintf(result, "%s/wikid", xdg);
|
|
else
|
|
sprintf(result, "%s/.local/share/wikid", home);
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ── entry (wiki page) ───────────────────────────────────────────── */
|
|
|
|
struct entry {
|
|
char *id;
|
|
char *title;
|
|
char *text;
|
|
char *tags;
|
|
char *source;
|
|
long created;
|
|
long updated;
|
|
int dim;
|
|
};
|
|
|
|
static void free_entry(struct entry *e)
|
|
{
|
|
if (!e)
|
|
return;
|
|
free(e->id);
|
|
free(e->title);
|
|
free(e->text);
|
|
free(e->tags);
|
|
free(e->source);
|
|
memset(e, 0, sizeof(*e));
|
|
}
|
|
|
|
static int entry_load(const char *slug, const char *id, struct entry *out)
|
|
{
|
|
char leaf[32], path[MAX_PATH];
|
|
char *raw;
|
|
|
|
memset(out, 0, sizeof(*out));
|
|
snprintf(leaf, sizeof(leaf), "%s.json", id);
|
|
snprintf(path, sizeof(path), "%s/%s", slug, leaf);
|
|
|
|
{
|
|
char *root = wiki_root();
|
|
char full[MAX_PATH];
|
|
snprintf(full, sizeof(full), "%s/%s", root, path);
|
|
raw = read_file(full);
|
|
free(root);
|
|
}
|
|
if (!raw)
|
|
return -1;
|
|
|
|
out->id = json_str(raw, "id");
|
|
out->title = json_str(raw, "title");
|
|
out->text = json_str(raw, "text");
|
|
out->tags = json_str(raw, "tags");
|
|
out->source = json_str(raw, "source");
|
|
out->created = json_num(raw, "created", 0);
|
|
out->updated = json_num(raw, "updated", out->created);
|
|
out->dim = (int)json_num(raw, "embedding", 0);
|
|
|
|
if (!out->id || !out->title || !out->text) {
|
|
free_entry(out);
|
|
free(raw);
|
|
return -1;
|
|
}
|
|
free(raw);
|
|
return 0;
|
|
}
|
|
|
|
/* ── knowledge base (wiki) ───────────────────────────────────────── */
|
|
|
|
static int kb_meta(const char *slug, char *name, size_t namelen, char *desc, size_t desclen)
|
|
{
|
|
char path[MAX_PATH];
|
|
char *raw;
|
|
|
|
snprintf(path, sizeof(path), "%s/kb.json", slug);
|
|
{
|
|
char *root = wiki_root();
|
|
char full[MAX_PATH];
|
|
snprintf(full, sizeof(full), "%s/%s", root, path);
|
|
raw = read_file(full);
|
|
free(root);
|
|
}
|
|
if (!raw)
|
|
return -1;
|
|
|
|
if (name) {
|
|
const char *s = json_str(raw, "name");
|
|
snprintf(name, namelen, "%s", s ? s : "");
|
|
}
|
|
if (desc) {
|
|
const char *s = json_str(raw, "description");
|
|
snprintf(desc, desclen, "%s", s ? s : "");
|
|
}
|
|
free(raw);
|
|
return 0;
|
|
}
|
|
|
|
/* ── slugify ──────────────────────────────────────────────────────── */
|
|
|
|
static void slugify(const char *name, char *out, size_t outlen)
|
|
{
|
|
size_t i = 0;
|
|
int last_dash = 0;
|
|
|
|
for (const char *p = name; *p && i + 1 < outlen; p++) {
|
|
unsigned char ch = (unsigned char)*p;
|
|
if (isalnum(ch)) {
|
|
out[i++] = (char)tolower(ch);
|
|
last_dash = 0;
|
|
} else if (!last_dash) {
|
|
out[i++] = '-';
|
|
last_dash = 1;
|
|
}
|
|
}
|
|
while (i > 0 && out[i - 1] == '-')
|
|
i--;
|
|
out[i] = '\0';
|
|
}
|
|
|
|
/* ── list wiki slugs (all wikis) ─────────────────────────────────── */
|
|
|
|
#define MAX_SLUGS 64
|
|
|
|
static int list_slugs(char slugs[][MAX_SLUG], int max)
|
|
{
|
|
char *root = wiki_root();
|
|
DIR *d;
|
|
struct dirent *de;
|
|
int count = 0;
|
|
|
|
*slugs[0] = '\0';
|
|
d = opendir(root);
|
|
free(root);
|
|
if (!d)
|
|
return -1;
|
|
while ((de = readdir(d)) && count < max) {
|
|
char path[MAX_PATH];
|
|
FILE *f;
|
|
if (de->d_name[0] == '.')
|
|
continue;
|
|
snprintf(path, sizeof(path), "%s/%s/kb.json", root, de->d_name);
|
|
f = fopen(path, "r");
|
|
if (!f)
|
|
continue;
|
|
fclose(f);
|
|
snprintf(slugs[count], MAX_SLUG, "%s", de->d_name);
|
|
count++;
|
|
}
|
|
closedir(d);
|
|
/* sort */
|
|
for (int i = 1; i < count; i++)
|
|
for (int j = i; j > 0 && strcmp(slugs[j-1], slugs[j]) > 0; j--) {
|
|
char tmp[MAX_SLUG];
|
|
snprintf(tmp, sizeof(tmp), "%s", slugs[j]);
|
|
snprintf(slugs[j], sizeof(slugs[j]), "%s", slugs[j-1]);
|
|
snprintf(slugs[j-1], sizeof(slugs[j-1]), "%s", tmp);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/* ── list page ids in a wiki ─────────────────────────────────────── */
|
|
|
|
static int list_entry_ids(const char *slug, char ids[][16], int max)
|
|
{
|
|
char *root = wiki_root();
|
|
char dir[MAX_PATH];
|
|
DIR *d;
|
|
struct dirent *de;
|
|
int count = 0;
|
|
|
|
snprintf(dir, sizeof(dir), "%s/%s", root, slug);
|
|
free(root);
|
|
d = opendir(dir);
|
|
if (!d)
|
|
return -1;
|
|
while ((de = readdir(d)) && count < max) {
|
|
size_t len = strlen(de->d_name);
|
|
if (len < 6 || de->d_name[0] != 'e' ||
|
|
strcmp(de->d_name + len - 5, ".json") != 0)
|
|
continue;
|
|
snprintf(ids[count], 16, "%.*s", (int)(len - 5), de->d_name);
|
|
count++;
|
|
}
|
|
closedir(d);
|
|
return count;
|
|
}
|
|
|
|
/* ── HTML escaping ──────────────────────────────────────────────── */
|
|
|
|
static void html_escape(cgi_buf *b, const char *s)
|
|
{
|
|
if (!s)
|
|
return;
|
|
for (; *s; s++) {
|
|
switch (*s) {
|
|
case '&': cgi_buf_puts(b, "&"); break;
|
|
case '<': cgi_buf_puts(b, "<"); break;
|
|
case '>': cgi_buf_puts(b, ">"); break;
|
|
case '"': cgi_buf_puts(b, """); break;
|
|
default: cgi_buf_add(b, s, 1); break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ── HTTP helpers ───────────────────────────────────────────────── */
|
|
|
|
static void send_header(cgi_buf *b, int code, const char *reason, const char *ctype)
|
|
{
|
|
const char *statuses[] = {
|
|
[200] = "OK", [404] = "Not Found", [400] = "Bad Request",
|
|
[500] = "Internal Server Error"
|
|
};
|
|
if (!reason)
|
|
reason = statuses[code] ? statuses[code] : "Error";
|
|
cgi_buf_printf(b, "HTTP/1.1 %d %s\r\n", code, reason);
|
|
cgi_buf_printf(b, "Content-Type: %s\r\n", ctype);
|
|
cgi_buf_puts(b, "Connection: close\r\n\r\n");
|
|
}
|
|
|
|
/* ── Markdown → simple HTML 4.01 ────────────────────────────────── */
|
|
|
|
static int md_to_html(cgi_buf *out, const char *text)
|
|
{
|
|
const char *p = text;
|
|
int in_para = 0;
|
|
int in_list = 0;
|
|
|
|
while (*p) {
|
|
/* Heading */
|
|
if (*p == '#') {
|
|
int level = 0;
|
|
while (*p == '#') { level++; p++; }
|
|
while (*p == ' ') p++;
|
|
if (in_para) { cgi_buf_puts(out, "</p>\n"); in_para = 0; }
|
|
if (in_list) { cgi_buf_puts(out, "</ul>\n"); in_list = 0; }
|
|
if (level > 6) level = 6;
|
|
cgi_buf_printf(out, "<h%d>", level);
|
|
while (*p && *p != '\n') {
|
|
if (*p == '`') {
|
|
cgi_buf_puts(out, "<code>"); p++;
|
|
while (*p && *p != '`' && *p != '\n') {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
if (*p == '`') p++;
|
|
cgi_buf_puts(out, "</code>");
|
|
} else {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
}
|
|
cgi_buf_printf(out, "</h%d>\n", level);
|
|
continue;
|
|
}
|
|
/* Unordered list */
|
|
if (*p == '-' && (p[1] == ' ' || p[1] == ' ')) {
|
|
if (!in_list) { cgi_buf_puts(out, "<ul>\n"); in_list = 1; }
|
|
cgi_buf_puts(out, "<li>");
|
|
p += 2;
|
|
while (*p && *p != '\n') {
|
|
if (*p == '`') {
|
|
cgi_buf_puts(out, "<code>"); p++;
|
|
while (*p && *p != '`' && *p != '\n') {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
if (*p == '`') p++;
|
|
cgi_buf_puts(out, "</code>");
|
|
} else {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
}
|
|
cgi_buf_puts(out, "</li>\n");
|
|
continue;
|
|
}
|
|
if (in_list) { cgi_buf_puts(out, "</ul>\n"); in_list = 0; }
|
|
/* Horizontal rule */
|
|
if (strncmp(p, "---", 3) == 0 && (p[3] == '\n' || !p[3])) {
|
|
if (in_para) { cgi_buf_puts(out, "</p>\n"); in_para = 0; }
|
|
cgi_buf_puts(out, "<hr />\n");
|
|
p += 3;
|
|
while (*p == '\n') p++;
|
|
continue;
|
|
}
|
|
/* Blank line */
|
|
if (*p == '\n') {
|
|
if (in_para) { cgi_buf_puts(out, "</p>\n"); in_para = 0; }
|
|
p++;
|
|
continue;
|
|
}
|
|
/* Paragraph */
|
|
if (!in_para) { cgi_buf_puts(out, "<p>"); in_para = 1; }
|
|
while (*p && *p != '\n') {
|
|
if (*p == '*' && p[1] == '*') {
|
|
cgi_buf_puts(out, "<b>"); p += 2;
|
|
while (*p && !(*p == '*' && p[1] == '*') && *p != '\n') {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
if (*p == '*') p++;
|
|
cgi_buf_puts(out, "</b>");
|
|
} else if (*p == '*' && p[1] != '*') {
|
|
cgi_buf_puts(out, "<i>"); p++;
|
|
while (*p && *p != '*' && *p != '\n') {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
if (*p == '*') p++;
|
|
cgi_buf_puts(out, "</i>");
|
|
} else if (*p == '`') {
|
|
cgi_buf_puts(out, "<code>"); p++;
|
|
while (*p && *p != '`' && *p != '\n') {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
if (*p == '`') p++;
|
|
cgi_buf_puts(out, "</code>");
|
|
} else {
|
|
cgi_buf_add(out, p, 1); p++;
|
|
}
|
|
}
|
|
if (*p == '\n') p++;
|
|
}
|
|
if (in_para) cgi_buf_puts(out, "</p>\n");
|
|
if (in_list) cgi_buf_puts(out, "</ul>\n");
|
|
return 0;
|
|
}
|
|
|
|
/* ── HTML page shell ────────────────────────────────────────────── */
|
|
|
|
static const char *css =
|
|
"body{font-family:serif;margin:2em;max-width:72em;background:#fff;color:#222}\n"
|
|
"h1,h2,h3,h4,h5,h6{margin-top:1.2em;margin-bottom:0.4em}\n"
|
|
"a{color:#0055aa;text-decoration:none}\n"
|
|
"a:hover{text-decoration:underline}\n"
|
|
"ul{margin:0.5em 0}\n"
|
|
"li{margin:0.2em 0}\n"
|
|
"code{background:#f4f4f4;padding:0.1em 0.3em;font-size:0.9em}\n"
|
|
"table{border-collapse:collapse;margin:1em 0}\n"
|
|
"td,th{border:1px solid #ccc;padding:0.3em 0.6em;text-align:left}\n"
|
|
"th{background:#f0f0f0}\n"
|
|
".meta{font-size:0.85em;color:#666;margin-bottom:1em}\n"
|
|
".nav{margin:1em 0;border-top:1px solid #ddd;padding-top:0.5em}\n"
|
|
".nav a{margin-right:1em}\n";
|
|
|
|
static void html_head(cgi_buf *b, const char *title)
|
|
{
|
|
cgi_buf_puts(b, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"
|
|
"\"http://www.w3.org/TR/html4/strict.dtd\">\n"
|
|
"<html lang=\"en\" dir=\"ltr\">\n"
|
|
"<head>\n"
|
|
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
|
|
"<meta name=\"viewport\" content=\"width=device-width\">\n"
|
|
"<title>");
|
|
html_escape(b, title);
|
|
cgi_buf_puts(b, "</title>\n"
|
|
"<style type=\"text/css\">\n"
|
|
);
|
|
cgi_buf_puts(b, css);
|
|
cgi_buf_puts(b, "</style>\n"
|
|
"</head>\n"
|
|
"<body>\n");
|
|
}
|
|
|
|
static void html_foot(cgi_buf *b)
|
|
{
|
|
cgi_buf_puts(b, "</body>\n</html>\n");
|
|
}
|
|
|
|
/* ── render one page ────────────────────────────────────────────── */
|
|
|
|
static void render_page(cgi_buf *out, const char *slug, const char *id)
|
|
{
|
|
struct entry e;
|
|
char name[MAX_SLUG] = "";
|
|
|
|
kb_meta(slug, name, sizeof(name), NULL, 0);
|
|
|
|
html_head(out, e.title ? e.title : "Page not found");
|
|
|
|
cgi_buf_printf(out, "<h1>");
|
|
html_escape(out, e.title);
|
|
cgi_buf_printf(out, " <a href=\"#edit\" style=\"font-size:0.7em;color:#999\">[edit]</a></h1>\n");
|
|
|
|
cgi_buf_puts(out, "<p class=\"meta\">");
|
|
if (e.tags && *e.tags) {
|
|
cgi_buf_puts(out, "tags: ");
|
|
html_escape(out, e.tags);
|
|
cgi_buf_puts(out, " ");
|
|
}
|
|
cgi_buf_printf(out, "updated: %ld ", e.updated);
|
|
if (e.dim > 0)
|
|
cgi_buf_puts(out, "embedded");
|
|
cgi_buf_puts(out, "</p>\n");
|
|
|
|
md_to_html(out, e.text);
|
|
|
|
cgi_buf_puts(out, "<div class=\"nav\">\n");
|
|
cgi_buf_printf(out, " <a href=\"/wikid/%s/\">Back to %s</a> ", slug, name[0] ? name : slug);
|
|
cgi_buf_printf(out, " <a href=\"/wikid/\">All wikis</a>\n");
|
|
cgi_buf_puts(out, "</div>\n");
|
|
|
|
html_foot(out);
|
|
}
|
|
|
|
/* ── render wiki index (list of wikis) ──────────────────────────── */
|
|
|
|
static void render_wiki_index(cgi_buf *out)
|
|
{
|
|
char slugs[MAX_SLUGS][MAX_SLUG];
|
|
int n = list_slugs(slugs, MAX_SLUGS);
|
|
|
|
html_head(out, "Wikis");
|
|
|
|
cgi_buf_puts(out, "<h1>Wikis</h1>\n");
|
|
|
|
if (n <= 0) {
|
|
cgi_buf_puts(out, "<p>No wikis yet — create one with <code>wikid</code> or <code>kb_create</code>.</p>\n");
|
|
} else {
|
|
cgi_buf_printf(out, "<p>%d wiki(s) below.</p>\n", n);
|
|
for (int i = 0; i < n; i++) {
|
|
char name[MAX_SLUG] = "", desc[512] = "";
|
|
char ids[256][16];
|
|
int np;
|
|
|
|
kb_meta(slugs[i], name, sizeof(name), desc, sizeof(desc));
|
|
np = list_entry_ids(slugs[i], ids, 256);
|
|
|
|
cgi_buf_printf(out, "<h2><a href=\"/wikid/%s/\">", slugs[i]);
|
|
html_escape(out, name[0] ? name : slugs[i]);
|
|
cgi_buf_printf(out, "</a> (%d page%s)</h2>\n", np, np == 1 ? "" : "s");
|
|
if (desc[0]) {
|
|
cgi_buf_puts(out, "<p class=\"meta\">");
|
|
html_escape(out, desc);
|
|
cgi_buf_puts(out, "</p>\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
html_foot(out);
|
|
}
|
|
|
|
/* ── render wiki pages listing ──────────────────────────────────── */
|
|
|
|
static void render_wiki_list(cgi_buf *out, const char *slug)
|
|
{
|
|
char name[MAX_SLUG] = "", desc[512] = "";
|
|
char ids[256][16];
|
|
int np;
|
|
|
|
kb_meta(slug, name, sizeof(name), desc, sizeof(desc));
|
|
|
|
html_head(out, name[0] ? name : slug);
|
|
|
|
cgi_buf_printf(out, "<h1>%s</h1>\n", name[0] ? name : slug);
|
|
if (desc[0]) {
|
|
cgi_buf_printf(out, "<p class=\"meta\">");
|
|
html_escape(out, desc);
|
|
cgi_buf_puts(out, "</p>\n");
|
|
}
|
|
|
|
np = list_entry_ids(slug, ids, 256);
|
|
if (np <= 0) {
|
|
cgi_buf_puts(out, "<p>No pages yet.</p>\n");
|
|
} else {
|
|
cgi_buf_printf(out, "<p>%d page(s)</p>\n", np);
|
|
for (int i = 0; i < np; i++) {
|
|
struct entry e;
|
|
if (entry_load(slug, ids[i], &e) != 0)
|
|
continue;
|
|
|
|
cgi_buf_printf(out, "<h3><a href=\"/wikid/%s/%s\">", slug, ids[i]);
|
|
html_escape(out, e.title);
|
|
cgi_buf_puts(out, "</a></h3>\n");
|
|
|
|
cgi_buf_printf(out, "<p class=\"meta\">%s ", e.id);
|
|
if (e.tags && *e.tags) {
|
|
cgi_buf_puts(out, "tags: ");
|
|
html_escape(out, e.tags);
|
|
cgi_buf_puts(out, " ");
|
|
}
|
|
if (e.dim > 0)
|
|
cgi_buf_puts(out, "embedded ");
|
|
cgi_buf_printf(out, "updated: %ld</p>\n", e.updated);
|
|
|
|
/* First 300 chars of text */
|
|
cgi_buf_puts(out, "<p>");
|
|
{
|
|
const char *p = e.text;
|
|
int len = 0;
|
|
while (*p && *p != '\n' && len < 300) {
|
|
if (*p == '*' && p[1] == '*') { p += 2; continue; }
|
|
if (*p == '*') { p++; continue; }
|
|
if (*p == '`') { p++; continue; }
|
|
cgi_buf_add(out, p, 1);
|
|
p++;
|
|
len++;
|
|
}
|
|
}
|
|
cgi_buf_puts(out, "…</p>\n");
|
|
free_entry(&e);
|
|
}
|
|
}
|
|
|
|
cgi_buf_puts(out, "<div class=\"nav\">\n");
|
|
cgi_buf_puts(out, " <a href=\"/wikid/\">All wikis</a>\n");
|
|
cgi_buf_puts(out, "</div>\n");
|
|
|
|
html_foot(out);
|
|
}
|