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

Annotation of mandoc/html.c, Revision 1.252

1.252   ! schwarze    1: /*     $Id: html.c,v 1.251 2019/01/11 12:56:42 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.196     schwarze   45: #define        HTML_NOSTACK     (1 << 0)
                     46: #define        HTML_AUTOCLOSE   (1 << 1)
                     47: #define        HTML_NLBEFORE    (1 << 2)
                     48: #define        HTML_NLBEGIN     (1 << 3)
                     49: #define        HTML_NLEND       (1 << 4)
                     50: #define        HTML_NLAFTER     (1 << 5)
                     51: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     52: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     53: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
                     54: #define        HTML_INDENT      (1 << 6)
                     55: #define        HTML_NOINDENT    (1 << 7)
1.29      kristaps   56: };
1.7       kristaps   57:
1.29      kristaps   58: static const struct htmldata htmltags[TAG_MAX] = {
1.196     schwarze   59:        {"html",        HTML_NLALL},
                     60:        {"head",        HTML_NLALL | HTML_INDENT},
                     61:        {"body",        HTML_NLALL},
                     62:        {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     63:        {"title",       HTML_NLAROUND},
                     64:        {"div",         HTML_NLAROUND},
1.225     schwarze   65:        {"div",         0},
1.196     schwarze   66:        {"h1",          HTML_NLAROUND},
                     67:        {"h2",          HTML_NLAROUND},
                     68:        {"span",        0},
                     69:        {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     70:        {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     71:        {"a",           0},
                     72:        {"table",       HTML_NLALL | HTML_INDENT},
                     73:        {"tr",          HTML_NLALL | HTML_INDENT},
                     74:        {"td",          HTML_NLAROUND},
                     75:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     76:        {"ul",          HTML_NLALL | HTML_INDENT},
                     77:        {"ol",          HTML_NLALL | HTML_INDENT},
                     78:        {"dl",          HTML_NLALL | HTML_INDENT},
                     79:        {"dt",          HTML_NLAROUND},
                     80:        {"dd",          HTML_NLAROUND | HTML_INDENT},
1.249     schwarze   81:        {"p",           HTML_NLAROUND | HTML_INDENT},
1.196     schwarze   82:        {"pre",         HTML_NLALL | HTML_NOINDENT},
1.207     schwarze   83:        {"var",         0},
1.206     schwarze   84:        {"cite",        0},
1.196     schwarze   85:        {"b",           0},
                     86:        {"i",           0},
                     87:        {"code",        0},
                     88:        {"small",       0},
                     89:        {"style",       HTML_NLALL | HTML_INDENT},
                     90:        {"math",        HTML_NLALL | HTML_INDENT},
                     91:        {"mrow",        0},
                     92:        {"mi",          0},
1.215     schwarze   93:        {"mn",          0},
1.196     schwarze   94:        {"mo",          0},
                     95:        {"msup",        0},
                     96:        {"msub",        0},
                     97:        {"msubsup",     0},
                     98:        {"mfrac",       0},
                     99:        {"msqrt",       0},
                    100:        {"mfenced",     0},
                    101:        {"mtable",      0},
                    102:        {"mtr",         0},
                    103:        {"mtd",         0},
                    104:        {"munderover",  0},
                    105:        {"munder",      0},
                    106:        {"mover",       0},
1.90      kristaps  107: };
                    108:
1.229     schwarze  109: /* Avoid duplicate HTML id= attributes. */
                    110: static struct ohash     id_unique;
                    111:
1.197     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.184     schwarze  117: static void     print_ctag(struct html *, struct tag *);
1.197     schwarze  118: static int      print_escape(struct html *, char);
1.195     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.82      kristaps  121:
1.156     schwarze  122:
1.180     schwarze  123: void *
1.191     schwarze  124: html_alloc(const struct manoutput *outopts)
1.10      kristaps  125: {
1.30      kristaps  126:        struct html     *h;
                    127:
1.128     kristaps  128:        h = mandoc_calloc(1, sizeof(struct html));
1.10      kristaps  129:
1.204     schwarze  130:        h->tag = NULL;
1.186     schwarze  131:        h->style = outopts->style;
1.240     schwarze  132:        if ((h->base_man1 = outopts->man) == NULL)
                    133:                h->base_man2 = NULL;
                    134:        else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
                    135:                *h->base_man2++ = '\0';
1.186     schwarze  136:        h->base_includes = outopts->includes;
                    137:        if (outopts->fragment)
                    138:                h->oflags |= HTML_FRAGMENT;
1.241     schwarze  139:        if (outopts->toc)
                    140:                h->oflags |= HTML_TOC;
1.43      kristaps  141:
1.229     schwarze  142:        mandoc_ohash_init(&id_unique, 4, 0);
                    143:
1.188     schwarze  144:        return h;
1.29      kristaps  145: }
1.10      kristaps  146:
1.29      kristaps  147: void
                    148: html_free(void *p)
                    149: {
1.30      kristaps  150:        struct tag      *tag;
                    151:        struct html     *h;
1.229     schwarze  152:        char            *cp;
                    153:        unsigned int     slot;
1.30      kristaps  154:
                    155:        h = (struct html *)p;
1.204     schwarze  156:        while ((tag = h->tag) != NULL) {
                    157:                h->tag = tag->next;
1.30      kristaps  158:                free(tag);
                    159:        }
1.229     schwarze  160:        free(h);
1.53      kristaps  161:
1.229     schwarze  162:        cp = ohash_first(&id_unique, &slot);
                    163:        while (cp != NULL) {
                    164:                free(cp);
                    165:                cp = ohash_next(&id_unique, &slot);
                    166:        }
                    167:        ohash_delete(&id_unique);
1.10      kristaps  168: }
1.2       kristaps  169:
1.51      kristaps  170: void
1.29      kristaps  171: print_gen_head(struct html *h)
                    172: {
1.165     kristaps  173:        struct tag      *t;
1.41      kristaps  174:
1.194     schwarze  175:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222     schwarze  176:        if (h->style != NULL) {
                    177:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    178:                    h->style, "type", "text/css", "media", "all");
                    179:                return;
                    180:        }
1.165     kristaps  181:
1.168     kristaps  182:        /*
1.222     schwarze  183:         * Print a minimal embedded style sheet.
1.168     kristaps  184:         */
1.196     schwarze  185:
1.194     schwarze  186:        t = print_otag(h, TAG_STYLE, "");
1.196     schwarze  187:        print_text(h, "table.head, table.foot { width: 100%; }");
1.197     schwarze  188:        print_endline(h);
1.196     schwarze  189:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197     schwarze  190:        print_endline(h);
1.196     schwarze  191:        print_text(h, "td.head-vol { text-align: center; }");
1.197     schwarze  192:        print_endline(h);
1.198     schwarze  193:        print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.225     schwarze  194:        print_endline(h);
                    195:        print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.226     schwarze  196:        print_endline(h);
1.227     schwarze  197:        print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.228     schwarze  198:        print_endline(h);
                    199:        print_text(h, "span.Ms { font-weight: bold; }");
1.224     schwarze  200:        print_endline(h);
                    201:        print_text(h, "dl.Bl-diag ");
                    202:        print_byte(h, '>');
                    203:        print_text(h, " dt { font-weight: bold; }");
1.223     schwarze  204:        print_endline(h);
                    205:        print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
                    206:            "code.In, code.Fd, code.Fn,");
                    207:        print_endline(h);
                    208:        print_text(h, "code.Cd { font-weight: bold; "
                    209:            "font-family: inherit; }");
1.165     kristaps  210:        print_tagq(h, t);
1.4       kristaps  211: }
                    212:
1.247     schwarze  213: void
1.132     kristaps  214: print_metaf(struct html *h, enum mandoc_esc deco)
1.88      kristaps  215: {
1.90      kristaps  216:        enum htmlfont    font;
1.88      kristaps  217:
                    218:        switch (deco) {
1.156     schwarze  219:        case ESCAPE_FONTPREV:
1.90      kristaps  220:                font = h->metal;
1.88      kristaps  221:                break;
1.156     schwarze  222:        case ESCAPE_FONTITALIC:
1.90      kristaps  223:                font = HTMLFONT_ITALIC;
                    224:                break;
1.156     schwarze  225:        case ESCAPE_FONTBOLD:
1.90      kristaps  226:                font = HTMLFONT_BOLD;
1.88      kristaps  227:                break;
1.156     schwarze  228:        case ESCAPE_FONTBI:
1.152     schwarze  229:                font = HTMLFONT_BI;
                    230:                break;
1.242     schwarze  231:        case ESCAPE_FONTCW:
                    232:                font = HTMLFONT_CW;
                    233:                break;
1.156     schwarze  234:        case ESCAPE_FONT:
                    235:        case ESCAPE_FONTROMAN:
1.90      kristaps  236:                font = HTMLFONT_NONE;
1.88      kristaps  237:                break;
                    238:        default:
1.247     schwarze  239:                return;
1.88      kristaps  240:        }
                    241:
1.122     kristaps  242:        if (h->metaf) {
                    243:                print_tagq(h, h->metaf);
                    244:                h->metaf = NULL;
                    245:        }
                    246:
                    247:        h->metal = h->metac;
                    248:        h->metac = font;
                    249:
1.152     schwarze  250:        switch (font) {
1.156     schwarze  251:        case HTMLFONT_ITALIC:
1.194     schwarze  252:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  253:                break;
1.156     schwarze  254:        case HTMLFONT_BOLD:
1.194     schwarze  255:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  256:                break;
1.156     schwarze  257:        case HTMLFONT_BI:
1.194     schwarze  258:                h->metaf = print_otag(h, TAG_B, "");
                    259:                print_otag(h, TAG_I, "");
1.152     schwarze  260:                break;
1.242     schwarze  261:        case HTMLFONT_CW:
                    262:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
                    263:                break;
1.152     schwarze  264:        default:
                    265:                break;
                    266:        }
1.248     schwarze  267: }
                    268:
1.249     schwarze  269: void
                    270: html_close_paragraph(struct html *h)
                    271: {
                    272:        struct tag      *t;
                    273:
1.252   ! schwarze  274:        for (t = h->tag; t != NULL && t->closed == 0; t = t->next) {
        !           275:                switch(t->tag) {
        !           276:                case TAG_P:
        !           277:                case TAG_PRE:
1.249     schwarze  278:                        print_tagq(h, t);
                    279:                        break;
1.252   ! schwarze  280:                case TAG_A:
        !           281:                        print_tagq(h, t);
        !           282:                        continue;
        !           283:                default:
        !           284:                        continue;
1.249     schwarze  285:                }
1.252   ! schwarze  286:                break;
1.249     schwarze  287:        }
                    288: }
                    289:
1.248     schwarze  290: /*
                    291:  * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
                    292:  * TOKEN_NONE does not switch.  The old mode is returned.
                    293:  */
                    294: enum roff_tok
                    295: html_fillmode(struct html *h, enum roff_tok want)
                    296: {
                    297:        struct tag      *t;
                    298:        enum roff_tok    had;
                    299:
                    300:        for (t = h->tag; t != NULL; t = t->next)
                    301:                if (t->tag == TAG_PRE)
                    302:                        break;
                    303:
                    304:        had = t == NULL ? ROFF_fi : ROFF_nf;
                    305:
                    306:        if (want != had) {
                    307:                switch (want) {
                    308:                case ROFF_fi:
                    309:                        print_tagq(h, t);
                    310:                        break;
                    311:                case ROFF_nf:
1.249     schwarze  312:                        html_close_paragraph(h);
1.248     schwarze  313:                        print_otag(h, TAG_PRE, "");
                    314:                        break;
                    315:                case TOKEN_NONE:
                    316:                        break;
                    317:                default:
                    318:                        abort();
                    319:                }
                    320:        }
                    321:        return had;
1.210     schwarze  322: }
                    323:
                    324: char *
