about summary refs log tree commit diff
diff options
context:
space:
mode:
-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);