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

Annotation of mandoc/html.c, Revision 1.251

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

CVSweb