[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.32

1.32    ! schwarze    1: /*     $Id: html.c,v 1.31 2013/08/08 20:07:24 schwarze Exp $ */
1.1       schwarze    2: /*
1.22      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.31      schwarze    4:  * Copyright (c) 2011, 2012, 2013 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.26      schwarze   30: #include "libmandoc.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:        {"p",           HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_P */
                     69:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.19      schwarze   70:        {"b",           0 }, /* TAG_B */
                     71:        {"i",           0 }, /* TAG_I */
1.20      schwarze   72:        {"code",        0 }, /* TAG_CODE */
                     73:        {"small",       0 }, /* TAG_SMALL */
1.5       schwarze   74: };
                     75:
                     76: static const char      *const htmlattrs[ATTR_MAX] = {
1.19      schwarze   77:        "http-equiv", /* ATTR_HTTPEQUIV */
                     78:        "content", /* ATTR_CONTENT */
                     79:        "name", /* ATTR_NAME */
                     80:        "rel", /* ATTR_REL */
                     81:        "href", /* ATTR_HREF */
                     82:        "type", /* ATTR_TYPE */
                     83:        "media", /* ATTR_MEDIA */
                     84:        "class", /* ATTR_CLASS */
                     85:        "style", /* ATTR_STYLE */
                     86:        "width", /* ATTR_WIDTH */
                     87:        "id", /* ATTR_ID */
                     88:        "summary", /* ATTR_SUMMARY */
                     89:        "align", /* ATTR_ALIGN */
1.22      schwarze   90:        "colspan", /* ATTR_COLSPAN */
1.1       schwarze   91: };
                     92:
1.26      schwarze   93: static const char      *const roffscales[SCALE_MAX] = {
                     94:        "cm", /* SCALE_CM */
                     95:        "in", /* SCALE_IN */
                     96:        "pc", /* SCALE_PC */
                     97:        "pt", /* SCALE_PT */
                     98:        "em", /* SCALE_EM */
                     99:        "em", /* SCALE_MM */
                    100:        "ex", /* SCALE_EN */
                    101:        "ex", /* SCALE_BU */
                    102:        "em", /* SCALE_VS */
                    103:        "ex", /* SCALE_FS */
                    104: };
1.5       schwarze  105:
1.26      schwarze  106: static void     bufncat(struct html *, const char *, size_t);
                    107: static void     print_ctag(struct html *, enum htmltag);
                    108: static int      print_encode(struct html *, const char *, int);
                    109: static void     print_metaf(struct html *, enum mandoc_esc);
                    110: static void     print_attr(struct html *, const char *, const char *);
                    111: static void     *ml_alloc(char *, enum htmltype);
1.5       schwarze  112:
1.6       schwarze  113: static void *
                    114: ml_alloc(char *outopts, enum htmltype type)
1.1       schwarze  115: {
                    116:        struct html     *h;
1.29      schwarze  117:        const char      *toks[5];
1.1       schwarze  118:        char            *v;
                    119:
                    120:        toks[0] = "style";
                    121:        toks[1] = "man";
                    122:        toks[2] = "includes";
1.29      schwarze  123:        toks[3] = "fragment";
                    124:        toks[4] = NULL;
1.1       schwarze  125:
1.24      schwarze  126:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  127:
1.6       schwarze  128:        h->type = type;
1.2       schwarze  129:        h->tags.head = NULL;
1.26      schwarze  130:        h->symtab = mchars_alloc();
1.1       schwarze  131:
                    132:        while (outopts && *outopts)
                    133:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
                    134:                case (0):
                    135:                        h->style = v;
                    136:                        break;
                    137:                case (1):
                    138:                        h->base_man = v;
                    139:                        break;
                    140:                case (2):
                    141:                        h->base_includes = v;
1.29      schwarze  142:                        break;
                    143:                case (3):
                    144:                        h->oflags |= HTML_FRAGMENT;
1.1       schwarze  145:                        break;
                    146:                default:
                    147:                        break;
                    148:                }
                    149:
                    150:        return(h);
                    151: }
                    152:
1.6       schwarze  153: void *
                    154: html_alloc(char *outopts)
                    155: {
                    156:
                    157:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    158: }
                    159:
                    160:
                    161: void *
                    162: xhtml_alloc(char *outopts)
                    163: {
                    164:
                    165:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    166: }
                    167:
