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

1.63    ! schwarze    1: /*     $OpenBSD: html.c,v 1.62 2015/12/25 20:43:04 bentley Exp $ */
1.1       schwarze    2: /*
1.42      schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.55      schwarze    4:  * Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.56      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.56      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   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"
1.56      schwarze   33: #include "manconf.h"
1.1       schwarze   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);
1.54      schwarze  123: static void     print_ctag(struct html *, struct tag *);
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 *
1.61      schwarze  131: html_alloc(const struct manoutput *outopts)
1.1       schwarze  132: {
                    133:        struct html     *h;
                    134:
1.24      schwarze  135:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  136:
1.2       schwarze  137:        h->tags.head = NULL;
1.56      schwarze  138:        h->style = outopts->style;
                    139:        h->base_man = outopts->man;
                    140:        h->base_includes = outopts->includes;
                    141:        if (outopts->fragment)
                    142:                h->oflags |= HTML_FRAGMENT;
1.1       schwarze  143:
1.58      schwarze  144:        return h;
1.1       schwarze  145: }
                    146:
                    147: void
                    148: html_free(void *p)
                    149: {
                    150:        struct tag      *tag;
                    151:        struct html     *h;
                    152:
                    153:        h = (struct html *)p;
                    154:
1.2       schwarze  155:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  156:                h->tags.head = tag->next;
1.1       schwarze  157:                free(tag);
                    158:        }
                    159:
                    160:        free(h);
                    161: }
                    162:
                    163: void
                    164: print_gen_head(struct html *h)
                    165: {
                    166:        struct htmlpair  tag[4];
1.42      schwarze  167:        struct tag      *t;
                    168:
                    169:        tag[0].key = ATTR_CHARSET;
                    170:        tag[0].val = "utf-8";
                    171:        print_otag(h, TAG_META, 1, tag);
1.1       schwarze  172:
1.42      schwarze  173:        /*
                    174:         * Print a default style-sheet.
                    175:         */
                    176:        t = print_otag(h, TAG_STYLE, 0, NULL);
                    177:        print_text(h, "table.head, table.foot { width: 100%; }\n"
                    178:              "td.head-rtitle, td.foot-os { text-align: right; }\n"
                    179:              "td.head-vol { text-align: center; }\n"
                    180:              "table.foot td { width: 50%; }\n"
                    181:              "table.head td { width: 33%; }\n"
                    182:              "div.spacer { margin: 1em 0; }\n");
                    183:        print_tagq(h, t);
1.1       schwarze  184:
                    185:        if (h->style) {
                    186:                tag[0].key = ATTR_REL;
                    187:                tag[0].val = "stylesheet";
                    188:                tag[1].key = ATTR_HREF;
                    189:                tag[1].val = h->style;
                    190:                tag[2].key = ATTR_TYPE;
                    191:                tag[2].val = "text/css";
                    192:                tag[3].key = ATTR_MEDIA;
                    193:                tag[3].val = "all";
                    194:                print_otag(h, TAG_LINK, 4, tag);
                    195:        }
                    196: }
                    197:
