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

1.53    ! schwarze    1: /*     $OpenBSD: html.c,v 1.52 2014/12/01 04:32:34 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.1       schwarze   31: #include "out.h"
                     32: #include "html.h"
                     33: #include "main.h"
                     34:
                     35: struct htmldata {
                     36:        const char       *name;
                     37:        int               flags;
                     38: #define        HTML_CLRLINE     (1 << 0)
                     39: #define        HTML_NOSTACK     (1 << 1)
1.6       schwarze   40: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.1       schwarze   41: };
                     42:
                     43: static const struct htmldata htmltags[TAG_MAX] = {
                     44:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     45:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     46:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.6       schwarze   47:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1       schwarze   48:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
                     49:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
                     50:        {"h1",          0}, /* TAG_H1 */
                     51:        {"h2",          0}, /* TAG_H2 */
                     52:        {"span",        0}, /* TAG_SPAN */
1.8       schwarze   53:        {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6       schwarze   54:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1       schwarze   55:        {"a",           0}, /* TAG_A */
                     56:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
1.18      schwarze   57:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.6       schwarze   58:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1       schwarze   59:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     60:        {"td",          HTML_CLRLINE}, /* TAG_TD */
                     61:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     62:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     63:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.18      schwarze   64:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
                     65:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
                     66:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
                     67:        {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
                     68:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.19      schwarze   69:        {"b",           0 }, /* TAG_B */
                     70:        {"i",           0 }, /* TAG_I */
1.20      schwarze   71:        {"code",        0 }, /* TAG_CODE */
                     72:        {"small",       0 }, /* TAG_SMALL */
1.42      schwarze   73:        {"style",       HTML_CLRLINE}, /* TAG_STYLE */
1.43      schwarze   74:        {"math",        HTML_CLRLINE}, /* TAG_MATH */
                     75:        {"mrow",        0}, /* TAG_MROW */
                     76:        {"mi",          0}, /* TAG_MI */
                     77:        {"mo",          0}, /* TAG_MO */
                     78:        {"msup",        0}, /* TAG_MSUP */
                     79:        {"msub",        0}, /* TAG_MSUB */
                     80:        {"msubsup",     0}, /* TAG_MSUBSUP */
                     81:        {"mfrac",       0}, /* TAG_MFRAC */
                     82:        {"msqrt",       0}, /* TAG_MSQRT */
                     83:        {"mfenced",     0}, /* TAG_MFENCED */
                     84:        {"mtable",      0}, /* TAG_MTABLE */
                     85:        {"mtr",         0}, /* TAG_MTR */
                     86:        {"mtd",         0}, /* TAG_MTD */
1.44      schwarze   87:        {"munderover",  0}, /* TAG_MUNDEROVER */
                     88:        {"munder",      0}, /* TAG_MUNDER*/
                     89:        {"mover",       0}, /* TAG_MOVER*/
1.5       schwarze   90: };
                     91:
                     92: static const char      *const htmlattrs[ATTR_MAX] = {
1.19      schwarze   93:        "name", /* ATTR_NAME */
                     94:        "rel", /* ATTR_REL */
                     95:        "href", /* ATTR_HREF */
                     96:        "type", /* ATTR_TYPE */
                     97:        "media", /* ATTR_MEDIA */
                     98:        "class", /* ATTR_CLASS */
                     99:        "style", /* ATTR_STYLE */
                    100:        "id", /* ATTR_ID */
1.22      schwarze  101:        "colspan", /* ATTR_COLSPAN */
1.42      schwarze  102:        "charset", /* ATTR_CHARSET */
1.43      schwarze  103:        "open", /* ATTR_OPEN */
                    104:        "close", /* ATTR_CLOSE */
1.45      schwarze  105:        "mathvariant", /* ATTR_MATHVARIANT */
1.1       schwarze  106: };
                    107:
1.26      schwarze  108: static const char      *const roffscales[SCALE_MAX] = {
                    109:        "cm", /* SCALE_CM */
                    110:        "in", /* SCALE_IN */
                    111:        "pc", /* SCALE_PC */
                    112:        "pt", /* SCALE_PT */
                    113:        "em", /* SCALE_EM */
                    114:        "em", /* SCALE_MM */
                    115:        "ex", /* SCALE_EN */
                    116:        "ex", /* SCALE_BU */
                    117:        "em", /* SCALE_VS */
                    118:        "ex", /* SCALE_FS */
                    119: };
1.5       schwarze  120:
1.26      schwarze  121: static void     bufncat(struct html *, const char *, size_t);
                    122: static void     print_ctag(struct html *, enum htmltag);
1.38      schwarze  123: static int      print_escape(char);
1.26      schwarze  124: static int      print_encode(struct html *, const char *, int);
                    125: static void     print_metaf(struct html *, enum mandoc_esc);
                    126: static void     print_attr(struct html *, const char *, const char *);
1.5       schwarze  127:
1.35      schwarze  128:
1.50      schwarze  129: void *
                    130: html_alloc(const struct mchars *mchars, char *outopts)
1.1       schwarze  131: {
                    132:        struct html     *h;
1.29      schwarze  133:        const char      *toks[5];
1.1       schwarze  134:        char            *v;
                    135:
                    136:        toks[0] = "style";
                    137:        toks[1] = "man";
                    138:        toks[2] = "includes";
1.29      schwarze  139:        toks[3] = "fragment";
                    140:        toks[4] = NULL;
1.1       schwarze  141:
1.24      schwarze  142:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  143:
1.2       schwarze  144:        h->tags.head = NULL;
1.50      schwarze  145:        h->symtab = mchars;
1.1       schwarze  146:
                    147:        while (outopts && *outopts)
                    148:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.35      schwarze  149:                case 0:
1.1       schwarze  150:                        h->style = v;
                    151:                        break;
1.35      schwarze  152:                case 1:
1.1       schwarze  153:                        h->base_man = v;
                    154:                        break;
1.35      schwarze  155:                case 2:
1.1       schwarze  156:                        h->base_includes = v;
1.29      schwarze  157:                        break;
1.35      schwarze  158:                case 3:
1.29      schwarze  159:                        h->oflags |= HTML_FRAGMENT;
1.1       schwarze  160:                        break;
                    161:                default:
                    162:                        break;
                    163:                }
                    164:
                    165:        return(h);
                    166: }
                    167:
                    168: void
                    169: html_free(void *p)
                    170: {
                    171:        struct tag      *tag;
                    172:        struct html     *h;
                    173:
                    174:        h = (struct html *)p;
                    175:
1.2       schwarze  176:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  177:                h->tags.head = tag->next;
1.1       schwarze  178:                free(tag);
                    179:        }
                    180:
                    181:        free(h);
                    182: }
                    183:
                    184: void
                    185: print_gen_head(struct html *h)
                    186: {
                    187:        struct htmlpair  tag[4];
1.42      schwarze  188:        struct tag      *t;
                    189:
                    190:        tag[0].key = ATTR_CHARSET;
                    191:        tag[0].val = "utf-8";
                    192:        print_otag(h, TAG_META, 1, tag);
1.1       schwarze  193:
1.42      schwarze  194:        /*
                    195:         * Print a default style-sheet.
                    196:         */
                    197:        t = print_otag(h, TAG_STYLE, 0, NULL);
                    198:        print_text(h, "table.head, table.foot { width: 100%; }\n"
                    199:              "td.head-rtitle, td.foot-os { text-align: right; }\n"
                    200:              "td.head-vol { text-align: center; }\n"
                    201:              "table.foot td { width: 50%; }\n"
                    202:              "table.head td { width: 33%; }\n"
                    203:              "div.spacer { margin: 1em 0; }\n");
                    204:        print_tagq(h, t);
1.1       schwarze  205:
                    206:        if (h->style) {
                    207:                tag[0].key = ATTR_REL;
                    208:                tag[0].val = "stylesheet";
                    209:                tag[1].key = ATTR_HREF;
                    210:                tag[1].val = h->style;
                    211:                tag[2].key = ATTR_TYPE;
                    212:                tag[2].val = "text/css";
                    213:                tag[3].key = ATTR_MEDIA;
                    214:                tag[3].val = "all";
                    215:                print_otag(h, TAG_LINK, 4, tag);
                    216:        }
                    217: }
                    218:
1.5       schwarze  219: static void
1.26      schwarze  220: print_metaf(struct html *h, enum mandoc_esc deco)
1.5       schwarze  221: {
                    222:        enum htmlfont    font;
1.1       schwarze  223:
1.5       schwarze  224:        switch (deco) {
1.35      schwarze  225:        case ESCAPE_FONTPREV:
1.5       schwarze  226:                font = h->metal;
                    227:                break;
1.35      schwarze  228:        case ESCAPE_FONTITALIC:
1.5       schwarze  229:                font = HTMLFONT_ITALIC;
                    230:                break;
1.35      schwarze  231:        case ESCAPE_FONTBOLD:
1.5       schwarze  232:                font = HTMLFONT_BOLD;
                    233:                break;
1.35      schwarze  234:        case ESCAPE_FONTBI:
1.31      schwarze  235:                font = HTMLFONT_BI;
                    236:                break;
1.35      schwarze  237:        case ESCAPE_FONT:
1.26      schwarze  238:                /* FALLTHROUGH */
1.35      schwarze  239:        case ESCAPE_FONTROMAN:
1.5       schwarze  240:                font = HTMLFONT_NONE;
                    241:                break;
                    242:        default:
                    243:                abort();
                    244:                /* NOTREACHED */
1.1       schwarze  245:        }
                    246:
1.20      schwarze  247:        if (h->metaf) {
                    248:                print_tagq(h, h->metaf);
                    249:                h->metaf = NULL;
                    250:        }
                    251:
                    252:        h->metal = h->metac;
                    253:        h->metac = font;
                    254:
1.31      schwarze  255:        switch (font) {
1.35      schwarze  256:        case HTMLFONT_ITALIC:
1.31      schwarze  257:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    258:                break;
1.35      schwarze  259:        case HTMLFONT_BOLD:
1.31      schwarze  260:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    261:                break;
1.35      schwarze  262:        case HTMLFONT_BI:
1.31      schwarze  263:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    264:                print_otag(h, TAG_I, 0, NULL);
                    265:                break;
                    266:        default:
                    267:                break;
                    268:        }
1.1       schwarze  269: }
                    270:
1.26      schwarze  271: int
                    272: html_strlen(const char *cp)
                    273: {
1.30      schwarze  274:        size_t           rsz;
                    275:        int              skip, sz;
1.26      schwarze  276:
                    277:        /*
                    278:         * Account for escaped sequences within string length
                    279:         * calculations.  This follows the logic in term_strlen() as we
                    280:         * must calculate the width of produced strings.
                    281:         * Assume that characters are always width of "1".  This is
                    282:         * hacky, but it gets the job done for approximation of widths.
                    283:         */
                    284:
                    285:        sz = 0;
1.30      schwarze  286:        skip = 0;
                    287:        while (1) {
                    288:                rsz = strcspn(cp, "\\");
                    289:                if (rsz) {
                    290:                        cp += rsz;
                    291:                        if (skip) {
                    292:                                skip = 0;
                    293:                                rsz--;
                    294:                        }
                    295:                        sz += rsz;
                    296:                }
                    297:                if ('\0' == *cp)
                    298:                        break;
                    299:                cp++;
                    300:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.35      schwarze  301:                case ESCAPE_ERROR:
1.26      schwarze  302:                        return(sz);
1.35      schwarze  303:                case ESCAPE_UNICODE:
1.26      schwarze  304:                        /* FALLTHROUGH */
1.35      schwarze  305:                case ESCAPE_NUMBERED:
1.26      schwarze  306:                        /* FALLTHROUGH */
1.35      schwarze  307:                case ESCAPE_SPECIAL:
1.30      schwarze  308:                        if (skip)
                    309:                                skip = 0;
                    310:                        else
                    311:                                sz++;
                    312:                        break;
1.35      schwarze  313:                case ESCAPE_SKIPCHAR:
1.30      schwarze  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
1.38      schwarze  324: print_escape(char c)
                    325: {
                    326:
                    327:        switch (c) {
                    328:        case '<':
                    329:                printf("&lt;");
                    330:                break;
                    331:        case '>':
                    332:                printf("&gt;");
                    333:                break;
                    334:        case '&':
                    335:                printf("&amp;");
                    336:                break;
                    337:        case '"':
                    338:                printf("&quot;");
                    339:                break;
                    340:        case ASCII_NBRSP:
                    341:                putchar('-');
                    342:                break;
                    343:        case ASCII_HYPH:
                    344:                putchar('-');
                    345:                /* FALLTHROUGH */
                    346:        case ASCII_BREAK:
                    347:                break;
                    348:        default:
                    349:                return(0);
                    350:        }
                    351:        return(1);
                    352: }
                    353:
                    354: static int
1.5       schwarze  355: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  356: {
1.4       schwarze  357:        size_t           sz;
1.26      schwarze  358:        int              c, len, nospace;
1.5       schwarze  359:        const char      *seq;
1.26      schwarze  360:        enum mandoc_esc  esc;
1.37      schwarze  361:        static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33      schwarze  362:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  363:
                    364:        nospace = 0;
1.1       schwarze  365:
1.26      schwarze  366:        while ('\0' != *p) {
1.30      schwarze  367:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    368:                        h->flags &= ~HTML_SKIPCHAR;
                    369:                        p++;
                    370:                        continue;
                    371:                }
                    372:
1.9       schwarze  373:                sz = strcspn(p, rejs);
1.4       schwarze  374:
                    375:                fwrite(p, 1, sz, stdout);
1.26      schwarze  376:                p += (int)sz;
1.4       schwarze  377:
1.26      schwarze  378:                if ('\0' == *p)
                    379:                        break;
                    380:
1.38      schwarze  381:                if (print_escape(*p++))
1.33      schwarze  382:                        continue;
1.4       schwarze  383:
1.26      schwarze  384:                esc = mandoc_escape(&p, &seq, &len);
                    385:                if (ESCAPE_ERROR == esc)
                    386:                        break;
1.5       schwarze  387:
1.26      schwarze  388:                switch (esc) {
1.35      schwarze  389:                case ESCAPE_FONT:
1.30      schwarze  390:                        /* FALLTHROUGH */
1.35      schwarze  391:                case ESCAPE_FONTPREV:
1.30      schwarze  392:                        /* FALLTHROUGH */
1.35      schwarze  393:                case ESCAPE_FONTBOLD:
1.30      schwarze  394:                        /* FALLTHROUGH */
1.35      schwarze  395:                case ESCAPE_FONTITALIC:
1.30      schwarze  396:                        /* FALLTHROUGH */
1.35      schwarze  397:                case ESCAPE_FONTBI:
1.31      schwarze  398:                        /* FALLTHROUGH */
1.35      schwarze  399:                case ESCAPE_FONTROMAN:
1.30      schwarze  400:                        if (0 == norecurse)
                    401:                                print_metaf(h, esc);
                    402:                        continue;
1.35      schwarze  403:                case ESCAPE_SKIPCHAR:
1.30      schwarze  404:                        h->flags |= HTML_SKIPCHAR;
                    405:                        continue;
                    406:                default:
                    407:                        break;
                    408:                }
                    409:
                    410:                if (h->flags & HTML_SKIPCHAR) {
                    411:                        h->flags &= ~HTML_SKIPCHAR;
                    412:                        continue;
                    413:                }
                    414:
                    415:                switch (esc) {
1.35      schwarze  416:                case ESCAPE_UNICODE:
1.38      schwarze  417:                        /* Skip past "u" header. */
1.26      schwarze  418:                        c = mchars_num2uc(seq + 1, len - 1);
                    419:                        break;
1.35      schwarze  420:                case ESCAPE_NUMBERED:
1.26      schwarze  421:                        c = mchars_num2char(seq, len);
1.51      schwarze  422:                        if (c < 0)
                    423:                                continue;
1.26      schwarze  424:                        break;
1.35      schwarze  425:                case ESCAPE_SPECIAL:
1.26      schwarze  426:                        c = mchars_spec2cp(h->symtab, seq, len);
1.51      schwarze  427:                        if (c <= 0)
                    428:                                continue;
1.26      schwarze  429:                        break;
1.35      schwarze  430:                case ESCAPE_NOSPACE:
1.26      schwarze  431:                        if ('\0' == *p)
                    432:                                nospace = 1;
1.49      schwarze  433:                        continue;
1.5       schwarze  434:                default:
1.49      schwarze  435:                        continue;
1.5       schwarze  436:                }
1.51      schwarze  437:                if ((c < 0x20 && c != 0x09) ||
                    438:                    (c > 0x7E && c < 0xA0))
1.49      schwarze  439:                        c = 0xFFFD;
                    440:                if (c > 0x7E)
                    441:                        printf("&#%d;", c);
                    442:                else if ( ! print_escape(c))
                    443:                        putchar(c);
1.1       schwarze  444:        }
1.5       schwarze  445:
                    446:        return(nospace);
1.1       schwarze  447: }
                    448:
1.6       schwarze  449: static void
                    450: print_attr(struct html *h, const char *key, const char *val)
                    451: {
                    452:        printf(" %s=\"", key);
                    453:        (void)print_encode(h, val, 1);
                    454:        putchar('\"');
                    455: }
                    456:
1.1       schwarze  457: struct tag *
1.35      schwarze  458: print_otag(struct html *h, enum htmltag tag,
1.1       schwarze  459:                int sz, const struct htmlpair *p)
                    460: {
                    461:        int              i;
                    462:        struct tag      *t;
                    463:
1.6       schwarze  464:        /* Push this tags onto the stack of open scopes. */
                    465:
1.1       schwarze  466:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24      schwarze  467:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  468:                t->tag = tag;
1.2       schwarze  469:                t->next = h->tags.head;
                    470:                h->tags.head = t;
1.1       schwarze  471:        } else
                    472:                t = NULL;
                    473:
                    474:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  475:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    476:                        /* Manage keeps! */
                    477:                        if ( ! (HTML_KEEP & h->flags)) {
                    478:                                if (HTML_PREKEEP & h->flags)
                    479:                                        h->flags |= HTML_KEEP;
                    480:                                putchar(' ');
                    481:                        } else
                    482:                                printf("&#160;");
                    483:                }
