[BACK]Return to html.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/html.c, Revision 1.261

1.261   ! schwarze    1: /*     $Id: html.c,v 1.260 2019/09/03 18:08:02 schwarze Exp $ */
1.1       kristaps    2: /*
1.176     schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.249     schwarze    4:  * Copyright (c) 2011-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.186     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29      kristaps   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.
1.1       kristaps   17:  */
1.92      kristaps   18: #include "config.h"
                     19:
1.41      kristaps   20: #include <sys/types.h>
1.240     schwarze   21: #include <sys/stat.h>
1.30      kristaps   22:
1.1       kristaps   23: #include <assert.h>
1.68      kristaps   24: #include <ctype.h>
1.76      kristaps   25: #include <stdarg.h>
1.229     schwarze   26: #include <stddef.h>
1.29      kristaps   27: #include <stdio.h>
1.63      kristaps   28: #include <stdint.h>
1.1       kristaps   29: #include <stdlib.h>
1.33      kristaps   30: #include <string.h>
1.45      kristaps   31: #include <unistd.h>
1.1       kristaps   32:
1.210     schwarze   33: #include "mandoc_aux.h"
1.229     schwarze   34: #include "mandoc_ohash.h"
1.100     kristaps   35: #include "mandoc.h"
1.210     schwarze   36: #include "roff.h"
1.58      kristaps   37: #include "out.h"
1.51      kristaps   38: #include "html.h"
1.186     schwarze   39: #include "manconf.h"
1.64      kristaps   40: #include "main.h"
1.63      kristaps   41:
1.29      kristaps   42: struct htmldata {
1.63      kristaps   43:        const char       *name;
1.29      kristaps   44:        int               flags;
1.257     schwarze   45: #define        HTML_INPHRASE    (1 << 0)  /* Can appear in phrasing context. */
                     46: #define        HTML_TOPHRASE    (1 << 1)  /* Establishes phrasing context. */
                     47: #define        HTML_NOSTACK     (1 << 2)  /* Does not have an end tag. */
                     48: #define        HTML_NLBEFORE    (1 << 3)  /* Output line break before opening. */
                     49: #define        HTML_NLBEGIN     (1 << 4)  /* Output line break after opening. */
                     50: #define        HTML_NLEND       (1 << 5)  /* Output line break before closing. */
                     51: #define        HTML_NLAFTER     (1 << 6)  /* Output line break after closing. */
1.196     schwarze   52: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     53: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     54: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
1.257     schwarze   55: #define        HTML_INDENT      (1 << 7)  /* Indent content by two spaces. */
                     56: #define        HTML_NOINDENT    (1 << 8)  /* Exception: never indent content. */
1.29      kristaps   57: };
1.7       kristaps   58:
1.29      kristaps   59: static const struct htmldata htmltags[TAG_MAX] = {
1.196     schwarze   60:        {"html",        HTML_NLALL},
                     61:        {"head",        HTML_NLALL | HTML_INDENT},
1.257     schwarze   62:        {"meta",        HTML_NOSTACK | HTML_NLALL},
                     63:        {"link",        HTML_NOSTACK | HTML_NLALL},
                     64:        {"style",       HTML_NLALL | HTML_INDENT},
                     65:        {"title",       HTML_NLAROUND},
1.196     schwarze   66:        {"body",        HTML_NLALL},
                     67:        {"div",         HTML_NLAROUND},
1.253     schwarze   68:        {"section",     HTML_NLALL},
1.196     schwarze   69:        {"table",       HTML_NLALL | HTML_INDENT},
                     70:        {"tr",          HTML_NLALL | HTML_INDENT},
                     71:        {"td",          HTML_NLAROUND},
                     72:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     73:        {"ul",          HTML_NLALL | HTML_INDENT},
                     74:        {"ol",          HTML_NLALL | HTML_INDENT},
                     75:        {"dl",          HTML_NLALL | HTML_INDENT},
                     76:        {"dt",          HTML_NLAROUND},
                     77:        {"dd",          HTML_NLAROUND | HTML_INDENT},
1.257     schwarze   78:        {"h1",          HTML_TOPHRASE | HTML_NLAROUND},
                     79:        {"h2",          HTML_TOPHRASE | HTML_NLAROUND},
                     80:        {"p",           HTML_TOPHRASE | HTML_NLAROUND | HTML_INDENT},
                     81:        {"pre",         HTML_TOPHRASE | HTML_NLALL | HTML_NOINDENT},
                     82:        {"a",           HTML_INPHRASE | HTML_TOPHRASE},
                     83:        {"b",           HTML_INPHRASE | HTML_TOPHRASE},
                     84:        {"cite",        HTML_INPHRASE | HTML_TOPHRASE},
                     85:        {"code",        HTML_INPHRASE | HTML_TOPHRASE},
                     86:        {"i",           HTML_INPHRASE | HTML_TOPHRASE},
                     87:        {"small",       HTML_INPHRASE | HTML_TOPHRASE},
                     88:        {"span",        HTML_INPHRASE | HTML_TOPHRASE},
                     89:        {"var",         HTML_INPHRASE | HTML_TOPHRASE},
                     90:        {"br",          HTML_INPHRASE | HTML_NOSTACK | HTML_NLALL},
                     91:        {"math",        HTML_INPHRASE | HTML_NLALL | HTML_INDENT},
1.196     schwarze   92:        {"mrow",        0},
                     93:        {"mi",          0},
1.215     schwarze   94:        {"mn",          0},
1.196     schwarze   95:        {"mo",          0},
                     96:        {"msup",        0},
                     97:        {"msub",        0},
                     98:        {"msubsup",     0},
                     99:        {"mfrac",       0},
                    100:        {"msqrt",       0},
                    101:        {"mfenced",     0},
                    102:        {"mtable",      0},
                    103:        {"mtr",         0},
                    104:        {"mtd",         0},
                    105:        {"munderover",  0},
                    106:        {"munder",      0},
                    107:        {"mover",       0},
1.90      kristaps  108: };
                    109:
