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

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