[BACK]Return to html.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/html.c, Revision 1.55

1.55    ! schwarze    1: /*     $OpenBSD: html.c,v 1.54 2014/12/20 00:19:54 schwarze Exp $ */
1.1       schwarze    2: /*
1.42      schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.55    ! schwarze    4:  * Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include <sys/types.h>
                     19:
                     20: #include <assert.h>
1.3       schwarze   21: #include <ctype.h>
1.4       schwarze   22: #include <stdarg.h>
1.1       schwarze   23: #include <stdio.h>
                     24: #include <stdint.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
1.9       schwarze   29: #include "mandoc.h"
1.34      schwarze   30: #include "mandoc_aux.h"
1.1       schwarze   31: #include "out.h"
                     32: #include "html.h"
                     33: #include "main.h"
                     34:
                     35: struct htmldata {
                     36:        const char       *name;
                     37:        int               flags;
                     38: #define        HTML_CLRLINE     (1 << 0)
                     39: #define        HTML_NOSTACK     (1 << 1)
1.6       schwarze   40: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.1       schwarze   41: };
                     42:
                     43: static const struct htmldata htmltags[TAG_MAX] = {
                     44:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     45:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     46:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.6       schwarze   47:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1       schwarze   48:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
                     49:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
                     50:        {"h1",          0}, /* TAG_H1 */
                     51:        {"h2",          0}, /* TAG_H2 */
                     52:        {"span",        0}, /* TAG_SPAN */
