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

Annotation of mandoc/html.c, Revision 1.244

1.244   ! schwarze    1: /*     $Id: html.c,v 1.243 2018/11/23 19:17:05 schwarze Exp $ */
1.1       kristaps    2: /*
1.176     schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.221     schwarze    4:  * Copyright (c) 2011-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.186     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29      kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.92      kristaps   18: #include "config.h"
                     19:
1.41      kristaps   20: #include <sys/types.h>
1.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},
                     81:        {"pre",         HTML_NLALL | HTML_NOINDENT},
1.207     schwarze   82:        {"var",         0},
1.206     schwarze   83:        {"cite",        0},
1.196     schwarze   84:        {"b",           0},
                     85:        {"i",           0},
                     86:        {"code",        0},
                     87:        {"small",       0},
                     88:        {"style",       HTML_NLALL | HTML_INDENT},
                     89:        {"math",        HTML_NLALL | HTML_INDENT},
                     90:        {"mrow",        0},
                     91:        {"mi",          0},
1.215     schwarze   92:        {"mn",          0},
1.196     schwarze   93:        {"mo",          0},
                     94:        {"msup",        0},
                     95:        {"msub",        0},
                     96:        {"msubsup",     0},
                     97:        {"mfrac",       0},
                     98:        {"msqrt",       0},
                     99:        {"mfenced",     0},
                    100:        {"mtable",      0},
                    101:        {"mtr",         0},
                    102:        {"mtd",         0},
                    103:        {"munderover",  0},
                    104:        {"munder",      0},
                    105:        {"mover",       0},
1.90      kristaps  106: };
                    107:
1.229     schwarze  108: /* Avoid duplicate HTML id= attributes. */
                    109: static struct ohash     id_unique;
                    110:
1.197     schwarze  111: static void     print_byte(struct html *, char);
                    112: static void     print_endword(struct html *);
                    113: static void     print_indent(struct html *);
                    114: static void     print_word(struct html *, const char *);
                    115:
1.184     schwarze  116: static void     print_ctag(struct html *, struct tag *);
1.197     schwarze  117: static int      print_escape(struct html *, char);
1.195     schwarze  118: static int      print_encode(struct html *, const char *, const char *, int);
                    119: static void     print_href(struct html *, const char *, const char *, int);
1.141     kristaps  120: static void     print_metaf(struct html *, enum mandoc_esc);
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.126     schwarze  213: static 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:
                    239:                abort();
                    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.210     schwarze  267: }
                    268:
                    269: char *
