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

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