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

Annotation of mandoc/html.c, Revision 1.201

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

CVSweb