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

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