=================================================================== RCS file: /cvs/mandoc/html.c,v retrieving revision 1.4 retrieving revision 1.194 diff -u -p -r1.4 -r1.194 --- mandoc/html.c 2008/12/04 11:25:29 1.4 +++ mandoc/html.c 2017/01/17 01:47:51 1.194 @@ -1,450 +1,812 @@ -/* $Id: html.c,v 1.4 2008/12/04 11:25:29 kristaps Exp $ */ +/* $Id: html.c,v 1.194 2017/01/17 01:47:51 schwarze Exp $ */ /* - * Copyright (c) 2008 Kristaps Dzonsons + * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons + * Copyright (c) 2011-2015, 2017 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the - * above copyright notice and this permission notice appear in all - * copies. + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL - * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE - * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER - * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include +#include "config.h" +#include + #include -#include -#include -#include +#include +#include #include +#include +#include #include #include -#include "libmdocml.h" -#include "private.h" -#include "ml.h" +#include "mandoc.h" +#include "mandoc_aux.h" +#include "out.h" +#include "html.h" +#include "manconf.h" +#include "main.h" +struct htmldata { + const char *name; + int flags; +#define HTML_CLRLINE (1 << 0) +#define HTML_NOSTACK (1 << 1) +#define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */ +}; -static int html_loadcss(struct md_mbuf *, const char *); +static const struct htmldata htmltags[TAG_MAX] = { + {"html", HTML_CLRLINE}, /* TAG_HTML */ + {"head", HTML_CLRLINE}, /* TAG_HEAD */ + {"body", HTML_CLRLINE}, /* TAG_BODY */ + {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */ + {"title", HTML_CLRLINE}, /* TAG_TITLE */ + {"div", HTML_CLRLINE}, /* TAG_DIV */ + {"h1", 0}, /* TAG_H1 */ + {"h2", 0}, /* TAG_H2 */ + {"span", 0}, /* TAG_SPAN */ + {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */ + {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */ + {"a", 0}, /* TAG_A */ + {"table", HTML_CLRLINE}, /* TAG_TABLE */ + {"tbody", HTML_CLRLINE}, /* TAG_TBODY */ + {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */ + {"tr", HTML_CLRLINE}, /* TAG_TR */ + {"td", HTML_CLRLINE}, /* TAG_TD */ + {"li", HTML_CLRLINE}, /* TAG_LI */ + {"ul", HTML_CLRLINE}, /* TAG_UL */ + {"ol", HTML_CLRLINE}, /* TAG_OL */ + {"dl", HTML_CLRLINE}, /* TAG_DL */ + {"dt", HTML_CLRLINE}, /* TAG_DT */ + {"dd", HTML_CLRLINE}, /* TAG_DD */ + {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */ + {"pre", HTML_CLRLINE }, /* TAG_PRE */ + {"b", 0 }, /* TAG_B */ + {"i", 0 }, /* TAG_I */ + {"code", 0 }, /* TAG_CODE */ + {"small", 0 }, /* TAG_SMALL */ + {"style", HTML_CLRLINE}, /* TAG_STYLE */ + {"math", HTML_CLRLINE}, /* TAG_MATH */ + {"mrow", 0}, /* TAG_MROW */ + {"mi", 0}, /* TAG_MI */ + {"mo", 0}, /* TAG_MO */ + {"msup", 0}, /* TAG_MSUP */ + {"msub", 0}, /* TAG_MSUB */ + {"msubsup", 0}, /* TAG_MSUBSUP */ + {"mfrac", 0}, /* TAG_MFRAC */ + {"msqrt", 0}, /* TAG_MSQRT */ + {"mfenced", 0}, /* TAG_MFENCED */ + {"mtable", 0}, /* TAG_MTABLE */ + {"mtr", 0}, /* TAG_MTR */ + {"mtd", 0}, /* TAG_MTD */ + {"munderover", 0}, /* TAG_MUNDEROVER */ + {"munder", 0}, /* TAG_MUNDER*/ + {"mover", 0}, /* TAG_MOVER*/ +}; -static ssize_t html_endtag(struct md_mbuf *, - const struct md_args *, - enum md_ns, int); -static ssize_t html_begintag(struct md_mbuf *, - const struct md_args *, - enum md_ns, int, - const int *, const char **); -static int html_begin(struct md_mbuf *, - const struct md_args *, - const struct tm *, - const char *, const char *, - const char *, const char *); -static int html_end(struct md_mbuf *, - const struct md_args *); -static ssize_t html_blocktagname(struct md_mbuf *, - const struct md_args *, int); -static ssize_t html_blocktagargs(struct md_mbuf *, - const struct md_args *, int, - const int *, const char **); -static ssize_t html_blockheadtagname(struct md_mbuf *, - const struct md_args *, int); -static ssize_t html_blockheadtagargs(struct md_mbuf *, - const struct md_args *, int, - const int *, const char **); -static ssize_t html_blockbodytagname(struct md_mbuf *, - const struct md_args *, int); -static ssize_t html_blockbodytagargs(struct md_mbuf *, - const struct md_args *, int, - const int *, const char **); -static ssize_t html_inlinetagname(struct md_mbuf *, - const struct md_args *, int); -static ssize_t html_inlinetagargs(struct md_mbuf *, - const struct md_args *, int, - const int *, const char **); +static const char *const roffscales[SCALE_MAX] = { + "cm", /* SCALE_CM */ + "in", /* SCALE_IN */ + "pc", /* SCALE_PC */ + "pt", /* SCALE_PT */ + "em", /* SCALE_EM */ + "em", /* SCALE_MM */ + "ex", /* SCALE_EN */ + "ex", /* SCALE_BU */ + "em", /* SCALE_VS */ + "ex", /* SCALE_FS */ +}; +static void a2width(const char *, struct roffsu *); +static void bufncat(struct html *, const char *, size_t); +static void print_ctag(struct html *, struct tag *); +static int print_escape(char); +static int print_encode(struct html *, const char *, int); +static void print_metaf(struct html *, enum mandoc_esc); +static void print_attr(struct html *, const char *, const char *); -static int -html_loadcss(struct md_mbuf *mbuf, const char *css) + +void * +html_alloc(const struct manoutput *outopts) { - size_t res, bufsz; - char *buf; - struct stat st; - int fd, c; - ssize_t ssz; + struct html *h; - c = 0; - res = 0; - buf = NULL; + h = mandoc_calloc(1, sizeof(struct html)); - if (-1 == (fd = open(css, O_RDONLY, 0))) { - warn("%s", css); - return(0); - } - - if (-1 == fstat(fd, &st)) { - warn("%s", css); - goto out; - } + h->tags.head = NULL; + h->style = outopts->style; + h->base_man = outopts->man; + h->base_includes = outopts->includes; + if (outopts->fragment) + h->oflags |= HTML_FRAGMENT; - bufsz = MAX(st.st_blksize, BUFSIZ); - if (NULL == (buf = malloc(bufsz))) { - warn("malloc"); - goto out; - } + return h; +} - for (;;) { - if (-1 == (ssz = read(fd, buf, bufsz))) { - warn("%s", css); - goto out; - } else if (0 == ssz) - break; - if ( ! ml_nputs(mbuf, buf, (size_t)ssz, &res)) - goto out; - } +void +html_free(void *p) +{ + struct tag *tag; + struct html *h; - c = 1; + h = (struct html *)p; -out: - if (-1 == close(fd)) { - warn("%s", css); - c = 0; + while ((tag = h->tags.head) != NULL) { + h->tags.head = tag->next; + free(tag); } - if (buf) - free(buf); - - return(c); + free(h); } - -/* ARGSUSED */ -static int -html_begin(struct md_mbuf *mbuf, const struct md_args *args, - const struct tm *tm, const char *os, - const char *title, const char *section, - const char *vol) +void +print_gen_head(struct html *h) { - const char *preamble, *css, *trail; - char buf[512]; - size_t res; + struct tag *t; - preamble = - "\n" - "\n" - "\n" - " \n" - " \n" - " Manual Page for %s(%s)\n"; + print_otag(h, TAG_META, "?", "charset", "utf-8"); - css = - " \n"; - trail = - "\n" - "\n" - "
\n"; + /* + * Print a default style-sheet. + */ + t = print_otag(h, TAG_STYLE, ""); + print_text(h, "table.head, table.foot { width: 100%; }\n" + "td.head-rtitle, td.foot-os { text-align: right; }\n" + "td.head-vol { text-align: center; }\n" + "table.foot td { width: 50%; }\n" + "table.head td { width: 33%; }\n" + "div.spacer { margin: 1em 0; }\n"); + print_tagq(h, t); - res = 0; + if (h->style) + print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet", + h->style, "type", "text/css", "media", "all"); +} - (void)snprintf(buf, sizeof(buf) - 1, - preamble, title, section); +static void +print_metaf(struct html *h, enum mandoc_esc deco) +{ + enum htmlfont font; - if ( ! ml_puts(mbuf, buf, &res)) - return(0); + switch (deco) { + case ESCAPE_FONTPREV: + font = h->metal; + break; + case ESCAPE_FONTITALIC: + font = HTMLFONT_ITALIC; + break; + case ESCAPE_FONTBOLD: + font = HTMLFONT_BOLD; + break; + case ESCAPE_FONTBI: + font = HTMLFONT_BI; + break; + case ESCAPE_FONT: + case ESCAPE_FONTROMAN: + font = HTMLFONT_NONE; + break; + default: + abort(); + } - assert(args->params.html.css); - if (HTML_CSS_EMBED & args->params.html.flags) { - if ( ! ml_puts(mbuf, " \n", &res)) - return(0); - } else { - (void)snprintf(buf, sizeof(buf) - 1, css, - args->params.html.css); - if ( ! ml_puts(mbuf, buf, &res)) - return(0); + if (h->metaf) { + print_tagq(h, h->metaf); + h->metaf = NULL; } - if ( ! ml_puts(mbuf, trail, &res)) - return(0); + h->metal = h->metac; + h->metac = font; - return(1); + switch (font) { + case HTMLFONT_ITALIC: + h->metaf = print_otag(h, TAG_I, ""); + break; + case HTMLFONT_BOLD: + h->metaf = print_otag(h, TAG_B, ""); + break; + case HTMLFONT_BI: + h->metaf = print_otag(h, TAG_B, ""); + print_otag(h, TAG_I, ""); + break; + default: + break; + } } - -/* ARGSUSED */ -static int -html_end(struct md_mbuf *mbuf, const struct md_args *args) +int +html_strlen(const char *cp) { - size_t res; + size_t rsz; + int skip, sz; - res = 0; - if ( ! ml_puts(mbuf, "
\n", &res)) - return(0); + /* + * Account for escaped sequences within string length + * calculations. This follows the logic in term_strlen() as we + * must calculate the width of produced strings. + * Assume that characters are always width of "1". This is + * hacky, but it gets the job done for approximation of widths. + */ - return(1); + sz = 0; + skip = 0; + while (1) { + rsz = strcspn(cp, "\\"); + if (rsz) { + cp += rsz; + if (skip) { + skip = 0; + rsz--; + } + sz += rsz; + } + if ('\0' == *cp) + break; + cp++; + switch (mandoc_escape(&cp, NULL, NULL)) { + case ESCAPE_ERROR: + return sz; + case ESCAPE_UNICODE: + case ESCAPE_NUMBERED: + case ESCAPE_SPECIAL: + case ESCAPE_OVERSTRIKE: + if (skip) + skip = 0; + else + sz++; + break; + case ESCAPE_SKIPCHAR: + skip = 1; + break; + default: + break; + } + } + return sz; } +static int +print_escape(char c) +{ -/* ARGSUSED */ -static ssize_t -html_blockbodytagname(struct md_mbuf *mbuf, - const struct md_args *args, int tok) + switch (c) { + case '<': + printf("<"); + break; + case '>': + printf(">"); + break; + case '&': + printf("&"); + break; + case '"': + printf("""); + break; + case ASCII_NBRSP: + printf(" "); + break; + case ASCII_HYPH: + putchar('-'); + break; + case ASCII_BREAK: + break; + default: + return 0; + } + return 1; +} + +static int +print_encode(struct html *h, const char *p, int norecurse) { - size_t res; + size_t sz; + int c, len, nospace; + const char *seq; + enum mandoc_esc esc; + static const char rejs[9] = { '\\', '<', '>', '&', '"', + ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' }; - res = 0; - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); + nospace = 0; - return((ssize_t)res); -} + while ('\0' != *p) { + if (HTML_SKIPCHAR & h->flags && '\\' != *p) { + h->flags &= ~HTML_SKIPCHAR; + p++; + continue; + } + sz = strcspn(p, rejs); + fwrite(p, 1, sz, stdout); + p += (int)sz; + if ('\0' == *p) + break; -/* ARGSUSED */ -static ssize_t -html_blockheadtagname(struct md_mbuf *mbuf, - const struct md_args *args, int tok) -{ - size_t res; + if (print_escape(*p++)) + continue; - res = 0; - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); + esc = mandoc_escape(&p, &seq, &len); + if (ESCAPE_ERROR == esc) + break; - return((ssize_t)res); + switch (esc) { + case ESCAPE_FONT: + case ESCAPE_FONTPREV: + case ESCAPE_FONTBOLD: + case ESCAPE_FONTITALIC: + case ESCAPE_FONTBI: + case ESCAPE_FONTROMAN: + if (0 == norecurse) + print_metaf(h, esc); + continue; + case ESCAPE_SKIPCHAR: + h->flags |= HTML_SKIPCHAR; + continue; + default: + break; + } + + if (h->flags & HTML_SKIPCHAR) { + h->flags &= ~HTML_SKIPCHAR; + continue; + } + + switch (esc) { + case ESCAPE_UNICODE: + /* Skip past "u" header. */ + c = mchars_num2uc(seq + 1, len - 1); + break; + case ESCAPE_NUMBERED: + c = mchars_num2char(seq, len); + if (c < 0) + continue; + break; + case ESCAPE_SPECIAL: + c = mchars_spec2cp(seq, len); + if (c <= 0) + continue; + break; + case ESCAPE_NOSPACE: + if ('\0' == *p) + nospace = 1; + continue; + case ESCAPE_OVERSTRIKE: + if (len == 0) + continue; + c = seq[len - 1]; + break; + default: + continue; + } + if ((c < 0x20 && c != 0x09) || + (c > 0x7E && c < 0xA0)) + c = 0xFFFD; + if (c > 0x7E) + printf("&#%d;", c); + else if ( ! print_escape(c)) + putchar(c); + } + + return nospace; } +static void +print_attr(struct html *h, const char *key, const char *val) +{ + printf(" %s=\"", key); + (void)print_encode(h, val, 1); + putchar('\"'); +} -/* ARGSUSED */ -static ssize_t -html_blocktagname(struct md_mbuf *mbuf, - const struct md_args *args, int tok) +struct tag * +print_otag(struct html *h, enum htmltag tag, const char *fmt, ...) { - size_t res; + va_list ap; + struct roffsu mysu, *su; + struct tag *t; + char *s; + int i, have_style; - res = 0; - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); + /* Push this tags onto the stack of open scopes. */ - return((ssize_t)res); -} + if ( ! (HTML_NOSTACK & htmltags[tag].flags)) { + t = mandoc_malloc(sizeof(struct tag)); + t->tag = tag; + t->next = h->tags.head; + h->tags.head = t; + } else + t = NULL; + if ( ! (HTML_NOSPACE & h->flags)) + if ( ! (HTML_CLRLINE & htmltags[tag].flags)) { + /* Manage keeps! */ + if ( ! (HTML_KEEP & h->flags)) { + if (HTML_PREKEEP & h->flags) + h->flags |= HTML_KEEP; + putchar(' '); + } else + printf(" "); + } -/* ARGSUSED */ -static ssize_t -html_blockheadtagargs(struct md_mbuf *mbuf, const struct md_args *args, - int tok, const int *argc, const char **argv) -{ - size_t res; + if ( ! (h->flags & HTML_NONOSPACE)) + h->flags &= ~HTML_NOSPACE; + else + h->flags |= HTML_NOSPACE; - res = 0; + /* Print out the tag name and attributes. */ - if ( ! ml_puts(mbuf, " class=\"head-", &res)) - return(0); - if ( ! ml_puts(mbuf, toknames[tok], &res)) - return(0); - if ( ! ml_puts(mbuf, "\"", &res)) - return(0); + printf("<%s", htmltags[tag].name); - switch (tok) { - default: - break; + va_start(ap, fmt); + + have_style = 0; + while (*fmt != '\0') { + if (*fmt == 's') { + printf(" style=\""); + have_style = 1; + fmt++; + break; + } + s = va_arg(ap, char *); + switch (*fmt++) { + case 'c': + print_attr(h, "class", s); + break; + case 'h': + print_attr(h, "href", s); + break; + case 'i': + print_attr(h, "id", s); + break; + case '?': + print_attr(h, s, va_arg(ap, char *)); + break; + default: + abort(); + } } - return(0); -} + /* Print out styles. */ + s = NULL; + su = &mysu; + while (*fmt != '\0') { -/* ARGSUSED */ -static ssize_t -html_blockbodytagargs(struct md_mbuf *mbuf, const struct md_args *args, - int tok, const int *argc, const char **argv) -{ - size_t res; + /* First letter: input argument type. */ - res = 0; + switch (*fmt++) { + case 'h': + i = va_arg(ap, int); + SCALE_HS_INIT(su, i); + break; + case 's': + s = va_arg(ap, char *); + break; + case 'u': + su = va_arg(ap, struct roffsu *); + break; + case 'v': + i = va_arg(ap, int); + SCALE_VS_INIT(su, i); + break; + case 'w': + s = va_arg(ap, char *); + a2width(s, su); + break; + default: + abort(); + } - if ( ! ml_puts(mbuf, " class=\"body-", &res)) - return(0); - if ( ! ml_puts(mbuf, toknames[tok], &res)) - return(0); - if ( ! ml_puts(mbuf, "\"", &res)) - return(0); + /* Second letter: style name. */ - switch (tok) { - default: - break; + bufinit(h); + switch (*fmt++) { + case 'b': + bufcat_su(h, "margin-bottom", su); + break; + case 'h': + bufcat_su(h, "height", su); + break; + case 'i': + bufcat_su(h, "text-indent", su); + break; + case 'l': + bufcat_su(h, "margin-left", su); + break; + case 't': + bufcat_su(h, "margin-top", su); + break; + case 'w': + bufcat_su(h, "width", su); + break; + case 'W': + bufcat_su(h, "min-width", su); + break; + case '?': + bufcat_style(h, s, va_arg(ap, char *)); + break; + default: + abort(); + } + printf("%s", h->buf); } + if (have_style) + putchar('"'); - return(res); -} + va_end(ap); + /* Accommodate for "well-formed" singleton escaping. */ -/* ARGSUSED */ -static ssize_t -html_blocktagargs(struct md_mbuf *mbuf, const struct md_args *args, - int tok, const int *argc, const char **argv) -{ - size_t res; + if (HTML_AUTOCLOSE & htmltags[tag].flags) + putchar('/'); - res = 0; + putchar('>'); - if ( ! ml_puts(mbuf, " class=\"block-", &res)) - return(0); - if ( ! ml_puts(mbuf, toknames[tok], &res)) - return(0); - if ( ! ml_puts(mbuf, "\"", &res)) - return(0); + h->flags |= HTML_NOSPACE; - switch (tok) { - default: - break; - } + if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags) + putchar('\n'); - return(0); + return t; } - -/* ARGSUSED */ -static ssize_t -html_inlinetagargs(struct md_mbuf *mbuf, const struct md_args *args, - int tok, const int *argc, const char **argv) +static void +print_ctag(struct html *h, struct tag *tag) { - size_t res; - res = 0; + /* + * Remember to close out and nullify the current + * meta-font and table, if applicable. + */ + if (tag == h->metaf) + h->metaf = NULL; + if (tag == h->tblt) + h->tblt = NULL; - if ( ! ml_puts(mbuf, " class=\"inline-", &res)) - return(0); - if ( ! ml_puts(mbuf, toknames[tok], &res)) - return(0); - if ( ! ml_puts(mbuf, "\"", &res)) - return(0); - - - switch (tok) { - default: - break; + printf("", htmltags[tag->tag].name); + if (HTML_CLRLINE & htmltags[tag->tag].flags) { + h->flags |= HTML_NOSPACE; + putchar('\n'); } - return(0); + h->tags.head = tag->next; + free(tag); } +void +print_gen_decls(struct html *h) +{ -/* ARGSUSED */ -static ssize_t -html_inlinetagname(struct md_mbuf *mbuf, - const struct md_args *args, int tok) + puts(""); +} + +void +print_text(struct html *h, const char *word) { - size_t res; - res = 0; + if ( ! (HTML_NOSPACE & h->flags)) { + /* Manage keeps! */ + if ( ! (HTML_KEEP & h->flags)) { + if (HTML_PREKEEP & h->flags) + h->flags |= HTML_KEEP; + putchar(' '); + } else + printf(" "); + } - switch (tok) { - case (ROFF_Pp): - if ( ! ml_puts(mbuf, "div", &res)) - return(-1); + assert(NULL == h->metaf); + switch (h->metac) { + case HTMLFONT_ITALIC: + h->metaf = print_otag(h, TAG_I, ""); break; + case HTMLFONT_BOLD: + h->metaf = print_otag(h, TAG_B, ""); + break; + case HTMLFONT_BI: + h->metaf = print_otag(h, TAG_B, ""); + print_otag(h, TAG_I, ""); + break; default: - if ( ! ml_puts(mbuf, "span", &res)) - return(-1); break; } - return((ssize_t)res); + assert(word); + if ( ! print_encode(h, word, 0)) { + if ( ! (h->flags & HTML_NONOSPACE)) + h->flags &= ~HTML_NOSPACE; + h->flags &= ~HTML_NONEWLINE; + } else + h->flags |= HTML_NOSPACE | HTML_NONEWLINE; + + if (h->metaf) { + print_tagq(h, h->metaf); + h->metaf = NULL; + } + + h->flags &= ~HTML_IGNDELIM; } +void +print_tagq(struct html *h, const struct tag *until) +{ + struct tag *tag; -static ssize_t -html_begintag(struct md_mbuf *mbuf, const struct md_args *args, - enum md_ns ns, int tok, - const int *argc, const char **argv) + while ((tag = h->tags.head) != NULL) { + print_ctag(h, tag); + if (until && tag == until) + return; + } +} + +void +print_stagq(struct html *h, const struct tag *suntil) { + struct tag *tag; - assert(ns != MD_NS_DEFAULT); - switch (ns) { - case (MD_NS_BLOCK): - if ( ! html_blocktagname(mbuf, args, tok)) - return(0); - return(html_blocktagargs(mbuf, args, - tok, argc, argv)); - case (MD_NS_BODY): - if ( ! html_blockbodytagname(mbuf, args, tok)) - return(0); - return(html_blockbodytagargs(mbuf, args, - tok, argc, argv)); - case (MD_NS_HEAD): - if ( ! html_blockheadtagname(mbuf, args, tok)) - return(0); - return(html_blockheadtagargs(mbuf, args, - tok, argc, argv)); - default: - break; + while ((tag = h->tags.head) != NULL) { + if (suntil && tag == suntil) + return; + print_ctag(h, tag); } +} - if ( ! html_inlinetagname(mbuf, args, tok)) - return(0); - return(html_inlinetagargs(mbuf, args, tok, argc, argv)); +void +print_paragraph(struct html *h) +{ + struct tag *t; + + t = print_otag(h, TAG_DIV, "c", "spacer"); + print_tagq(h, t); } -static ssize_t -html_endtag(struct md_mbuf *mbuf, const struct md_args *args, - enum md_ns ns, int tok) +/* + * Calculate the scaling unit passed in a `-width' argument. This uses + * either a native scaling unit (e.g., 1i, 2m) or the string length of + * the value. + */ +static void +a2width(const char *p, struct roffsu *su) { + if (a2roffsu(p, su, SCALE_MAX) < 2) { + su->unit = SCALE_EN; + su->scale = html_strlen(p); + } else if (su->scale < 0.0) + su->scale = 0.0; +} - assert(ns != MD_NS_DEFAULT); - switch (ns) { - case (MD_NS_BLOCK): - return(html_blocktagname(mbuf, args, tok)); - case (MD_NS_BODY): - return(html_blockbodytagname(mbuf, args, tok)); - case (MD_NS_HEAD): - return(html_blockheadtagname(mbuf, args, tok)); - default: - break; - } +void +bufinit(struct html *h) +{ - return(html_inlinetagname(mbuf, args, tok)); + h->buf[0] = '\0'; + h->buflen = 0; } +void +bufcat_style(struct html *h, const char *key, const char *val) +{ -int -md_line_html(void *data, char *buf) + bufcat(h, key); + bufcat(h, ":"); + bufcat(h, val); + bufcat(h, ";"); +} + +void +bufcat(struct html *h, const char *p) { - return(mlg_line((struct md_mlg *)data, buf)); + /* + * XXX This is broken and not easy to fix. + * When using the -Oincludes option, buffmt_includes() + * may pass in strings overrunning BUFSIZ, causing a crash. + */ + + h->buflen = strlcat(h->buf, p, BUFSIZ); + assert(h->buflen < BUFSIZ); } +void +bufcat_fmt(struct html *h, const char *fmt, ...) +{ + va_list ap; -int -md_exit_html(void *data, int flush) + va_start(ap, fmt); + (void)vsnprintf(h->buf + (int)h->buflen, + BUFSIZ - h->buflen - 1, fmt, ap); + va_end(ap); + h->buflen = strlen(h->buf); +} + +static void +bufncat(struct html *h, const char *p, size_t sz) { - return(mlg_exit((struct md_mlg *)data, flush)); + assert(h->buflen + sz + 1 < BUFSIZ); + strncat(h->buf, p, sz); + h->buflen += sz; } +void +buffmt_includes(struct html *h, const char *name) +{ + const char *p, *pp; -void * -md_init_html(const struct md_args *args, - struct md_mbuf *mbuf, const struct md_rbuf *rbuf) + pp = h->base_includes; + + bufinit(h); + while (NULL != (p = strchr(pp, '%'))) { + bufncat(h, pp, (size_t)(p - pp)); + switch (*(p + 1)) { + case 'I': + bufcat(h, name); + break; + default: + bufncat(h, p, 2); + break; + } + pp = p + 2; + } + if (pp) + bufcat(h, pp); +} + +void +buffmt_man(struct html *h, const char *name, const char *sec) { + const char *p, *pp; - return(mlg_alloc(args, rbuf, mbuf, html_begintag, - html_endtag, html_begin, html_end)); + pp = h->base_man; + + bufinit(h); + while (NULL != (p = strchr(pp, '%'))) { + bufncat(h, pp, (size_t)(p - pp)); + switch (*(p + 1)) { + case 'S': + bufcat(h, sec ? sec : "1"); + break; + case 'N': + bufcat_fmt(h, "%s", name); + break; + default: + bufncat(h, p, 2); + break; + } + pp = p + 2; + } + if (pp) + bufcat(h, pp); } +void +bufcat_su(struct html *h, const char *p, const struct roffsu *su) +{ + double v; + + v = su->scale; + if (SCALE_MM == su->unit && 0.0 == (v /= 100.0)) + v = 1.0; + else if (SCALE_BU == su->unit) + v /= 24.0; + + bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]); +} + +void +bufcat_id(struct html *h, const char *src) +{ + + /* Cf. . */ + + for (; '\0' != *src; src++) + bufncat(h, *src == ' ' ? "_" : src, 1); +}