From f961526240fa0dc77f6f94163a523697293395c2 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Fri, 10 Apr 2020 10:51:46 -0400 Subject: Rearrange HTML templating code --- html.c | 110 +++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/html.c b/html.c index f078b75..bc6035e 100644 --- a/html.c +++ b/html.c @@ -23,31 +23,40 @@ #include "archive.h" -static const char *Mailto = { - "mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]" -}; - -static const char *Summary = TEMPLATE( -
- -

[subject]

-
[from]
- -
-); - -static const char *Address = TEMPLATE( -
  • [name]
  • -); +static const char *addrName(struct Address addr) { + return (addr.name ? addr.name : addr.mailbox); +} -int htmlEnvelope(FILE *file, const struct Envelope *envelope) { - struct Variable fragmentVars[] = { - { "messageID", envelope->messageID }, +static int +htmlAddressList(FILE *file, const char *class, struct AddressList list) { + if (!list.len) return 0; + struct Variable vars[] = { + { "class", class }, {0}, }; - char *fragment = templateURL("#[messageID]", fragmentVars); + int error = templateRender( + file, TEMPLATE(), vars, escapeXML); +} - struct Variable mailtoVars[] = { +int htmlEnvelope(FILE *file, const struct Envelope *envelope) { + struct Variable urlVars[] = { { "mailbox", envelope->replyTo.mailbox }, { "host", envelope->replyTo.host }, { "re", (strncmp(envelope->subject, "Re: ", 4) ? "Re: " : "") }, @@ -55,55 +64,40 @@ int htmlEnvelope(FILE *file, const struct Envelope *envelope) { { "messageID", envelope->messageID }, {0}, }; - char *mailto = templateURL(Mailto, mailtoVars); - - const char *from = envelope->from.name; - if (!from) from = envelope->from.mailbox; - - char utc[sizeof("0000-00-00T00:00:00Z")]; - strftime(utc, sizeof(utc), "%FT%TZ", gmtime(&envelope->utc)); + char *fragment = templateURL("#[messageID]", urlVars); + char *mailto = templateURL( + "mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]", + urlVars + ); char date[256]; + char utc[sizeof("0000-00-00T00:00:00Z")]; strftime(date, sizeof(date), "%c %z", &envelope->date); - - struct Variable summaryVars[] = { + strftime(utc, sizeof(utc), "%FT%TZ", gmtime(&envelope->utc)); + struct Variable vars[] = { { "messageID", envelope->messageID }, { "fragment", fragment }, { "subject", envelope->subject }, { "mailto", mailto }, - { "from", from }, - { "utc", utc }, + { "from", addrName(envelope->from) }, { "date", date }, + { "utc", utc }, {0}, }; - int error = templateRender(file, Summary, summaryVars, escapeXML); + const char *Summary = TEMPLATE( +
    + +

    [subject]

    +
    [from]
    + +
    + ); + int error = templateRender(file, Summary, vars, escapeXML); free(fragment); free(mailto); if (error) return error; - if (0 > fprintf(file, "
    To:
      ")) return -1; - for (size_t i = 0; i < envelope->to.len; ++i) { - struct Address addr = envelope->to.addrs[i]; - struct Variable vars[] = { - { "class", "to" }, - { "name", (addr.name ? addr.name : addr.mailbox) }, - {0}, - }; - error = templateRender(file, Address, vars, escapeXML); - if (error) return error; - } - if (0 > fprintf(file, "
    Cc:
      ")) return -1; - for (size_t i = 0; i < envelope->cc.len; ++i) { - struct Address addr = envelope->cc.addrs[i]; - struct Variable vars[] = { - { "class", "cc" }, - { "name", (addr.name ? addr.name : addr.mailbox) }, - {0}, - }; - error = templateRender(file, Address, vars, escapeXML); - if (error) return error; - } - if (0 > fprintf(file, "
    ")) return -1; - - return 0; + return 0 + || htmlAddressList(file, "to", envelope->to) + || htmlAddressList(file, "cc", envelope->cc); } -- cgit 1.4.1