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

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