1.229     schwarze  110: /* Avoid duplicate HTML id= attributes. */
                    111: static struct ohash     id_unique;
                    112:
1.254     schwarze  113: static void     html_reset_internal(struct html *);
1.197     schwarze  114: static void     print_byte(struct html *, char);
                    115: static void     print_endword(struct html *);
                    116: static void     print_indent(struct html *);
                    117: static void     print_word(struct html *, const char *);
                    118:
1.184     schwarze  119: static void     print_ctag(struct html *, struct tag *);
1.197     schwarze  120: static int      print_escape(struct html *, char);
1.195     schwarze  121: static int      print_encode(struct html *, const char *, const char *, int);
                    122: static void     print_href(struct html *, const char *, const char *, int);
1.255     schwarze  123: static void     print_metaf(struct html *);
1.82      kristaps  124:
1.156     schwarze  125:
1.180     schwarze  126: void *
1.191     schwarze  127: html_alloc(const struct manoutput *outopts)
1.10      kristaps  128: {
1.30      kristaps  129:        struct html     *h;
                    130:
1.128     kristaps  131:        h = mandoc_calloc(1, sizeof(struct html));
1.10      kristaps  132:
1.204     schwarze  133:        h->tag = NULL;
1.186     schwarze  134:        h->style = outopts->style;
1.240     schwarze  135:        if ((h->base_man1 = outopts->man) == NULL)
                    136:                h->base_man2 = NULL;
                    137:        else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
                    138:                *h->base_man2++ = '\0';
1.186     schwarze  139:        h->base_includes = outopts->includes;
                    140:        if (outopts->fragment)
                    141:                h->oflags |= HTML_FRAGMENT;
1.241     schwarze  142:        if (outopts->toc)
                    143:                h->oflags |= HTML_TOC;
1.43      kristaps  144:
1.229     schwarze  145:        mandoc_ohash_init(&id_unique, 4, 0);
                    146:
1.188     schwarze  147:        return h;
1.29      kristaps  148: }
1.10      kristaps  149:
1.254     schwarze  150: static void
                    151: html_reset_internal(struct html *h)
1.29      kristaps  152: {
1.30      kristaps  153:        struct tag      *tag;
1.229     schwarze  154:        char            *cp;
                    155:        unsigned int     slot;
1.30      kristaps  156:
1.204     schwarze  157:        while ((tag = h->tag) != NULL) {
                    158:                h->tag = tag->next;
1.30      kristaps  159:                free(tag);
                    160:        }
1.229     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.254     schwarze  167: }
                    168:
                    169: void
                    170: html_reset(void *p)
                    171: {
                    172:        html_reset_internal(p);
                    173:        mandoc_ohash_init(&id_unique, 4, 0);
                    174: }
                    175:
                    176: void
                    177: html_free(void *p)
                    178: {
                    179:        html_reset_internal(p);
                    180:        free(p);
1.10      kristaps  181: }
1.2       kristaps  182:
1.51      kristaps  183: void
1.29      kristaps  184: print_gen_head(struct html *h)
                    185: {
1.165     kristaps  186:        struct tag      *t;
1.41      kristaps  187:
1.194     schwarze  188:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222     schwarze  189:        if (h->style != NULL) {
                    190:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    191:                    h->style, "type", "text/css", "media", "all");
                    192:                return;
                    193:        }
