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

Annotation of mandoc/html.c, Revision 1.207

1.207   ! schwarze    1: /*     $Id: html.c,v 1.206 2017/02/05 19:29:19 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) {
                    537:                case 'M':
1.203     schwarze  538:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  539:                        fmt++;
                    540:                        break;
                    541:                case 'I':
1.203     schwarze  542:                        print_href(h, arg1, NULL, 0);
1.195     schwarze  543:                        fmt++;
                    544:                        break;
                    545:                case 'R':
1.197     schwarze  546:                        print_byte(h, '#');
1.195     schwarze  547:                        fmt++;
                    548:                        /* FALLTHROUGH */
                    549:                default:
1.203     schwarze  550:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  551:                        break;
                    552:                }
1.197     schwarze  553:                print_byte(h, '"');
1.194     schwarze  554:        }
                    555:
                    556:        /* Print out styles. */
                    557:
                    558:        while (*fmt != '\0') {
1.203     schwarze  559:                arg1 = NULL;
                    560:                su = NULL;
1.194     schwarze  561:
                    562:                /* First letter: input argument type. */
                    563:
                    564:                switch (*fmt++) {
                    565:                case 'h':
                    566:                        i = va_arg(ap, int);
1.203     schwarze  567:                        su = &mysu;
1.194     schwarze  568:                        SCALE_HS_INIT(su, i);
                    569:                        break;
                    570:                case 's':
1.203     schwarze  571:                        arg1 = va_arg(ap, char *);
1.194     schwarze  572:                        break;
                    573:                case 'u':
                    574:                        su = va_arg(ap, struct roffsu *);
                    575:                        break;
                    576:                case 'v':
                    577:                        i = va_arg(ap, int);
1.203     schwarze  578:                        su = &mysu;
1.194     schwarze  579:                        SCALE_VS_INIT(su, i);
                    580:                        break;
                    581:                case 'w':
1.201     schwarze  582:                case 'W':
1.203     schwarze  583:                        if ((arg2 = va_arg(ap, char *)) == NULL)
                    584:                                break;
                    585:                        su = &mysu;
                    586:                        a2width(arg2, su);
1.201     schwarze  587:                        if (fmt[-1] == 'W')
                    588:                                su->scale *= -1.0;
1.194     schwarze  589:                        break;
                    590:                default:
                    591:                        abort();
                    592:                }
                    593:
                    594:                /* Second letter: style name. */
                    595:
                    596:                switch (*fmt++) {
                    597:                case 'b':
1.195     schwarze  598:                        attr = "margin-bottom";
1.194     schwarze  599:                        break;
                    600:                case 'h':
1.195     schwarze  601:                        attr = "height";
1.194     schwarze  602:                        break;
                    603:                case 'i':
1.195     schwarze  604:                        attr = "text-indent";
1.194     schwarze  605:                        break;
                    606:                case 'l':
1.195     schwarze  607:                        attr = "margin-left";
1.194     schwarze  608:                        break;
                    609:                case 't':
1.195     schwarze  610:                        attr = "margin-top";
1.194     schwarze  611:                        break;
                    612:                case 'w':
1.195     schwarze  613:                        attr = "width";
1.194     schwarze  614:                        break;
                    615:                case 'W':
1.195     schwarze  616:                        attr = "min-width";
1.194     schwarze  617:                        break;
                    618:                case '?':
1.203     schwarze  619:                        attr = arg1;
                    620:                        arg1 = va_arg(ap, char *);
                    621:                        break;
1.194     schwarze  622:                default:
                    623:                        abort();
                    624:                }
1.203     schwarze  625:                if (su == NULL && arg1 == NULL)
                    626:                        continue;
                    627:
                    628:                if (have_style == 1)
                    629:                        print_word(h, " style=\"");
                    630:                else
                    631:                        print_byte(h, ' ');
1.197     schwarze  632:                print_word(h, attr);
                    633:                print_byte(h, ':');
                    634:                print_byte(h, ' ');
1.203     schwarze  635:                if (su != NULL) {
                    636:                        v = su->scale;
                    637:                        if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
                    638:                                v = 1.0;
                    639:                        else if (su->unit == SCALE_BU)
                    640:                                v /= 24.0;
                    641:                        (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
                    642:                        print_word(h, numbuf);
                    643:                        print_word(h, roffscales[su->unit]);
                    644:                } else
                    645:                        print_word(h, arg1);
1.197     schwarze  646:                print_byte(h, ';');
1.203     schwarze  647:                have_style = 2;
1.194     schwarze  648:        }
1.203     schwarze  649:        if (have_style == 2)
1.197     schwarze  650:                print_byte(h, '"');
1.194     schwarze  651:
                    652:        va_end(ap);
1.94      kristaps  653:
1.172     kristaps  654:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  655:
1.93      kristaps  656:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  657:                print_byte(h, '/');
1.93      kristaps  658:
1.197     schwarze  659:        print_byte(h, '>');
1.14      kristaps  660:
1.196     schwarze  661:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  662:                print_endline(h);
1.196     schwarze  663:        else
                    664:                h->flags |= HTML_NOSPACE;
1.117     kristaps  665:
1.196     schwarze  666:        if (tflags & HTML_INDENT)
                    667:                h->indent++;
                    668:        if (tflags & HTML_NOINDENT)
                    669:                h->noindent++;
1.117     kristaps  670:
1.188     schwarze  671:        return t;
1.14      kristaps  672: }
                    673:
