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

Annotation of mandoc/html.c, Revision 1.198

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

CVSweb