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

1.50    ! schwarze    1: /*     $OpenBSD: html.c,v 1.49 2014/10/27 16:28:30 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);
                    423:                        break;
1.35      schwarze  424:                case ESCAPE_SPECIAL:
1.26      schwarze  425:                        c = mchars_spec2cp(h->symtab, seq, len);
                    426:                        break;
1.35      schwarze  427:                case ESCAPE_NOSPACE:
1.26      schwarze  428:                        if ('\0' == *p)
                    429:                                nospace = 1;
1.49      schwarze  430:                        continue;
1.5       schwarze  431:                default:
1.49      schwarze  432:                        continue;
1.5       schwarze  433:                }
1.49      schwarze  434:                if (c <= 0)
                    435:                        continue;
                    436:                if (c < 0x20 || (c > 0x7E && c < 0xA0))
                    437:                        c = 0xFFFD;
                    438:                if (c > 0x7E)
                    439:                        printf("&#%d;", c);
                    440:                else if ( ! print_escape(c))
                    441:                        putchar(c);
1.1       schwarze  442:        }
1.5       schwarze  443:
                    444:        return(nospace);
1.1       schwarze  445: }
                    446:
1.6       schwarze  447: static void
                    448: print_attr(struct html *h, const char *key, const char *val)
                    449: {
                    450:        printf(" %s=\"", key);
                    451:        (void)print_encode(h, val, 1);
                    452:        putchar('\"');
                    453: }
                    454:
1.1       schwarze  455: struct tag *
1.35      schwarze  456: print_otag(struct html *h, enum htmltag tag,
1.1       schwarze  457:                int sz, const struct htmlpair *p)
                    458: {
                    459:        int              i;
                    460:        struct tag      *t;
                    461:
1.6       schwarze  462:        /* Push this tags onto the stack of open scopes. */
                    463:
1.1       schwarze  464:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24      schwarze  465:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  466:                t->tag = tag;
1.2       schwarze  467:                t->next = h->tags.head;
                    468:                h->tags.head = t;
1.1       schwarze  469:        } else
                    470:                t = NULL;
                    471:
                    472:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  473:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    474:                        /* Manage keeps! */
                    475:                        if ( ! (HTML_KEEP & h->flags)) {
                    476:                                if (HTML_PREKEEP & h->flags)
                    477:                                        h->flags |= HTML_KEEP;
                    478:                                putchar(' ');
                    479:                        } else
                    480:                                printf("&#160;");
                    481:                }
1.1       schwarze  482:
1.13      schwarze  483:        if ( ! (h->flags & HTML_NONOSPACE))
                    484:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  485:        else
                    486:                h->flags |= HTML_NOSPACE;
1.13      schwarze  487:
1.6       schwarze  488:        /* Print out the tag name and attributes. */
                    489:
1.1       schwarze  490:        printf("<%s", htmltags[tag].name);
1.6       schwarze  491:        for (i = 0; i < sz; i++)
                    492:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    493:
1.42      schwarze  494:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  495:
                    496:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42      schwarze  497:                putchar('/');
1.6       schwarze  498:
1.4       schwarze  499:        putchar('>');
1.1       schwarze  500:
                    501:        h->flags |= HTML_NOSPACE;
1.18      schwarze  502:
                    503:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    504:                putchar('\n');
                    505:
1.1       schwarze  506:        return(t);
                    507: }
                    508:
                    509: static void
                    510: print_ctag(struct html *h, enum htmltag tag)
                    511: {
1.35      schwarze  512:
1.1       schwarze  513:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  514:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  515:                h->flags |= HTML_NOSPACE;
1.4       schwarze  516:                putchar('\n');
1.35      schwarze  517:        }
1.1       schwarze  518: }
                    519:
                    520: void
1.6       schwarze  521: print_gen_decls(struct html *h)
                    522: {
                    523:
1.42      schwarze  524:        puts("<!DOCTYPE html>");
1.1       schwarze  525: }
                    526:
                    527: void
