about summary refs log tree commit diff
path: root/html.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-04-15 20:25:26 -0400
committerJune McEnroe <june@causal.agency>2020-04-15 20:25:26 -0400
commit2c222f14103a1c3b6e29ad5fcd84e92594665dbe (patch)
treea7d4f6f829b954210a28740f8bb95f3ee6ff6c80 /html.c
parentRewrite HTML rendering (diff)
downloadbubger-2c222f14103a1c3b6e29ad5fcd84e92594665dbe.tar.gz
bubger-2c222f14103a1c3b6e29ad5fcd84e92594665dbe.zip
Wrap subthreads in <details> with reply count
Diffstat (limited to 'html.c')
-rw-r--r--html.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/html.c b/html.c
index 090bfab..352e229 100644
--- a/html.c
+++ b/html.c
@@ -205,14 +205,36 @@ int htmlThreadOpen(FILE *file, const struct Envelope *envelope) {
 	return error;
 }
 
-int htmlSubthreadOpen(FILE *file) {
-	return templateRender(
-		file, TEMPLATE(<section class="subthread">), NULL, NULL
+static size_t threadCount(struct List thread) {
+	size_t count = 0;
+	for (size_t i = 0; i < thread.len; ++i) {
+		if (thread.ptr[i].type == List) {
+			count += threadCount(thread.ptr[i].list);
+		} else {
+			count++;
+		}
+	}
+	return count;
+}
+
+int htmlSubthreadOpen(FILE *file, struct List thread) {
+	const char *template = TEMPLATE(
+		<details class="subthread" open>
+			<summary>[replies] repl[ies]</summary>
 	);
+	size_t count = threadCount(thread);
+	char replies[32];
+	snprintf(replies, sizeof(replies), "%zu", count);
+	struct Variable vars[] = {
+		{ "replies", replies },
+		{ "ies", (count > 1 ? "ies" : "y") },
+		{0},
+	};
+	return templateRender(file, template, vars, escapeXML);
 }
 
 int htmlSubthreadClose(FILE *file) {
-	return templateRender(file, TEMPLATE(</section>), NULL, NULL);
+	return templateRender(file, TEMPLATE(</details>), NULL, NULL);
 }
 
 int htmlThreadClose(FILE *file) {