[BACK]Return to html.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/html.c, Revision 1.126

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