1.1       schwarze  168:
                    169: void
                    170: html_free(void *p)
                    171: {
                    172:        struct tag      *tag;
                    173:        struct html     *h;
                    174:
                    175:        h = (struct html *)p;
                    176:
1.2       schwarze  177:        while ((tag = h->tags.head) != NULL) {
                    178:                h->tags.head = tag->next;
1.1       schwarze  179:                free(tag);
                    180:        }
                    181:
                    182:        if (h->symtab)
1.26      schwarze  183:                mchars_free(h->symtab);
1.1       schwarze  184:
                    185:        free(h);
                    186: }
                    187:
                    188:
                    189: void
                    190: print_gen_head(struct html *h)
                    191: {
                    192:        struct htmlpair  tag[4];
                    193:
                    194:        tag[0].key = ATTR_HTTPEQUIV;
                    195:        tag[0].val = "Content-Type";
                    196:        tag[1].key = ATTR_CONTENT;
                    197:        tag[1].val = "text/html; charset=utf-8";
                    198:        print_otag(h, TAG_META, 2, tag);
                    199:
                    200:        tag[0].key = ATTR_NAME;
                    201:        tag[0].val = "resource-type";
                    202:        tag[1].key = ATTR_CONTENT;
                    203:        tag[1].val = "document";
                    204:        print_otag(h, TAG_META, 2, tag);
                    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.26      schwarze  225:        case (ESCAPE_FONTPREV):
1.5       schwarze  226:                font = h->metal;
                    227:                break;
1.26      schwarze  228:        case (ESCAPE_FONTITALIC):
1.5       schwarze  229:                font = HTMLFONT_ITALIC;
                    230:                break;
1.26      schwarze  231:        case (ESCAPE_FONTBOLD):
1.5       schwarze  232:                font = HTMLFONT_BOLD;
                    233:                break;
1.31      schwarze  234:        case (ESCAPE_FONTBI):
                    235:                font = HTMLFONT_BI;
                    236:                break;
1.26      schwarze  237:        case (ESCAPE_FONT):
                    238:                /* FALLTHROUGH */
                    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) {
                    256:        case (HTMLFONT_ITALIC):
                    257:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    258:                break;
                    259:        case (HTMLFONT_BOLD):
                    260:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    261:                break;
                    262:        case (HTMLFONT_BI):
                    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.26      schwarze  301:                case (ESCAPE_ERROR):
                    302:                        return(sz);
                    303:                case (ESCAPE_UNICODE):
                    304:                        /* FALLTHROUGH */
                    305:                case (ESCAPE_NUMBERED):
                    306:                        /* FALLTHROUGH */
                    307:                case (ESCAPE_SPECIAL):
1.30      schwarze  308:                        if (skip)
                    309:                                skip = 0;
                    310:                        else
                    311:                                sz++;
                    312:                        break;
                    313:                case (ESCAPE_SKIPCHAR):
                    314:                        skip = 1;
1.26      schwarze  315:                        break;
                    316:                default:
                    317:                        break;
                    318:                }
                    319:        }