1.8       schwarze   53:        {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6       schwarze   54:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1       schwarze   55:        {"a",           0}, /* TAG_A */
                     56:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
1.18      schwarze   57:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.6       schwarze   58:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1       schwarze   59:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     60:        {"td",          HTML_CLRLINE}, /* TAG_TD */
                     61:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     62:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     63:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.18      schwarze   64:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
                     65:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
                     66:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
                     67:        {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
                     68:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.19      schwarze   69:        {"b",           0 }, /* TAG_B */
                     70:        {"i",           0 }, /* TAG_I */
1.20      schwarze   71:        {"code",        0 }, /* TAG_CODE */
                     72:        {"small",       0 }, /* TAG_SMALL */
1.42      schwarze   73:        {"style",       HTML_CLRLINE}, /* TAG_STYLE */
1.43      schwarze   74:        {"math",        HTML_CLRLINE}, /* TAG_MATH */
                     75:        {"mrow",        0}, /* TAG_MROW */
                     76:        {"mi",          0}, /* TAG_MI */
                     77:        {"mo",          0}, /* TAG_MO */
                     78:        {"msup",        0}, /* TAG_MSUP */
                     79:        {"msub",        0}, /* TAG_MSUB */
                     80:        {"msubsup",     0}, /* TAG_MSUBSUP */
                     81:        {"mfrac",       0}, /* TAG_MFRAC */
                     82:        {"msqrt",       0}, /* TAG_MSQRT */
                     83:        {"mfenced",     0}, /* TAG_MFENCED */
                     84:        {"mtable",      0}, /* TAG_MTABLE */
                     85:        {"mtr",         0}, /* TAG_MTR */
                     86:        {"mtd",         0}, /* TAG_MTD */
1.44      schwarze   87:        {"munderover",  0}, /* TAG_MUNDEROVER */
                     88:        {"munder",      0}, /* TAG_MUNDER*/
                     89:        {"mover",       0}, /* TAG_MOVER*/
1.5       schwarze   90: };
                     91:
                     92: static const char      *const htmlattrs[ATTR_MAX] = {
1.19      schwarze   93:        "name", /* ATTR_NAME */
                     94:        "rel", /* ATTR_REL */
                     95:        "href", /* ATTR_HREF */
                     96:        "type", /* ATTR_TYPE */
                     97:        "media", /* ATTR_MEDIA */
                     98:        "class", /* ATTR_CLASS */
                     99:        "style", /* ATTR_STYLE */
                    100:        "id", /* ATTR_ID */
1.22      schwarze  101:        "colspan", /* ATTR_COLSPAN */
1.42      schwarze  102:        "charset", /* ATTR_CHARSET */
1.43      schwarze  103:        "open", /* ATTR_OPEN */
                    104:        "close", /* ATTR_CLOSE */
1.45      schwarze  105:        "mathvariant", /* ATTR_MATHVARIANT */
1.1       schwarze  106: };
                    107:
1.26      schwarze  108: static const char      *const roffscales[SCALE_MAX] = {
                    109:        "cm", /* SCALE_CM */
                    110:        "in", /* SCALE_IN */
                    111:        "pc", /* SCALE_PC */
                    112:        "pt", /* SCALE_PT */
                    113:        "em", /* SCALE_EM */
                    114:        "em", /* SCALE_MM */
                    115:        "ex", /* SCALE_EN */
                    116:        "ex", /* SCALE_BU */
                    117:        "em", /* SCALE_VS */
                    118:        "ex", /* SCALE_FS */
                    119: };
1.5       schwarze  120:
1.26      schwarze  121: static void     bufncat(struct html *, const char *, size_t);
1.54      schwarze  122: static void     print_ctag(struct html *, struct tag *);
1.38      schwarze  123: static int      print_escape(char);
1.26      schwarze  124: static int      print_encode(struct html *, const char *, int);
                    125: static void     print_metaf(struct html *, enum mandoc_esc);
                    126: static void     print_attr(struct html *, const char *, const char *);
1.5       schwarze  127:
1.35      schwarze  128:
1.50      schwarze  129: void *
                    130: html_alloc(const struct mchars *mchars, char *outopts)
1.1       schwarze  131: {
                    132:        struct html     *h;
1.29      schwarze  133:        const char      *toks[5];
1.1       schwarze  134:        char            *v;
                    135:
                    136:        toks[0] = "style";
                    137:        toks[1] = "man";
                    138:        toks[2] = "includes";
1.29      schwarze  139:        toks[3] = "fragment";
                    140:        toks[4] = NULL;
1.1       schwarze  141:
1.24      schwarze  142:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  143:
1.2       schwarze  144:        h->tags.head = NULL;
1.50      schwarze  145:        h->symtab = mchars;
1.1       schwarze  146:
                    147:        while (outopts && *outopts)
                    148:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.35      schwarze  149:                case 0:
1.1       schwarze  150:                        h->style = v;
                    151:                        break;
1.35      schwarze  152:                case 1:
1.1       schwarze  153:                        h->base_man = v;
                    154:                        break;
1.35      schwarze  155:                case 2:
1.1       schwarze  156:                        h->base_includes = v;
1.29      schwarze  157:                        break;
1.35      schwarze  158:                case 3:
1.29      schwarze  159:                        h->oflags |= HTML_FRAGMENT;
1.1       schwarze  160:                        break;
                    161:                default:
                    162:                        break;
                    163:                }
                    164:
                    165:        return(h);
                    166: }
                    167:
                    168: void
                    169: html_free(void *p)
                    170: {
                    171:        struct tag      *tag;
                    172:        struct html     *h;
                    173:
                    174:        h = (struct html *)p;
                    175:
1.2       schwarze  176:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  177:                h->tags.head = tag->next;
1.1       schwarze  178:                free(tag);
                    179:        }
                    180:
                    181:        free(h);
                    182: }
                    183:
                    184: void
                    185: print_gen_head(struct html *h)
                    186: {
                    187:        struct htmlpair  tag[4];
1.42      schwarze  188:        struct tag      *t;
                    189:
                    190:        tag[0].key = ATTR_CHARSET;
                    191:        tag[0].val = "utf-8";
                    192:        print_otag(h, TAG_META, 1, tag);
1.1       schwarze  193:
1.42      schwarze  194:        /*
                    195:         * Print a default style-sheet.
                    196:         */
                    197:        t = print_otag(h, TAG_STYLE, 0, NULL);
                    198:        print_text(h, "table.head, table.foot { width: 100%; }\n"
                    199:              "td.head-rtitle, td.foot-os { text-align: right; }\n"
                    200:              "td.head-vol { text-align: center; }\n"
                    201:              "table.foot td { width: 50%; }\n"
                    202:              "table.head td { width: 33%; }\n"
                    203:              "div.spacer { margin: 1em 0; }\n");
                    204:        print_tagq(h, t);
1.1       schwarze  205:
                    206:        if (h->style) {
                    207:                tag[0].key = ATTR_REL;
                    208:                tag[0].val = "stylesheet";
                    209:                tag[1].key = ATTR_HREF;
                    210:                tag[1].val = h->style;
                    211:                tag[2].key = ATTR_TYPE;
                    212:                tag[2].val = "text/css";
                    213:                tag[3].key = ATTR_MEDIA;
                    214:                tag[3].val = "all";
                    215:                print_otag(h, TAG_LINK, 4, tag);
                    216:        }
                    217: }
                    218:
