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

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