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

1.123   ! schwarze    1: /*     $OpenBSD: html.c,v 1.122 2019/01/18 14:36:16 schwarze Exp $ */
1.1       schwarze    2: /*
1.42      schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.119     schwarze    4:  * Copyright (c) 2011-2015, 2017-2019 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>
1.110     schwarze   19: #include <sys/stat.h>
1.1       schwarze   20:
                     21: #include <assert.h>
1.3       schwarze   22: #include <ctype.h>
1.4       schwarze   23: #include <stdarg.h>
1.99      schwarze   24: #include <stddef.h>
1.1       schwarze   25: #include <stdio.h>
                     26: #include <stdint.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29: #include <unistd.h>
                     30:
1.80      schwarze   31: #include "mandoc_aux.h"
1.99      schwarze   32: #include "mandoc_ohash.h"
1.9       schwarze   33: #include "mandoc.h"
1.80      schwarze   34: #include "roff.h"
1.1       schwarze   35: #include "out.h"
                     36: #include "html.h"
1.56      schwarze   37: #include "manconf.h"
1.1       schwarze   38: #include "main.h"
                     39:
                     40: struct htmldata {
                     41:        const char       *name;
                     42:        int               flags;
1.66      schwarze   43: #define        HTML_NOSTACK     (1 << 0)
                     44: #define        HTML_AUTOCLOSE   (1 << 1)
                     45: #define        HTML_NLBEFORE    (1 << 2)
                     46: #define        HTML_NLBEGIN     (1 << 3)
                     47: #define        HTML_NLEND       (1 << 4)
                     48: #define        HTML_NLAFTER     (1 << 5)
                     49: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     50: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     51: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
                     52: #define        HTML_INDENT      (1 << 6)
                     53: #define        HTML_NOINDENT    (1 << 7)
1.1       schwarze   54: };
                     55:
                     56: static const struct htmldata htmltags[TAG_MAX] = {
1.66      schwarze   57:        {"html",        HTML_NLALL},
                     58:        {"head",        HTML_NLALL | HTML_INDENT},
                     59:        {"body",        HTML_NLALL},
                     60:        {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     61:        {"title",       HTML_NLAROUND},
                     62:        {"div",         HTML_NLAROUND},
1.95      schwarze   63:        {"div",         0},
1.123   ! schwarze   64:        {"section",     HTML_NLALL},
1.66      schwarze   65:        {"h1",          HTML_NLAROUND},
                     66:        {"h2",          HTML_NLAROUND},
                     67:        {"span",        0},
                     68:        {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     69:        {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     70:        {"a",           0},
                     71:        {"table",       HTML_NLALL | HTML_INDENT},
                     72:        {"tr",          HTML_NLALL | HTML_INDENT},
                     73:        {"td",          HTML_NLAROUND},
                     74:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     75:        {"ul",          HTML_NLALL | HTML_INDENT},
                     76:        {"ol",          HTML_NLALL | HTML_INDENT},
                     77:        {"dl",          HTML_NLALL | HTML_INDENT},
                     78:        {"dt",          HTML_NLAROUND},
                     79:        {"dd",          HTML_NLAROUND | HTML_INDENT},
1.119     schwarze   80:        {"p",           HTML_NLAROUND | HTML_INDENT},
1.66      schwarze   81:        {"pre",         HTML_NLALL | HTML_NOINDENT},
1.77      schwarze   82:        {"var",         0},
1.76      schwarze   83:        {"cite",        0},
1.66      schwarze   84:        {"b",           0},
                     85:        {"i",           0},
                     86:        {"code",        0},
                     87:        {"small",       0},
                     88:        {"style",       HTML_NLALL | HTML_INDENT},
                     89:        {"math",        HTML_NLALL | HTML_INDENT},
                     90:        {"mrow",        0},
                     91:        {"mi",          0},
1.85      schwarze   92:        {"mn",          0},
1.66      schwarze   93:        {"mo",          0},
                     94:        {"msup",        0},
                     95:        {"msub",        0},
                     96:        {"msubsup",     0},
                     97:        {"mfrac",       0},
                     98:        {"msqrt",       0},
                     99:        {"mfenced",     0},
                    100:        {"mtable",      0},
                    101:        {"mtr",         0},
                    102:        {"mtd",         0},
                    103:        {"munderover",  0},
                    104:        {"munder",      0},
                    105:        {"mover",       0},
1.5       schwarze  106: };
                    107:
1.99      schwarze  108: /* Avoid duplicate HTML id= attributes. */
                    109: static struct ohash     id_unique;
                    110:
1.67      schwarze  111: static void     print_byte(struct html *, char);
                    112: static void     print_endword(struct html *);
                    113: static void     print_indent(struct html *);
                    114: static void     print_word(struct html *, const char *);
                    115:
1.54      schwarze  116: static void     print_ctag(struct html *, struct tag *);
1.67      schwarze  117: static int      print_escape(struct html *, char);
1.65      schwarze  118: static int      print_encode(struct html *, const char *, const char *, int);
                    119: static void     print_href(struct html *, const char *, const char *, int);
