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 on SIGCHLDHerbert Xu 2018-05-03Release 0.5.10.Herbert Xu 2018-04-19eval: Variable assignments on functions are no longer persistentHerbert Xu 2018-04-19parser: Fix parameter expansion inside inner double quotesHerbert Xu 2018-04-19parser: Fix parsing of ${}Herbert Xu 2018-04-19man: correct typos, iff -> ifMartijn Dekker 2018-04-19expand: Do not quote backslashes in unquoted parameter expansionHerbert Xu 2018-04-19shell: Add subdir-objects to AM_INIT_AUTOMAKEJason Bowen 2018-04-19eval: Restore input files in evalcommandHerbert Xu 2018-04-19eval: Reap zombies after built-in commands and functionsHerbert Xu 2018-04-19redir: Fix typo in noclobber codeHerbert Xu 2018-04-19expand: Fix glibc glob(3) supportHerbert Xu 2018-04-02expand: Fix buffer overflow in expandmetaHerbert Xu 2018-04-02builtin: Move echo space/nl handling into print_escape_strHerbert Xu 2018-04-02builtin: Fix echo performance regressionHerbert Xu 2018-04-02expand: Fix ghost fields with unquoted $@/$*Herbert Xu 2018-04-02parser: Allow newlines within parameter substitutionHerbert Xu 2018-04-02expand: Fix bugs with words connected to the right of $@Herbert Xu 2018-03-25Revert "[BUILTIN] Remove unnecessary restoration of format string in printf"Herbert Xu 2018-03-22parser: Fix backquote support in here-document EOF markHerbert Xu