[BACK]Return to html.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/html.c, Revision 1.209

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

CVSweb