1.5       schwarze  120:
1.35      schwarze  121:
1.50      schwarze  122: void *
1.61      schwarze  123: html_alloc(const struct manoutput *outopts)
1.1       schwarze  124: {
                    125:        struct html     *h;
                    126:
1.24      schwarze  127:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  128:
1.74      schwarze  129:        h->tag = NULL;
1.56      schwarze  130:        h->style = outopts->style;
1.110     schwarze  131:        if ((h->base_man1 = outopts->man) == NULL)
                    132:                h->base_man2 = NULL;
                    133:        else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
                    134:                *h->base_man2++ = '\0';
1.56      schwarze  135:        h->base_includes = outopts->includes;
                    136:        if (outopts->fragment)
                    137:                h->oflags |= HTML_FRAGMENT;
1.111     schwarze  138:        if (outopts->toc)
                    139:                h->oflags |= HTML_TOC;
1.1       schwarze  140:
1.99      schwarze  141:        mandoc_ohash_init(&id_unique, 4, 0);
                    142:
1.58      schwarze  143:        return h;
1.1       schwarze  144: }
                    145:
                    146: void
                    147: html_free(void *p)
                    148: {
                    149:        struct tag      *tag;
                    150:        struct html     *h;
1.99      schwarze  151:        char            *cp;
                    152:        unsigned int     slot;
1.1       schwarze  153:
                    154:        h = (struct html *)p;
1.74      schwarze  155:        while ((tag = h->tag) != NULL) {
                    156:                h->tag = tag->next;
1.1       schwarze  157:                free(tag);
                    158:        }
1.99      schwarze  159:        free(h);
1.1       schwarze  160:
1.99      schwarze  161:        cp = ohash_first(&id_unique, &slot);
                    162:        while (cp != NULL) {
                    163:                free(cp);
                    164:                cp = ohash_next(&id_unique, &slot);
                    165:        }
                    166:        ohash_delete(&id_unique);
1.1       schwarze  167: }
                    168:
                    169: void
                    170: print_gen_head(struct html *h)
                    171: {
1.42      schwarze  172:        struct tag      *t;
                    173:
1.64      schwarze  174:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.92      schwarze  175:        if (h->style != NULL) {
                    176:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    177:                    h->style, "type", "text/css", "media", "all");
                    178:                return;
                    179:        }
1.1       schwarze  180:
1.42      schwarze  181:        /*
1.92      schwarze  182:         * Print a minimal embedded style sheet.
1.42      schwarze  183:         */
1.66      schwarze  184:
1.64      schwarze  185:        t = print_otag(h, TAG_STYLE, "");
1.66      schwarze  186:        print_text(h, "table.head, table.foot { width: 100%; }");
1.67      schwarze  187:        print_endline(h);
1.66      schwarze  188:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.67      schwarze  189:        print_endline(h);
1.66      schwarze  190:        print_text(h, "td.head-vol { text-align: center; }");
1.67      schwarze  191:        print_endline(h);
1.68      schwarze  192:        print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.95      schwarze  193:        print_endline(h);
                    194:        print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.96      schwarze  195:        print_endline(h);
1.97      schwarze  196:        print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.98      schwarze  197:        print_endline(h);
                    198:        print_text(h, "span.Ms { font-weight: bold; }");
1.94      schwarze  199:        print_endline(h);
                    200:        print_text(h, "dl.Bl-diag ");
                    201:        print_byte(h, '>');
                    202:        print_text(h, " dt { font-weight: bold; }");
1.93      schwarze  203:        print_endline(h);
                    204:        print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
                    205:            "code.In, code.Fd, code.Fn,");
                    206:        print_endline(h);
                    207:        print_text(h, "code.Cd { font-weight: bold; "
                    208:            "font-family: inherit; }");
1.42      schwarze  209:        print_tagq(h, t);
1.1       schwarze  210: }
                    211:
1.117     schwarze  212: void
1.26      schwarze  213: print_metaf(struct html *h, enum mandoc_esc deco)
1.5       schwarze  214: {
                    215:        enum htmlfont    font;
1.1       schwarze  216:
1.5       schwarze  217:        switch (deco) {
1.35      schwarze  218:        case ESCAPE_FONTPREV:
1.5       schwarze  219:                font = h->metal;
                    220:                break;
1.35      schwarze  221:        case ESCAPE_FONTITALIC:
1.5       schwarze  222:                font = HTMLFONT_ITALIC;
                    223:                break;
1.35      schwarze  224:        case ESCAPE_FONTBOLD:
1.5       schwarze  225:                font = HTMLFONT_BOLD;
                    226:                break;
1.35      schwarze  227:        case ESCAPE_FONTBI:
1.31      schwarze  228:                font = HTMLFONT_BI;
                    229:                break;
1.112     schwarze  230:        case ESCAPE_FONTCW:
                    231:                font = HTMLFONT_CW;
                    232:                break;
1.35      schwarze  233:        case ESCAPE_FONT:
                    234:        case ESCAPE_FONTROMAN:
1.5       schwarze  235:                font = HTMLFONT_NONE;
                    236:                break;
                    237:        default:
1.117     schwarze  238:                return;
1.1       schwarze  239:        }
                    240:
1.20      schwarze  241:        if (h->metaf) {
                    242:                print_tagq(h, h->metaf);
                    243:                h->metaf = NULL;
                    244:        }
                    245:
                    246:        h->metal = h->metac;
                    247:        h->metac = font;
                    248:
1.31      schwarze  249:        switch (font) {
1.35      schwarze  250:        case HTMLFONT_ITALIC:
1.64      schwarze  251:                h->metaf = print_otag(h, TAG_I, "");
1.31      schwarze  252:                break;
1.35      schwarze  253:        case HTMLFONT_BOLD:
1.64      schwarze  254:                h->metaf = print_otag(h, TAG_B, "");
1.31      schwarze  255:                break;
1.35      schwarze  256:        case HTMLFONT_BI:
1.64      schwarze  257:                h->metaf = print_otag(h, TAG_B, "");
                    258:                print_otag(h, TAG_I, "");
1.31      schwarze  259:                break;
1.112     schwarze  260:        case HTMLFONT_CW:
                    261:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
                    262:                break;
1.31      schwarze  263:        default:
                    264:                break;
                    265:        }
1.118     schwarze  266: }
                    267:
1.119     schwarze  268: void
                    269: html_close_paragraph(struct html *h)
                    270: {
                    271:        struct tag      *t;
                    272:
1.122     schwarze  273:        for (t = h->tag; t != NULL && t->closed == 0; t = t->next) {
                    274:                switch(t->tag) {
                    275:                case TAG_P:
                    276:                case TAG_PRE:
1.119     schwarze  277:                        print_tagq(h, t);
                    278:                        break;
1.122     schwarze  279:                case TAG_A:
                    280:                        print_tagq(h, t);
                    281:                        continue;
                    282:                default:
                    283:                        continue;
1.119     schwarze  284:                }
1.122     schwarze  285:                break;
1.119     schwarze  286:        }
                    287: }
                    288:
1.118     schwarze  289: /*
                    290:  * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
                    291:  * TOKEN_NONE does not switch.  The old mode is returned.
                    292:  */
                    293: enum roff_tok
                    294: html_fillmode(struct html *h, enum roff_tok want)
                    295: {
                    296:        struct tag      *t;
                    297:        enum roff_tok    had;
                    298:
                    299:        for (t = h->tag; t != NULL; t = t->next)
                    300:                if (t->tag == TAG_PRE)
                    301:                        break;
                    302:
                    303:        had = t == NULL ? ROFF_fi : ROFF_nf;
                    304:
                    305:        if (want != had) {
                    306:                switch (want) {
                    307:                case ROFF_fi:
                    308:                        print_tagq(h, t);
                    309:                        break;
                    310:                case ROFF_nf:
1.119     schwarze  311:                        html_close_paragraph(h);
1.118     schwarze  312:                        print_otag(h, TAG_PRE, "");
                    313:                        break;
                    314:                case TOKEN_NONE:
                    315:                        break;
                    316:                default:
                    317:                        abort();
                    318:                }
                    319:        }
                    320:        return had;
1.80      schwarze  321: }
                    322:
                    323: char *