1.5       schwarze  219: static void
1.26      schwarze  220: print_metaf(struct html *h, enum mandoc_esc deco)
1.5       schwarze  221: {
                    222:        enum htmlfont    font;
1.1       schwarze  223:
1.5       schwarze  224:        switch (deco) {
1.35      schwarze  225:        case ESCAPE_FONTPREV:
1.5       schwarze  226:                font = h->metal;
                    227:                break;
1.35      schwarze  228:        case ESCAPE_FONTITALIC:
1.5       schwarze  229:                font = HTMLFONT_ITALIC;
                    230:                break;
1.35      schwarze  231:        case ESCAPE_FONTBOLD:
1.5       schwarze  232:                font = HTMLFONT_BOLD;
                    233:                break;
1.35      schwarze  234:        case ESCAPE_FONTBI:
1.31      schwarze  235:                font = HTMLFONT_BI;
                    236:                break;
1.35      schwarze  237:        case ESCAPE_FONT:
1.26      schwarze  238:                /* FALLTHROUGH */
1.35      schwarze  239:        case ESCAPE_FONTROMAN:
1.5       schwarze  240:                font = HTMLFONT_NONE;
                    241:                break;
                    242:        default:
                    243:                abort();
                    244:                /* NOTREACHED */
1.1       schwarze  245:        }
                    246:
1.20      schwarze  247:        if (h->metaf) {
                    248:                print_tagq(h, h->metaf);
                    249:                h->metaf = NULL;
                    250:        }
                    251:
                    252:        h->metal = h->metac;
                    253:        h->metac = font;
                    254:
1.31      schwarze  255:        switch (font) {
1.35      schwarze  256:        case HTMLFONT_ITALIC:
1.31      schwarze  257:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    258:                break;
1.35      schwarze  259:        case HTMLFONT_BOLD:
1.31      schwarze  260:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    261:                break;
1.35      schwarze  262:        case HTMLFONT_BI:
1.31      schwarze  263:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    264:                print_otag(h, TAG_I, 0, NULL);
                    265:                break;
                    266:        default:
                    267:                break;
                    268:        }
1.1       schwarze  269: }
                    270:
1.26      schwarze  271: int
                    272: html_strlen(const char *cp)
                    273: {
1.30      schwarze  274:        size_t           rsz;
                    275:        int              skip, sz;
1.26      schwarze  276:
                    277:        /*
                    278:         * Account for escaped sequences within string length
                    279:         * calculations.  This follows the logic in term_strlen() as we
                    280:         * must calculate the width of produced strings.
                    281:         * Assume that characters are always width of "1".  This is
                    282:         * hacky, but it gets the job done for approximation of widths.
                    283:         */
                    284:
                    285:        sz = 0;
1.30      schwarze  286:        skip = 0;
                    287:        while (1) {
                    288:                rsz = strcspn(cp, "\\");
                    289:                if (rsz) {
                    290:                        cp += rsz;
                    291:                        if (skip) {
                    292:                                skip = 0;
                    293:                                rsz--;
                    294:                        }
                    295:                        sz += rsz;
                    296:                }
                    297:                if ('\0' == *cp)
                    298:                        break;
                    299:                cp++;
                    300:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.35      schwarze  301:                case ESCAPE_ERROR:
1.26      schwarze  302:                        return(sz);
1.35      schwarze  303:                case ESCAPE_UNICODE:
1.26      schwarze  304:                        /* FALLTHROUGH */
1.35      schwarze  305:                case ESCAPE_NUMBERED:
1.26      schwarze  306:                        /* FALLTHROUGH */
1.35      schwarze  307:                case ESCAPE_SPECIAL:
1.55    ! schwarze  308:                        /* FALLTHROUGH */
        !           309:                case ESCAPE_OVERSTRIKE:
1.30      schwarze  310:                        if (skip)
                    311:                                skip = 0;
                    312:                        else
                    313:                                sz++;
                    314:                        break;
1.35      schwarze  315:                case ESCAPE_SKIPCHAR:
1.30      schwarze  316:                        skip = 1;
1.26      schwarze  317:                        break;
                    318:                default:
                    319:                        break;
                    320:                }
                    321:        }