1.5       schwarze  198: static void
1.26      schwarze  199: print_metaf(struct html *h, enum mandoc_esc deco)
1.5       schwarze  200: {
                    201:        enum htmlfont    font;
1.1       schwarze  202:
1.5       schwarze  203:        switch (deco) {
1.35      schwarze  204:        case ESCAPE_FONTPREV:
1.5       schwarze  205:                font = h->metal;
                    206:                break;
1.35      schwarze  207:        case ESCAPE_FONTITALIC:
1.5       schwarze  208:                font = HTMLFONT_ITALIC;
                    209:                break;
1.35      schwarze  210:        case ESCAPE_FONTBOLD:
1.5       schwarze  211:                font = HTMLFONT_BOLD;
                    212:                break;
1.35      schwarze  213:        case ESCAPE_FONTBI:
1.31      schwarze  214:                font = HTMLFONT_BI;
                    215:                break;
1.35      schwarze  216:        case ESCAPE_FONT:
                    217:        case ESCAPE_FONTROMAN:
1.5       schwarze  218:                font = HTMLFONT_NONE;
                    219:                break;
                    220:        default:
                    221:                abort();
1.1       schwarze  222:        }
                    223:
1.20      schwarze  224:        if (h->metaf) {
                    225:                print_tagq(h, h->metaf);
                    226:                h->metaf = NULL;
                    227:        }
                    228:
                    229:        h->metal = h->metac;
                    230:        h->metac = font;
                    231:
1.31      schwarze  232:        switch (font) {
1.35      schwarze  233:        case HTMLFONT_ITALIC:
1.31      schwarze  234:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    235:                break;
1.35      schwarze  236:        case HTMLFONT_BOLD:
1.31      schwarze  237:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    238:                break;
1.35      schwarze  239:        case HTMLFONT_BI:
1.31      schwarze  240:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    241:                print_otag(h, TAG_I, 0, NULL);
                    242:                break;
                    243:        default:
                    244:                break;
                    245:        }
1.1       schwarze  246: }
                    247:
1.26      schwarze  248: int
                    249: html_strlen(const char *cp)
                    250: {
1.30      schwarze  251:        size_t           rsz;
                    252:        int              skip, sz;
1.26      schwarze  253:
                    254:        /*
                    255:         * Account for escaped sequences within string length
                    256:         * calculations.  This follows the logic in term_strlen() as we
                    257:         * must calculate the width of produced strings.
                    258:         * Assume that characters are always width of "1".  This is
                    259:         * hacky, but it gets the job done for approximation of widths.
                    260:         */
                    261:
                    262:        sz = 0;
1.30      schwarze  263:        skip = 0;
                    264:        while (1) {
                    265:                rsz = strcspn(cp, "\\");
                    266:                if (rsz) {
                    267:                        cp += rsz;
                    268:                        if (skip) {
                    269:                                skip = 0;
                    270:                                rsz--;
                    271:                        }
                    272:                        sz += rsz;
                    273:                }
                    274:                if ('\0' == *cp)
                    275:                        break;
                    276:                cp++;
                    277:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.35      schwarze  278:                case ESCAPE_ERROR:
1.58      schwarze  279:                        return sz;
1.35      schwarze  280:                case ESCAPE_UNICODE:
                    281:                case ESCAPE_NUMBERED:
                    282:                case ESCAPE_SPECIAL:
1.55      schwarze  283:                case ESCAPE_OVERSTRIKE:
1.30      schwarze  284:                        if (skip)
                    285:                                skip = 0;
                    286:                        else
                    287:                                sz++;
                    288:                        break;
1.35      schwarze  289:                case ESCAPE_SKIPCHAR:
1.30      schwarze  290:                        skip = 1;
1.26      schwarze  291:                        break;
                    292:                default:
                    293:                        break;
                    294:                }
                    295:        }
1.58      schwarze  296:        return sz;
1.26      schwarze  297: }
1.1       schwarze  298:
1.5       schwarze  299: static int
1.38      schwarze  300: print_escape(char c)
                    301: {
                    302:
                    303:        switch (c) {
                    304:        case '<':
                    305:                printf("&lt;");
                    306:                break;
                    307:        case '>':
                    308:                printf("&gt;");
                    309:                break;
                    310:        case '&':
                    311:                printf("&amp;");
                    312:                break;
                    313:        case '"':
                    314:                printf("&quot;");
                    315:                break;
                    316:        case ASCII_NBRSP:
1.60      schwarze  317:                printf("&nbsp;");
1.38      schwarze  318:                break;
                    319:        case ASCII_HYPH:
                    320:                putchar('-');
1.59      schwarze  321:                break;
1.38      schwarze  322:        case ASCII_BREAK:
                    323:                break;
                    324:        default:
1.58      schwarze  325:                return 0;
1.38      schwarze  326:        }
1.58      schwarze  327:        return 1;
1.38      schwarze  328: }
                    329:
                    330: static int