1.229     schwarze  325: html_make_id(const struct roff_node *n, int unique)
1.210     schwarze  326: {
                    327:        const struct roff_node  *nch;
1.229     schwarze  328:        char                    *buf, *bufs, *cp;
                    329:        unsigned int             slot;
                    330:        int                      suffix;
1.210     schwarze  331:
                    332:        for (nch = n->child; nch != NULL; nch = nch->next)
                    333:                if (nch->type != ROFFT_TEXT)
                    334:                        return NULL;
                    335:
                    336:        buf = NULL;
                    337:        deroff(&buf, n);
1.220     schwarze  338:        if (buf == NULL)
                    339:                return NULL;
1.210     schwarze  340:
1.230     schwarze  341:        /*
                    342:         * In ID attributes, only use ASCII characters that are
                    343:         * permitted in URL-fragment strings according to the
                    344:         * explicit list at:
                    345:         * https://url.spec.whatwg.org/#url-fragment-string
                    346:         */
1.210     schwarze  347:
                    348:        for (cp = buf; *cp != '\0'; cp++)
1.230     schwarze  349:                if (isalnum((unsigned char)*cp) == 0 &&
                    350:                    strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210     schwarze  351:                        *cp = '_';
                    352:
1.229     schwarze  353:        if (unique == 0)
                    354:                return buf;
                    355:
                    356:        /* Avoid duplicate HTML id= attributes. */
                    357:
                    358:        bufs = NULL;
                    359:        suffix = 1;
                    360:        slot = ohash_qlookup(&id_unique, buf);
                    361:        cp = ohash_find(&id_unique, slot);
                    362:        if (cp != NULL) {
                    363:                while (cp != NULL) {
                    364:                        free(bufs);
                    365:                        if (++suffix > 127) {
                    366:                                free(buf);
                    367:                                return NULL;
                    368:                        }
                    369:                        mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
                    370:                        slot = ohash_qlookup(&id_unique, bufs);
                    371:                        cp = ohash_find(&id_unique, slot);
                    372:                }
                    373:                free(buf);
                    374:                buf = bufs;
                    375:        }
                    376:        ohash_insert(&id_unique, slot, buf);
1.210     schwarze  377:        return buf;
1.88      kristaps  378: }
                    379:
1.85      kristaps  380: static int
1.197     schwarze  381: print_escape(struct html *h, char c)
1.159     schwarze  382: {
                    383:
                    384:        switch (c) {
                    385:        case '<':
1.197     schwarze  386:                print_word(h, "&lt;");
1.159     schwarze  387:                break;
                    388:        case '>':
1.197     schwarze  389:                print_word(h, "&gt;");
1.159     schwarze  390:                break;
                    391:        case '&':
1.197     schwarze  392:                print_word(h, "&amp;");
1.159     schwarze  393:                break;
                    394:        case '"':
1.197     schwarze  395:                print_word(h, "&quot;");
1.159     schwarze  396:                break;
                    397:        case ASCII_NBRSP:
1.197     schwarze  398:                print_word(h, "&nbsp;");
1.159     schwarze  399:                break;
                    400:        case ASCII_HYPH:
1.197     schwarze  401:                print_byte(h, '-');
1.189     schwarze  402:                break;
1.159     schwarze  403:        case ASCII_BREAK:
                    404:                break;
                    405:        default:
1.188     schwarze  406:                return 0;
1.159     schwarze  407:        }
1.188     schwarze  408:        return 1;
1.159     schwarze  409: }
                    410:
                    411: static int
1.195     schwarze  412: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29      kristaps  413: {
1.197     schwarze  414:        char             numbuf[16];
1.214     schwarze  415:        const char      *seq;
1.77      kristaps  416:        size_t           sz;
1.214     schwarze  417:        int              c, len, breakline, nospace;
1.132     kristaps  418:        enum mandoc_esc  esc;
1.214     schwarze  419:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154     schwarze  420:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  421:
1.195     schwarze  422:        if (pend == NULL)
                    423:                pend = strchr(p, '\0');
                    424:
1.214     schwarze  425:        breakline = 0;
1.85      kristaps  426:        nospace = 0;
                    427:
1.195     schwarze  428:        while (p < pend) {
1.151     schwarze  429:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    430:                        h->flags &= ~HTML_SKIPCHAR;
                    431:                        p++;
                    432:                        continue;
                    433:                }
                    434:
1.197     schwarze  435:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214     schwarze  436:                        print_byte(h, *p);
                    437:
                    438:                if (breakline &&
                    439:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.245     schwarze  440:                        print_otag(h, TAG_BR, "");
1.214     schwarze  441:                        breakline = 0;
                    442:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    443:                                p++;
                    444:                        continue;
                    445:                }
1.77      kristaps  446:
1.195     schwarze  447:                if (p >= pend)
1.132     kristaps  448:                        break;
                    449:
1.214     schwarze  450:                if (*p == ' ') {
                    451:                        print_endword(h);
                    452:                        p++;
                    453:                        continue;
                    454:                }
                    455:
1.197     schwarze  456:                if (print_escape(h, *p++))
1.154     schwarze  457:                        continue;
1.77      kristaps  458:
1.132     kristaps  459:                esc = mandoc_escape(&p, &seq, &len);
                    460:                switch (esc) {
1.156     schwarze  461:                case ESCAPE_FONT:
                    462:                case ESCAPE_FONTPREV:
                    463:                case ESCAPE_FONTBOLD:
                    464:                case ESCAPE_FONTITALIC:
                    465:                case ESCAPE_FONTBI:
1.242     schwarze  466:                case ESCAPE_FONTCW:
1.156     schwarze  467:                case ESCAPE_FONTROMAN:
1.243     schwarze  468:                        if (0 == norecurse) {
                    469:                                h->flags |= HTML_NOSPACE;
1.151     schwarze  470:                                print_metaf(h, esc);
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.204     schwarze  584:        /* Push this tag onto the stack of open scopes. */
1.94      kristaps  585:
1.196     schwarze  586:        if ((tflags & HTML_NOSTACK) == 0) {
1.128     kristaps  587:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  588:                t->tag = tag;
1.204     schwarze  589:                t->next = h->tag;
1.252   ! schwarze  590:                t->refcnt = 0;
        !           591:                t->closed = 0;
1.204     schwarze  592:                h->tag = t;
1.30      kristaps  593:        } else
                    594:                t = NULL;
1.29      kristaps  595:
1.196     schwarze  596:        if (tflags & HTML_NLBEFORE)
1.197     schwarze  597:                print_endline(h);
                    598:        if (h->col == 0)
                    599:                print_indent(h);
1.196     schwarze  600:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    601:                if (h->flags & HTML_KEEP)
1.216     schwarze  602:                        print_word(h, "&#x00A0;");
1.196     schwarze  603:                else {
                    604:                        if (h->flags & HTML_PREKEEP)
                    605:                                h->flags |= HTML_KEEP;
1.197     schwarze  606:                        print_endword(h);
1.105     kristaps  607:                }
1.196     schwarze  608:        }
1.29      kristaps  609:
1.109     kristaps  610:        if ( ! (h->flags & HTML_NONOSPACE))
                    611:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  612:        else
                    613:                h->flags |= HTML_NOSPACE;
1.109     kristaps  614:
1.94      kristaps  615:        /* Print out the tag name and attributes. */
                    616:
1.197     schwarze  617:        print_byte(h, '<');
                    618:        print_word(h, htmltags[tag].name);
1.194     schwarze  619:
                    620:        va_start(ap, fmt);
                    621:
1.244     schwarze  622:        while (*fmt != '\0' && *fmt != 's') {
1.203     schwarze  623:
1.238     schwarze  624:                /* Parse attributes and arguments. */
1.203     schwarze  625:
                    626:                arg1 = va_arg(ap, char *);
1.238     schwarze  627:                arg2 = NULL;
1.194     schwarze  628:                switch (*fmt++) {
                    629:                case 'c':
1.195     schwarze  630:                        attr = "class";
1.194     schwarze  631:                        break;
                    632:                case 'h':
1.195     schwarze  633:                        attr = "href";
1.194     schwarze  634:                        break;
                    635:                case 'i':
1.195     schwarze  636:                        attr = "id";
1.194     schwarze  637:                        break;
                    638:                case '?':
1.203     schwarze  639:                        attr = arg1;
                    640:                        arg1 = va_arg(ap, char *);
1.194     schwarze  641:                        break;
                    642:                default:
                    643:                        abort();
                    644:                }
1.203     schwarze  645:                if (*fmt == 'M')
                    646:                        arg2 = va_arg(ap, char *);
                    647:                if (arg1 == NULL)
                    648:                        continue;
                    649:
1.238     schwarze  650:                /* Print the attributes. */
1.203     schwarze  651:
1.197     schwarze  652:                print_byte(h, ' ');
                    653:                print_word(h, attr);
                    654:                print_byte(h, '=');
                    655:                print_byte(h, '"');
1.195     schwarze  656:                switch (*fmt) {
1.208     schwarze  657:                case 'I':
                    658:                        print_href(h, arg1, NULL, 0);
                    659:                        fmt++;
                    660:                        break;
1.195     schwarze  661:                case 'M':
1.203     schwarze  662:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  663:                        fmt++;
                    664:                        break;
1.208     schwarze  665:                case 'R':
                    666:                        print_byte(h, '#');
                    667:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  668:                        fmt++;
1.208     schwarze  669:                        break;
1.195     schwarze  670:                default:
1.244     schwarze  671:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  672:                        break;
                    673:                }
1.197     schwarze  674:                print_byte(h, '"');
1.194     schwarze  675:        }
1.244     schwarze  676:
                    677:        style_written = 0;
                    678:        while (*fmt++ == 's') {
                    679:                arg1 = va_arg(ap, char *);
                    680:                arg2 = va_arg(ap, char *);
                    681:                if (arg2 == NULL)
                    682:                        continue;
                    683:                print_byte(h, ' ');
                    684:                if (style_written == 0) {
                    685:                        print_word(h, "style=\"");
                    686:                        style_written = 1;
                    687:                }
                    688:                print_word(h, arg1);
                    689:                print_byte(h, ':');
                    690:                print_byte(h, ' ');
                    691:                print_word(h, arg2);
                    692:                print_byte(h, ';');
                    693:        }
                    694:        if (style_written)
                    695:                print_byte(h, '"');
                    696:
1.194     schwarze  697:        va_end(ap);
1.94      kristaps  698:
1.172     kristaps  699:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  700:
1.93      kristaps  701:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  702:                print_byte(h, '/');
1.93      kristaps  703:
1.197     schwarze  704:        print_byte(h, '>');
1.14      kristaps  705:
1.196     schwarze  706:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  707:                print_endline(h);
1.196     schwarze  708:        else
                    709:                h->flags |= HTML_NOSPACE;
1.117     kristaps  710:
1.196     schwarze  711:        if (tflags & HTML_INDENT)
                    712:                h->indent++;
                    713:        if (tflags & HTML_NOINDENT)
                    714:                h->noindent++;
1.117     kristaps  715:
1.188     schwarze  716:        return t;
1.14      kristaps  717: }
                    718:
1.29      kristaps  719: static void
1.184     schwarze  720: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  721: {
1.196     schwarze  722:        int      tflags;
1.156     schwarze  723:
1.252   ! schwarze  724:        if (tag->closed == 0) {
        !           725:                tag->closed = 1;
        !           726:                if (tag == h->metaf)
        !           727:                        h->metaf = NULL;
        !           728:                if (tag == h->tblt)
        !           729:                        h->tblt = NULL;
        !           730:
        !           731:                tflags = htmltags[tag->tag].flags;
        !           732:                if (tflags & HTML_INDENT)
        !           733:                        h->indent--;
        !           734:                if (tflags & HTML_NOINDENT)
        !           735:                        h->noindent--;
        !           736:                if (tflags & HTML_NLEND)
        !           737:                        print_endline(h);
        !           738:                print_indent(h);
        !           739:                print_byte(h, '<');
        !           740:                print_byte(h, '/');
        !           741:                print_word(h, htmltags[tag->tag].name);
        !           742:                print_byte(h, '>');
        !           743:                if (tflags & HTML_NLAFTER)
        !           744:                        print_endline(h);
        !           745:        }
        !           746:        if (tag->refcnt == 0) {
        !           747:                h->tag = tag->next;
        !           748:                free(tag);
        !           749:        }
1.14      kristaps  750: }
                    751:
1.51      kristaps  752: void
1.93      kristaps  753: print_gen_decls(struct html *h)
1.1       kristaps  754: {
1.197     schwarze  755:        print_word(h, "<!DOCTYPE html>");
                    756:        print_endline(h);
1.221     schwarze  757: }
                    758:
                    759: void
                    760: print_gen_comment(struct html *h, struct roff_node *n)
                    761: {
                    762:        int      wantblank;
                    763:
                    764:        print_word(h, "<!-- This is an automatically generated file."
                    765:            "  Do not edit.");
                    766:        h->indent = 1;
                    767:        wantblank = 0;
                    768:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    769:                if (strstr(n->string, "-->") == NULL &&
                    770:                    (wantblank || *n->string != '\0')) {
                    771:                        print_endline(h);
                    772:                        print_indent(h);
                    773:                        print_word(h, n->string);
                    774:                        wantblank = *n->string != '\0';
                    775:                }
                    776:                n = n->next;
                    777:        }
                    778:        if (wantblank)
                    779:                print_endline(h);
                    780:        print_word(h, " -->");
                    781:        print_endline(h);
                    782:        h->indent = 0;
1.1       kristaps  783: }
                    784:
1.51      kristaps  785: void
1.104     kristaps  786: print_text(struct html *h, const char *word)
1.1       kristaps  787: {
1.197     schwarze  788:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  789:                if ( ! (HTML_KEEP & h->flags)) {
                    790:                        if (HTML_PREKEEP & h->flags)
                    791:                                h->flags |= HTML_KEEP;
1.197     schwarze  792:                        print_endword(h);
1.105     kristaps  793:                } else
1.216     schwarze  794:                        print_word(h, "&#x00A0;");
1.105     kristaps  795:        }
1.30      kristaps  796:
1.122     kristaps  797:        assert(NULL == h->metaf);
1.152     schwarze  798:        switch (h->metac) {
1.156     schwarze  799:        case HTMLFONT_ITALIC:
1.194     schwarze  800:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  801:                break;
1.156     schwarze  802:        case HTMLFONT_BOLD:
1.194     schwarze  803:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  804:                break;
1.156     schwarze  805:        case HTMLFONT_BI:
1.194     schwarze  806:                h->metaf = print_otag(h, TAG_B, "");
                    807:                print_otag(h, TAG_I, "");
1.242     schwarze  808:                break;
                    809:        case HTMLFONT_CW:
                    810:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
1.152     schwarze  811:                break;
                    812:        default:
1.197     schwarze  813:                print_indent(h);
1.152     schwarze  814:                break;
                    815:        }
1.122     kristaps  816:
1.104     kristaps  817:        assert(word);
1.195     schwarze  818:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  819:                if ( ! (h->flags & HTML_NONOSPACE))
                    820:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  821:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  822:        } else