1.165     kristaps  194:
1.168     kristaps  195:        /*
1.222     schwarze  196:         * Print a minimal embedded style sheet.
1.168     kristaps  197:         */
1.196     schwarze  198:
1.194     schwarze  199:        t = print_otag(h, TAG_STYLE, "");
1.196     schwarze  200:        print_text(h, "table.head, table.foot { width: 100%; }");
1.197     schwarze  201:        print_endline(h);
1.196     schwarze  202:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197     schwarze  203:        print_endline(h);
1.196     schwarze  204:        print_text(h, "td.head-vol { text-align: center; }");
1.197     schwarze  205:        print_endline(h);
1.256     schwarze  206:        print_text(h, ".Nd, .Bf, .Op { display: inline; }");
1.225     schwarze  207:        print_endline(h);
1.256     schwarze  208:        print_text(h, ".Pa, .Ad { font-style: italic; }");
1.226     schwarze  209:        print_endline(h);
1.256     schwarze  210:        print_text(h, ".Ms { font-weight: bold; }");
1.228     schwarze  211:        print_endline(h);
1.256     schwarze  212:        print_text(h, ".Bl-diag ");
1.224     schwarze  213:        print_byte(h, '>');
                    214:        print_text(h, " dt { font-weight: bold; }");
1.223     schwarze  215:        print_endline(h);
1.256     schwarze  216:        print_text(h, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
                    217:            "{ font-weight: bold; font-family: inherit; }");
1.165     kristaps  218:        print_tagq(h, t);
1.4       kristaps  219: }
                    220:
1.255     schwarze  221: int
                    222: html_setfont(struct html *h, enum mandoc_esc font)
1.88      kristaps  223: {
1.255     schwarze  224:        switch (font) {
1.156     schwarze  225:        case ESCAPE_FONTPREV:
1.90      kristaps  226:                font = h->metal;
1.88      kristaps  227:                break;
1.156     schwarze  228:        case ESCAPE_FONTITALIC:
                    229:        case ESCAPE_FONTBOLD:
                    230:        case ESCAPE_FONTBI:
1.242     schwarze  231:        case ESCAPE_FONTCW:
1.255     schwarze  232:        case ESCAPE_FONTROMAN:
1.242     schwarze  233:                break;
1.156     schwarze  234:        case ESCAPE_FONT:
1.255     schwarze  235:                font = ESCAPE_FONTROMAN;
1.88      kristaps  236:                break;
                    237:        default:
1.255     schwarze  238:                return 0;
1.88      kristaps  239:        }
1.255     schwarze  240:        h->metal = h->metac;
                    241:        h->metac = font;
                    242:        return 1;
                    243: }
1.88      kristaps  244:
1.255     schwarze  245: static void
                    246: print_metaf(struct html *h)
                    247: {
1.122     kristaps  248:        if (h->metaf) {
                    249:                print_tagq(h, h->metaf);
                    250:                h->metaf = NULL;
                    251:        }
1.255     schwarze  252:        switch (h->metac) {
                    253:        case ESCAPE_FONTITALIC:
1.194     schwarze  254:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  255:                break;
1.255     schwarze  256:        case ESCAPE_FONTBOLD:
1.194     schwarze  257:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  258:                break;
1.255     schwarze  259:        case ESCAPE_FONTBI:
1.194     schwarze  260:                h->metaf = print_otag(h, TAG_B, "");
                    261:                print_otag(h, TAG_I, "");
1.152     schwarze  262:                break;
1.255     schwarze  263:        case ESCAPE_FONTCW:
1.242     schwarze  264:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
                    265:                break;
1.152     schwarze  266:        default:
                    267:                break;
                    268:        }
1.248     schwarze  269: }
                    270:
1.249     schwarze  271: void
                    272: html_close_paragraph(struct html *h)
                    273: {
1.259     schwarze  274:        struct tag      *this, *next;
                    275:        int              flags;
1.249     schwarze  276:
1.259     schwarze  277:        this = h->tag;
                    278:        for (;;) {
                    279:                next = this->next;
                    280:                flags = htmltags[this->tag].flags;
                    281:                if (flags & (HTML_INPHRASE | HTML_TOPHRASE))
                    282:                        print_ctag(h, this);
                    283:                if ((flags & HTML_INPHRASE) == 0)
1.249     schwarze  284:                        break;
1.259     schwarze  285:                this = next;
1.249     schwarze  286:        }
                    287: }
                    288:
1.248     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.249     schwarze  311:                        html_close_paragraph(h);
1.248     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.210     schwarze  321: }
                    322:
                    323: char *