1.5       schwarze  331: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  332: {
1.4       schwarze  333:        size_t           sz;
1.26      schwarze  334:        int              c, len, nospace;
1.5       schwarze  335:        const char      *seq;
1.26      schwarze  336:        enum mandoc_esc  esc;
1.37      schwarze  337:        static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33      schwarze  338:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  339:
                    340:        nospace = 0;
1.1       schwarze  341:
1.26      schwarze  342:        while ('\0' != *p) {
1.30      schwarze  343:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    344:                        h->flags &= ~HTML_SKIPCHAR;
                    345:                        p++;
                    346:                        continue;
                    347:                }
                    348:
1.9       schwarze  349:                sz = strcspn(p, rejs);
1.4       schwarze  350:
                    351:                fwrite(p, 1, sz, stdout);
1.26      schwarze  352:                p += (int)sz;
1.4       schwarze  353:
1.26      schwarze  354:                if ('\0' == *p)
                    355:                        break;
                    356:
1.38      schwarze  357:                if (print_escape(*p++))
1.33      schwarze  358:                        continue;
1.4       schwarze  359:
1.26      schwarze  360:                esc = mandoc_escape(&p, &seq, &len);
                    361:                if (ESCAPE_ERROR == esc)
                    362:                        break;
1.5       schwarze  363:
1.26      schwarze  364:                switch (esc) {
1.35      schwarze  365:                case ESCAPE_FONT:
                    366:                case ESCAPE_FONTPREV:
                    367:                case ESCAPE_FONTBOLD:
                    368:                case ESCAPE_FONTITALIC:
                    369:                case ESCAPE_FONTBI:
                    370:                case ESCAPE_FONTROMAN:
1.30      schwarze  371:                        if (0 == norecurse)
                    372:                                print_metaf(h, esc);
                    373:                        continue;
1.35      schwarze  374:                case ESCAPE_SKIPCHAR:
1.30      schwarze  375:                        h->flags |= HTML_SKIPCHAR;
                    376:                        continue;
                    377:                default:
                    378:                        break;
                    379:                }
                    380:
                    381:                if (h->flags & HTML_SKIPCHAR) {
                    382:                        h->flags &= ~HTML_SKIPCHAR;
                    383:                        continue;
                    384:                }
                    385:
                    386:                switch (esc) {
1.35      schwarze  387:                case ESCAPE_UNICODE:
1.38      schwarze  388:                        /* Skip past "u" header. */
1.26      schwarze  389:                        c = mchars_num2uc(seq + 1, len - 1);
                    390:                        break;
1.35      schwarze  391:                case ESCAPE_NUMBERED:
1.26      schwarze  392:                        c = mchars_num2char(seq, len);
1.51      schwarze  393:                        if (c < 0)
                    394:                                continue;
1.26      schwarze  395:                        break;
1.35      schwarze  396:                case ESCAPE_SPECIAL:
1.61      schwarze  397:                        c = mchars_spec2cp(seq, len);
1.51      schwarze  398:                        if (c <= 0)
                    399:                                continue;
1.26      schwarze  400:                        break;
1.35      schwarze  401:                case ESCAPE_NOSPACE:
1.26      schwarze  402:                        if ('\0' == *p)
                    403:                                nospace = 1;
1.49      schwarze  404:                        continue;
1.55      schwarze  405:                case ESCAPE_OVERSTRIKE:
                    406:                        if (len == 0)
                    407:                                continue;
                    408:                        c = seq[len - 1];
                    409:                        break;
1.5       schwarze  410:                default:
1.49      schwarze  411:                        continue;
1.5       schwarze  412:                }
1.51      schwarze  413:                if ((c < 0x20 && c != 0x09) ||
                    414:                    (c > 0x7E && c < 0xA0))
1.49      schwarze  415:                        c = 0xFFFD;
                    416:                if (c > 0x7E)
                    417:                        printf("&#%d;", c);
                    418:                else if ( ! print_escape(c))
                    419:                        putchar(c);
1.1       schwarze  420:        }
1.5       schwarze  421:
1.58      schwarze  422:        return nospace;
1.1       schwarze  423: }
                    424:
1.6       schwarze  425: static void
                    426: print_attr(struct html *h, const char *key, const char *val)
                    427: {
                    428:        printf(" %s=\"", key);
                    429:        (void)print_encode(h, val, 1);
                    430:        putchar('\"');
                    431: }
                    432:
1.1       schwarze  433: struct tag *
1.35      schwarze  434: print_otag(struct html *h, enum htmltag tag,
1.1       schwarze  435:                int sz, const struct htmlpair *p)
                    436: {
                    437:        int              i;
                    438:        struct tag      *t;
                    439:
1.6       schwarze  440:        /* Push this tags onto the stack of open scopes. */
                    441:
1.1       schwarze  442:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24      schwarze  443:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  444:                t->tag = tag;
1.2       schwarze  445:                t->next = h->tags.head;
                    446:                h->tags.head = t;
1.1       schwarze  447:        } else
                    448:                t = NULL;
                    449:
                    450:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  451:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    452:                        /* Manage keeps! */
                    453:                        if ( ! (HTML_KEEP & h->flags)) {
                    454:                                if (HTML_PREKEEP & h->flags)
                    455:                                        h->flags |= HTML_KEEP;
                    456:                                putchar(' ');
                    457:                        } else
                    458:                                printf("&#160;");
                    459:                }
1.1       schwarze  460:
1.13      schwarze  461:        if ( ! (h->flags & HTML_NONOSPACE))
                    462:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  463:        else
                    464:                h->flags |= HTML_NOSPACE;
1.13      schwarze  465:
1.6       schwarze  466:        /* Print out the tag name and attributes. */
                    467:
1.1       schwarze  468:        printf("<%s", htmltags[tag].name);
1.6       schwarze  469:        for (i = 0; i < sz; i++)
                    470:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    471:
1.42      schwarze  472:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  473:
                    474:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42      schwarze  475:                putchar('/');
1.6       schwarze  476:
1.4       schwarze  477:        putchar('>');
1.1       schwarze  478:
                    479:        h->flags |= HTML_NOSPACE;
1.18      schwarze  480:
                    481:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    482:                putchar('\n');
                    483:
1.58      schwarze  484:        return t;
1.1       schwarze  485: }
                    486:
                    487: static void
1.54      schwarze  488: print_ctag(struct html *h, struct tag *tag)
1.1       schwarze  489: {
1.35      schwarze  490:
1.54      schwarze  491:        /*
                    492:         * Remember to close out and nullify the current
                    493:         * meta-font and table, if applicable.
                    494:         */
                    495:        if (tag == h->metaf)
                    496:                h->metaf = NULL;
                    497:        if (tag == h->tblt)
                    498:                h->tblt = NULL;
                    499:
                    500:        printf("</%s>", htmltags[tag->tag].name);
                    501:        if (HTML_CLRLINE & htmltags[tag->tag].flags) {
1.1       schwarze  502:                h->flags |= HTML_NOSPACE;
1.4       schwarze  503:                putchar('\n');
1.35      schwarze  504:        }
1.54      schwarze  505:
                    506:        h->tags.head = tag->next;
                    507:        free(tag);
1.1       schwarze  508: }
                    509:
                    510: void
1.6       schwarze  511: print_gen_decls(struct html *h)
                    512: {
                    513:
1.42      schwarze  514:        puts("<!DOCTYPE html>");
1.1       schwarze  515: }
                    516:
                    517: void
