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

Annotation of mandoc/html.c, Revision 1.223

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

CVSweb