From d72dcfd4665f937eec2d325c10d35a644e9c06ad Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Wed, 15 Apr 2020 19:06:42 -0400 Subject: Rewrite HTML rendering --- html.c | 218 ++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 127 insertions(+), 91 deletions(-) (limited to 'html.c') diff --git a/html.c b/html.c index d6bf723..090bfab 100644 --- a/html.c +++ b/html.c @@ -24,161 +24,197 @@ #include "archive.h" static int htmlAddress(FILE *file, const char *class, struct Address addr) { - struct Variable vars[] = { - { "class", class }, - { "name", addressName(addr) }, - { "mailbox", addr.mailbox }, - {0}, - }; + const char *template; if (addr.host) { - return templateRender( - file, - TEMPLATE(
  • [name]
  • ), - vars, - escapeXML + template = TEMPLATE( +
  • [name]
  • ); } else if (addr.mailbox) { - return templateRender( - file, - TEMPLATE(
  • [mailbox]
      ), - vars, - escapeXML + template = TEMPLATE( +
    • +
      [mailbox]
      +
        ); } else { - return templateRender( - file, - TEMPLATE(
  • ), - vars, - escapeXML + template = TEMPLATE( + + ); } + struct Variable vars[] = { + { "class", class }, + { "name", addressName(addr) }, + { "mailbox", addr.mailbox }, + {0}, + }; + return templateRender(file, template, vars, escapeXML); } static int htmlAddressList(FILE *file, const char *class, struct AddressList list) { if (!list.len) return 0; + const char *template = TEMPLATE( + ), NULL, NULL); } -int htmlMessageHead(FILE *file, const struct Envelope *envelope) { - struct Variable urlVars[] = { - { "mailbox", envelope->replyTo.mailbox }, - { "host", envelope->replyTo.host }, +static char *htmlMailto(const struct Envelope *envelope) { + const char *template = { + "mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]" + }; + struct Variable vars[] = { + { "mailbox", envelope->from.mailbox }, + { "host", envelope->from.host }, { "re", (strncmp(envelope->subject, "Re: ", 4) ? "Re: " : "") }, { "subject", envelope->subject }, { "messageID", envelope->messageID }, - { "pathID", pathSafe(envelope->messageID) }, {0}, }; - char *fragment = templateURL("#[messageID]", urlVars); - char *mailto = templateURL( - "mailto:[mailbox]@[host]?subject=[re][subject]&In-Reply-To=[messageID]", - urlVars - ); - char *mbox = templateURL("../message/[pathID].mbox", urlVars); + return templateURL(template, vars); +} + +static char *htmlFragment(const struct Envelope *envelope) { + struct Variable vars[] = { + { "messageID", envelope->messageID }, + {0}, + }; + return templateURL("#[messageID]", vars); +} +static char *htmlMbox(const struct Envelope *envelope) { + struct Variable vars[] = { + { "name", pathSafe(envelope->messageID) }, + {0}, + }; + return templateURL("../message/[name].mbox", vars); +} + +int htmlMessageOpen(FILE *file, const struct Envelope *envelope) { + // TODO: Conditionally include mailto: link. + const char *template1 = TEMPLATE( +
    +
    +

    [subject]

    +
    + [from] +
    + + ); + const char *template2 = TEMPLATE( + +
    + ); + char *mailto = htmlMailto(envelope); char utc[sizeof("0000-00-00T00:00:00Z")]; strftime(utc, sizeof(utc), "%FT%TZ", gmtime(&envelope->time)); + char *fragment = htmlFragment(envelope); + char *mbox = htmlMbox(envelope); struct Variable vars[] = { { "messageID", envelope->messageID }, - { "fragment", fragment }, { "subject", envelope->subject }, { "mailto", mailto }, { "from", addressName(envelope->from) }, - { "date", envelope->date }, { "utc", utc }, + { "date", envelope->date }, + { "fragment", fragment }, { "mbox", mbox }, {0}, }; - const char *Summary = TEMPLATE( -
    - - [subject] -
    [from]
    - - mbox -
    - ); - int error = templateRender(file, Summary, vars, escapeXML); - free(fragment); + int error = 0 + || templateRender(file, template1, vars, escapeXML) + || htmlAddressList(file, "to", envelope->to) + || htmlAddressList(file, "cc", envelope->cc) + || templateRender(file, template2, vars, escapeXML); free(mailto); + free(fragment); free(mbox); - if (error) return error; - - return 0 - || htmlAddressList(file, "to", envelope->to) - || htmlAddressList(file, "cc", envelope->cc); + return error; } -int htmlMessageTail(FILE *file) { - int n = fprintf(file, "
    \n"); - return (n < 0 ? n : 0); +int htmlMessageClose(FILE *file) { + return templateRender(file, TEMPLATE(
    ), NULL, NULL); } -int htmlThreadHead(FILE *file, const struct Envelope *envelope) { - struct Variable urlVars[] = { - { "pathID", pathSafe(envelope->messageID) }, - {0}, - }; - char *path = templateURL("[pathID]", urlVars); +const char *htmlTitle; +static char *htmlThreadURL(const struct Envelope *envelope) { struct Variable vars[] = { - { "subject", envelope->subject }, - { "path", path }, + { "name", pathSafe(envelope->messageID) }, {0}, }; - const char *Head = TEMPLATE( + return templateURL("[name]", vars); +} + +int htmlThreadHead(FILE *file, const struct Envelope *envelope) { + const char *template = TEMPLATE( - [subject] - - - + [subject] · [title] + + ); - int error = templateRender(file, Head, vars, escapeXML); - free(path); + char *url = htmlThreadURL(envelope); + struct Variable vars[] = { + { "subject", envelope->subject }, + { "title", htmlTitle }, + { "url", url }, + {0}, + }; + int error = templateRender(file, template, vars, escapeXML); + free(url); return error; } -int htmlThreadHeader(FILE *file, const struct Envelope *envelope) { +int htmlThreadOpen(FILE *file, const struct Envelope *envelope) { + const char *template = TEMPLATE( +
    +

    [subject]

    + +
    +
    + ); + char *url = htmlThreadURL(envelope); struct Variable vars[] = { { "subject", envelope->subject }, + { "url", url }, {0}, }; - const char *Header = TEMPLATE( -

    [subject]

    - ); - return templateRender(file, Header, vars, escapeXML); + int error = templateRender(file, template, vars, escapeXML); + free(url); + return error; } -int htmlThreadOpen(FILE *file) { - int n = fprintf(file, TEMPLATE(
    )); - return (n < 0 ? n : 0); +int htmlSubthreadOpen(FILE *file) { + return templateRender( + file, TEMPLATE(
    ), NULL, NULL + ); } -int htmlThreadClose(FILE *file) { - int n = fprintf(file, TEMPLATE(
    )); - return (n < 0 ? n : 0); +int htmlSubthreadClose(FILE *file) { + return templateRender(file, TEMPLATE(
    ), NULL, NULL); } -int htmlThreadTail(FILE *file) { - return 0; +int htmlThreadClose(FILE *file) { + return templateRender(file, TEMPLATE(
    ), NULL, NULL); } -- cgit 1.4.1