1.12      schwarze  518: print_text(struct html *h, const char *word)
1.1       schwarze  519: {
                    520:
1.12      schwarze  521:        if ( ! (HTML_NOSPACE & h->flags)) {
                    522:                /* Manage keeps! */
                    523:                if ( ! (HTML_KEEP & h->flags)) {
                    524:                        if (HTML_PREKEEP & h->flags)
                    525:                                h->flags |= HTML_KEEP;
                    526:                        putchar(' ');
                    527:                } else
                    528:                        printf("&#160;");
                    529:        }
1.1       schwarze  530:
1.20      schwarze  531:        assert(NULL == h->metaf);
1.31      schwarze  532:        switch (h->metac) {
1.35      schwarze  533:        case HTMLFONT_ITALIC:
1.31      schwarze  534:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    535:                break;
1.35      schwarze  536:        case HTMLFONT_BOLD:
1.31      schwarze  537:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    538:                break;
1.35      schwarze  539:        case HTMLFONT_BI:
1.31      schwarze  540:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    541:                print_otag(h, TAG_I, 0, NULL);
                    542:                break;
                    543:        default:
                    544:                break;
                    545:        }
1.20      schwarze  546:
1.12      schwarze  547:        assert(word);
1.28      schwarze  548:        if ( ! print_encode(h, word, 0)) {
1.13      schwarze  549:                if ( ! (h->flags & HTML_NONOSPACE))
                    550:                        h->flags &= ~HTML_NOSPACE;
1.53      schwarze  551:                h->flags &= ~HTML_NONEWLINE;
1.28      schwarze  552:        } else
1.53      schwarze  553:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20      schwarze  554:
                    555:        if (h->metaf) {
                    556:                print_tagq(h, h->metaf);
                    557:                h->metaf = NULL;
                    558:        }
1.17      schwarze  559:
                    560:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  561: }
                    562:
                    563: void
                    564: print_tagq(struct html *h, const struct tag *until)
                    565: {
                    566:        struct tag      *tag;
                    567:
1.2       schwarze  568:        while ((tag = h->tags.head) != NULL) {
1.54      schwarze  569:                print_ctag(h, tag);
1.1       schwarze  570:                if (until && tag == until)
                    571:                        return;
                    572:        }
                    573: }
                    574:
                    575: void
                    576: print_stagq(struct html *h, const struct tag *suntil)
                    577: {
                    578:        struct tag      *tag;
                    579:
1.2       schwarze  580:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  581:                if (suntil && tag == suntil)
                    582:                        return;
1.54      schwarze  583:                print_ctag(h, tag);
1.1       schwarze  584:        }
                    585: }
1.42      schwarze  586:
                    587: void
                    588: print_paragraph(struct html *h)
                    589: {
                    590:        struct tag      *t;
                    591:        struct htmlpair  tag;
                    592:
                    593:        PAIR_CLASS_INIT(&tag, "spacer");
                    594:        t = print_otag(h, TAG_DIV, 1, &tag);
                    595:        print_tagq(h, t);
                    596: }
                    597:
1.1       schwarze  598:
                    599: void
                    600: bufinit(struct html *h)
                    601: {
                    602:
                    603:        h->buf[0] = '\0';
                    604:        h->buflen = 0;
                    605: }
                    606:
                    607: void
                    608: bufcat_style(struct html *h, const char *key, const char *val)
                    609: {
                    610:
                    611:        bufcat(h, key);
1.26      schwarze  612:        bufcat(h, ":");
1.1       schwarze  613:        bufcat(h, val);
1.26      schwarze  614:        bufcat(h, ";");
1.1       schwarze  615: }
                    616:
                    617: void
                    618: bufcat(struct html *h, const char *p)
                    619: {
1.36      schwarze  620:
                    621:        /*
                    622:         * XXX This is broken and not easy to fix.
                    623:         * When using the -Oincludes option, buffmt_includes()
                    624:         * may pass in strings overrunning BUFSIZ, causing a crash.
                    625:         */
1.1       schwarze  626:
1.26      schwarze  627:        h->buflen = strlcat(h->buf, p, BUFSIZ);
                    628:        assert(h->buflen < BUFSIZ);
1.1       schwarze  629: }
                    630:
                    631: void
