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

Annotation of mandoc/html.c, Revision 1.240

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

CVSweb