1.183     schwarze  823:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  824:
                    825:        if (h->metaf) {
                    826:                print_tagq(h, h->metaf);
                    827:                h->metaf = NULL;
                    828:        }
1.113     schwarze  829:
                    830:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  831: }
1.30      kristaps  832:
1.51      kristaps  833: void
1.30      kristaps  834: print_tagq(struct html *h, const struct tag *until)
                    835: {
1.252   ! schwarze  836:        struct tag      *this, *next;
1.30      kristaps  837:
1.252   ! schwarze  838:        for (this = h->tag; this != NULL; this = next) {
        !           839:                next = this == until ? NULL : this->next;
        !           840:                print_ctag(h, this);
1.30      kristaps  841:        }
                    842: }
                    843:
1.250     schwarze  844: /*
                    845:  * Close out all open elements up to but excluding suntil.
                    846:  * Note that a paragraph just inside stays open together with it
                    847:  * because paragraphs include subsequent phrasing content.
                    848:  */
1.51      kristaps  849: void
1.30      kristaps  850: print_stagq(struct html *h, const struct tag *suntil)
                    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->next;
        !           856:                if (this == suntil || (next == suntil &&
        !           857:                    (this->tag == TAG_P || this->tag == TAG_PRE)))
        !           858:                        break;
        !           859:                print_ctag(h, this);
