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

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