1.30      schwarze  320:        return(sz);
1.26      schwarze  321: }
1.1       schwarze  322:
1.5       schwarze  323: static int
                    324: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  325: {
1.4       schwarze  326:        size_t           sz;
1.26      schwarze  327:        int              c, len, nospace;
1.5       schwarze  328:        const char      *seq;
1.26      schwarze  329:        enum mandoc_esc  esc;
1.9       schwarze  330:        static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.5       schwarze  331:
                    332:        nospace = 0;
1.1       schwarze  333:
1.26      schwarze  334:        while ('\0' != *p) {
1.30      schwarze  335:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    336:                        h->flags &= ~HTML_SKIPCHAR;
                    337:                        p++;
                    338:                        continue;
                    339:                }
                    340:
1.9       schwarze  341:                sz = strcspn(p, rejs);
1.4       schwarze  342:
                    343:                fwrite(p, 1, sz, stdout);
1.26      schwarze  344:                p += (int)sz;
1.4       schwarze  345:
1.26      schwarze  346:                if ('\0' == *p)
                    347:                        break;
                    348:
                    349:                switch (*p++) {
                    350:                case ('<'):
1.5       schwarze  351:                        printf("&lt;");
                    352:                        continue;
1.26      schwarze  353:                case ('>'):
1.5       schwarze  354:                        printf("&gt;");
                    355:                        continue;
1.26      schwarze  356:                case ('&'):
1.5       schwarze  357:                        printf("&amp;");
1.1       schwarze  358:                        continue;
1.26      schwarze  359:                case (ASCII_HYPH):
1.9       schwarze  360:                        putchar('-');
                    361:                        continue;
1.26      schwarze  362:                default:
1.4       schwarze  363:                        break;
1.26      schwarze  364:                }
1.4       schwarze  365:
1.26      schwarze  366:                esc = mandoc_escape(&p, &seq, &len);
                    367:                if (ESCAPE_ERROR == esc)
                    368:                        break;
1.5       schwarze  369:
1.26      schwarze  370:                switch (esc) {
1.30      schwarze  371:                case (ESCAPE_FONT):
                    372:                        /* FALLTHROUGH */
                    373:                case (ESCAPE_FONTPREV):
                    374:                        /* FALLTHROUGH */
                    375:                case (ESCAPE_FONTBOLD):
                    376:                        /* FALLTHROUGH */
                    377:                case (ESCAPE_FONTITALIC):
                    378:                        /* FALLTHROUGH */
1.31      schwarze  379:                case (ESCAPE_FONTBI):
                    380:                        /* FALLTHROUGH */
1.30      schwarze  381:                case (ESCAPE_FONTROMAN):
                    382:                        if (0 == norecurse)
                    383:                                print_metaf(h, esc);
                    384:                        continue;
                    385:                case (ESCAPE_SKIPCHAR):
                    386:                        h->flags |= HTML_SKIPCHAR;
                    387:                        continue;
                    388:                default:
                    389:                        break;
                    390:                }
                    391:
                    392:                if (h->flags & HTML_SKIPCHAR) {
                    393:                        h->flags &= ~HTML_SKIPCHAR;
                    394:                        continue;
                    395:                }
                    396:
                    397:                switch (esc) {
1.26      schwarze  398:                case (ESCAPE_UNICODE):
                    399:                        /* Skip passed "u" header. */
                    400:                        c = mchars_num2uc(seq + 1, len - 1);
                    401:                        if ('\0' != c)
                    402:                                printf("&#x%x;", c);
                    403:                        break;
                    404:                case (ESCAPE_NUMBERED):
                    405:                        c = mchars_num2char(seq, len);
                    406:                        if ('\0' != c)
                    407:                                putchar(c);
                    408:                        break;
                    409:                case (ESCAPE_SPECIAL):
                    410:                        c = mchars_spec2cp(h->symtab, seq, len);
                    411:                        if (c > 0)
                    412:                                printf("&#%d;", c);
                    413:                        else if (-1 == c && 1 == len)
                    414:                                putchar((int)*seq);
                    415:                        break;
                    416:                case (ESCAPE_NOSPACE):
                    417:                        if ('\0' == *p)
                    418:                                nospace = 1;
1.5       schwarze  419:                        break;
                    420:                default:
                    421:                        break;
                    422:                }
1.1       schwarze  423:        }
1.5       schwarze  424:
                    425:        return(nospace);
1.1       schwarze  426: }
                    427:
                    428:
1.6       schwarze  429: static void
                    430: print_attr(struct html *h, const char *key, const char *val)
                    431: {
                    432:        printf(" %s=\"", key);
                    433:        (void)print_encode(h, val, 1);
                    434:        putchar('\"');
                    435: }
                    436:
                    437:
1.1       schwarze  438: struct tag *
                    439: print_otag(struct html *h, enum htmltag tag,
                    440:                int sz, const struct htmlpair *p)
                    441: {
                    442:        int              i;
                    443:        struct tag      *t;
                    444:
1.6       schwarze  445:        /* Push this tags onto the stack of open scopes. */
                    446:
1.1       schwarze  447:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24      schwarze  448:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  449:                t->tag = tag;
1.2       schwarze  450:                t->next = h->tags.head;
                    451:                h->tags.head = t;
1.1       schwarze  452:        } else
                    453:                t = NULL;
                    454:
                    455:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  456:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    457:                        /* Manage keeps! */
                    458:                        if ( ! (HTML_KEEP & h->flags)) {
                    459:                                if (HTML_PREKEEP & h->flags)
                    460:                                        h->flags |= HTML_KEEP;
                    461:                                putchar(' ');
                    462:                        } else
                    463:                                printf("&#160;");
                    464:                }
1.1       schwarze  465:
1.13      schwarze  466:        if ( ! (h->flags & HTML_NONOSPACE))
                    467:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  468:        else
                    469:                h->flags |= HTML_NOSPACE;
1.13      schwarze  470:
1.6       schwarze  471:        /* Print out the tag name and attributes. */
                    472:
1.1       schwarze  473:        printf("<%s", htmltags[tag].name);
1.6       schwarze  474:        for (i = 0; i < sz; i++)
                    475:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    476:
                    477:        /* Add non-overridable attributes. */
                    478:
                    479:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    480:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    481:                print_attr(h, "xml:lang", "en");
                    482:                print_attr(h, "lang", "en");
