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

Annotation of mandoc/html.c, Revision 1.234

1.234   ! schwarze    1: /*     $Id$ */
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:                        }
1.211     schwarze  697:                        if (*fmt == '+') {
1.219     schwarze  698:                                if (su != NULL) {
                    699:                                        /* Make even bold text fit. */
                    700:                                        su->scale *= 1.2;
                    701:                                        /* Add padding. */
                    702:                                        su->scale += 3.0;
                    703:                                }
1.211     schwarze  704:                                fmt++;
                    705:                        }
1.194     schwarze  706:                        break;
                    707:                default:
                    708:                        abort();
                    709:                }
                    710:
                    711:                /* Second letter: style name. */
                    712:
                    713:                switch (*fmt++) {
1.233     schwarze  714:                case 'h':
                    715:                        attr = "height";
1.194     schwarze  716:                        break;
                    717:                case 'l':
1.195     schwarze  718:                        attr = "margin-left";
1.194     schwarze  719:                        break;
                    720:                case 'w':
1.195     schwarze  721:                        attr = "width";
1.194     schwarze  722:                        break;
                    723:                case 'W':
1.195     schwarze  724:                        attr = "min-width";
1.194     schwarze  725:                        break;
                    726:                case '?':
1.203     schwarze  727:                        attr = arg1;
                    728:                        arg1 = va_arg(ap, char *);
                    729:                        break;
1.194     schwarze  730:                default:
                    731:                        abort();
                    732:                }
1.203     schwarze  733:                if (su == NULL && arg1 == NULL)
                    734:                        continue;
                    735:
                    736:                if (have_style == 1)
                    737:                        print_word(h, " style=\"");
                    738:                else
                    739:                        print_byte(h, ' ');
1.197     schwarze  740:                print_word(h, attr);
                    741:                print_byte(h, ':');
                    742:                print_byte(h, ' ');
1.203     schwarze  743:                if (su != NULL) {
                    744:                        v = su->scale;
                    745:                        if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
                    746:                                v = 1.0;
                    747:                        else if (su->unit == SCALE_BU)
                    748:                                v /= 24.0;
                    749:                        (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
                    750:                        print_word(h, numbuf);
                    751:                        print_word(h, roffscales[su->unit]);
                    752:                } else
                    753:                        print_word(h, arg1);
1.197     schwarze  754:                print_byte(h, ';');
1.203     schwarze  755:                have_style = 2;
1.194     schwarze  756:        }
1.203     schwarze  757:        if (have_style == 2)
1.197     schwarze  758:                print_byte(h, '"');
1.194     schwarze  759:
                    760:        va_end(ap);
1.94      kristaps  761:
1.172     kristaps  762:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  763:
1.93      kristaps  764:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  765:                print_byte(h, '/');
1.93      kristaps  766:
1.197     schwarze  767:        print_byte(h, '>');
1.14      kristaps  768:
1.196     schwarze  769:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  770:                print_endline(h);
1.196     schwarze  771:        else
                    772:                h->flags |= HTML_NOSPACE;
1.117     kristaps  773:
1.196     schwarze  774:        if (tflags & HTML_INDENT)
                    775:                h->indent++;
                    776:        if (tflags & HTML_NOINDENT)
                    777:                h->noindent++;
1.117     kristaps  778:
1.188     schwarze  779:        return t;
1.14      kristaps  780: }
                    781:
1.29      kristaps  782: static void
1.184     schwarze  783: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  784: {
1.196     schwarze  785:        int      tflags;
1.156     schwarze  786:
1.184     schwarze  787:        /*
                    788:         * Remember to close out and nullify the current
                    789:         * meta-font and table, if applicable.
                    790:         */
                    791:        if (tag == h->metaf)
                    792:                h->metaf = NULL;
                    793:        if (tag == h->tblt)
                    794:                h->tblt = NULL;
                    795:
1.196     schwarze  796:        tflags = htmltags[tag->tag].flags;
                    797:
                    798:        if (tflags & HTML_INDENT)
                    799:                h->indent--;
                    800:        if (tflags & HTML_NOINDENT)
                    801:                h->noindent--;
                    802:        if (tflags & HTML_NLEND)
1.197     schwarze  803:                print_endline(h);
                    804:        print_indent(h);
                    805:        print_byte(h, '<');
                    806:        print_byte(h, '/');
                    807:        print_word(h, htmltags[tag->tag].name);
                    808:        print_byte(h, '>');
1.196     schwarze  809:        if (tflags & HTML_NLAFTER)
1.197     schwarze  810:                print_endline(h);
1.184     schwarze  811:
1.204     schwarze  812:        h->tag = tag->next;
1.184     schwarze  813:        free(tag);
1.14      kristaps  814: }
                    815:
1.51      kristaps  816: void
1.93      kristaps  817: print_gen_decls(struct html *h)
1.1       kristaps  818: {
1.197     schwarze  819:        print_word(h, "<!DOCTYPE html>");
                    820:        print_endline(h);
1.221     schwarze  821: }
                    822:
                    823: void
                    824: print_gen_comment(struct html *h, struct roff_node *n)
                    825: {
                    826:        int      wantblank;
                    827:
                    828:        print_word(h, "<!-- This is an automatically generated file."
                    829:            "  Do not edit.");
                    830:        h->indent = 1;
                    831:        wantblank = 0;
                    832:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    833:                if (strstr(n->string, "-->") == NULL &&
                    834:                    (wantblank || *n->string != '\0')) {
                    835:                        print_endline(h);
                    836:                        print_indent(h);
                    837:                        print_word(h, n->string);
                    838:                        wantblank = *n->string != '\0';
                    839:                }
                    840:                n = n->next;
                    841:        }
                    842:        if (wantblank)
                    843:                print_endline(h);
                    844:        print_word(h, " -->");
                    845:        print_endline(h);
                    846:        h->indent = 0;
1.1       kristaps  847: }
                    848:
1.51      kristaps  849: void
1.104     kristaps  850: print_text(struct html *h, const char *word)
1.1       kristaps  851: {
1.197     schwarze  852:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  853:                if ( ! (HTML_KEEP & h->flags)) {
                    854:                        if (HTML_PREKEEP & h->flags)
                    855:                                h->flags |= HTML_KEEP;
1.197     schwarze  856:                        print_endword(h);
1.105     kristaps  857:                } else
1.216     schwarze  858:                        print_word(h, "&#x00A0;");
1.105     kristaps  859:        }
1.30      kristaps  860:
1.122     kristaps  861:        assert(NULL == h->metaf);
1.152     schwarze  862:        switch (h->metac) {
1.156     schwarze  863:        case HTMLFONT_ITALIC:
1.194     schwarze  864:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  865:                break;
1.156     schwarze  866:        case HTMLFONT_BOLD:
1.194     schwarze  867:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  868:                break;
1.156     schwarze  869:        case HTMLFONT_BI:
1.194     schwarze  870:                h->metaf = print_otag(h, TAG_B, "");
                    871:                print_otag(h, TAG_I, "");
1.152     schwarze  872:                break;
                    873:        default:
1.197     schwarze  874:                print_indent(h);
1.152     schwarze  875:                break;
                    876:        }
1.122     kristaps  877:
1.104     kristaps  878:        assert(word);
1.195     schwarze  879:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  880:                if ( ! (h->flags & HTML_NONOSPACE))
                    881:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  882:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  883:        } else
1.183     schwarze  884:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  885:
                    886:        if (h->metaf) {
                    887:                print_tagq(h, h->metaf);
                    888:                h->metaf = NULL;
                    889:        }
1.113     schwarze  890:
                    891:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  892: }
1.30      kristaps  893:
1.51      kristaps  894: void
1.30      kristaps  895: print_tagq(struct html *h, const struct tag *until)
                    896: {
                    897:        struct tag      *tag;
                    898:
1.204     schwarze  899:        while ((tag = h->tag) != NULL) {
1.184     schwarze  900:                print_ctag(h, tag);
1.30      kristaps  901:                if (until && tag == until)
                    902:                        return;
                    903:        }
                    904: }
                    905:
1.51      kristaps  906: void
1.30      kristaps  907: print_stagq(struct html *h, const struct tag *suntil)
                    908: {
                    909:        struct tag      *tag;
                    910:
1.204     schwarze  911:        while ((tag = h->tag) != NULL) {
1.30      kristaps  912:                if (suntil && tag == suntil)
                    913:                        return;
1.184     schwarze  914:                print_ctag(h, tag);
1.30      kristaps  915:        }
                    916: }
1.171     kristaps  917:
                    918: void
                    919: print_paragraph(struct html *h)
                    920: {
                    921:        struct tag      *t;
                    922:
1.198     schwarze  923:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.171     kristaps  924:        print_tagq(h, t);
                    925: }
                    926:
