diff options
-rw-r--r-- | html.c | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/html.c b/html.c index 25c9118..7fccc0a 100644 --- a/html.c +++ b/html.c @@ -189,6 +189,13 @@ int htmlMessageOpen(FILE *file, const struct Envelope *envelope) { return error; } +static void compile(regex_t *regex, const char *pattern) { + if (!regex->re_nsub) { + int error = regcomp(regex, pattern, REG_EXTENDED); + assert(!error); + } +} + static void swap(char *a, char *b) { char ch = *a; *a = *b; @@ -196,12 +203,8 @@ static void swap(char *a, char *b) { } static int htmlMarkupURLs(FILE *file, char *buf) { - static const char *Pattern = "(^|[[:space:]<])(https?:[^[:space:]>]+)(.|$)"; static regex_t regex; - if (!regex.re_nsub) { - int error = regcomp(®ex, Pattern, REG_EXTENDED); - assert(!error); - } + compile(®ex, "(^|[[:space:]<])(https?:[^[:space:]>]+)(.|$)"); int error; char *ptr; @@ -255,10 +258,6 @@ static int htmlMarkup(FILE *file, const char *content) { } memcpy(buf, content, len); buf[len] = '\0'; - struct Variable vars[] = { - { "line", buf }, - {0}, - }; if (!strcmp(buf, "---")) { patch = true; @@ -266,38 +265,35 @@ static int htmlMarkup(FILE *file, const char *content) { patch = false; } - static const char *Pattern = "^(diff|index|---|[+]{3}) "; - static regex_t regex; - if (!regex.re_nsub) { - error = regcomp(®ex, Pattern, REG_EXTENDED); - assert(!error); - } - if (patch && !regexec(®ex, buf, 0, NULL, 0)) { - error = templateRender( - file, Q(<span class="diff head">[line]</span>), vars, escapeXML - ); - } else if (patch && buf[0] == '@' && buf[1] == '@') { - error = templateRender( - file, Q(<span class="diff hunk">[line]</span>), vars, escapeXML - ); - } else if (patch && buf[0] == '-' && strcmp(buf, "---")) { - error = templateRender( - file, Q(<span class="diff old">[line]</span>), vars, escapeXML - ); - } else if (patch && buf[0] == '+') { - error = templateRender( - file, Q(<span class="diff new">[line]</span>), vars, escapeXML - ); - } else if (patch) { - error = escapeXML(file, buf); + 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(®ex, "^(diff|index|---|[+]{3}) "); + if (!regexec(®ex, buf, 0, NULL, 0)) { + vars[0].value = "head"; + } else if (!strncmp(buf, "@@", 2)) { + vars[0].value = "hunk"; + } else if (buf[0] == '-' && strcmp(buf, "---")) { + vars[0].value = "old"; + } else if (buf[0] == '+') { + vars[0].value = "new"; + } + error = templateRender(file, template, vars, escapeXML); } else if (buf[0] == '>') { error = htmlMarkupQuote(file, buf); } else { error = htmlMarkupURLs(file, buf); } - if (error) break; - error = templateRender(file, "\n", NULL, NULL); + error = error || templateRender(file, "\n", NULL, NULL); if (error) break; } free(buf); |