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

Annotation of mandoc/html.c, Revision 1.199

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

CVSweb