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

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