From 814fdc536984fa200b316fdd5444c11121a6182b Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 18 Jan 2021 18:10:27 -0500 Subject: 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. --- html.c | 43 +++++++++++++++++++++---------------------- 1 file 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][-][line][+class][-] - ); - struct Variable vars[] = { - { "class", NULL }, - { "line", buf }, - {0}, - }; - - if (patch) { - static regex_t regex; - compile(®ex, "^(diff|index|---|[+]{3}) "); - if (!regexec(®ex, buf, 0, NULL, 0)) { - vars[0].value = "head"; - } else if (!strncmp(buf, "@@", 2)) { + if (state == Diff) { + const char *template = Q( + [line] + ); + 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); -- cgit 1.4.1