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

Annotation of mandoc/html.c, Revision 1.213

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

CVSweb