1.229     schwarze  324: html_make_id(const struct roff_node *n, int unique)
1.210     schwarze  325: {
                    326:        const struct roff_node  *nch;
1.229     schwarze  327:        char                    *buf, *bufs, *cp;
                    328:        unsigned int             slot;
                    329:        int                      suffix;
1.210     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.220     schwarze  337:        if (buf == NULL)
                    338:                return NULL;
1.210     schwarze  339:
1.230     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.210     schwarze  346:
                    347:        for (cp = buf; *cp != '\0'; cp++)
1.230     schwarze  348:                if (isalnum((unsigned char)*cp) == 0 &&
                    349:                    strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210     schwarze  350:                        *cp = '_';
                    351:
1.229     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.210     schwarze  376:        return buf;
1.88      kristaps  377: }
                    378:
1.85      kristaps  379: static int
1.197     schwarze  380: print_escape(struct html *h, char c)
1.159     schwarze  381: {
                    382:
                    383:        switch (c) {
                    384:        case '<':
1.197     schwarze  385:                print_word(h, "&lt;");
1.159     schwarze  386:                break;
                    387:        case '>':
1.197     schwarze  388:                print_word(h, "&gt;");
1.159     schwarze  389:                break;
                    390:        case '&':
1.197     schwarze  391:                print_word(h, "&amp;");
1.159     schwarze  392:                break;
                    393:        case '"':
1.197     schwarze  394:                print_word(h, "&quot;");
1.159     schwarze  395:                break;
                    396:        case ASCII_NBRSP:
1.197     schwarze  397:                print_word(h, "&nbsp;");
1.159     schwarze  398:                break;
                    399:        case ASCII_HYPH:
1.197     schwarze  400:                print_byte(h, '-');
1.189     schwarze  401:                break;
1.159     schwarze  402:        case ASCII_BREAK:
                    403:                break;
                    404:        default:
1.188     schwarze  405:                return 0;
1.159     schwarze  406:        }
1.188     schwarze  407:        return 1;
1.159     schwarze  408: }
                    409:
                    410: static int
1.195     schwarze  411: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29      kristaps  412: {
1.197     schwarze  413:        char             numbuf[16];
1.214     schwarze  414:        const char      *seq;
1.77      kristaps  415:        size_t           sz;
1.214     schwarze  416:        int              c, len, breakline, nospace;
1.132     kristaps  417:        enum mandoc_esc  esc;
1.214     schwarze  418:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154     schwarze  419:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  420:
1.195     schwarze  421:        if (pend == NULL)
                    422:                pend = strchr(p, '\0');
                    423:
1.214     schwarze  424:        breakline = 0;
1.85      kristaps  425:        nospace = 0;
                    426:
1.195     schwarze  427:        while (p < pend) {
1.151     schwarze  428:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    429:                        h->flags &= ~HTML_SKIPCHAR;
                    430:                        p++;
                    431:                        continue;
                    432:                }
                    433:
1.197     schwarze  434:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214     schwarze  435:                        print_byte(h, *p);
                    436:
                    437:                if (breakline &&
                    438:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.245     schwarze  439:                        print_otag(h, TAG_BR, "");
1.214     schwarze  440:                        breakline = 0;
                    441:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    442:                                p++;
                    443:                        continue;
                    444:                }
1.77      kristaps  445:
1.195     schwarze  446:                if (p >= pend)
1.132     kristaps  447:                        break;
                    448:
1.214     schwarze  449:                if (*p == ' ') {
                    450:                        print_endword(h);
                    451:                        p++;
                    452:                        continue;
                    453:                }
                    454:
1.197     schwarze  455:                if (print_escape(h, *p++))
1.154     schwarze  456:                        continue;
1.77      kristaps  457:
1.132     kristaps  458:                esc = mandoc_escape(&p, &seq, &len);
                    459:                switch (esc) {
1.156     schwarze  460:                case ESCAPE_FONT:
                    461:                case ESCAPE_FONTPREV:
                    462:                case ESCAPE_FONTBOLD:
                    463:                case ESCAPE_FONTITALIC:
                    464:                case ESCAPE_FONTBI:
1.242     schwarze  465:                case ESCAPE_FONTCW:
1.156     schwarze  466:                case ESCAPE_FONTROMAN:
1.243     schwarze  467:                        if (0 == norecurse) {
                    468:                                h->flags |= HTML_NOSPACE;
1.255     schwarze  469:                                if (html_setfont(h, esc))
                    470:                                        print_metaf(h);
1.243     schwarze  471:                                h->flags &= ~HTML_NOSPACE;
                    472:                        }
1.151     schwarze  473:                        continue;
1.156     schwarze  474:                case ESCAPE_SKIPCHAR:
1.151     schwarze  475:                        h->flags |= HTML_SKIPCHAR;
                    476:                        continue;
1.246     schwarze  477:                case ESCAPE_ERROR:
                    478:                        continue;
1.151     schwarze  479:                default:
                    480:                        break;
                    481:                }
                    482:
                    483:                if (h->flags & HTML_SKIPCHAR) {
                    484:                        h->flags &= ~HTML_SKIPCHAR;
                    485:                        continue;
                    486:                }
                    487:
                    488:                switch (esc) {
1.156     schwarze  489:                case ESCAPE_UNICODE:
1.159     schwarze  490:                        /* Skip past "u" header. */
1.144     kristaps  491:                        c = mchars_num2uc(seq + 1, len - 1);
                    492:                        break;
1.156     schwarze  493:                case ESCAPE_NUMBERED:
1.141     kristaps  494:                        c = mchars_num2char(seq, len);
1.181     schwarze  495:                        if (c < 0)
                    496:                                continue;
1.82      kristaps  497:                        break;
1.156     schwarze  498:                case ESCAPE_SPECIAL:
1.191     schwarze  499:                        c = mchars_spec2cp(seq, len);
1.181     schwarze  500:                        if (c <= 0)
                    501:                                continue;
1.246     schwarze  502:                        break;
                    503:                case ESCAPE_UNDEF:
                    504:                        c = *seq;
1.132     kristaps  505:                        break;
1.239     schwarze  506:                case ESCAPE_DEVICE:
                    507:                        print_word(h, "html");
                    508:                        continue;
1.214     schwarze  509:                case ESCAPE_BREAK:
                    510:                        breakline = 1;
                    511:                        continue;
1.156     schwarze  512:                case ESCAPE_NOSPACE:
1.132     kristaps  513:                        if ('\0' == *p)
                    514:                                nospace = 1;
1.179     schwarze  515:                        continue;
1.185     schwarze  516:                case ESCAPE_OVERSTRIKE:
                    517:                        if (len == 0)
                    518:                                continue;
                    519:                        c = seq[len - 1];
                    520:                        break;
1.82      kristaps  521:                default:
1.179     schwarze  522:                        continue;
1.82      kristaps  523:                }
1.181     schwarze  524:                if ((c < 0x20 && c != 0x09) ||
                    525:                    (c > 0x7E && c < 0xA0))
1.179     schwarze  526:                        c = 0xFFFD;
1.197     schwarze  527:                if (c > 0x7E) {
1.216     schwarze  528:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197     schwarze  529:                        print_word(h, numbuf);
                    530:                } else if (print_escape(h, c) == 0)
                    531:                        print_byte(h, c);
1.32      kristaps  532:        }
1.85      kristaps  533:
1.188     schwarze  534:        return nospace;
1.14      kristaps  535: }
                    536:
