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

Annotation of mandoc/html.c, Revision 1.206

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

CVSweb