1.29      kristaps  674: static void
1.184     schwarze  675: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  676: {
1.196     schwarze  677:        int      tflags;
1.156     schwarze  678:
1.184     schwarze  679:        /*
                    680:         * Remember to close out and nullify the current
                    681:         * meta-font and table, if applicable.
                    682:         */
                    683:        if (tag == h->metaf)
                    684:                h->metaf = NULL;
                    685:        if (tag == h->tblt)
                    686:                h->tblt = NULL;
                    687:
1.196     schwarze  688:        tflags = htmltags[tag->tag].flags;
                    689:
                    690:        if (tflags & HTML_INDENT)
                    691:                h->indent--;
                    692:        if (tflags & HTML_NOINDENT)
                    693:                h->noindent--;
                    694:        if (tflags & HTML_NLEND)
1.197     schwarze  695:                print_endline(h);
                    696:        print_indent(h);
                    697:        print_byte(h, '<');
                    698:        print_byte(h, '/');
                    699:        print_word(h, htmltags[tag->tag].name);
                    700:        print_byte(h, '>');
1.196     schwarze  701:        if (tflags & HTML_NLAFTER)
1.197     schwarze  702:                print_endline(h);
1.184     schwarze  703:
1.204     schwarze  704:        h->tag = tag->next;
1.184     schwarze  705:        free(tag);
1.14      kristaps  706: }
                    707:
1.51      kristaps  708: void
1.93      kristaps  709: print_gen_decls(struct html *h)
1.1       kristaps  710: {
1.197     schwarze  711:        print_word(h, "<!DOCTYPE html>");
                    712:        print_endline(h);
1.1       kristaps  713: }
                    714:
1.51      kristaps  715: void
1.104     kristaps  716: print_text(struct html *h, const char *word)
1.1       kristaps  717: {
1.197     schwarze  718:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  719:                if ( ! (HTML_KEEP & h->flags)) {
                    720:                        if (HTML_PREKEEP & h->flags)
                    721:                                h->flags |= HTML_KEEP;
1.197     schwarze  722:                        print_endword(h);
1.105     kristaps  723:                } else
1.197     schwarze  724:                        print_word(h, "&#160;");
1.105     kristaps  725:        }
1.30      kristaps  726:
1.122     kristaps  727:        assert(NULL == h->metaf);
1.152     schwarze  728:        switch (h->metac) {
1.156     schwarze  729:        case HTMLFONT_ITALIC:
1.194     schwarze  730:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  731:                break;
1.156     schwarze  732:        case HTMLFONT_BOLD:
1.194     schwarze  733:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  734:                break;
1.156     schwarze  735:        case HTMLFONT_BI:
1.194     schwarze  736:                h->metaf = print_otag(h, TAG_B, "");
                    737:                print_otag(h, TAG_I, "");
1.152     schwarze  738:                break;
                    739:        default:
1.197     schwarze  740:                print_indent(h);
1.152     schwarze  741:                break;
                    742:        }
1.122     kristaps  743:
1.104     kristaps  744:        assert(word);
1.195     schwarze  745:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  746:                if ( ! (h->flags & HTML_NONOSPACE))
                    747:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  748:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  749:        } else
1.183     schwarze  750:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  751:
                    752:        if (h->metaf) {
                    753:                print_tagq(h, h->metaf);
                    754:                h->metaf = NULL;
                    755:        }
1.113     schwarze  756:
                    757:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  758: }
1.30      kristaps  759:
1.51      kristaps  760: void
1.30      kristaps  761: print_tagq(struct html *h, const struct tag *until)
                    762: {
                    763:        struct tag      *tag;
                    764:
1.204     schwarze  765:        while ((tag = h->tag) != NULL) {
1.184     schwarze  766:                print_ctag(h, tag);
1.30      kristaps  767:                if (until && tag == until)
                    768:                        return;
                    769:        }
                    770: }
                    771:
1.51      kristaps  772: void
1.30      kristaps  773: print_stagq(struct html *h, const struct tag *suntil)
                    774: {
                    775:        struct tag      *tag;
                    776:
1.204     schwarze  777:        while ((tag = h->tag) != NULL) {
1.30      kristaps  778:                if (suntil && tag == suntil)
                    779:                        return;
1.184     schwarze  780:                print_ctag(h, tag);
1.30      kristaps  781:        }
                    782: }