1.94      kristaps  537: static void
1.195     schwarze  538: print_href(struct html *h, const char *name, const char *sec, int man)
1.94      kristaps  539: {
1.240     schwarze  540:        struct stat      sb;
1.195     schwarze  541:        const char      *p, *pp;
1.240     schwarze  542:        char            *filename;
                    543:
                    544:        if (man) {
                    545:                pp = h->base_man1;
                    546:                if (h->base_man2 != NULL) {
                    547:                        mandoc_asprintf(&filename, "%s.%s", name, sec);
                    548:                        if (stat(filename, &sb) == -1)
                    549:                                pp = h->base_man2;
                    550:                        free(filename);
                    551:                }
                    552:        } else
                    553:                pp = h->base_includes;
1.195     schwarze  554:
                    555:        while ((p = strchr(pp, '%')) != NULL) {
                    556:                print_encode(h, pp, p, 1);
                    557:                if (man && p[1] == 'S') {
                    558:                        if (sec == NULL)
1.197     schwarze  559:                                print_byte(h, '1');
1.195     schwarze  560:                        else
                    561:                                print_encode(h, sec, NULL, 1);
                    562:                } else if ((man && p[1] == 'N') ||
                    563:                    (man == 0 && p[1] == 'I'))
                    564:                        print_encode(h, name, NULL, 1);
                    565:                else
                    566:                        print_encode(h, p, p + 2, 1);
                    567:                pp = p + 2;
                    568:        }
                    569:        if (*pp != '\0')
                    570:                print_encode(h, pp, NULL, 1);
1.94      kristaps  571: }
                    572:
1.51      kristaps  573: struct tag *
1.194     schwarze  574: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14      kristaps  575: {
1.194     schwarze  576:        va_list          ap;
1.30      kristaps  577:        struct tag      *t;
1.195     schwarze  578:        const char      *attr;
1.203     schwarze  579:        char            *arg1, *arg2;
1.244     schwarze  580:        int              style_written, tflags;
1.196     schwarze  581:
                    582:        tflags = htmltags[tag].flags;
1.30      kristaps  583:
1.257     schwarze  584:        /* Flow content is not allowed in phrasing context. */
                    585:
                    586:        if ((tflags & HTML_INPHRASE) == 0) {
                    587:                for (t = h->tag; t != NULL; t = t->next) {
                    588:                        if (t->closed)
                    589:                                continue;
                    590:                        assert((htmltags[t->tag].flags & HTML_TOPHRASE) == 0);
                    591:                        break;
                    592:                }
1.260     schwarze  593:
                    594:        /*
                    595:         * Always wrap phrasing elements in a paragraph
                    596:         * unless already contained in some flow container;
                    597:         * never put them directly into a section.
                    598:         */
                    599:
                    600:        } else if (tflags & HTML_TOPHRASE && h->tag->tag == TAG_SECTION)
                    601:                print_otag(h, TAG_P, "c", "Pp");
1.257     schwarze  602:
1.204     schwarze  603:        /* Push this tag onto the stack of open scopes. */
1.94      kristaps  604:
1.196     schwarze  605:        if ((tflags & HTML_NOSTACK) == 0) {
1.128     kristaps  606:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  607:                t->tag = tag;
1.204     schwarze  608:                t->next = h->tag;
1.252     schwarze  609:                t->refcnt = 0;
                    610:                t->closed = 0;
1.204     schwarze  611:                h->tag = t;
1.30      kristaps  612:        } else
                    613:                t = NULL;
1.29      kristaps  614:
1.196     schwarze  615:        if (tflags & HTML_NLBEFORE)
1.197     schwarze  616:                print_endline(h);
                    617:        if (h->col == 0)
                    618:                print_indent(h);
1.196     schwarze  619:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    620:                if (h->flags & HTML_KEEP)
1.216     schwarze  621:                        print_word(h, "&#x00A0;");
1.196     schwarze  622:                else {
                    623:                        if (h->flags & HTML_PREKEEP)
                    624:                                h->flags |= HTML_KEEP;
1.197     schwarze  625:                        print_endword(h);
1.105     kristaps  626:                }
1.196     schwarze  627:        }
1.29      kristaps  628:
1.109     kristaps  629:        if ( ! (h->flags & HTML_NONOSPACE))
                    630:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  631:        else
                    632:                h->flags |= HTML_NOSPACE;
