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

Annotation of mandoc/html.c, Revision 1.220

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

CVSweb