summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-04-10 10:57:10 -0400
committerJune McEnroe <june@causal.agency>2020-04-10 10:57:10 -0400
commit6f1fc87b39335b4f60d2368413c6da7e732a0d97 (patch)
treeb9c836c20c8901f83e3353cae267114c019fa7a4
parentRearrange HTML templating code (diff)
downloadbubger-6f1fc87b39335b4f60d2368413c6da7e732a0d97.tar.gz
bubger-6f1fc87b39335b4f60d2368413c6da7e732a0d97.zip
Rearrange Atom templating code
-rw-r--r--archive.h4
-rw-r--r--atom.c33
-rw-r--r--html.c8
3 files changed, 20 insertions, 25 deletions
diff --git a/archive.h b/archive.h
index 540b898..cca5775 100644
--- a/archive.h
+++ b/archive.h
@@ -26,6 +26,10 @@ struct Address {
 	const char *host;
 };
 
+static inline const char *addressName(struct Address addr) {
+	return (addr.name ? addr.name : addr.mailbox);
+}
+
 struct AddressList {
 	size_t len;
 	struct Address *addrs;
diff --git a/atom.c b/atom.c
index 8991ca7..6692acc 100644
--- a/atom.c
+++ b/atom.c
@@ -22,40 +22,35 @@
 
 #include "archive.h"
 
-static const char *Envelope = TEMPLATE(
-	<entry>
-	<title>[subject]</title>
-	<author>
-		<name>[from.name]</name>
-		<email>[from.mailbox]@[from.host]</email>
-	</author>
-	<updated>[date]</updated>
-	<id>[id]</id>
-);
-
 int atomEnvelope(FILE *file, const struct Envelope *envelope) {
-	const char *from = envelope->from.name;
-	if (!from) from = envelope->from.mailbox;
-
-	char date[sizeof("0000-00-00T00:00:00Z")];
-	strftime(date, sizeof(date), "%FT%TZ", gmtime(&envelope->utc));
-
 	struct Variable idVars[] = {
 		{ "messageID", envelope->messageID },
 		{0},
 	};
 	char *id = templateURL("mailto:?In-Reply-To=[messageID]", idVars);
 
+	char date[sizeof("0000-00-00T00:00:00Z")];
+	strftime(date, sizeof(date), "%FT%TZ", gmtime(&envelope->utc));
 	struct Variable vars[] = {
 		{ "subject", envelope->subject },
-		{ "from.name", from },
+		{ "from.name", addressName(envelope->from) },
 		{ "from.mailbox", envelope->from.mailbox },
 		{ "from.host", envelope->from.host },
 		{ "date", date },
 		{ "id", id },
 		{0},
 	};
-	int error = templateRender(file, Envelope, vars, escapeXML);
+	const char *Entry = TEMPLATE(
+		<entry>
+		<title>[subject]</title>
+		<author>
+			<name>[from.name]</name>
+			<email>[from.mailbox]@[from.host]</email>
+		</author>
+		<updated>[date]</updated>
+		<id>[id]</id>
+	);
+	int error = templateRender(file, Entry, vars, escapeXML);
 	free(id);
 	return error;
 }
diff --git a/html.c b/html.c
index bc6035e..8aa872d 100644
--- a/html.c
+++ b/html.c
@@ -23,10 +23,6 @@
 
 #include "archive.h"
 
-static const char *addrName(struct Address addr) {
-	return (addr.name ? addr.name : addr.mailbox);
-}
-
 static int
 htmlAddressList(FILE *file, const char *class, struct AddressList list) {
 	if (!list.len) return 0;
@@ -41,7 +37,7 @@ htmlAddressList(FILE *file, const char *class, struct AddressList list) {
 	for (size_t i = 0; i < list.len; ++i) {
 		struct Variable vars[] = {
 			{ "class", class },
-			{ "name", addrName(list.addrs[i]) },
+			{ "name", addressName(list.addrs[i]) },
 			{0},
 		};
 		error = templateRender(
@@ -79,7 +75,7 @@ int htmlEnvelope(FILE *file, const struct Envelope *envelope) {
 		{ "fragment", fragment },
 		{ "subject", envelope->subject },
 		{ "mailto", mailto },
-		{ "from", addrName(envelope->from) },
+		{ "from", addressName(envelope->from) },
 		{ "date", date },
 		{ "utc", utc },
 		{0},