1.99      schwarze  324: html_make_id(const struct roff_node *n, int unique)
1.80      schwarze  325: {
                    326:        const struct roff_node  *nch;
1.99      schwarze  327:        char                    *buf, *bufs, *cp;
                    328:        unsigned int             slot;
                    329:        int                      suffix;
1.80      schwarze  330:
                    331:        for (nch = n->child; nch != NULL; nch = nch->next)
                    332:                if (nch->type != ROFFT_TEXT)
                    333:                        return NULL;
                    334:
                    335:        buf = NULL;
                    336:        deroff(&buf, n);
1.90      schwarze  337:        if (buf == NULL)
                    338:                return NULL;
1.80      schwarze  339:
1.100     schwarze  340:        /*
                    341:         * In ID attributes, only use ASCII characters that are
                    342:         * permitted in URL-fragment strings according to the
                    343:         * explicit list at:
                    344:         * https://url.spec.whatwg.org/#url-fragment-string
                    345:         */
1.80      schwarze  346:
                    347:        for (cp = buf; *cp != '\0'; cp++)
1.100     schwarze  348:                if (isalnum((unsigned char)*cp) == 0 &&
                    349:                    strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.80      schwarze  350:                        *cp = '_';
                    351:
1.99      schwarze  352:        if (unique == 0)
                    353:                return buf;
                    354:
                    355:        /* Avoid duplicate HTML id= attributes. */
                    356:
                    357:        bufs = NULL;
                    358:        suffix = 1;
                    359:        slot = ohash_qlookup(&id_unique, buf);
                    360:        cp = ohash_find(&id_unique, slot);
                    361:        if (cp != NULL) {
                    362:                while (cp != NULL) {
                    363:                        free(bufs);
                    364:                        if (++suffix > 127) {
                    365:                                free(buf);
                    366:                                return NULL;
                    367:                        }
                    368:                        mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
                    369:                        slot = ohash_qlookup(&id_unique, bufs);
                    370:                        cp = ohash_find(&id_unique, slot);
                    371:                }
                    372:                free(buf);
                    373:                buf = bufs;
                    374:        }
                    375:        ohash_insert(&id_unique, slot, buf);
1.80      schwarze  376:        return buf;
1.1       schwarze  377: }
                    378:
1.5       schwarze  379: static int
1.67      schwarze  380: print_escape(struct html *h, char c)
1.38      schwarze  381: {
                    382:
                    383:        switch (c) {
                    384:        case '<':
1.67      schwarze  385:                print_word(h, "&lt;");
1.38      schwarze  386:                break;
                    387:        case '>':
1.67      schwarze  388:                print_word(h, "&gt;");
1.38      schwarze  389:                break;
                    390:        case '&':
1.67      schwarze  391:                print_word(h, "&amp;");
1.38      schwarze  392:                break;
                    393:        case '"':
1.67      schwarze  394:                print_word(h, "&quot;");
1.38      schwarze  395:                break;
                    396:        case ASCII_NBRSP:
1.67      schwarze  397:                print_word(h, "&nbsp;");
1.38      schwarze  398:                break;
                    399:        case ASCII_HYPH:
1.67      schwarze  400:                print_byte(h, '-');
1.59      schwarze  401:                break;
1.38      schwarze  402:        case ASCII_BREAK:
                    403:                break;
                    404:        default:
1.58      schwarze  405:                return 0;
1.38      schwarze  406:        }
1.58      schwarze  407:        return 1;
1.38      schwarze  408: }
                    409:
                    410: static int
1.65      schwarze  411: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.1       schwarze  412: {
1.67      schwarze  413:        char             numbuf[16];
1.84      schwarze  414:        const char      *seq;
1.4       schwarze  415:        size_t           sz;
1.84      schwarze  416:        int              c, len, breakline, nospace;
1.26      schwarze  417:        enum mandoc_esc  esc;
1.84      schwarze  418:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.33      schwarze  419:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  420:
1.65      schwarze  421:        if (pend == NULL)
                    422:                pend = strchr(p, '\0');
                    423:
1.84      schwarze  424:        breakline = 0;
1.5       schwarze  425:        nospace = 0;
1.1       schwarze  426:
1.65      schwarze  427:        while (p < pend) {
1.30      schwarze  428:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    429:                        h->flags &= ~HTML_SKIPCHAR;
                    430:                        p++;
                    431:                        continue;
                    432:                }
                    433:
1.67      schwarze  434:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.84      schwarze  435:                        print_byte(h, *p);
                    436:
                    437:                if (breakline &&
                    438:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.115     schwarze  439:                        print_otag(h, TAG_BR, "");
1.84      schwarze  440:                        breakline = 0;
                    441:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    442:                                p++;
                    443:                        continue;
                    444:                }
1.4       schwarze  445:
1.65      schwarze  446:                if (p >= pend)
1.26      schwarze  447:                        break;
                    448:
1.84      schwarze  449:                if (*p == ' ') {
                    450:                        print_endword(h);
                    451:                        p++;
                    452:                        continue;
                    453:                }
                    454:
1.67      schwarze  455:                if (print_escape(h, *p++))
1.33      schwarze  456:                        continue;
1.4       schwarze  457:
1.26      schwarze  458:                esc = mandoc_escape(&p, &seq, &len);
                    459:                switch (esc) {
1.35      schwarze  460:                case ESCAPE_FONT:
                    461:                case ESCAPE_FONTPREV:
                    462:                case ESCAPE_FONTBOLD:
                    463:                case ESCAPE_FONTITALIC:
                    464:                case ESCAPE_FONTBI:
1.112     schwarze  465:                case ESCAPE_FONTCW:
1.35      schwarze  466:                case ESCAPE_FONTROMAN:
1.113     schwarze  467:                        if (0 == norecurse) {
                    468:                                h->flags |= HTML_NOSPACE;
1.30      schwarze  469:                                print_metaf(h, esc);
1.113     schwarze  470:                                h->flags &= ~HTML_NOSPACE;
                    471:                        }
1.30      schwarze  472:                        continue;
1.35      schwarze  473:                case ESCAPE_SKIPCHAR:
1.30      schwarze  474:                        h->flags |= HTML_SKIPCHAR;
                    475:                        continue;
1.116     schwarze  476:                case ESCAPE_ERROR:
                    477:                        continue;
1.30      schwarze  478:                default:
                    479:                        break;
                    480:                }
                    481:
                    482:                if (h->flags & HTML_SKIPCHAR) {
                    483:                        h->flags &= ~HTML_SKIPCHAR;
                    484:                        continue;
                    485:                }
                    486:
                    487:                switch (esc) {
1.35      schwarze  488:                case ESCAPE_UNICODE:
1.38      schwarze  489:                        /* Skip past "u" header. */
1.26      schwarze  490:                        c = mchars_num2uc(seq + 1, len - 1);
                    491:                        break;
1.35      schwarze  492:                case ESCAPE_NUMBERED:
1.26      schwarze  493:                        c = mchars_num2char(seq, len);
1.51      schwarze  494:                        if (c < 0)
                    495:                                continue;
1.26      schwarze  496:                        break;
1.35      schwarze  497:                case ESCAPE_SPECIAL:
1.61      schwarze  498:                        c = mchars_spec2cp(seq, len);
1.51      schwarze  499:                        if (c <= 0)
                    500:                                continue;
1.116     schwarze  501:                        break;
                    502:                case ESCAPE_UNDEF:
                    503:                        c = *seq;
1.26      schwarze  504:                        break;
1.109     schwarze  505:                case ESCAPE_DEVICE:
                    506:                        print_word(h, "html");
                    507:                        continue;
1.84      schwarze  508:                case ESCAPE_BREAK:
                    509:                        breakline = 1;
                    510:                        continue;
1.35      schwarze  511:                case ESCAPE_NOSPACE:
1.26      schwarze  512:                        if ('\0' == *p)
                    513:                                nospace = 1;
1.49      schwarze  514:                        continue;
1.55      schwarze  515:                case ESCAPE_OVERSTRIKE:
                    516:                        if (len == 0)
                    517:                                continue;
                    518:                        c = seq[len - 1];
                    519:                        break;
1.5       schwarze  520:                default:
1.49      schwarze  521:                        continue;
1.5       schwarze  522:                }
1.51      schwarze  523:                if ((c < 0x20 && c != 0x09) ||
                    524:                    (c > 0x7E && c < 0xA0))
1.49      schwarze  525:                        c = 0xFFFD;
1.67      schwarze  526:                if (c > 0x7E) {
1.86      bentley   527:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.67      schwarze  528:                        print_word(h, numbuf);
                    529:                } else if (print_escape(h, c) == 0)
                    530:                        print_byte(h, c);
1.1       schwarze  531:        }
1.5       schwarze  532:
1.58      schwarze  533:        return nospace;
1.1       schwarze  534: }
                    535:
1.6       schwarze  536: static void
1.65      schwarze  537: print_href(struct html *h, const char *name, const char *sec, int man)
1.6       schwarze  538: {
1.110     schwarze  539:        struct stat      sb;
1.65      schwarze  540:        const char      *p, *pp;
1.110     schwarze  541:        char            *filename;
                    542:
                    543:        if (man) {
                    544:                pp = h->base_man1;
                    545:                if (h->base_man2 != NULL) {
                    546:                        mandoc_asprintf(&filename, "%s.%s", name, sec);
                    547:                        if (stat(filename, &sb) == -1)
                    548:                                pp = h->base_man2;
                    549:                        free(filename);
                    550:                }
                    551:        } else
                    552:                pp = h->base_includes;
1.65      schwarze  553:
                    554:        while ((p = strchr(pp, '%')) != NULL) {
                    555:                print_encode(h, pp, p, 1);
                    556:                if (man && p[1] == 'S') {
                    557:                        if (sec == NULL)
1.67      schwarze  558:                                print_byte(h, '1');
1.65      schwarze  559:                        else
                    560:                                print_encode(h, sec, NULL, 1);
                    561:                } else if ((man && p[1] == 'N') ||
                    562:                    (man == 0 && p[1] == 'I'))
                    563:                        print_encode(h, name, NULL, 1);
                    564:                else
                    565:                        print_encode(h, p, p + 2, 1);
                    566:                pp = p + 2;
                    567:        }
                    568:        if (*pp != '\0')
                    569:                print_encode(h, pp, NULL, 1);
1.6       schwarze  570: }
                    571:
1.1       schwarze  572: struct tag *
1.64      schwarze  573: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.1       schwarze  574: {
1.64      schwarze  575:        va_list          ap;
1.1       schwarze  576:        struct tag      *t;
1.65      schwarze  577:        const char      *attr;
1.73      schwarze  578:        char            *arg1, *arg2;
1.114     schwarze  579:        int              style_written, tflags;
1.66      schwarze  580:
                    581:        tflags = htmltags[tag].flags;
1.1       schwarze  582:
1.74      schwarze  583:        /* Push this tag onto the stack of open scopes. */
1.6       schwarze  584:
1.66      schwarze  585:        if ((tflags & HTML_NOSTACK) == 0) {
1.24      schwarze  586:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  587:                t->tag = tag;
1.74      schwarze  588:                t->next = h->tag;
1.122     schwarze  589:                t->refcnt = 0;
                    590:                t->closed = 0;
1.74      schwarze  591:                h->tag = t;
1.1       schwarze  592:        } else
                    593:                t = NULL;
                    594:
1.66      schwarze  595:        if (tflags & HTML_NLBEFORE)
1.67      schwarze  596:                print_endline(h);
                    597:        if (h->col == 0)
                    598:                print_indent(h);
1.66      schwarze  599:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    600:                if (h->flags & HTML_KEEP)
1.86      bentley   601:                        print_word(h, "&#x00A0;");
1.66      schwarze  602:                else {
                    603:                        if (h->flags & HTML_PREKEEP)
                    604:                                h->flags |= HTML_KEEP;
1.67      schwarze  605:                        print_endword(h);
1.12      schwarze  606:                }
1.66      schwarze  607:        }
1.1       schwarze  608:
1.13      schwarze  609:        if ( ! (h->flags & HTML_NONOSPACE))
                    610:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  611:        else
                    612:                h->flags |= HTML_NOSPACE;