1.12      schwarze  528: print_text(struct html *h, const char *word)
1.1       schwarze  529: {
                    530:
1.12      schwarze  531:        if ( ! (HTML_NOSPACE & h->flags)) {
                    532:                /* Manage keeps! */
                    533:                if ( ! (HTML_KEEP & h->flags)) {
                    534:                        if (HTML_PREKEEP & h->flags)
                    535:                                h->flags |= HTML_KEEP;
                    536:                        putchar(' ');
                    537:                } else
                    538:                        printf("&#160;");
                    539:        }
1.1       schwarze  540:
1.20      schwarze  541:        assert(NULL == h->metaf);
1.31      schwarze  542:        switch (h->metac) {
1.35      schwarze  543:        case HTMLFONT_ITALIC:
1.31      schwarze  544:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    545:                break;
1.35      schwarze  546:        case HTMLFONT_BOLD:
1.31      schwarze  547:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    548:                break;
1.35      schwarze  549:        case HTMLFONT_BI:
1.31      schwarze  550:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    551:                print_otag(h, TAG_I, 0, NULL);
                    552:                break;
                    553:        default:
                    554:                break;
                    555:        }
1.20      schwarze  556:
1.12      schwarze  557:        assert(word);
1.28      schwarze  558:        if ( ! print_encode(h, word, 0)) {
1.13      schwarze  559:                if ( ! (h->flags & HTML_NONOSPACE))
                    560:                        h->flags &= ~HTML_NOSPACE;
1.28      schwarze  561:        } else
                    562:                h->flags |= HTML_NOSPACE;
1.20      schwarze  563:
                    564:        if (h->metaf) {
                    565:                print_tagq(h, h->metaf);
                    566:                h->metaf = NULL;
                    567:        }
1.17      schwarze  568:
                    569:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  570: }
                    571:
                    572: void
                    573: print_tagq(struct html *h, const struct tag *until)
                    574: {
                    575:        struct tag      *tag;
                    576:
1.2       schwarze  577:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  578:                /*
1.22      schwarze  579:                 * Remember to close out and nullify the current
                    580:                 * meta-font and table, if applicable.
                    581:                 */
1.5       schwarze  582:                if (tag == h->metaf)
                    583:                        h->metaf = NULL;
1.22      schwarze  584:                if (tag == h->tblt)
                    585:                        h->tblt = NULL;
1.1       schwarze  586:                print_ctag(h, tag->tag);
1.2       schwarze  587:                h->tags.head = tag->next;
1.1       schwarze  588:                free(tag);
                    589:                if (until && tag == until)
                    590:                        return;
                    591:        }
                    592: }
                    593:
                    594: void
                    595: print_stagq(struct html *h, const struct tag *suntil)
                    596: {
                    597:        struct tag      *tag;
                    598:
1.2       schwarze  599:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  600:                if (suntil && tag == suntil)
                    601:                        return;
1.35      schwarze  602:                /*
1.22      schwarze  603:                 * Remember to close out and nullify the current
                    604:                 * meta-font and table, if applicable.
                    605:                 */
1.5       schwarze  606:                if (tag == h->metaf)
                    607:                        h->metaf = NULL;
1.22      schwarze  608:                if (tag == h->tblt)
                    609:                        h->tblt = NULL;
1.1       schwarze  610:                print_ctag(h, tag->tag);
1.2       schwarze  611:                h->tags.head = tag->next;
1.1       schwarze  612:                free(tag);
                    613:        }
                    614: }
1.42      schwarze  615:
                    616: void
                    617: print_paragraph(struct html *h)
                    618: {
                    619:        struct tag      *t;
                    620:        struct htmlpair  tag;
                    621:
                    622:        PAIR_CLASS_INIT(&tag, "spacer");
                    623:        t = print_otag(h, TAG_DIV, 1, &tag);
                    624:        print_tagq(h, t);
                    625: }
                    626:
1.1       schwarze  627:
                    628: void
                    629: bufinit(struct html *h)
                    630: {
                    631:
                    632:        h->buf[0] = '\0';
                    633:        h->buflen = 0;
                    634: }
                    635:
                    636: void
                    637: bufcat_style(struct html *h, const char *key, const char *val)
                    638: {
                    639:
                    640:        bufcat(h, key);
1.26      schwarze  641:        bufcat(h, ":");
1.1       schwarze  642:        bufcat(h, val);
1.26      schwarze  643:        bufcat(h, ";");
1.1       schwarze  644: }
                    645:
                    646: void
                    647: bufcat(struct html *h, const char *p)
                    648: {
1.36      schwarze  649:
                    650:        /*
                    651:         * XXX This is broken and not easy to fix.
                    652:         * When using the -Oincludes option, buffmt_includes()
                    653:         * may pass in strings overrunning BUFSIZ, causing a crash.
                    654:         */
1.1       schwarze  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);
1.35      schwarze  666:        (void)vsnprintf(h->buf + (int)h->buflen,
                    667:            BUFSIZ - h->buflen - 1, fmt, ap);
1.1       schwarze  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;
1.35      schwarze  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)) {
1.35      schwarze  692:                case'I':
1.1       schwarze  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
1.35      schwarze  706: buffmt_man(struct html *h, const char *name, const char *sec)
1.1       schwarze  707: {
                    708:        const char      *p, *pp;
                    709:
                    710:        pp = h->base_man;
1.35      schwarze  711:
1.26      schwarze  712:        bufinit(h);
1.1       schwarze  713:        while (NULL != (p = strchr(pp, '%'))) {
                    714:                bufncat(h, pp, (size_t)(p - pp));
                    715:                switch (*(p + 1)) {
1.35      schwarze  716:                case 'S':
1.1       schwarze  717:                        bufcat(h, sec ? sec : "1");
                    718:                        break;
1.35      schwarze  719:                case 'N':
1.32      schwarze  720:                        bufcat_fmt(h, "%s", name);
1.1       schwarze  721:                        break;
                    722:                default:
                    723:                        bufncat(h, p, 2);
                    724:                        break;
                    725:                }
                    726:                pp = p + 2;
                    727:        }
                    728:        if (pp)
                    729:                bufcat(h, pp);
                    730: }
                    731:
                    732: void
                    733: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    734: {
                    735:        double           v;
                    736:
                    737:        v = su->scale;
1.26      schwarze  738:        if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                    739:                v = 1.0;
1.40      schwarze  740:        else if (SCALE_BU == su->unit)
                    741:                v /= 24.0;
1.1       schwarze  742:
1.26      schwarze  743:        bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1       schwarze  744: }
                    745:
1.3       schwarze  746: void
1.26      schwarze  747: bufcat_id(struct html *h, const char *src)
1.3       schwarze  748: {
                    749:
                    750:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    751:
1.26      schwarze  752:        while ('\0' != *src)
                    753:                bufcat_fmt(h, "%.2x", *src++);
1.3       schwarze  754: }