1.1       schwarze  483:        }
1.6       schwarze  484:
1.26      schwarze  485:        /* Accommodate for XML "well-formed" singleton escaping. */
1.6       schwarze  486:
                    487:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    488:                switch (h->type) {
                    489:                case (HTML_XHTML_1_0_STRICT):
                    490:                        putchar('/');
                    491:                        break;
                    492:                default:
                    493:                        break;
                    494:                }
                    495:
1.4       schwarze  496:        putchar('>');
1.1       schwarze  497:
                    498:        h->flags |= HTML_NOSPACE;
1.18      schwarze  499:
                    500:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    501:                putchar('\n');
                    502:
1.1       schwarze  503:        return(t);
                    504: }
                    505:
                    506:
                    507: static void
                    508: print_ctag(struct html *h, enum htmltag tag)
                    509: {
                    510:
                    511:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  512:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  513:                h->flags |= HTML_NOSPACE;
1.4       schwarze  514:                putchar('\n');
1.5       schwarze  515:        }
1.1       schwarze  516: }
                    517:
                    518: void
1.6       schwarze  519: print_gen_decls(struct html *h)
                    520: {
                    521:        const char      *doctype;
                    522:        const char      *dtd;
                    523:        const char      *name;
                    524:
                    525:        switch (h->type) {
                    526:        case (HTML_HTML_4_01_STRICT):
                    527:                name = "HTML";
                    528:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    529:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    530:                break;
                    531:        default:
1.26      schwarze  532:                puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.6       schwarze  533:                name = "html";
                    534:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    535:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    536:                break;
                    537:        }
                    538:
                    539:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    540:                        name, doctype, dtd);
1.1       schwarze  541: }
                    542:
                    543: void
1.12      schwarze  544: print_text(struct html *h, const char *word)
1.1       schwarze  545: {
                    546:
1.12      schwarze  547:        if ( ! (HTML_NOSPACE & h->flags)) {
                    548:                /* Manage keeps! */
                    549:                if ( ! (HTML_KEEP & h->flags)) {
                    550:                        if (HTML_PREKEEP & h->flags)
                    551:                                h->flags |= HTML_KEEP;
                    552:                        putchar(' ');
                    553:                } else
                    554:                        printf("&#160;");
                    555:        }
1.1       schwarze  556:
1.20      schwarze  557:        assert(NULL == h->metaf);
1.31      schwarze  558:        switch (h->metac) {
                    559:        case (HTMLFONT_ITALIC):
                    560:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    561:                break;
                    562:        case (HTMLFONT_BOLD):
                    563:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    564:                break;
                    565:        case (HTMLFONT_BI):
                    566:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    567:                print_otag(h, TAG_I, 0, NULL);
                    568:                break;
                    569:        default:
                    570:                break;
                    571:        }
1.20      schwarze  572:
1.12      schwarze  573:        assert(word);
1.28      schwarze  574:        if ( ! print_encode(h, word, 0)) {
1.13      schwarze  575:                if ( ! (h->flags & HTML_NONOSPACE))
                    576:                        h->flags &= ~HTML_NOSPACE;
1.28      schwarze  577:        } else
                    578:                h->flags |= HTML_NOSPACE;
1.20      schwarze  579:
                    580:        if (h->metaf) {
                    581:                print_tagq(h, h->metaf);
                    582:                h->metaf = NULL;
                    583:        }
1.17      schwarze  584:
                    585:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  586: }
                    587:
                    588:
                    589: void
                    590: print_tagq(struct html *h, const struct tag *until)
                    591: {
                    592:        struct tag      *tag;
                    593:
1.2       schwarze  594:        while ((tag = h->tags.head) != NULL) {
1.22      schwarze  595:                /*
                    596:                 * Remember to close out and nullify the current
                    597:                 * meta-font and table, if applicable.
                    598:                 */
1.5       schwarze  599:                if (tag == h->metaf)
                    600:                        h->metaf = NULL;
1.22      schwarze  601:                if (tag == h->tblt)
                    602:                        h->tblt = NULL;
1.1       schwarze  603:                print_ctag(h, tag->tag);
1.2       schwarze  604:                h->tags.head = tag->next;
1.1       schwarze  605:                free(tag);
                    606:                if (until && tag == until)
                    607:                        return;
                    608:        }
                    609: }
                    610:
                    611:
                    612: void
                    613: print_stagq(struct html *h, const struct tag *suntil)
                    614: {
                    615:        struct tag      *tag;
                    616:
1.2       schwarze  617:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  618:                if (suntil && tag == suntil)
                    619:                        return;
1.22      schwarze  620:                /*
                    621:                 * Remember to close out and nullify the current
                    622:                 * meta-font and table, if applicable.
                    623:                 */
1.5       schwarze  624:                if (tag == h->metaf)
                    625:                        h->metaf = NULL;
1.22      schwarze  626:                if (tag == h->tblt)
                    627:                        h->tblt = NULL;
1.1       schwarze  628:                print_ctag(h, tag->tag);
1.2       schwarze  629:                h->tags.head = tag->next;
1.1       schwarze  630:                free(tag);
                    631:        }
                    632: }
                    633:
                    634: void
                    635: bufinit(struct html *h)
                    636: {
                    637:
                    638:        h->buf[0] = '\0';
                    639:        h->buflen = 0;
                    640: }
                    641:
                    642: void
                    643: bufcat_style(struct html *h, const char *key, const char *val)
                    644: {
                    645:
                    646:        bufcat(h, key);
1.26      schwarze  647:        bufcat(h, ":");
1.1       schwarze  648:        bufcat(h, val);
1.26      schwarze  649:        bufcat(h, ";");
1.1       schwarze  650: }
                    651:
                    652: void
                    653: bufcat(struct html *h, const char *p)
                    654: {
                    655:
1.26      schwarze  656:        h->buflen = strlcat(h->buf, p, BUFSIZ);
                    657:        assert(h->buflen < BUFSIZ);
1.1       schwarze  658: }
                    659:
                    660: void
