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

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