1.30      schwarze  322:        return(sz);
1.26      schwarze  323: }
1.1       schwarze  324:
1.5       schwarze  325: static int
1.38      schwarze  326: print_escape(char c)
                    327: {
                    328:
                    329:        switch (c) {
                    330:        case '<':
                    331:                printf("&lt;");
                    332:                break;
                    333:        case '>':
                    334:                printf("&gt;");
                    335:                break;
                    336:        case '&':
                    337:                printf("&amp;");
                    338:                break;
                    339:        case '"':
                    340:                printf("&quot;");
                    341:                break;
                    342:        case ASCII_NBRSP:
                    343:                putchar('-');
                    344:                break;
                    345:        case ASCII_HYPH:
                    346:                putchar('-');
                    347:                /* FALLTHROUGH */
                    348:        case ASCII_BREAK:
                    349:                break;
                    350:        default:
                    351:                return(0);
                    352:        }
                    353:        return(1);
                    354: }
                    355:
                    356: static int
1.5       schwarze  357: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  358: {
1.4       schwarze  359:        size_t           sz;
1.26      schwarze  360:        int              c, len, nospace;
1.5       schwarze  361:        const char      *seq;
1.26      schwarze  362:        enum mandoc_esc  esc;
1.37      schwarze  363:        static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33      schwarze  364:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  365:
                    366:        nospace = 0;
1.1       schwarze  367:
1.26      schwarze  368:        while ('\0' != *p) {
1.30      schwarze  369:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    370:                        h->flags &= ~HTML_SKIPCHAR;
                    371:                        p++;
                    372:                        continue;
                    373:                }
                    374:
1.9       schwarze  375:                sz = strcspn(p, rejs);
1.4       schwarze  376:
                    377:                fwrite(p, 1, sz, stdout);
1.26      schwarze  378:                p += (int)sz;
1.4       schwarze  379:
1.26      schwarze  380:                if ('\0' == *p)
                    381:                        break;
                    382:
1.38      schwarze  383:                if (print_escape(*p++))
1.33      schwarze  384:                        continue;
1.4       schwarze  385:
1.26      schwarze  386:                esc = mandoc_escape(&p, &seq, &len);
                    387:                if (ESCAPE_ERROR == esc)
                    388:                        break;
1.5       schwarze  389:
1.26      schwarze  390:                switch (esc) {
1.35      schwarze  391:                case ESCAPE_FONT:
1.30      schwarze  392:                        /* FALLTHROUGH */
1.35      schwarze  393:                case ESCAPE_FONTPREV:
1.30      schwarze  394:                        /* FALLTHROUGH */
1.35      schwarze  395:                case ESCAPE_FONTBOLD:
1.30      schwarze  396:                        /* FALLTHROUGH */
1.35      schwarze  397:                case ESCAPE_FONTITALIC:
1.30      schwarze  398:                        /* FALLTHROUGH */
1.35      schwarze  399:                case ESCAPE_FONTBI:
1.31      schwarze  400:                        /* FALLTHROUGH */
1.35      schwarze  401:                case ESCAPE_FONTROMAN:
1.30      schwarze  402:                        if (0 == norecurse)
                    403:                                print_metaf(h, esc);
                    404:                        continue;
1.35      schwarze  405:                case ESCAPE_SKIPCHAR:
1.30      schwarze  406:                        h->flags |= HTML_SKIPCHAR;
                    407:                        continue;
                    408:                default:
                    409:                        break;
                    410:                }
                    411:
                    412:                if (h->flags & HTML_SKIPCHAR) {
                    413:                        h->flags &= ~HTML_SKIPCHAR;
                    414:                        continue;
                    415:                }
                    416:
                    417:                switch (esc) {
1.35      schwarze  418:                case ESCAPE_UNICODE:
1.38      schwarze  419:                        /* Skip past "u" header. */
1.26      schwarze  420:                        c = mchars_num2uc(seq + 1, len - 1);
                    421:                        break;
1.35      schwarze  422:                case ESCAPE_NUMBERED:
1.26      schwarze  423:                        c = mchars_num2char(seq, len);
1.51      schwarze  424:                        if (c < 0)
                    425:                                continue;
1.26      schwarze  426:                        break;
1.35      schwarze  427:                case ESCAPE_SPECIAL:
1.26      schwarze  428:                        c = mchars_spec2cp(h->symtab, seq, len);
1.51      schwarze  429:                        if (c <= 0)
                    430:                                continue;
1.26      schwarze  431:                        break;
1.35      schwarze  432:                case ESCAPE_NOSPACE:
1.26      schwarze  433:                        if ('\0' == *p)
                    434:                                nospace = 1;
1.49      schwarze  435:                        continue;
1.55    ! schwarze  436:                case ESCAPE_OVERSTRIKE:
        !           437:                        if (len == 0)
        !           438:                                continue;
        !           439:                        c = seq[len - 1];
        !           440:                        break;
1.5       schwarze  441:                default:
1.49      schwarze  442:                        continue;
1.5       schwarze  443:                }
1.51      schwarze  444:                if ((c < 0x20 && c != 0x09) ||
                    445:                    (c > 0x7E && c < 0xA0))
1.49      schwarze  446:                        c = 0xFFFD;
                    447:                if (c > 0x7E)
                    448:                        printf("&#%d;", c);
                    449:                else if ( ! print_escape(c))
                    450:                        putchar(c);
1.1       schwarze  451:        }
1.5       schwarze  452:
                    453:        return(nospace);