1.197     schwarze  927:
                    928: /***********************************************************************
                    929:  * Low level output functions.
                    930:  * They implement line breaking using a short static buffer.
                    931:  ***********************************************************************/
                    932:
                    933: /*
                    934:  * Buffer one HTML output byte.
                    935:  * If the buffer is full, flush and deactivate it and start a new line.
                    936:  * If the buffer is inactive, print directly.
                    937:  */
                    938: static void
                    939: print_byte(struct html *h, char c)
                    940: {
                    941:        if ((h->flags & HTML_BUFFER) == 0) {
                    942:                putchar(c);
                    943:                h->col++;
                    944:                return;
                    945:        }
                    946:
                    947:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    948:                h->buf[h->bufcol++] = c;
                    949:                return;
                    950:        }
                    951:
                    952:        putchar('\n');
                    953:        h->col = 0;
                    954:        print_indent(h);
                    955:        putchar(' ');
                    956:        putchar(' ');
                    957:        fwrite(h->buf, h->bufcol, 1, stdout);
                    958:        putchar(c);
                    959:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    960:        h->bufcol = 0;
                    961:        h->flags &= ~HTML_BUFFER;
                    962: }
                    963:
1.196     schwarze  964: /*
                    965:  * If something was printed on the current output line, end it.
1.197     schwarze  966:  * Not to be called right after print_indent().
1.196     schwarze  967:  */
1.202     schwarze  968: void
1.197     schwarze  969: print_endline(struct html *h)
1.196     schwarze  970: {
1.197     schwarze  971:        if (h->col == 0)
1.196     schwarze  972:                return;
                    973:
1.197     schwarze  974:        if (h->bufcol) {
                    975:                putchar(' ');
                    976:                fwrite(h->buf, h->bufcol, 1, stdout);
                    977:                h->bufcol = 0;
                    978:        }
1.196     schwarze  979:        putchar('\n');
1.197     schwarze  980:        h->col = 0;
                    981:        h->flags |= HTML_NOSPACE;
                    982:        h->flags &= ~HTML_BUFFER;
                    983: }
                    984:
                    985: /*
                    986:  * Flush the HTML output buffer.
                    987:  * If it is inactive, activate it.
                    988:  */
                    989: static void
                    990: print_endword(struct html *h)
                    991: {
                    992:        if (h->noindent) {
                    993:                print_byte(h, ' ');
                    994:                return;
                    995:        }
                    996:
                    997:        if ((h->flags & HTML_BUFFER) == 0) {
                    998:                h->col++;
                    999:                h->flags |= HTML_BUFFER;
                   1000:        } else if (h->bufcol) {
                   1001:                putchar(' ');
                   1002:                fwrite(h->buf, h->bufcol, 1, stdout);
                   1003:                h->col += h->bufcol + 1;
                   1004:        }
                   1005:        h->bufcol = 0;
1.196     schwarze 1006: }
                   1007:
                   1008: /*
                   1009:  * If at the beginning of a new output line,
                   1010:  * perform indentation and mark the line as containing output.
                   1011:  * Make sure to really produce some output right afterwards,
                   1012:  * but do not use print_otag() for producing it.
                   1013:  */
                   1014: static void
1.197     schwarze 1015: print_indent(struct html *h)
1.196     schwarze 1016: {
1.197     schwarze 1017:        size_t   i;
1.196     schwarze 1018:
1.197     schwarze 1019:        if (h->col)
1.196     schwarze 1020:                return;
                   1021:
1.197     schwarze 1022:        if (h->noindent == 0) {
                   1023:                h->col = h->indent * 2;
                   1024:                for (i = 0; i < h->col; i++)
1.196     schwarze 1025:                        putchar(' ');
1.197     schwarze 1026:        }
                   1027:        h->flags &= ~HTML_NOSPACE;
                   1028: }
                   1029:
                   1030: /*
                   1031:  * Print or buffer some characters
                   1032:  * depending on the current HTML output buffer state.
                   1033:  */
                   1034: static void
                   1035: print_word(struct html *h, const char *cp)
                   1036: {
                   1037:        while (*cp != '\0')
                   1038:                print_byte(h, *cp++);
1.196     schwarze 1039: }
1.194     schwarze 1040:
                   1041: /*
                   1042:  * Calculate the scaling unit passed in a `-width' argument.  This uses
                   1043:  * either a native scaling unit (e.g., 1i, 2m) or the string length of
                   1044:  * the value.
                   1045:  */
                   1046: static void
                   1047: a2width(const char *p, struct roffsu *su)
                   1048: {
1.213     schwarze 1049:        const char      *end;
                   1050:
                   1051:        end = a2roffsu(p, su, SCALE_MAX);
                   1052:        if (end == NULL || *end != '\0') {
1.194     schwarze 1053:                su->unit = SCALE_EN;
                   1054:                su->scale = html_strlen(p);
                   1055:        } else if (su->scale < 0.0)
                   1056:                su->scale = 0.0;
1.68      kristaps 1057: }

CVSweb