1.1       schwarze  484:
1.13      schwarze  485:        if ( ! (h->flags & HTML_NONOSPACE))
                    486:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  487:        else
                    488:                h->flags |= HTML_NOSPACE;
1.13      schwarze  489:
1.6       schwarze  490:        /* Print out the tag name and attributes. */
                    491:
1.1       schwarze  492:        printf("<%s", htmltags[tag].name);
1.6       schwarze  493:        for (i = 0; i < sz; i++)
                    494:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    495:
1.42      schwarze  496:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  497:
                    498:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42      schwarze  499:                putchar('/');
1.6       schwarze  500:
1.4       schwarze  501:        putchar('>');
1.1       schwarze  502:
                    503:        h->flags |= HTML_NOSPACE;
1.18      schwarze  504:
                    505:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    506:                putchar('\n');
                    507:
1.1       schwarze  508:        return(t);
                    509: }
                    510:
                    511: static void
                    512: print_ctag(struct html *h, enum htmltag tag)
                    513: {
1.35      schwarze  514:
1.1       schwarze  515:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  516:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  517:                h->flags |= HTML_NOSPACE;
1.4       schwarze  518:                putchar('\n');
1.35      schwarze  519:        }
1.1       schwarze  520: }
                    521:
                    522: void
1.6       schwarze  523: print_gen_decls(struct html *h)
                    524: {
                    525:
1.42      schwarze  526:        puts("<!DOCTYPE html>");
1.1       schwarze  527: }
                    528:
                    529: void
