changeset 64:e8d8eaa0da53

pasterd: support <title> in header
author David Demelier <markand@malikania.fr>
date Thu, 26 May 2022 13:28:10 +0200
parents 1075658abc74
children 664300f3fd87
files http.c page-download.c page-fork.c page-index.c page-new.c page-paste.c page-search.c page-static.c page.c page.h themes/mini/fragments/header.html themes/minimal/fragments/header.html themes/siimple/fragments/header.html
diffstat 13 files changed, 61 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/http.c	Thu May 26 13:09:33 2022 +0200
+++ b/http.c	Thu May 26 13:28:10 2022 +0200
@@ -75,7 +75,7 @@
 	log_debug("http: accessing page '%s'", req->path);
 
 	if (req->page == PAGE_LAST)
-		page(req, NULL, KHTTP_404, "pages/404.html");
+		page(req, NULL, KHTTP_404, "pages/404.html", "404");
 	else
 		handlers[req->page](req);
 }
--- a/page-download.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-download.c	Thu May 26 13:28:10 2022 +0200
@@ -33,7 +33,7 @@
 	struct paste paste = {0};
 
 	if (!database_get(&paste, r->path))
-		page(r, NULL, KHTTP_404, "404.html");
+		page(r, NULL, KHTTP_404, "404.html", "404");
 	else {
 		khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_OCTET_STREAM]);
 #if 0
@@ -58,7 +58,7 @@
 		get(r);
 		break;
 	default:
-		page(r, NULL, KHTTP_400, "400.html");
+		page(r, NULL, KHTTP_400, "400.html", "400");
 		break;
 	}
 }
--- a/page-fork.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-fork.c	Thu May 26 13:28:10 2022 +0200
@@ -35,7 +35,7 @@
 	struct paste paste = {0};
 
 	if (!database_get(&paste, req->path))
-		page(req, NULL, KHTTP_404, "404.html");
+		page(req, NULL, KHTTP_404, "404.html", "404");
 	else {
 		page_new_render(req, &paste);
 		paste_finish(&paste);
@@ -50,7 +50,7 @@
 		get(req);
 		break;
 	default:
-		page(req, NULL, KHTTP_400, "400.html");
+		page(req, NULL, KHTTP_400, "400.html", "400");
 		break;
 	}
 }
--- a/page-index.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-index.c	Thu May 26 13:28:10 2022 +0200
@@ -64,7 +64,7 @@
 	size_t pastesz = NELEM(pastes);
 
 	if (!database_recents(pastes, &pastesz))
-		page(r, NULL, KHTTP_500, "pages/500.html");
+		page(r, NULL, KHTTP_500, "pages/500.html", "500");
 	else
 		page_index_render(r, pastes, pastesz);
 
@@ -88,7 +88,7 @@
 		.cb = template
 	};
 
-	page(r, &kt, KHTTP_200, "pages/index.html");
+	page(r, &kt, KHTTP_200, "pages/index.html", "Recent pastes");
 }
 
 void
@@ -99,7 +99,7 @@
 		get(r);
 		break;
 	default:
-		page(r, NULL, KHTTP_400, "400.html");
+		page(r, NULL, KHTTP_400, "400.html", "400");
 		break;
 	}
 }
--- a/page-new.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-new.c	Thu May 26 13:28:10 2022 +0200
@@ -148,7 +148,7 @@
 	}
 
 	if (!database_insert(&paste))
-		page(r, NULL, KHTTP_500, "500.html");
+		page(r, NULL, KHTTP_500, "500.html", "500");
 	else {
 		if (raw) {
 			/* For CLI users (e.g. paster) just print the location. */
@@ -184,7 +184,8 @@
 		.arg = &tp
 	};
 
-	page(r, &kt, KHTTP_200, "pages/new.html");
+	page(r, &kt, KHTTP_200, "pages/new.html",
+	    paste ? paste->title : "Create new paste");
 }
 
 void
--- a/page-paste.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-paste.c	Thu May 26 13:28:10 2022 +0200
@@ -99,7 +99,7 @@
 	};
 
 	if (!database_get(&paste, r->path))
-		page(r, NULL, KHTTP_404, "pages/404.html");
+		page(r, NULL, KHTTP_404, "pages/404.html", "404");
 	else {
 		const struct ktemplate kt = {
 			.key = keywords,
@@ -108,7 +108,7 @@
 			.arg = &data
 		};
 
-		page(r, &kt, KHTTP_200, "pages/paste.html");
+		page(r, &kt, KHTTP_200, "pages/paste.html", paste.title);
 		paste_finish(&paste);
 	}
 }
@@ -123,7 +123,7 @@
 		get(r);
 		break;
 	default:
-		page(r, NULL, KHTTP_400, "pages/400.html");
+		page(r, NULL, KHTTP_400, "pages/400.html", "400");
 		break;
 	}
 }
--- a/page-search.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-search.c	Thu May 26 13:28:10 2022 +0200
@@ -64,7 +64,7 @@
 		.arg = r
 	};
 