1.171     kristaps  783:
                    784: void
                    785: print_paragraph(struct html *h)
                    786: {
                    787:        struct tag      *t;
                    788:
1.198     schwarze  789:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.171     kristaps  790:        print_tagq(h, t);
                    791: }
                    792:
1.197     schwarze  793:
                    794: /***********************************************************************
                    795:  * Low level output functions.
                    796:  * They implement line breaking using a short static buffer.
                    797:  ***********************************************************************/
                    798:
                    799: /*
                    800:  * Buffer one HTML output byte.
                    801:  * If the buffer is full, flush and deactivate it and start a new line.
                    802:  * If the buffer is inactive, print directly.
                    803:  */
                    804: static void
                    805: print_byte(struct html *h, char c)
                    806: {
                    807:        if ((h->flags & HTML_BUFFER) == 0) {
                    808:                putchar(c);
                    809:                h->col++;
                    810:                return;
                    811:        }
                    812:
                    813:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    814:                h->buf[h->bufcol++] = c;
                    815:                return;
                    816:        }
                    817:
                    818:        putchar('\n');
                    819:        h->col = 0;
                    820:        print_indent(h);
                    821:        putchar(' ');
                    822:        putchar(' ');
                    823:        fwrite(h->buf, h->bufcol, 1, stdout);
                    824:        putchar(c);
                    825:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    826:        h->bufcol = 0;
                    827:        h->flags &= ~HTML_BUFFER;
                    828: }
                    829:
1.196     schwarze  830: /*
                    831:  * If something was printed on the current output line, end it.
1.197     schwarze  832:  * Not to be called right after print_indent().
1.196     schwarze  833:  */
1.202     schwarze  834: void
1.197     schwarze  835: print_endline(struct html *h)
1.196     schwarze  836: {
1.197     schwarze  837:        if (h->col == 0)
1.196     schwarze  838:                return;
                    839:
1.197     schwarze  840:        if (h->bufcol) {
                    841:                putchar(' ');
                    842:                fwrite(h->buf, h->bufcol, 1, stdout);
                    843:                h->bufcol = 0;
                    844:        }
1.196     schwarze  845:        putchar('\n');
1.197     schwarze  846:        h->col = 0;
                    847:        h->flags |= HTML_NOSPACE;
                    848:        h->flags &= ~HTML_BUFFER;
                    849: }
                    850:
                    851: /*
                    852:  * Flush the HTML output buffer.
                    853:  * If it is inactive, activate it.
                    854:  */
                    855: static void
                    856: print_endword(struct html *h)
                    857: {
                    858:        if (h->noindent) {
                    859:                print_byte(h, ' ');
                    860:                return;
                    861:        }
                    862:
                    863:        if ((h->flags & HTML_BUFFER) == 0) {
                    864:                h->col++;
                    865:                h->flags |= HTML_BUFFER;
                    866:        } else if (h->bufcol) {
                    867:                putchar(' ');
                    868:                fwrite(h->buf, h->bufcol, 1, stdout);
                    869:                h->col += h->bufcol + 1;
                    870:        }
                    871:        h->bufcol = 0;
1.196     schwarze  872: }
                    873:
                    874: /*
                    875:  * If at the beginning of a new output line,
                    876:  * perform indentation and mark the line as containing output.
                    877:  * Make sure to really produce some output right afterwards,
                    878:  * but do not use print_otag() for producing it.
                    879:  */
                    880: static void
1.197     schwarze  881: print_indent(struct html *h)
1.196     schwarze  882: {
1.197     schwarze  883:        size_t   i;
1.196     schwarze  884:
1.197     schwarze  885:        if (h->col)
1.196     schwarze  886:                return;
                    887:
1.197     schwarze  888:        if (h->noindent == 0) {
                    889:                h->col = h->indent * 2;
                    890:                for (i = 0; i < h->col; i++)
1.196     schwarze  891:                        putchar(' ');
1.197     schwarze  892:        }
                    893:        h->flags &= ~HTML_NOSPACE;
                    894: }
                    895:
                    896: /*
                    897:  * Print or buffer some characters
                    898:  * depending on the current HTML output buffer state.
                    899:  */
                    900: static void
                    901: print_word(struct html *h, const char *cp)
                    902: {
                    903:        while (*cp != '\0')
                    904:                print_byte(h, *cp++);
1.196     schwarze  905: }
1.194     schwarze  906:
                    907: /*
                    908:  * Calculate the scaling unit passed in a `-width' argument.  This uses
                    909:  * either a native scaling unit (e.g., 1i, 2m) or the string length of
                    910:  * the value.
                    911:  */
                    912: static void
                    913: a2width(const char *p, struct roffsu *su)
                    914: {
                    915:        if (a2roffsu(p, su, SCALE_MAX) < 2) {
                    916:                su->unit = SCALE_EN;
                    917:                su->scale = html_strlen(p);
                    918:        } else if (su->scale < 0.0)
                    919:                su->scale = 0.0;
1.68      kristaps  920: }

CVSweb