1.12      schwarze  530: print_text(struct html *h, const char *word)
1.1       schwarze  531: {
                    532:
1.12      schwarze  533:        if ( ! (HTML_NOSPACE & h->flags)) {
                    534:                /* Manage keeps! */
                    535:                if ( ! (HTML_KEEP & h->flags)) {
                    536:                        if (HTML_PREKEEP & h->flags)
                    537:                                h->flags |= HTML_KEEP;
                    538:                        putchar(' ');
                    539:                } else
                    540:                        printf("&#160;");
                    541:        }
1.1       schwarze  542:
1.20      schwarze  543:        assert(NULL == h->metaf);
1.31      schwarze  544:        switch (h->metac) {
1.35      schwarze  545:        case HTMLFONT_ITALIC:
1.31      schwarze  546:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    547:                break;
1.35      schwarze  548:        case HTMLFONT_BOLD:
1.31      schwarze  549:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    550:                break;
1.35      schwarze  551:        case HTMLFONT_BI:
1.31      schwarze  552:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    553:                print_otag(h, TAG_I, 0, NULL);
                    554:                break;
                    555:        default:
                    556:                break;
                    557:        }
1.20      schwarze  558:
1.12      schwarze  559:        assert(word);
1.28      schwarze  560:        if ( ! print_encode(h, word, 0)) {
1.13      schwarze  561:                if ( ! (h->flags & HTML_NONOSPACE))
                    562:                        h->flags &= ~HTML_NOSPACE;
1.53    ! schwarze  563:                h->flags &= ~HTML_NONEWLINE;
1.28      schwarze  564:        } else
1.53    ! schwarze  565:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
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: }