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

Annotation of mandoc/html.c, Revision 1.255

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

CVSweb