1.109     kristaps  633:
1.94      kristaps  634:        /* Print out the tag name and attributes. */
                    635:
1.197     schwarze  636:        print_byte(h, '<');
                    637:        print_word(h, htmltags[tag].name);
1.194     schwarze  638:
                    639:        va_start(ap, fmt);
                    640:
1.244     schwarze  641:        while (*fmt != '\0' && *fmt != 's') {
1.203     schwarze  642:
1.238     schwarze  643:                /* Parse attributes and arguments. */
1.203     schwarze  644:
                    645:                arg1 = va_arg(ap, char *);
1.238     schwarze  646:                arg2 = NULL;
1.194     schwarze  647:                switch (*fmt++) {
                    648:                case 'c':
1.195     schwarze  649:                        attr = "class";
1.194     schwarze  650:                        break;
                    651:                case 'h':
1.195     schwarze  652:                        attr = "href";
1.194     schwarze  653:                        break;
                    654:                case 'i':
1.195     schwarze  655:                        attr = "id";
1.194     schwarze  656:                        break;
                    657:                case '?':
1.203     schwarze  658:                        attr = arg1;
                    659:                        arg1 = va_arg(ap, char *);
1.194     schwarze  660:                        break;
                    661:                default:
                    662:                        abort();
                    663:                }
1.203     schwarze  664:                if (*fmt == 'M')
                    665:                        arg2 = va_arg(ap, char *);
                    666:                if (arg1 == NULL)
                    667:                        continue;
                    668:
1.238     schwarze  669:                /* Print the attributes. */
1.203     schwarze  670:
1.197     schwarze  671:                print_byte(h, ' ');
                    672:                print_word(h, attr);
                    673:                print_byte(h, '=');
                    674:                print_byte(h, '"');
1.195     schwarze  675:                switch (*fmt) {
1.208     schwarze  676:                case 'I':
                    677:                        print_href(h, arg1, NULL, 0);
                    678:                        fmt++;
                    679:                        break;
1.195     schwarze  680:                case 'M':
1.203     schwarze  681:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  682:                        fmt++;
                    683:                        break;
1.208     schwarze  684:                case 'R':
                    685:                        print_byte(h, '#');
                    686:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  687:                        fmt++;
1.208     schwarze  688:                        break;
1.195     schwarze  689:                default:
1.244     schwarze  690:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  691:                        break;
                    692:                }
1.197     schwarze  693:                print_byte(h, '"');
1.194     schwarze  694:        }
1.244     schwarze  695:
                    696:        style_written = 0;
                    697:        while (*fmt++ == 's') {
                    698:                arg1 = va_arg(ap, char *);
                    699:                arg2 = va_arg(ap, char *);
                    700:                if (arg2 == NULL)
                    701:                        continue;
                    702:                print_byte(h, ' ');
                    703:                if (style_written == 0) {
                    704:                        print_word(h, "style=\"");
                    705:                        style_written = 1;
                    706:                }
                    707:                print_word(h, arg1);
                    708:                print_byte(h, ':');
                    709:                print_byte(h, ' ');
                    710:                print_word(h, arg2);
                    711:                print_byte(h, ';');
                    712:        }
                    713:        if (style_written)
                    714:                print_byte(h, '"');
                    715:
1.194     schwarze  716:        va_end(ap);
1.94      kristaps  717:
1.172     kristaps  718:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  719:
1.257     schwarze  720:        if (htmltags[tag].flags & HTML_NOSTACK)
1.197     schwarze  721:                print_byte(h, '/');
1.93      kristaps  722:
1.197     schwarze  723:        print_byte(h, '>');
1.14      kristaps  724:
1.196     schwarze  725:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  726:                print_endline(h);
1.196     schwarze  727:        else
                    728:                h->flags |= HTML_NOSPACE;
