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

Annotation of mandoc/html.c, Revision 1.235

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

CVSweb