-	page(r, &kt, KHTTP_200, "pages/search.html");
+	page(r, &kt, KHTTP_200, "pages/search.html", "Search pastes");
 }
 
 static void
@@ -95,7 +95,7 @@
 		author = NULL;
 
 	if (!database_search(pastes, &pastesz, title, author, language))
-		page(r, NULL, KHTTP_500, "pages/500.html");
+		page(r, NULL, KHTTP_500, "pages/500.html", "500");
 	else
 		page_index_render(r, pastes, pastesz);
 
@@ -116,7 +116,7 @@
 		post(r);
 		break;
 	default:
-		page(r, NULL, KHTTP_400, "pages/400.html");
+		page(r, NULL, KHTTP_400, "pages/400.html", "400");
 		break;
 	}
 }
--- a/page-static.c	Thu May 26 13:09:33 2022 +0200
+++ b/page-static.c	Thu May 26 13:28:10 2022 +0200
@@ -36,7 +36,7 @@
 	snprintf(path, sizeof (path), "%s%s", config.themedir, req->fullpath);
 
 	if (stat(path, &st) < 0)
-		page(req, NULL, KHTTP_404, "pages/404.html");
+		page(req, NULL, KHTTP_404, "pages/404.html", "404");
 	else {
 		khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
 		khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[req->mime]);
@@ -58,7 +58,7 @@
 		get(r);
 		break;
 	default:
-		page(r, NULL, KHTTP_400, "pages/400.html");
+		page(r, NULL, KHTTP_400, "pages/400.html", "400");
 		break;
 	}
 }
--- a/page.c	Thu May 26 13:09:33 2022 +0200
+++ b/page.c	Thu May 26 13:28:10 2022 +0200
@@ -19,13 +19,49 @@
 #include "page.h"
 #include "util.h"
 
+struct template {
+	struct kreq *req;
+	const char *title;
+};
+
+static const char * const keywords[] = {
+	"title"
+};
+
+static int
+template(size_t keyword, void *arg)
+{
+	struct template *tp = arg;
+
+	switch (keyword) {
+	case 0:
+		khttp_printf(tp->req, "%s", tp->title);
+		break;
+	default:
+		break;
+	}
+
+	return 1;
+}
+
 void
-page(struct kreq *req, const struct ktemplate *tmpl, enum khttp status, const char *file)
+page(struct kreq *req, const struct ktemplate *tmpl, enum khttp status, const char *file, const char *title)
 {
+	struct template data = {
+		.req = req,
+		.title = title
+	};
+	struct ktemplate kt = {
+		.key = keywords,
+		.keysz = NELEM(keywords),
+		.arg = &data,
+		.cb = template
+	};
+
 	khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML]);
 	khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[status]);
 	khttp_body(req);
-	khttp_template(req, NULL, path("fragments/header.html"));
+	khttp_template(req, &kt, path("fragments/header.html"));
 	khttp_template(req, tmpl, path(file));
 	khttp_template(req, NULL, path("fragments/footer.html"));
 	khttp_free(req);
--- a/page.h	Thu May 26 13:09:33 2022 +0200
+++ b/page.h	Thu May 26 13:28:10 2022 +0200
@@ -25,6 +25,6 @@
 #include <kcgi.h>
 
 void
-page(struct kreq *, const struct ktemplate *, enum khttp, const char *);
+page(struct kreq *, const struct ktemplate *, enum khttp, const char *, const char *);
 
 #endif /* !PASTER_PAGE_H */
--- a/themes/mini/fragments/header.html	Thu May 26 13:09:33 2022 +0200
+++ b/themes/mini/fragments/header.html	Thu May 26 13:28:10 2022 +0200
@@ -4,6 +4,7 @@
 		<meta charset="UTF-8">
 		<link rel="stylesheet" href="/static/mini-default.min.css">
 		<meta name="viewport" content="width=device-width, initial-scale=1">
+		<title>@@title@@</title>
 		<style>
 		.container {
 			width: 80%;
--- a/themes/minimal/fragments/header.html	Thu May 26 13:09:33 2022 +0200
+++ b/themes/minimal/fragments/header.html	Thu May 26 13:28:10 2022 +0200
@@ -2,6 +2,7 @@
 <html>
 	<head>
 		<meta charset="UTF-8">
+		<title>@@title@@</title>
 	</head>
 	<body>
 
--- a/themes/siimple/fragments/header.html	Thu May 26 13:09:33 2022 +0200
+++ b/themes/siimple/fragments/header.html	Thu May 26 13:28:10 2022 +0200
@@ -2,6 +2,7 @@
 <html>
 	<head>
 		<meta charset="UTF-8">
+		<title>@@title@@</title>
 		<link rel="stylesheet" href="/static/siimple.min.css" />
 		<link rel="stylesheet" href="/static/styles/github.css" />
 		<script src="/static/highlight.min.js"></script>