1.1       schwarze  454: }
                    455:
1.6       schwarze  456: static void
                    457: print_attr(struct html *h, const char *key, const char *val)
                    458: {
                    459:        printf(" %s=\"", key);
                    460:        (void)print_encode(h, val, 1);
                    461:        putchar('\"');
                    462: }
                    463:
1.1       schwarze  464: struct tag *
1.35      schwarze  465: print_otag(struct html *h, enum htmltag tag,
1.1       schwarze  466:                int sz, const struct htmlpair *p)
                    467: {
                    468:        int              i;
                    469:        struct tag      *t;
                    470:
1.6       schwarze  471:        /* Push this tags onto the stack of open scopes. */
                    472:
1.1       schwarze  473:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24      schwarze  474:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  475:                t->tag = tag;
1.2       schwarze  476:                t->next = h->tags.head;
                    477:                h->tags.head = t;
1.1       schwarze  478:        } else
                    479:                t = NULL;
                    480:
                    481:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  482:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    483:                        /* Manage keeps! */
                    484:                        if ( ! (HTML_KEEP & h->flags)) {
                    485:                                if (HTML_PREKEEP & h->flags)
                    486:                                        h->flags |= HTML_KEEP;
                    487:                                putchar(' ');
                    488:                        } else
                    489:                                printf("&#160;");
                    490:                }
1.1       schwarze  491:
1.13      schwarze  492:        if ( ! (h->flags & HTML_NONOSPACE))
                    493:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  494:        else
                    495:                h->flags |= HTML_NOSPACE;
1.13      schwarze  496:
1.6       schwarze  497:        /* Print out the tag name and attributes. */
                    498:
1.1       schwarze  499:        printf("<%s", htmltags[tag].name);
1.6       schwarze  500:        for (i = 0; i < sz; i++)
                    501:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    502:
1.42      schwarze  503:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  504:
                    505:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42      schwarze  506:                putchar('/');
1.6       schwarze  507:
1.4       schwarze  508:        putchar('>');
1.1       schwarze  509:
                    510:        h->flags |= HTML_NOSPACE;
1.18      schwarze  511:
                    512:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    513:                putchar('\n');
                    514:
1.1       schwarze  515:        return(t);
                    516: }
                    517:
                    518: static void
