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

Annotation of mandoc/html.c, Revision 1.202

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

CVSweb