summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-18 18:10:27 -0500
committerJune McEnroe <june@causal.agency>2021-01-18 18:10:27 -0500
commit814fdc536984fa200b316fdd5444c11121a6182b (patch)
tree50823e3d6e1fa682526100c8eaf6d6d5551fc60d
parentAlphabetize STANDARDS section (diff)
downloadbubger-814fdc536984fa200b316fdd5444c11121a6182b.tar.gz
bubger-814fdc536984fa200b316fdd5444c11121a6182b.zip
Match any non-context diff lines as "head"
The explicit state between Patch and Diff also allows applying markup to
the diffstat in the future, perhaps.
-rw-r--r--html.c43
1 files changed, 21 insertions, 22 deletions
diff --git a/html.c b/html.c
index 75c2b7a..fe96a37 100644
--- a/html.c
+++ b/html.c
@@ -248,7 +248,7 @@ static int htmlMarkup(FILE *file, const char *content) {
 	int error = 0;
 	size_t cap = 0;
 	char *buf = NULL;
-	bool patch = false;
+	enum { Init, Patch, Diff } state = Init;
 	while (*content) {
 		size_t len = strcspn(content, "\n");
 		if (cap < len + 1) {
@@ -259,31 +259,30 @@ static int htmlMarkup(FILE *file, const char *content) {
 		memcpy(buf, content, len);
 		buf[len] = '\0';
 
-		if (!strcmp(buf, "---")) {
-			patch = true;
-		} else if (patch && !strcmp(buf, "-- ")) {
-			patch = false;
+		if (state == Init && !strcmp(buf, "---")) {
+			state = Patch;
+		} else if (state == Patch && !strncmp(buf, "diff", 4)) {
+			state = Diff;
+		} else if (!strcmp(buf, "-- ")) {
+			state = Init;
 		}
 
-		const char *template = Q(
-			[+class]<span class="diff [class]">[-][line][+class]</span>[-]
-		);
-		struct Variable vars[] = {
-			{ "class", NULL },
-			{ "line", buf },
-			{0},
-		};
-
-		if (patch) {
-			static regex_t regex;
-			compile(&regex, "^(diff|index|---|[+]{3}) ");
-			if (!regexec(&regex, buf, 0, NULL, 0)) {
-				vars[0].value = "head";
-			} else if (!strncmp(buf, "@@", 2)) {
+		if (state == Diff) {
+			const char *template = Q(
+				<span class="diff [class]">[line]</span>
+			);
+			struct Variable vars[] = {
+				{ "class", "head" },
+				{ "line", buf },
+				{0},
+			};
+			if (buf[0] == '@') {
 				vars[0].value = "hunk";
-			} else if (buf[0] == '-' && strcmp(buf, "---")) {
+			} else if (buf[0] == ' ') {
+				vars[0].value = "ctx";
+			} else if (buf[0] == '-' && strncmp(buf, "---", 3)) {
 				vars[0].value = "old";
-			} else if (buf[0] == '+') {
+			} else if (buf[0] == '+' && strncmp(buf, "+++", 3)) {
 				vars[0].value = "new";
 			}
 			error = templateRender(file, template, vars, escapeXML);