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

Annotation of mandoc/html.c, Revision 1.230

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

CVSweb