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

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