1.26      schwarze  661: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1       schwarze  662: {
                    663:        va_list          ap;
                    664:
                    665:        va_start(ap, fmt);
                    666:        (void)vsnprintf(h->buf + (int)h->buflen,
                    667:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    668:        va_end(ap);
                    669:        h->buflen = strlen(h->buf);
                    670: }
                    671:
1.26      schwarze  672: static void
1.1       schwarze  673: bufncat(struct html *h, const char *p, size_t sz)
                    674: {
                    675:
1.26      schwarze  676:        assert(h->buflen + sz + 1 < BUFSIZ);
                    677:        strncat(h->buf, p, sz);
1.1       schwarze  678:        h->buflen += sz;
                    679: }
                    680:
                    681: void
                    682: buffmt_includes(struct html *h, const char *name)
                    683: {
                    684:        const char      *p, *pp;
                    685:
                    686:        pp = h->base_includes;
                    687:
1.26      schwarze  688:        bufinit(h);
1.1       schwarze  689:        while (NULL != (p = strchr(pp, '%'))) {
                    690:                bufncat(h, pp, (size_t)(p - pp));
                    691:                switch (*(p + 1)) {
                    692:                case('I'):
                    693:                        bufcat(h, name);
                    694:                        break;
                    695:                default:
                    696:                        bufncat(h, p, 2);
                    697:                        break;
                    698:                }
                    699:                pp = p + 2;
                    700:        }
                    701:        if (pp)
                    702:                bufcat(h, pp);
                    703: }
                    704:
                    705: void
                    706: buffmt_man(struct html *h,
                    707:                const char *name, const char *sec)
                    708: {
                    709:        const char      *p, *pp;
                    710:
                    711:        pp = h->base_man;
                    712:
1.26      schwarze  713:        bufinit(h);
1.1       schwarze  714:        while (NULL != (p = strchr(pp, '%'))) {
                    715:                bufncat(h, pp, (size_t)(p - pp));
                    716:                switch (*(p + 1)) {
                    717:                case('S'):
                    718:                        bufcat(h, sec ? sec : "1");
                    719:                        break;
                    720:                case('N'):
1.32    ! schwarze  721:                        bufcat_fmt(h, "%s", name);
1.1       schwarze  722:                        break;
                    723:                default:
                    724:                        bufncat(h, p, 2);
                    725:                        break;
                    726:                }
                    727:                pp = p + 2;
                    728:        }
                    729:        if (pp)
                    730:                bufcat(h, pp);
                    731: }
                    732:
                    733: void
                    734: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    735: {
                    736:        double           v;
                    737:
                    738:        v = su->scale;
1.26      schwarze  739:        if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                    740:                v = 1.0;
1.1       schwarze  741:
1.26      schwarze  742:        bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1       schwarze  743: }
                    744:
1.3       schwarze  745: void
1.26      schwarze  746: bufcat_id(struct html *h, const char *src)
1.3       schwarze  747: {
                    748:
                    749:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    750:
1.26      schwarze  751:        while ('\0' != *src)
                    752:                bufcat_fmt(h, "%.2x", *src++);
1.3       schwarze  753: }