1.30      kristaps  860:        }
1.171     kristaps  861: }
                    862:
1.197     schwarze  863:
                    864: /***********************************************************************
                    865:  * Low level output functions.
                    866:  * They implement line breaking using a short static buffer.
                    867:  ***********************************************************************/
                    868:
                    869: /*
                    870:  * Buffer one HTML output byte.
                    871:  * If the buffer is full, flush and deactivate it and start a new line.
                    872:  * If the buffer is inactive, print directly.
                    873:  */
                    874: static void
                    875: print_byte(struct html *h, char c)
                    876: {
                    877:        if ((h->flags & HTML_BUFFER) == 0) {
                    878:                putchar(c);
                    879:                h->col++;
                    880:                return;
                    881:        }
                    882:
                    883:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    884:                h->buf[h->bufcol++] = c;
                    885:                return;
                    886:        }
                    887:
                    888:        putchar('\n');
                    889:        h->col = 0;
                    890:        print_indent(h);
                    891:        putchar(' ');
                    892:        putchar(' ');
                    893:        fwrite(h->buf, h->bufcol, 1, stdout);
                    894:        putchar(c);
                    895:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    896:        h->bufcol = 0;
                    897:        h->flags &= ~HTML_BUFFER;
                    898: }
                    899:
1.196     schwarze  900: /*
                    901:  * If something was printed on the current output line, end it.
1.197     schwarze  902:  * Not to be called right after print_indent().
1.196     schwarze  903:  */
1.202     schwarze  904: void
1.197     schwarze  905: print_endline(struct html *h)
1.196     schwarze  906: {
1.197     schwarze  907:        if (h->col == 0)
1.196     schwarze  908:                return;
                    909:
1.197     schwarze  910:        if (h->bufcol) {
                    911:                putchar(' ');
                    912:                fwrite(h->buf, h->bufcol, 1, stdout);
                    913:                h->bufcol = 0;
                    914:        }
1.196     schwarze  915:        putchar('\n');
1.197     schwarze  916:        h->col = 0;
                    917:        h->flags |= HTML_NOSPACE;
                    918:        h->flags &= ~HTML_BUFFER;
                    919: }
                    920:
                    921: /*
                    922:  * Flush the HTML output buffer.
                    923:  * If it is inactive, activate it.
                    924:  */
                    925: static void
                    926: print_endword(struct html *h)
                    927: {
                    928:        if (h->noindent) {
                    929:                print_byte(h, ' ');
                    930:                return;
                    931:        }
                    932:
                    933:        if ((h->flags & HTML_BUFFER) == 0) {
                    934:                h->col++;
                    935:                h->flags |= HTML_BUFFER;
                    936:        } else if (h->bufcol) {
                    937:                putchar(' ');
                    938:                fwrite(h->buf, h->bufcol, 1, stdout);
                    939:                h->col += h->bufcol + 1;
                    940:        }
                    941:        h->bufcol = 0;
1.196     schwarze  942: }
                    943:
                    944: /*
                    945:  * If at the beginning of a new output line,
                    946:  * perform indentation and mark the line as containing output.
                    947:  * Make sure to really produce some output right afterwards,
                    948:  * but do not use print_otag() for producing it.
                    949:  */
                    950: static void
1.197     schwarze  951: print_indent(struct html *h)
1.196     schwarze  952: {
1.197     schwarze  953:        size_t   i;
1.196     schwarze  954:
1.197     schwarze  955:        if (h->col)
1.196     schwarze  956:                return;
                    957:
1.197     schwarze  958:        if (h->noindent == 0) {
                    959:                h->col = h->indent * 2;
                    960:                for (i = 0; i < h->col; i++)
1.196     schwarze  961:                        putchar(' ');
1.197     schwarze  962:        }
                    963:        h->flags &= ~HTML_NOSPACE;
                    964: }
                    965:
                    966: /*
                    967:  * Print or buffer some characters
                    968:  * depending on the current HTML output buffer state.
                    969:  */
                    970: static void
                    971: print_word(struct html *h, const char *cp)
                    972: {
                    973:        while (*cp != '\0')
                    974:                print_byte(h, *cp++);
1.68      kristaps  975: }

CVSweb