1.13      schwarze  613:
1.6       schwarze  614:        /* Print out the tag name and attributes. */
                    615:
1.67      schwarze  616:        print_byte(h, '<');
                    617:        print_word(h, htmltags[tag].name);
1.64      schwarze  618:
                    619:        va_start(ap, fmt);
                    620:
1.114     schwarze  621:        while (*fmt != '\0' && *fmt != 's') {
1.73      schwarze  622:
1.108     schwarze  623:                /* Parse attributes and arguments. */
1.73      schwarze  624:
                    625:                arg1 = va_arg(ap, char *);
1.108     schwarze  626:                arg2 = NULL;
1.64      schwarze  627:                switch (*fmt++) {
                    628:                case 'c':
1.65      schwarze  629:                        attr = "class";
1.64      schwarze  630:                        break;
                    631:                case 'h':
1.65      schwarze  632:                        attr = "href";
1.64      schwarze  633:                        break;
                    634:                case 'i':
1.65      schwarze  635:                        attr = "id";
1.64      schwarze  636:                        break;
                    637:                case '?':
1.73      schwarze  638:                        attr = arg1;
                    639:                        arg1 = va_arg(ap, char *);
1.64      schwarze  640:                        break;
                    641:                default:
                    642:                        abort();
                    643:                }
1.73      schwarze  644:                if (*fmt == 'M')
                    645:                        arg2 = va_arg(ap, char *);
                    646:                if (arg1 == NULL)
                    647:                        continue;
                    648:
1.108     schwarze  649:                /* Print the attributes. */
1.73      schwarze  650:
1.67      schwarze  651:                print_byte(h, ' ');
                    652:                print_word(h, attr);
                    653:                print_byte(h, '=');
                    654:                print_byte(h, '"');
1.65      schwarze  655:                switch (*fmt) {
1.78      schwarze  656:                case 'I':
                    657:                        print_href(h, arg1, NULL, 0);
                    658:                        fmt++;
                    659:                        break;
1.65      schwarze  660:                case 'M':
1.73      schwarze  661:                        print_href(h, arg1, arg2, 1);
1.65      schwarze  662:                        fmt++;
                    663:                        break;
1.78      schwarze  664:                case 'R':
                    665:                        print_byte(h, '#');
                    666:                        print_encode(h, arg1, NULL, 1);
1.65      schwarze  667:                        fmt++;
1.78      schwarze  668:                        break;
1.65      schwarze  669:                default:
1.114     schwarze  670:                        print_encode(h, arg1, NULL, 1);
1.65      schwarze  671:                        break;
                    672:                }
1.67      schwarze  673:                print_byte(h, '"');
1.64      schwarze  674:        }
1.114     schwarze  675:
                    676:        style_written = 0;
                    677:        while (*fmt++ == 's') {
                    678:                arg1 = va_arg(ap, char *);
                    679:                arg2 = va_arg(ap, char *);
                    680:                if (arg2 == NULL)
                    681:                        continue;
                    682:                print_byte(h, ' ');
                    683:                if (style_written == 0) {
                    684:                        print_word(h, "style=\"");
                    685:                        style_written = 1;
                    686:                }
                    687:                print_word(h, arg1);
                    688:                print_byte(h, ':');
                    689:                print_byte(h, ' ');
                    690:                print_word(h, arg2);
                    691:                print_byte(h, ';');
                    692:        }
                    693:        if (style_written)
                    694:                print_byte(h, '"');
                    695:
1.64      schwarze  696:        va_end(ap);
1.6       schwarze  697:
1.42      schwarze  698:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  699:
                    700:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.67      schwarze  701:                print_byte(h, '/');
1.6       schwarze  702:
1.67      schwarze  703:        print_byte(h, '>');
1.1       schwarze  704:
1.66      schwarze  705:        if (tflags & HTML_NLBEGIN)
1.67      schwarze  706:                print_endline(h);
1.66      schwarze  707:        else
                    708:                h->flags |= HTML_NOSPACE;
1.18      schwarze  709:
1.66      schwarze  710:        if (tflags & HTML_INDENT)
                    711:                h->indent++;
                    712:        if (tflags & HTML_NOINDENT)
                    713:                h->noindent++;
1.18      schwarze  714:
1.58      schwarze  715:        return t;
1.1       schwarze  716: }
                    717:
                    718: static void