1.229     schwarze  270: html_make_id(const struct roff_node *n, int unique)
1.210     schwarze  271: {
                    272:        const struct roff_node  *nch;
1.229     schwarze  273:        char                    *buf, *bufs, *cp;
                    274:        unsigned int             slot;
                    275:        int                      suffix;
1.210     schwarze  276:
                    277:        for (nch = n->child; nch != NULL; nch = nch->next)
                    278:                if (nch->type != ROFFT_TEXT)
                    279:                        return NULL;
                    280:
                    281:        buf = NULL;
                    282:        deroff(&buf, n);
1.220     schwarze  283:        if (buf == NULL)
                    284:                return NULL;
1.210     schwarze  285:
1.230     schwarze  286:        /*
                    287:         * In ID attributes, only use ASCII characters that are
                    288:         * permitted in URL-fragment strings according to the
                    289:         * explicit list at:
                    290:         * https://url.spec.whatwg.org/#url-fragment-string
                    291:         */
1.210     schwarze  292:
                    293:        for (cp = buf; *cp != '\0'; cp++)
1.230     schwarze  294:                if (isalnum((unsigned char)*cp) == 0 &&
                    295:                    strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210     schwarze  296:                        *cp = '_';
                    297:
1.229     schwarze  298:        if (unique == 0)
                    299:                return buf;
                    300:
                    301:        /* Avoid duplicate HTML id= attributes. */
                    302:
                    303:        bufs = NULL;
                    304:        suffix = 1;
                    305:        slot = ohash_qlookup(&id_unique, buf);
                    306:        cp = ohash_find(&id_unique, slot);
                    307:        if (cp != NULL) {
                    308:                while (cp != NULL) {
                    309:                        free(bufs);
                    310:                        if (++suffix > 127) {
                    311:                                free(buf);
                    312:                                return NULL;
                    313:                        }
                    314:                        mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
                    315:                        slot = ohash_qlookup(&id_unique, bufs);
                    316:                        cp = ohash_find(&id_unique, slot);
                    317:                }
                    318:                free(buf);
                    319:                buf = bufs;
                    320:        }
                    321:        ohash_insert(&id_unique, slot, buf);
1.210     schwarze  322:        return buf;
1.88      kristaps  323: }
                    324:
1.85      kristaps  325: static int
1.197     schwarze  326: print_escape(struct html *h, char c)
1.159     schwarze  327: {
                    328:
                    329:        switch (c) {
                    330:        case '<':
1.197     schwarze  331:                print_word(h, "&lt;");
1.159     schwarze  332:                break;
                    333:        case '>':
1.197     schwarze  334:                print_word(h, "&gt;");
1.159     schwarze  335:                break;
                    336:        case '&':
1.197     schwarze  337:                print_word(h, "&amp;");
1.159     schwarze  338:                break;
                    339:        case '"':
1.197     schwarze  340:                print_word(h, "&quot;");
1.159     schwarze  341:                break;
                    342:        case ASCII_NBRSP:
1.197     schwarze  343:                print_word(h, "&nbsp;");
1.159     schwarze  344:                break;
                    345:        case ASCII_HYPH:
1.197     schwarze  346:                print_byte(h, '-');
1.189     schwarze  347:                break;
1.159     schwarze  348:        case ASCII_BREAK:
                    349:                break;
                    350:        default:
1.188     schwarze  351:                return 0;
1.159     schwarze  352:        }
1.188     schwarze  353:        return 1;
1.159     schwarze  354: }
                    355:
                    356: static int
1.195     schwarze  357: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29      kristaps  358: {
1.197     schwarze  359:        char             numbuf[16];
1.214     schwarze  360:        struct tag      *t;
                    361:        const char      *seq;
1.77      kristaps  362:        size_t           sz;
1.214     schwarze  363:        int              c, len, breakline, nospace;
1.132     kristaps  364:        enum mandoc_esc  esc;
1.214     schwarze  365:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154     schwarze  366:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  367:
1.195     schwarze  368:        if (pend == NULL)
                    369:                pend = strchr(p, '\0');
                    370:
1.214     schwarze  371:        breakline = 0;
1.85      kristaps  372:        nospace = 0;
                    373:
1.195     schwarze  374:        while (p < pend) {
1.151     schwarze  375:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    376:                        h->flags &= ~HTML_SKIPCHAR;
                    377:                        p++;
                    378:                        continue;
                    379:                }
                    380:
1.197     schwarze  381:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214     schwarze  382:                        print_byte(h, *p);
                    383:
                    384:                if (breakline &&
                    385:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
                    386:                        t = print_otag(h, TAG_DIV, "");
                    387:                        print_text(h, "\\~");
                    388:                        print_tagq(h, t);
                    389:                        breakline = 0;
                    390:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    391:                                p++;
                    392:                        continue;
                    393:                }
1.77      kristaps  394:
1.195     schwarze  395:                if (p >= pend)
1.132     kristaps  396:                        break;
                    397:
1.214     schwarze  398:                if (*p == ' ') {
                    399:                        print_endword(h);
                    400:                        p++;
                    401:                        continue;
                    402:                }
                    403:
1.197     schwarze  404:                if (print_escape(h, *p++))
1.154     schwarze  405:                        continue;
1.77      kristaps  406:
1.132     kristaps  407:                esc = mandoc_escape(&p, &seq, &len);
                    408:                if (ESCAPE_ERROR == esc)
                    409:                        break;
1.82      kristaps  410:
1.132     kristaps  411:                switch (esc) {
1.156     schwarze  412:                case ESCAPE_FONT:
                    413:                case ESCAPE_FONTPREV:
                    414:                case ESCAPE_FONTBOLD:
                    415:                case ESCAPE_FONTITALIC:
                    416:                case ESCAPE_FONTBI:
1.242     schwarze  417:                case ESCAPE_FONTCW:
1.156     schwarze  418:                case ESCAPE_FONTROMAN:
1.243     schwarze  419:                        if (0 == norecurse) {
                    420:                                h->flags |= HTML_NOSPACE;
1.151     schwarze  421:                                print_metaf(h, esc);
1.243     schwarze  422:                                h->flags &= ~HTML_NOSPACE;
                    423:                        }
1.151     schwarze  424:                        continue;
1.156     schwarze  425:                case ESCAPE_SKIPCHAR:
1.151     schwarze  426:                        h->flags |= HTML_SKIPCHAR;
                    427:                        continue;
                    428:                default:
                    429:                        break;
                    430:                }
                    431:
                    432:                if (h->flags & HTML_SKIPCHAR) {
                    433:                        h->flags &= ~HTML_SKIPCHAR;
                    434:                        continue;
                    435:                }
                    436:
                    437:                switch (esc) {
1.156     schwarze  438:                case ESCAPE_UNICODE:
1.159     schwarze  439:                        /* Skip past "u" header. */
1.144     kristaps  440:                        c = mchars_num2uc(seq + 1, len - 1);
                    441:                        break;
1.156     schwarze  442:                case ESCAPE_NUMBERED:
1.141     kristaps  443:                        c = mchars_num2char(seq, len);
1.181     schwarze  444:                        if (c < 0)
                    445:                                continue;
1.82      kristaps  446:                        break;
1.156     schwarze  447:                case ESCAPE_SPECIAL:
1.191     schwarze  448:                        c = mchars_spec2cp(seq, len);
1.181     schwarze  449:                        if (c <= 0)
                    450:                                continue;
1.132     kristaps  451:                        break;
1.239     schwarze  452:                case ESCAPE_DEVICE:
                    453:                        print_word(h, "html");
                    454:                        continue;
1.214     schwarze  455:                case ESCAPE_BREAK:
                    456:                        breakline = 1;
                    457:                        continue;
1.156     schwarze  458:                case ESCAPE_NOSPACE:
1.132     kristaps  459:                        if ('\0' == *p)
                    460:                                nospace = 1;
1.179     schwarze  461:                        continue;
1.185     schwarze  462:                case ESCAPE_OVERSTRIKE:
                    463:                        if (len == 0)
                    464:                                continue;
                    465:                        c = seq[len - 1];
                    466:                        break;
1.82      kristaps  467:                default:
1.179     schwarze  468:                        continue;
1.82      kristaps  469:                }
1.181     schwarze  470:                if ((c < 0x20 && c != 0x09) ||
                    471:                    (c > 0x7E && c < 0xA0))
1.179     schwarze  472:                        c = 0xFFFD;
1.197     schwarze  473:                if (c > 0x7E) {
1.216     schwarze  474:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197     schwarze  475:                        print_word(h, numbuf);
                    476:                } else if (print_escape(h, c) == 0)
                    477:                        print_byte(h, c);
1.32      kristaps  478:        }
1.85      kristaps  479:
1.188     schwarze  480:        return nospace;
1.14      kristaps  481: }
                    482:
1.94      kristaps  483: static void
1.195     schwarze  484: print_href(struct html *h, const char *name, const char *sec, int man)
1.94      kristaps  485: {
1.240     schwarze  486:        struct stat      sb;
1.195     schwarze  487:        const char      *p, *pp;
1.240     schwarze  488:        char            *filename;
                    489:
                    490:        if (man) {
                    491:                pp = h->base_man1;
                    492:                if (h->base_man2 != NULL) {
                    493:                        mandoc_asprintf(&filename, "%s.%s", name, sec);
                    494:                        if (stat(filename, &sb) == -1)
                    495:                                pp = h->base_man2;
                    496:                        free(filename);
                    497:                }
                    498:        } else
                    499:                pp = h->base_includes;
1.195     schwarze  500:
                    501:        while ((p = strchr(pp, '%')) != NULL) {
                    502:                print_encode(h, pp, p, 1);
                    503:                if (man && p[1] == 'S') {
                    504:                        if (sec == NULL)
1.197     schwarze  505:                                print_byte(h, '1');
1.195     schwarze  506:                        else
                    507:                                print_encode(h, sec, NULL, 1);
                    508:                } else if ((man && p[1] == 'N') ||
                    509:                    (man == 0 && p[1] == 'I'))
                    510:                        print_encode(h, name, NULL, 1);
                    511:                else
                    512:                        print_encode(h, p, p + 2, 1);
                    513:                pp = p + 2;
                    514:        }
                    515:        if (*pp != '\0')
                    516:                print_encode(h, pp, NULL, 1);
1.94      kristaps  517: }
                    518:
1.51      kristaps  519: struct tag *
1.194     schwarze  520: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14      kristaps  521: {
1.194     schwarze  522:        va_list          ap;
1.30      kristaps  523:        struct tag      *t;
1.195     schwarze  524:        const char      *attr;
1.203     schwarze  525:        char            *arg1, *arg2;
1.244   ! schwarze  526:        int              style_written, tflags;
1.196     schwarze  527:
                    528:        tflags = htmltags[tag].flags;
1.30      kristaps  529:
1.204     schwarze  530:        /* Push this tag onto the stack of open scopes. */
1.94      kristaps  531:
1.196     schwarze  532:        if ((tflags & HTML_NOSTACK) == 0) {
1.128     kristaps  533:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  534:                t->tag = tag;
1.204     schwarze  535:                t->next = h->tag;
                    536:                h->tag = t;
1.30      kristaps  537:        } else
                    538:                t = NULL;
1.29      kristaps  539:
1.196     schwarze  540:        if (tflags & HTML_NLBEFORE)
1.197     schwarze  541:                print_endline(h);
                    542:        if (h->col == 0)
                    543:                print_indent(h);
1.196     schwarze  544:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    545:                if (h->flags & HTML_KEEP)
1.216     schwarze  546:                        print_word(h, "&#x00A0;");
1.196     schwarze  547:                else {
                    548:                        if (h->flags & HTML_PREKEEP)
                    549:                                h->flags |= HTML_KEEP;
1.197     schwarze  550:                        print_endword(h);
1.105     kristaps  551:                }
1.196     schwarze  552:        }
1.29      kristaps  553:
1.109     kristaps  554:        if ( ! (h->flags & HTML_NONOSPACE))
                    555:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  556:        else
                    557:                h->flags |= HTML_NOSPACE;
1.109     kristaps  558:
1.94      kristaps  559:        /* Print out the tag name and attributes. */
                    560:
1.197     schwarze  561:        print_byte(h, '<');
                    562:        print_word(h, htmltags[tag].name);
1.194     schwarze  563:
                    564:        va_start(ap, fmt);
                    565:
1.244   ! schwarze  566:        while (*fmt != '\0' && *fmt != 's') {
1.203     schwarze  567:
1.238     schwarze  568:                /* Parse attributes and arguments. */
1.203     schwarze  569:
                    570:                arg1 = va_arg(ap, char *);
1.238     schwarze  571:                arg2 = NULL;
1.194     schwarze  572:                switch (*fmt++) {
                    573:                case 'c':
1.195     schwarze  574:                        attr = "class";
1.194     schwarze  575:                        break;
                    576:                case 'h':
1.195     schwarze  577:                        attr = "href";
1.194     schwarze  578:                        break;
                    579:                case 'i':
1.195     schwarze  580:                        attr = "id";
1.194     schwarze  581:                        break;
                    582:                case '?':
1.203     schwarze  583:                        attr = arg1;
                    584:                        arg1 = va_arg(ap, char *);
1.194     schwarze  585:                        break;
                    586:                default:
                    587:                        abort();
                    588:                }
1.203     schwarze  589:                if (*fmt == 'M')
                    590:                        arg2 = va_arg(ap, char *);
                    591:                if (arg1 == NULL)
                    592:                        continue;
                    593:
1.238     schwarze  594:                /* Print the attributes. */
1.203     schwarze  595:
1.197     schwarze  596:                print_byte(h, ' ');
                    597:                print_word(h, attr);
                    598:                print_byte(h, '=');
                    599:                print_byte(h, '"');
1.195     schwarze  600:                switch (*fmt) {
1.208     schwarze  601:                case 'I':
                    602:                        print_href(h, arg1, NULL, 0);
                    603:                        fmt++;
                    604:                        break;
1.195     schwarze  605:                case 'M':
1.203     schwarze  606:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  607:                        fmt++;
                    608:                        break;
1.208     schwarze  609:                case 'R':
                    610:                        print_byte(h, '#');
                    611:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  612:                        fmt++;
                    613:                        break;
1.208     schwarze  614:                case 'T':
                    615:                        print_encode(h, arg1, NULL, 1);
                    616:                        print_word(h, "\" title=\"");
                    617:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  618:                        fmt++;
1.208     schwarze  619:                        break;
1.195     schwarze  620:                default:
1.244   ! schwarze  621:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  622:                        break;
                    623:                }
1.197     schwarze  624:                print_byte(h, '"');
1.194     schwarze  625:        }
1.244   ! schwarze  626:
        !           627:        style_written = 0;
        !           628:        while (*fmt++ == 's') {
        !           629:                arg1 = va_arg(ap, char *);
        !           630:                arg2 = va_arg(ap, char *);
        !           631:                if (arg2 == NULL)
        !           632:                        continue;
        !           633:                print_byte(h, ' ');
        !           634:                if (style_written == 0) {
        !           635:                        print_word(h, "style=\"");
        !           636:                        style_written = 1;
        !           637:                }
        !           638:                print_word(h, arg1);
        !           639:                print_byte(h, ':');
        !           640:                print_byte(h, ' ');
        !           641:                print_word(h, arg2);
        !           642:                print_byte(h, ';');
        !           643:        }
        !           644:        if (style_written)
        !           645:                print_byte(h, '"');
        !           646:
1.194     schwarze  647:        va_end(ap);
1.94      kristaps  648:
1.172     kristaps  649:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  650:
1.93      kristaps  651:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  652:                print_byte(h, '/');
1.93      kristaps  653:
1.197     schwarze  654:        print_byte(h, '>');
1.14      kristaps  655:
1.196     schwarze  656:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  657:                print_endline(h);
1.196     schwarze  658:        else
                    659:                h->flags |= HTML_NOSPACE;
1.117     kristaps  660:
1.196     schwarze  661:        if (tflags & HTML_INDENT)
                    662:                h->indent++;
                    663:        if (tflags & HTML_NOINDENT)
                    664:                h->noindent++;
1.117     kristaps  665:
1.188     schwarze  666:        return t;
1.14      kristaps  667: }
                    668:
1.29      kristaps  669: static void
1.184     schwarze  670: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  671: {
1.196     schwarze  672:        int      tflags;
1.156     schwarze  673:
1.184     schwarze  674:        /*
                    675:         * Remember to close out and nullify the current
                    676:         * meta-font and table, if applicable.
                    677:         */
                    678:        if (tag == h->metaf)
                    679:                h->metaf = NULL;
                    680:        if (tag == h->tblt)
                    681:                h->tblt = NULL;
                    682:
1.196     schwarze  683:        tflags = htmltags[tag->tag].flags;
                    684:
                    685:        if (tflags & HTML_INDENT)
                    686:                h->indent--;
                    687:        if (tflags & HTML_NOINDENT)
                    688:                h->noindent--;
                    689:        if (tflags & HTML_NLEND)
1.197     schwarze  690:                print_endline(h);
                    691:        print_indent(h);
                    692:        print_byte(h, '<');
                    693:        print_byte(h, '/');
                    694:        print_word(h, htmltags[tag->tag].name);
                    695:        print_byte(h, '>');
1.196     schwarze  696:        if (tflags & HTML_NLAFTER)
1.197     schwarze  697:                print_endline(h);
1.184     schwarze  698:
1.204     schwarze  699:        h->tag = tag->next;
1.184     schwarze  700:        free(tag);
1.14      kristaps  701: }
                    702:
1.51      kristaps  703: void
1.93      kristaps  704: print_gen_decls(struct html *h)
1.1       kristaps  705: {
1.197     schwarze  706:        print_word(h, "<!DOCTYPE html>");
                    707:        print_endline(h);
1.221     schwarze  708: }
                    709:
                    710: void
                    711: print_gen_comment(struct html *h, struct roff_node *n)
                    712: {
                    713:        int      wantblank;
                    714:
                    715:        print_word(h, "<!-- This is an automatically generated file."
                    716:            "  Do not edit.");
                    717:        h->indent = 1;
                    718:        wantblank = 0;
                    719:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    720:                if (strstr(n->string, "-->") == NULL &&
                    721:                    (wantblank || *n->string != '\0')) {
                    722:                        print_endline(h);
                    723:                        print_indent(h);
                    724:                        print_word(h, n->string);
                    725:                        wantblank = *n->string != '\0';
                    726:                }
                    727:                n = n->next;
                    728:        }
                    729:        if (wantblank)
                    730:                print_endline(h);
                    731:        print_word(h, " -->");
                    732:        print_endline(h);
                    733:        h->indent = 0;
1.1       kristaps  734: }
                    735:
1.51      kristaps  736: void
1.104     kristaps  737: print_text(struct html *h, const char *word)
1.1       kristaps  738: {
1.197     schwarze  739:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  740:                if ( ! (HTML_KEEP & h->flags)) {
                    741:                        if (HTML_PREKEEP & h->flags)
                    742:                                h->flags |= HTML_KEEP;
1.197     schwarze  743:                        print_endword(h);
1.105     kristaps  744:                } else
1.216     schwarze  745:                        print_word(h, "&#x00A0;");
1.105     kristaps  746:        }
1.30      kristaps  747:
1.122     kristaps  748:        assert(NULL == h->metaf);
1.152     schwarze  749:        switch (h->metac) {
1.156     schwarze  750:        case HTMLFONT_ITALIC:
1.194     schwarze  751:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  752:                break;
1.156     schwarze  753:        case HTMLFONT_BOLD:
1.194     schwarze  754:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  755:                break;
1.156     schwarze  756:        case HTMLFONT_BI:
1.194     schwarze  757:                h->metaf = print_otag(h, TAG_B, "");
                    758:                print_otag(h, TAG_I, "");
1.242     schwarze  759:                break;
                    760:        case HTMLFONT_CW:
                    761:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
1.152     schwarze  762:                break;
                    763:        default:
1.197     schwarze  764:                print_indent(h);
1.152     schwarze  765:                break;
                    766:        }
1.122     kristaps  767:
1.104     kristaps  768:        assert(word);
1.195     schwarze  769:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  770:                if ( ! (h->flags & HTML_NONOSPACE))
                    771:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  772:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  773:        } else
1.183     schwarze  774:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  775:
                    776:        if (h->metaf) {
                    777:                print_tagq(h, h->metaf);
                    778:                h->metaf = NULL;
                    779:        }
1.113     schwarze  780:
                    781:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  782: }
1.30      kristaps  783:
1.51      kristaps  784: void
1.30      kristaps  785: print_tagq(struct html *h, const struct tag *until)
                    786: {
                    787:        struct tag      *tag;
                    788:
1.204     schwarze  789:        while ((tag = h->tag) != NULL) {
1.184     schwarze  790:                print_ctag(h, tag);
1.30      kristaps  791:                if (until && tag == until)
                    792:                        return;
                    793:        }
                    794: }
                    795:
1.51      kristaps  796: void
1.30      kristaps  797: print_stagq(struct html *h, const struct tag *suntil)
                    798: {
                    799:        struct tag      *tag;
                    800:
1.204     schwarze  801:        while ((tag = h->tag) != NULL) {
1.30      kristaps  802:                if (suntil && tag == suntil)
                    803:                        return;
1.184     schwarze  804:                print_ctag(h, tag);
1.30      kristaps  805:        }
                    806: }
1.171     kristaps  807:
                    808: void
                    809: print_paragraph(struct html *h)
                    810: {
                    811:        struct tag      *t;
                    812:
1.198     schwarze  813:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.171     kristaps  814:        print_tagq(h, t);
                    815: }
                    816:
1.197     schwarze  817:
                    818: /***********************************************************************
                    819:  * Low level output functions.
                    820:  * They implement line breaking using a short static buffer.
                    821:  ***********************************************************************/
                    822:
                    823: /*
                    824:  * Buffer one HTML output byte.
                    825:  * If the buffer is full, flush and deactivate it and start a new line.
                    826:  * If the buffer is inactive, print directly.
                    827:  */
                    828: static void
                    829: print_byte(struct html *h, char c)
                    830: {
                    831:        if ((h->flags & HTML_BUFFER) == 0) {
                    832:                putchar(c);
                    833:                h->col++;
                    834:                return;
                    835:        }
                    836:
                    837:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    838:                h->buf[h->bufcol++] = c;
                    839:                return;
                    840:        }
                    841:
                    842:        putchar('\n');
                    843:        h->col = 0;
                    844:        print_indent(h);
                    845:        putchar(' ');
                    846:        putchar(' ');
                    847:        fwrite(h->buf, h->bufcol, 1, stdout);
                    848:        putchar(c);
                    849:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    850:        h->bufcol = 0;
                    851:        h->flags &= ~HTML_BUFFER;
                    852: }
                    853:
1.196     schwarze  854: /*
                    855:  * If something was printed on the current output line, end it.
1.197     schwarze  856:  * Not to be called right after print_indent().
1.196     schwarze  857:  */
1.202     schwarze  858: void
1.197     schwarze  859: print_endline(struct html *h)
1.196     schwarze  860: {
1.197     schwarze  861:        if (h->col == 0)
1.196     schwarze  862:                return;
                    863:
1.197     schwarze  864:        if (h->bufcol) {
                    865:                putchar(' ');
                    866:                fwrite(h->buf, h->bufcol, 1, stdout);
                    867:                h->bufcol = 0;
                    868:        }
1.196     schwarze  869:        putchar('\n');
1.197     schwarze  870:        h->col = 0;
                    871:        h->flags |= HTML_NOSPACE;
                    872:        h->flags &= ~HTML_BUFFER;
                    873: }
                    874:
                    875: /*
                    876:  * Flush the HTML output buffer.
                    877:  * If it is inactive, activate it.
                    878:  */
                    879: static void
                    880: print_endword(struct html *h)
                    881: {
                    882:        if (h->noindent) {
                    883:                print_byte(h, ' ');
                    884:                return;
                    885:        }
                    886:
                    887:        if ((h->flags & HTML_BUFFER) == 0) {
                    888:                h->col++;
                    889:                h->flags |= HTML_BUFFER;
                    890:        } else if (h->bufcol) {
                    891:                putchar(' ');
                    892:                fwrite(h->buf, h->bufcol, 1, stdout);
                    893:                h->col += h->bufcol + 1;
                    894:        }
                    895:        h->bufcol = 0;
1.196     schwarze  896: }
                    897:
                    898: /*
                    899:  * If at the beginning of a new output line,
                    900:  * perform indentation and mark the line as containing output.
                    901:  * Make sure to really produce some output right afterwards,
                    902:  * but do not use print_otag() for producing it.
                    903:  */
                    904: static void
1.197     schwarze  905: print_indent(struct html *h)
1.196     schwarze  906: {
1.197     schwarze  907:        size_t   i;
1.196     schwarze  908:
1.197     schwarze  909:        if (h->col)
1.196     schwarze  910:                return;
                    911:
1.197     schwarze  912:        if (h->noindent == 0) {
                    913:                h->col = h->indent * 2;
                    914:                for (i = 0; i < h->col; i++)
1.196     schwarze  915:                        putchar(' ');
1.197     schwarze  916:        }
                    917:        h->flags &= ~HTML_NOSPACE;
                    918: }
                    919:
                    920: /*
                    921:  * Print or buffer some characters
                    922:  * depending on the current HTML output buffer state.
                    923:  */
                    924: static void
                    925: print_word(struct html *h, const char *cp)
                    926: {
                    927:        while (*cp != '\0')
                    928:                print_byte(h, *cp++);
1.68      kristaps  929: }

CVSweb