1.26      schwarze  632: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1       schwarze  633: {
                    634:        va_list          ap;
                    635:
                    636:        va_start(ap, fmt);
1.35      schwarze  637:        (void)vsnprintf(h->buf + (int)h->buflen,
                    638:            BUFSIZ - h->buflen - 1, fmt, ap);
1.1       schwarze  639:        va_end(ap);
                    640:        h->buflen = strlen(h->buf);
                    641: }
                    642:
1.26      schwarze  643: static void
1.1       schwarze  644: bufncat(struct html *h, const char *p, size_t sz)
                    645: {
                    646:
1.26      schwarze  647:        assert(h->buflen + sz + 1 < BUFSIZ);
                    648:        strncat(h->buf, p, sz);
1.1       schwarze  649:        h->buflen += sz;
                    650: }
                    651:
                    652: void
                    653: buffmt_includes(struct html *h, const char *name)
                    654: {
                    655:        const char      *p, *pp;
                    656:
                    657:        pp = h->base_includes;
1.35      schwarze  658:
1.26      schwarze  659:        bufinit(h);
1.1       schwarze  660:        while (NULL != (p = strchr(pp, '%'))) {
                    661:                bufncat(h, pp, (size_t)(p - pp));
                    662:                switch (*(p + 1)) {
1.63    ! schwarze  663:                case 'I':
1.1       schwarze  664:                        bufcat(h, name);
                    665:                        break;
                    666:                default:
                    667:                        bufncat(h, p, 2);
                    668:                        break;
                    669:                }
                    670:                pp = p + 2;
                    671:        }
                    672:        if (pp)
                    673:                bufcat(h, pp);
                    674: }
                    675:
                    676: void
1.35      schwarze  677: buffmt_man(struct html *h, const char *name, const char *sec)
1.1       schwarze  678: {
                    679:        const char      *p, *pp;
                    680:
                    681:        pp = h->base_man;
1.35      schwarze  682:
1.26      schwarze  683:        bufinit(h);
1.1       schwarze  684:        while (NULL != (p = strchr(pp, '%'))) {
                    685:                bufncat(h, pp, (size_t)(p - pp));
                    686:                switch (*(p + 1)) {
1.35      schwarze  687:                case 'S':
1.1       schwarze  688:                        bufcat(h, sec ? sec : "1");
                    689:                        break;
1.35      schwarze  690:                case 'N':
1.32      schwarze  691:                        bufcat_fmt(h, "%s", name);
1.1       schwarze  692:                        break;
                    693:                default:
                    694:                        bufncat(h, p, 2);
                    695:                        break;
                    696:                }
                    697:                pp = p + 2;
                    698:        }
                    699:        if (pp)
                    700:                bufcat(h, pp);
                    701: }
                    702:
                    703: void
                    704: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    705: {
                    706:        double           v;
                    707:
                    708:        v = su->scale;
1.26      schwarze  709:        if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                    710:                v = 1.0;
1.40      schwarze  711:        else if (SCALE_BU == su->unit)
                    712:                v /= 24.0;
1.1       schwarze  713:
1.26      schwarze  714:        bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1       schwarze  715: }
                    716:
1.3       schwarze  717: void
1.26      schwarze  718: bufcat_id(struct html *h, const char *src)
1.3       schwarze  719: {
                    720:
1.62      bentley   721:        /* Cf. <http://www.w3.org/TR/html5/dom.html#the-id-attribute>. */
1.3       schwarze  722:
1.62      bentley   723:        for (; '\0' != *src; src++)
                    724:                bufncat(h, *src == ' ' ? "_" : src, 1);
1.3       schwarze  725: }