1.54      schwarze  719: print_ctag(struct html *h, struct tag *tag)
1.1       schwarze  720: {
1.66      schwarze  721:        int      tflags;
1.35      schwarze  722:
1.122     schwarze  723:        if (tag->closed == 0) {
                    724:                tag->closed = 1;
                    725:                if (tag == h->metaf)
                    726:                        h->metaf = NULL;
                    727:                if (tag == h->tblt)
                    728:                        h->tblt = NULL;
                    729:
                    730:                tflags = htmltags[tag->tag].flags;
                    731:                if (tflags & HTML_INDENT)
                    732:                        h->indent--;
                    733:                if (tflags & HTML_NOINDENT)
                    734:                        h->noindent--;
                    735:                if (tflags & HTML_NLEND)
                    736:                        print_endline(h);
                    737:                print_indent(h);
                    738:                print_byte(h, '<');
                    739:                print_byte(h, '/');
                    740:                print_word(h, htmltags[tag->tag].name);
                    741:                print_byte(h, '>');
                    742:                if (tflags & HTML_NLAFTER)
                    743:                        print_endline(h);
                    744:        }
                    745:        if (tag->refcnt == 0) {
                    746:                h->tag = tag->next;
                    747:                free(tag);
                    748:        }
1.1       schwarze  749: }
                    750:
                    751: void
1.6       schwarze  752: print_gen_decls(struct html *h)
                    753: {
1.67      schwarze  754:        print_word(h, "<!DOCTYPE html>");
                    755:        print_endline(h);
1.91      schwarze  756: }
                    757:
                    758: void
                    759: print_gen_comment(struct html *h, struct roff_node *n)
                    760: {
                    761:        int      wantblank;
                    762:
                    763:        print_word(h, "<!-- This is an automatically generated file."
                    764:            "  Do not edit.");
                    765:        h->indent = 1;
                    766:        wantblank = 0;
                    767:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    768:                if (strstr(n->string, "-->") == NULL &&
                    769:                    (wantblank || *n->string != '\0')) {
                    770:                        print_endline(h);
                    771:                        print_indent(h);
                    772:                        print_word(h, n->string);
                    773:                        wantblank = *n->string != '\0';
                    774:                }
                    775:                n = n->next;
                    776:        }
                    777:        if (wantblank)
                    778:                print_endline(h);
                    779:        print_word(h, " -->");
                    780:        print_endline(h);
                    781:        h->indent = 0;
1.1       schwarze  782: }
                    783:
                    784: void
1.12      schwarze  785: print_text(struct html *h, const char *word)
1.1       schwarze  786: {
1.67      schwarze  787:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.12      schwarze  788:                if ( ! (HTML_KEEP & h->flags)) {
                    789:                        if (HTML_PREKEEP & h->flags)
                    790:                                h->flags |= HTML_KEEP;
1.67      schwarze  791:                        print_endword(h);
1.12      schwarze  792:                } else
1.86      bentley   793:                        print_word(h, "&#x00A0;");
1.12      schwarze  794:        }
1.1       schwarze  795:
1.20      schwarze  796:        assert(NULL == h->metaf);
1.31      schwarze  797:        switch (h->metac) {
1.35      schwarze  798:        case HTMLFONT_ITALIC:
1.64      schwarze  799:                h->metaf = print_otag(h, TAG_I, "");
1.31      schwarze  800:                break;
1.35      schwarze  801:        case HTMLFONT_BOLD:
1.64      schwarze  802:                h->metaf = print_otag(h, TAG_B, "");
1.31      schwarze  803:                break;
1.35      schwarze  804:        case HTMLFONT_BI:
1.64      schwarze  805:                h->metaf = print_otag(h, TAG_B, "");
                    806:                print_otag(h, TAG_I, "");
1.112     schwarze  807:                break;
                    808:        case HTMLFONT_CW:
                    809:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
1.31      schwarze  810:                break;
                    811:        default:
1.67      schwarze  812:                print_indent(h);
1.31      schwarze  813:                break;
                    814:        }
1.20      schwarze  815:
1.12      schwarze  816:        assert(word);
1.65      schwarze  817:        if ( ! print_encode(h, word, NULL, 0)) {
1.13      schwarze  818:                if ( ! (h->flags & HTML_NONOSPACE))
                    819:                        h->flags &= ~HTML_NOSPACE;
1.53      schwarze  820:                h->flags &= ~HTML_NONEWLINE;
1.28      schwarze  821:        } else
1.53      schwarze  822:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20      schwarze  823:
                    824:        if (h->metaf) {
                    825:                print_tagq(h, h->metaf);
                    826:                h->metaf = NULL;
                    827:        }
1.17      schwarze  828:
                    829:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  830: }
                    831:
                    832: void
                    833: print_tagq(struct html *h, const struct tag *until)
                    834: {
1.122     schwarze  835:        struct tag      *this, *next;
1.1       schwarze  836:
1.122     schwarze  837:        for (this = h->tag; this != NULL; this = next) {
                    838:                next = this == until ? NULL : this->next;
                    839:                print_ctag(h, this);
1.1       schwarze  840:        }
                    841: }
                    842:
1.120     schwarze  843: /*
                    844:  * Close out all open elements up to but excluding suntil.
                    845:  * Note that a paragraph just inside stays open together with it
                    846:  * because paragraphs include subsequent phrasing content.
                    847:  */
1.1       schwarze  848: void
                    849: print_stagq(struct html *h, const struct tag *suntil)
                    850: {
1.122     schwarze  851:        struct tag      *this, *next;
1.1       schwarze  852:
1.122     schwarze  853:        for (this = h->tag; this != NULL; this = next) {
                    854:                next = this->next;
                    855:                if (this == suntil || (next == suntil &&
                    856:                    (this->tag == TAG_P || this->tag == TAG_PRE)))
                    857:                        break;
                    858:                print_ctag(h, this);
1.1       schwarze  859:        }
1.42      schwarze  860: }
                    861:
1.67      schwarze  862:
                    863: /***********************************************************************
                    864:  * Low level output functions.
                    865:  * They implement line breaking using a short static buffer.
                    866:  ***********************************************************************/
                    867:
                    868: /*
                    869:  * Buffer one HTML output byte.
                    870:  * If the buffer is full, flush and deactivate it and start a new line.
                    871:  * If the buffer is inactive, print directly.
                    872:  */
                    873: static void
                    874: print_byte(struct html *h, char c)
                    875: {
                    876:        if ((h->flags & HTML_BUFFER) == 0) {
                    877:                putchar(c);
                    878:                h->col++;
                    879:                return;
                    880:        }
                    881:
                    882:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    883:                h->buf[h->bufcol++] = c;
                    884:                return;
                    885:        }
                    886:
                    887:        putchar('\n');
                    888:        h->col = 0;
                    889:        print_indent(h);
                    890:        putchar(' ');
                    891:        putchar(' ');
                    892:        fwrite(h->buf, h->bufcol, 1, stdout);
                    893:        putchar(c);
                    894:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    895:        h->bufcol = 0;
                    896:        h->flags &= ~HTML_BUFFER;
                    897: }
                    898:
1.66      schwarze  899: /*
                    900:  * If something was printed on the current output line, end it.
1.67      schwarze  901:  * Not to be called right after print_indent().
1.66      schwarze  902:  */
1.72      schwarze  903: void
1.67      schwarze  904: print_endline(struct html *h)
1.66      schwarze  905: {
1.67      schwarze  906:        if (h->col == 0)
1.66      schwarze  907:                return;
                    908:
1.67      schwarze  909:        if (h->bufcol) {
                    910:                putchar(' ');
                    911:                fwrite(h->buf, h->bufcol, 1, stdout);
                    912:                h->bufcol = 0;
                    913:        }
1.66      schwarze  914:        putchar('\n');
1.67      schwarze  915:        h->col = 0;
                    916:        h->flags |= HTML_NOSPACE;
                    917:        h->flags &= ~HTML_BUFFER;
                    918: }
                    919:
                    920: /*
                    921:  * Flush the HTML output buffer.
                    922:  * If it is inactive, activate it.
                    923:  */
                    924: static void
                    925: print_endword(struct html *h)
                    926: {
                    927:        if (h->noindent) {
                    928:                print_byte(h, ' ');
                    929:                return;
                    930:        }
                    931:
                    932:        if ((h->flags & HTML_BUFFER) == 0) {
                    933:                h->col++;
                    934:                h->flags |= HTML_BUFFER;
                    935:        } else if (h->bufcol) {
                    936:                putchar(' ');
                    937:                fwrite(h->buf, h->bufcol, 1, stdout);
                    938:                h->col += h->bufcol + 1;
                    939:        }
                    940:        h->bufcol = 0;
1.66      schwarze  941: }
                    942:
                    943: /*
                    944:  * If at the beginning of a new output line,
                    945:  * perform indentation and mark the line as containing output.
                    946:  * Make sure to really produce some output right afterwards,
                    947:  * but do not use print_otag() for producing it.
                    948:  */
                    949: static void
1.67      schwarze  950: print_indent(struct html *h)
1.66      schwarze  951: {
1.67      schwarze  952:        size_t   i;
1.66      schwarze  953:
1.67      schwarze  954:        if (h->col)
1.66      schwarze  955:                return;
                    956:
1.67      schwarze  957:        if (h->noindent == 0) {
                    958:                h->col = h->indent * 2;
                    959:                for (i = 0; i < h->col; i++)
1.66      schwarze  960:                        putchar(' ');
1.67      schwarze  961:        }
                    962:        h->flags &= ~HTML_NOSPACE;
                    963: }
                    964:
                    965: /*
                    966:  * Print or buffer some characters
                    967:  * depending on the current HTML output buffer state.
                    968:  */
                    969: static void
                    970: print_word(struct html *h, const char *cp)
                    971: {
                    972:        while (*cp != '\0')
                    973:                print_byte(h, *cp++);
1.3       schwarze  974: }