1.54      schwarze  519: print_ctag(struct html *h, struct tag *tag)
1.1       schwarze  520: {
1.35      schwarze  521:
1.54      schwarze  522:        /*
                    523:         * Remember to close out and nullify the current
                    524:         * meta-font and table, if applicable.
                    525:         */
                    526:        if (tag == h->metaf)
                    527:                h->metaf = NULL;
                    528:        if (tag == h->tblt)
                    529:                h->tblt = NULL;
                    530:
                    531:        printf("</%s>", htmltags[tag->tag].name);
                    532:        if (HTML_CLRLINE & htmltags[tag->tag].flags) {
1.1       schwarze  533:                h->flags |= HTML_NOSPACE;
1.4       schwarze  534:                putchar('\n');
1.35      schwarze  535:        }
1.54      schwarze  536:
                    537:        h->tags.head = tag->next;
                    538:        free(tag);
1.1       schwarze  539: }
                    540:
                    541: void
1.6       schwarze  542: print_gen_decls(struct html *h)
                    543: {
                    544:
1.42      schwarze  545:        puts("<!DOCTYPE html>");
1.1       schwarze  546: }
                    547:
                    548: void
1.12      schwarze  549: print_text(struct html *h, const char *word)
1.1       schwarze  550: {
                    551:
1.12      schwarze  552:        if ( ! (HTML_NOSPACE & h->flags)) {
                    553:                /* Manage keeps! */
                    554:                if ( ! (HTML_KEEP & h->flags)) {
                    555:                        if (HTML_PREKEEP & h->flags)
                    556:                                h->flags |= HTML_KEEP;
                    557:                        putchar(' ');
                    558:                } else
                    559:                        printf("&#160;");
                    560:        }
1.1       schwarze  561:
1.20      schwarze  562:        assert(NULL == h->metaf);
1.31      schwarze  563:        switch (h->metac) {
1.35      schwarze  564:        case HTMLFONT_ITALIC:
1.31      schwarze  565:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    566:                break;
1.35      schwarze  567:        case HTMLFONT_BOLD:
1.31      schwarze  568:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    569:                break;
1.35      schwarze  570:        case HTMLFONT_BI:
1.31      schwarze  571:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    572:                print_otag(h, TAG_I, 0, NULL);
                    573:                break;
                    574:        default:
                    575:                break;
                    576:        }
1.20      schwarze  577:
1.12      schwarze  578:        assert(word);
1.28      schwarze  579:        if ( ! print_encode(h, word, 0)) {
1.13      schwarze  580:                if ( ! (h->flags & HTML_NONOSPACE))
                    581:                        h->flags &= ~HTML_NOSPACE;
1.53      schwarze  582:                h->flags &= ~HTML_NONEWLINE;
1.28      schwarze  583:        } else
1.53      schwarze  584:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20      schwarze  585:
                    586:        if (h->metaf) {
                    587:                print_tagq(h, h->metaf);
                    588:                h->metaf = NULL;
                    589:        }
1.17      schwarze  590:
                    591:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  592: }
                    593:
                    594: void
                    595: print_tagq(struct html *h, const struct tag *until)
                    596: {
                    597:        struct tag      *tag;
                    598:
1.2       schwarze  599:        while ((tag = h->tags.head) != NULL) {
1.54      schwarze  600:                print_ctag(h, tag);
1.1       schwarze  601:                if (until && tag == until)
                    602:                        return;
                    603:        }
                    604: }
                    605:
                    606: void
                    607: print_stagq(struct html *h, const struct tag *suntil)
                    608: {
                    609:        struct tag      *tag;
                    610:
1.2       schwarze  611:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  612:                if (suntil && tag == suntil)
                    613:                        return;
1.54      schwarze  614:                print_ctag(h, tag);
1.1       schwarze  615:        }
                    616: }
1.42      schwarze  617:
                    618: void
                    619: print_paragraph(struct html *h)
                    620: {
                    621:        struct tag      *t;
                    622:        struct htmlpair  tag;
                    623:
                    624:        PAIR_CLASS_INIT(&tag, "spacer");
                    625:        t = print_otag(h, TAG_DIV, 1, &tag);
                    626:        print_tagq(h, t);
                    627: }
                    628:
1.1       schwarze  629:
                    630: void
                    631: bufinit(struct html *h)
                    632: {
                    633:
                    634:        h->buf[0] = '\0';
                    635:        h->buflen = 0;
                    636: }
                    637:
                    638: void
                    639: bufcat_style(struct html *h, const char *key, const char *val)
                    640: {
                    641:
                    642:        bufcat(h, key);
1.26      schwarze  643:        bufcat(h, ":");
1.1       schwarze  644:        bufcat(h, val);
1.26      schwarze  645:        bufcat(h, ";");
1.1       schwarze  646: }
                    647:
                    648: void
                    649: bufcat(struct html *h, const char *p)
                    650: {
1.36      schwarze  651:
                    652:        /*
                    653:         * XXX This is broken and not easy to fix.
                    654:         * When using the -Oincludes option, buffmt_includes()
                    655:         * may pass in strings overrunning BUFSIZ, causing a crash.
                    656:         */
1.1       schwarze  657:
1.26      schwarze  658:        h->buflen = strlcat(h->buf, p, BUFSIZ);
                    659:        assert(h->buflen < BUFSIZ);
1.1       schwarze  660: }
                    661:
                    662: void
1.26      schwarze  663: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1       schwarze  664: {
                    665:        va_list          ap;
                    666:
                    667:        va_start(ap, fmt);
1.35      schwarze  668:        (void)vsnprintf(h->buf + (int)h->buflen,
                    669:            BUFSIZ - h->buflen - 1, fmt, ap);
1.1       schwarze  670:        va_end(ap);
                    671:        h->buflen = strlen(h->buf);
                    672: }
                    673:
1.26      schwarze  674: static void
1.1       schwarze  675: bufncat(struct html *h, const char *p, size_t sz)
                    676: {
                    677:
1.26      schwarze  678:        assert(h->buflen + sz + 1 < BUFSIZ);
                    679:        strncat(h->buf, p, sz);
1.1       schwarze  680:        h->buflen += sz;
                    681: }
                    682:
                    683: void
                    684: buffmt_includes(struct html *h, const char *name)
                    685: {
                    686:        const char      *p, *pp;
                    687:
                    688:        pp = h->base_includes;
1.35      schwarze  689:
1.26      schwarze  690:        bufinit(h);
1.1       schwarze  691:        while (NULL != (p = strchr(pp, '%'))) {
                    692:                bufncat(h, pp, (size_t)(p - pp));
                    693:                switch (*(p + 1)) {
1.35      schwarze  694:                case'I':
1.1       schwarze  695:                        bufcat(h, name);
                    696:                        break;
                    697:                default:
                    698:                        bufncat(h, p, 2);
                    699:                        break;
                    700:                }
                    701:                pp = p + 2;
                    702:        }
                    703:        if (pp)
                    704:                bufcat(h, pp);
                    705: }
                    706:
                    707: void
1.35      schwarze  708: buffmt_man(struct html *h, const char *name, const char *sec)
1.1       schwarze  709: {
                    710:        const char      *p, *pp;
                    711:
                    712:        pp = h->base_man;
1.35      schwarze  713:
1.26      schwarze  714:        bufinit(h);
1.1       schwarze  715:        while (NULL != (p = strchr(pp, '%'))) {
                    716:                bufncat(h, pp, (size_t)(p - pp));
                    717:                switch (*(p + 1)) {
1.35      schwarze  718:                case 'S':
1.1       schwarze  719:                        bufcat(h, sec ? sec : "1");
                    720:                        break;
1.35      schwarze  721:                case 'N':
1.32      schwarze  722:                        bufcat_fmt(h, "%s", name);
1.1       schwarze  723:                        break;
                    724:                default:
                    725:                        bufncat(h, p, 2);
                    726:                        break;
                    727:                }
                    728:                pp = p + 2;
                    729:        }
                    730:        if (pp)
                    731:                bufcat(h, pp);
                    732: }
                    733:
                    734: void
                    735: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    736: {
                    737:        double           v;
                    738:
                    739:        v = su->scale;
1.26      schwarze  740:        if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                    741:                v = 1.0;
1.40      schwarze  742:        else if (SCALE_BU == su->unit)
                    743:                v /= 24.0;
1.1       schwarze  744:
1.26      schwarze  745:        bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1       schwarze  746: }
                    747:
1.3       schwarze  748: void
1.26      schwarze  749: bufcat_id(struct html *h, const char *src)
1.3       schwarze  750: {
                    751:
                    752:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    753:
1.26      schwarze  754:        while ('\0' != *src)
                    755:                bufcat_fmt(h, "%.2x", *src++);
1.3       schwarze  756: }