1.117     kristaps  729:
1.196     schwarze  730:        if (tflags & HTML_INDENT)
                    731:                h->indent++;
                    732:        if (tflags & HTML_NOINDENT)
                    733:                h->noindent++;
1.117     kristaps  734:
1.188     schwarze  735:        return t;
1.14      kristaps  736: }
                    737:
1.29      kristaps  738: static void
1.184     schwarze  739: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  740: {
1.196     schwarze  741:        int      tflags;
1.156     schwarze  742:
1.252     schwarze  743:        if (tag->closed == 0) {
                    744:                tag->closed = 1;
                    745:                if (tag == h->metaf)
                    746:                        h->metaf = NULL;
                    747:                if (tag == h->tblt)
                    748:                        h->tblt = NULL;
                    749:
                    750:                tflags = htmltags[tag->tag].flags;
                    751:                if (tflags & HTML_INDENT)
                    752:                        h->indent--;
                    753:                if (tflags & HTML_NOINDENT)
                    754:                        h->noindent--;
                    755:                if (tflags & HTML_NLEND)
                    756:                        print_endline(h);
                    757:                print_indent(h);
                    758:                print_byte(h, '<');
                    759:                print_byte(h, '/');
                    760:                print_word(h, htmltags[tag->tag].name);
                    761:                print_byte(h, '>');
                    762:                if (tflags & HTML_NLAFTER)
                    763:                        print_endline(h);
                    764:        }
                    765:        if (tag->refcnt == 0) {
                    766:                h->tag = tag->next;
                    767:                free(tag);
                    768:        }
1.14      kristaps  769: }
                    770:
1.51      kristaps  771: void
1.93      kristaps  772: print_gen_decls(struct html *h)
1.1       kristaps  773: {
1.197     schwarze  774:        print_word(h, "<!DOCTYPE html>");
                    775:        print_endline(h);
1.221     schwarze  776: }
                    777:
                    778: void
                    779: print_gen_comment(struct html *h, struct roff_node *n)
                    780: {
                    781:        int      wantblank;
                    782:
                    783:        print_word(h, "<!-- This is an automatically generated file."
                    784:            "  Do not edit.");
                    785:        h->indent = 1;
                    786:        wantblank = 0;
                    787:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    788:                if (strstr(n->string, "-->") == NULL &&
                    789:                    (wantblank || *n->string != '\0')) {
                    790:                        print_endline(h);
                    791:                        print_indent(h);
                    792:                        print_word(h, n->string);
                    793:                        wantblank = *n->string != '\0';
                    794:                }
                    795:                n = n->next;
                    796:        }
                    797:        if (wantblank)
                    798:                print_endline(h);
                    799:        print_word(h, " -->");
                    800:        print_endline(h);
                    801:        h->indent = 0;
1.1       kristaps  802: }
                    803:
1.51      kristaps  804: void
1.104     kristaps  805: print_text(struct html *h, const char *word)
1.1       kristaps  806: {
1.260     schwarze  807:        /*
                    808:         * Always wrap text in a paragraph unless already contained in
                    809:         * some flow container; never put it directly into a section.
                    810:         */
                    811:
                    812:        if (h->tag->tag == TAG_SECTION)
                    813:                print_otag(h, TAG_P, "c", "Pp");
                    814:
                    815:        /* Output whitespace before this text? */
                    816:
1.197     schwarze  817:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  818:                if ( ! (HTML_KEEP & h->flags)) {
                    819:                        if (HTML_PREKEEP & h->flags)
                    820:                                h->flags |= HTML_KEEP;
1.197     schwarze  821:                        print_endword(h);
1.105     kristaps  822:                } else
1.216     schwarze  823:                        print_word(h, "&#x00A0;");
1.105     kristaps  824:        }
1.260     schwarze  825:
                    826:        /*
                    827:         * Print the text, optionally surrounded by HTML whitespace,
                    828:         * optionally manually switching fonts before and after.
                    829:         */
1.30      kristaps  830:
1.255     schwarze  831:        assert(h->metaf == NULL);
                    832:        print_metaf(h);
                    833:        print_indent(h);
1.195     schwarze  834:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  835:                if ( ! (h->flags & HTML_NONOSPACE))
                    836:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  837:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  838:        } else
1.183     schwarze  839:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  840:
1.255     schwarze  841:        if (h->metaf != NULL) {
1.122     kristaps  842:                print_tagq(h, h->metaf);
                    843:                h->metaf = NULL;
                    844:        }
1.113     schwarze  845:
                    846:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  847: }
1.30      kristaps  848:
1.51      kristaps  849: void
1.30      kristaps  850: print_tagq(struct html *h, const struct tag *until)
                    851: {
1.252     schwarze  852:        struct tag      *this, *next;
1.30      kristaps  853:
1.252     schwarze  854:        for (this = h->tag; this != NULL; this = next) {
                    855:                next = this == until ? NULL : this->next;
                    856:                print_ctag(h, this);
1.30      kristaps  857:        }
                    858: }
                    859:
1.250     schwarze  860: /*
                    861:  * Close out all open elements up to but excluding suntil.
                    862:  * Note that a paragraph just inside stays open together with it
                    863:  * because paragraphs include subsequent phrasing content.
                    864:  */
1.51      kristaps  865: void
1.30      kristaps  866: print_stagq(struct html *h, const struct tag *suntil)
                    867: {
1.252     schwarze  868:        struct tag      *this, *next;
1.30      kristaps  869:
1.252     schwarze  870:        for (this = h->tag; this != NULL; this = next) {
                    871:                next = this->next;
                    872:                if (this == suntil || (next == suntil &&
                    873:                    (this->tag == TAG_P || this->tag == TAG_PRE)))
                    874:                        break;
                    875:                print_ctag(h, this);
1.30      kristaps  876:        }
1.171     kristaps  877: }
                    878:
1.197     schwarze  879:
                    880: /***********************************************************************
                    881:  * Low level output functions.
                    882:  * They implement line breaking using a short static buffer.
                    883:  ***********************************************************************/
                    884:
                    885: /*
                    886:  * Buffer one HTML output byte.
                    887:  * If the buffer is full, flush and deactivate it and start a new line.
                    888:  * If the buffer is inactive, print directly.
                    889:  */
                    890: static void
                    891: print_byte(struct html *h, char c)
                    892: {
                    893:        if ((h->flags & HTML_BUFFER) == 0) {
                    894:                putchar(c);
                    895:                h->col++;
                    896:                return;
                    897:        }
                    898:
                    899:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    900:                h->buf[h->bufcol++] = c;
                    901:                return;
                    902:        }
                    903:
                    904:        putchar('\n');
                    905:        h->col = 0;
                    906:        print_indent(h);
                    907:        putchar(' ');
                    908:        putchar(' ');
                    909:        fwrite(h->buf, h->bufcol, 1, stdout);
                    910:        putchar(c);
                    911:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    912:        h->bufcol = 0;
                    913:        h->flags &= ~HTML_BUFFER;
                    914: }
                    915:
1.196     schwarze  916: /*
                    917:  * If something was printed on the current output line, end it.
1.197     schwarze  918:  * Not to be called right after print_indent().
1.196     schwarze  919:  */
1.202     schwarze  920: void
1.197     schwarze  921: print_endline(struct html *h)
1.196     schwarze  922: {
1.197     schwarze  923:        if (h->col == 0)
1.196     schwarze  924:                return;
                    925:
1.197     schwarze  926:        if (h->bufcol) {
                    927:                putchar(' ');
                    928:                fwrite(h->buf, h->bufcol, 1, stdout);
                    929:                h->bufcol = 0;
                    930:        }
1.196     schwarze  931:        putchar('\n');
1.197     schwarze  932:        h->col = 0;
                    933:        h->flags |= HTML_NOSPACE;
                    934:        h->flags &= ~HTML_BUFFER;
                    935: }
                    936:
                    937: /*
                    938:  * Flush the HTML output buffer.
                    939:  * If it is inactive, activate it.
                    940:  */
                    941: static void
                    942: print_endword(struct html *h)
                    943: {
                    944:        if (h->noindent) {
                    945:                print_byte(h, ' ');
                    946:                return;
                    947:        }
                    948:
                    949:        if ((h->flags & HTML_BUFFER) == 0) {
                    950:                h->col++;
                    951:                h->flags |= HTML_BUFFER;
                    952:        } else if (h->bufcol) {
                    953:                putchar(' ');
                    954:                fwrite(h->buf, h->bufcol, 1, stdout);
                    955:                h->col += h->bufcol + 1;
                    956:        }
                    957:        h->bufcol = 0;
1.196     schwarze  958: }
                    959:
                    960: /*
                    961:  * If at the beginning of a new output line,
                    962:  * perform indentation and mark the line as containing output.
                    963:  * Make sure to really produce some output right afterwards,
                    964:  * but do not use print_otag() for producing it.
                    965:  */
                    966: static void
1.197     schwarze  967: print_indent(struct html *h)
1.196     schwarze  968: {
1.197     schwarze  969:        size_t   i;
1.196     schwarze  970:
1.261   ! schwarze  971:        if (h->col || h->noindent)
1.196     schwarze  972:                return;
                    973:
1.261   ! schwarze  974:        h->col = h->indent * 2;
        !           975:        for (i = 0; i < h->col; i++)
        !           976:                putchar(' ');
1.197     schwarze  977: }
                    978:
                    979: /*
                    980:  * Print or buffer some characters
                    981:  * depending on the current HTML output buffer state.
                    982:  */
                    983: static void
                    984: print_word(struct html *h, const char *cp)
                    985: {
                    986:        while (*cp != '\0')
                    987:                print_byte(h, *cp++);
1.68      kristaps  988: }

CVSweb