[BACK]Return to html.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/html.c, Revision 1.39

1.39    ! schwarze    1: /*     $Id: html.c,v 1.38 2014/07/23 15:00:00 schwarze Exp $ */
1.1       schwarze    2: /*
1.22      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.33      schwarze    4:  * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     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.
                     17:  */
                     18: #include <sys/types.h>
                     19:
                     20: #include <assert.h>
1.3       schwarze   21: #include <ctype.h>
1.4       schwarze   22: #include <stdarg.h>
1.1       schwarze   23: #include <stdio.h>
                     24: #include <stdint.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
1.9       schwarze   29: #include "mandoc.h"
1.34      schwarze   30: #include "mandoc_aux.h"
1.26      schwarze   31: #include "libmandoc.h"
1.1       schwarze   32: #include "out.h"
                     33: #include "html.h"
                     34: #include "main.h"
                     35:
                     36: struct htmldata {
                     37:        const char       *name;
                     38:        int               flags;
                     39: #define        HTML_CLRLINE     (1 << 0)
                     40: #define        HTML_NOSTACK     (1 << 1)
1.6       schwarze   41: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.1       schwarze   42: };
                     43:
                     44: static const struct htmldata htmltags[TAG_MAX] = {
                     45:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     46:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     47:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.6       schwarze   48:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1       schwarze   49:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
                     50:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
                     51:        {"h1",          0}, /* TAG_H1 */
                     52:        {"h2",          0}, /* TAG_H2 */
                     53:        {"span",        0}, /* TAG_SPAN */
1.8       schwarze   54:        {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6       schwarze   55:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1       schwarze   56:        {"a",           0}, /* TAG_A */
                     57:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
1.18      schwarze   58:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.6       schwarze   59:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1       schwarze   60:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     61:        {"td",          HTML_CLRLINE}, /* TAG_TD */
                     62:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     63:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     64:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.18      schwarze   65:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
                     66:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
                     67:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
                     68:        {"blockquote",  HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
                     69:        {"p",           HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_P */
                     70:        {"pre",         HTML_CLRLINE }, /* TAG_PRE */
1.19      schwarze   71:        {"b",           0 }, /* TAG_B */
                     72:        {"i",           0 }, /* TAG_I */
1.20      schwarze   73:        {"code",        0 }, /* TAG_CODE */
                     74:        {"small",       0 }, /* TAG_SMALL */
1.39    ! schwarze   75:        {"em",          0 }, /* TAG_EM */
1.5       schwarze   76: };
                     77:
                     78: static const char      *const htmlattrs[ATTR_MAX] = {
1.19      schwarze   79:        "http-equiv", /* ATTR_HTTPEQUIV */
                     80:        "content", /* ATTR_CONTENT */
                     81:        "name", /* ATTR_NAME */
                     82:        "rel", /* ATTR_REL */
                     83:        "href", /* ATTR_HREF */
                     84:        "type", /* ATTR_TYPE */
                     85:        "media", /* ATTR_MEDIA */
                     86:        "class", /* ATTR_CLASS */
                     87:        "style", /* ATTR_STYLE */
                     88:        "width", /* ATTR_WIDTH */
                     89:        "id", /* ATTR_ID */
                     90:        "summary", /* ATTR_SUMMARY */
                     91:        "align", /* ATTR_ALIGN */
1.22      schwarze   92:        "colspan", /* ATTR_COLSPAN */
1.1       schwarze   93: };
                     94:
1.26      schwarze   95: static const char      *const roffscales[SCALE_MAX] = {
                     96:        "cm", /* SCALE_CM */
                     97:        "in", /* SCALE_IN */
                     98:        "pc", /* SCALE_PC */
                     99:        "pt", /* SCALE_PT */
                    100:        "em", /* SCALE_EM */
                    101:        "em", /* SCALE_MM */
                    102:        "ex", /* SCALE_EN */
                    103:        "ex", /* SCALE_BU */
                    104:        "em", /* SCALE_VS */
                    105:        "ex", /* SCALE_FS */
                    106: };
1.5       schwarze  107:
1.26      schwarze  108: static void     bufncat(struct html *, const char *, size_t);
                    109: static void     print_ctag(struct html *, enum htmltag);
1.38      schwarze  110: static int      print_escape(char);
1.26      schwarze  111: static int      print_encode(struct html *, const char *, int);
                    112: static void     print_metaf(struct html *, enum mandoc_esc);
                    113: static void     print_attr(struct html *, const char *, const char *);
                    114: static void     *ml_alloc(char *, enum htmltype);
1.5       schwarze  115:
1.35      schwarze  116:
1.6       schwarze  117: static void *
                    118: ml_alloc(char *outopts, enum htmltype type)
1.1       schwarze  119: {
                    120:        struct html     *h;
1.29      schwarze  121:        const char      *toks[5];
1.1       schwarze  122:        char            *v;
                    123:
                    124:        toks[0] = "style";
                    125:        toks[1] = "man";
                    126:        toks[2] = "includes";
1.29      schwarze  127:        toks[3] = "fragment";
                    128:        toks[4] = NULL;
1.1       schwarze  129:
1.24      schwarze  130:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  131:
1.6       schwarze  132:        h->type = type;
1.2       schwarze  133:        h->tags.head = NULL;
1.26      schwarze  134:        h->symtab = mchars_alloc();
1.1       schwarze  135:
                    136:        while (outopts && *outopts)
                    137:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.35      schwarze  138:                case 0:
1.1       schwarze  139:                        h->style = v;
                    140:                        break;
1.35      schwarze  141:                case 1:
1.1       schwarze  142:                        h->base_man = v;
                    143:                        break;
1.35      schwarze  144:                case 2:
1.1       schwarze  145:                        h->base_includes = v;
1.29      schwarze  146:                        break;
1.35      schwarze  147:                case 3:
1.29      schwarze  148:                        h->oflags |= HTML_FRAGMENT;
1.1       schwarze  149:                        break;
                    150:                default:
                    151:                        break;
                    152:                }
                    153:
                    154:        return(h);
                    155: }
                    156:
1.6       schwarze  157: void *
                    158: html_alloc(char *outopts)
                    159: {
                    160:
                    161:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    162: }
                    163:
                    164: void *
                    165: xhtml_alloc(char *outopts)
                    166: {
                    167:
                    168:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    169: }
                    170:
1.1       schwarze  171: void
                    172: html_free(void *p)
                    173: {
                    174:        struct tag      *tag;
                    175:        struct html     *h;
                    176:
                    177:        h = (struct html *)p;
                    178:
1.2       schwarze  179:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  180:                h->tags.head = tag->next;
1.1       schwarze  181:                free(tag);
                    182:        }
1.35      schwarze  183:
1.1       schwarze  184:        if (h->symtab)
1.26      schwarze  185:                mchars_free(h->symtab);
1.1       schwarze  186:
                    187:        free(h);
                    188: }
                    189:
                    190: void
                    191: print_gen_head(struct html *h)
                    192: {
                    193:        struct htmlpair  tag[4];
                    194:
                    195:        tag[0].key = ATTR_HTTPEQUIV;
                    196:        tag[0].val = "Content-Type";
                    197:        tag[1].key = ATTR_CONTENT;
                    198:        tag[1].val = "text/html; charset=utf-8";
                    199:        print_otag(h, TAG_META, 2, tag);
                    200:
                    201:        tag[0].key = ATTR_NAME;
                    202:        tag[0].val = "resource-type";
                    203:        tag[1].key = ATTR_CONTENT;
                    204:        tag[1].val = "document";
                    205:        print_otag(h, TAG_META, 2, tag);
                    206:
                    207:        if (h->style) {
                    208:                tag[0].key = ATTR_REL;
                    209:                tag[0].val = "stylesheet";
                    210:                tag[1].key = ATTR_HREF;
                    211:                tag[1].val = h->style;
                    212:                tag[2].key = ATTR_TYPE;
                    213:                tag[2].val = "text/css";
                    214:                tag[3].key = ATTR_MEDIA;
                    215:                tag[3].val = "all";
                    216:                print_otag(h, TAG_LINK, 4, tag);
                    217:        }
                    218: }
                    219:
1.5       schwarze  220: static void
1.26      schwarze  221: print_metaf(struct html *h, enum mandoc_esc deco)
1.5       schwarze  222: {
                    223:        enum htmlfont    font;
1.1       schwarze  224:
1.5       schwarze  225:        switch (deco) {
1.35      schwarze  226:        case ESCAPE_FONTPREV:
1.5       schwarze  227:                font = h->metal;
                    228:                break;
1.35      schwarze  229:        case ESCAPE_FONTITALIC:
1.5       schwarze  230:                font = HTMLFONT_ITALIC;
                    231:                break;
1.35      schwarze  232:        case ESCAPE_FONTBOLD:
1.5       schwarze  233:                font = HTMLFONT_BOLD;
                    234:                break;
1.35      schwarze  235:        case ESCAPE_FONTBI:
1.31      schwarze  236:                font = HTMLFONT_BI;
                    237:                break;
1.35      schwarze  238:        case ESCAPE_FONT:
1.26      schwarze  239:                /* FALLTHROUGH */
1.35      schwarze  240:        case ESCAPE_FONTROMAN:
1.5       schwarze  241:                font = HTMLFONT_NONE;
                    242:                break;
                    243:        default:
                    244:                abort();
                    245:                /* NOTREACHED */
1.1       schwarze  246:        }
                    247:
1.20      schwarze  248:        if (h->metaf) {
                    249:                print_tagq(h, h->metaf);
                    250:                h->metaf = NULL;
                    251:        }
                    252:
                    253:        h->metal = h->metac;
                    254:        h->metac = font;
                    255:
1.31      schwarze  256:        switch (font) {
1.35      schwarze  257:        case HTMLFONT_ITALIC:
1.31      schwarze  258:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    259:                break;
1.35      schwarze  260:        case HTMLFONT_BOLD:
1.31      schwarze  261:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    262:                break;
1.35      schwarze  263:        case HTMLFONT_BI:
1.31      schwarze  264:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    265:                print_otag(h, TAG_I, 0, NULL);
                    266:                break;
                    267:        default:
                    268:                break;
                    269:        }
1.1       schwarze  270: }
                    271:
1.26      schwarze  272: int
                    273: html_strlen(const char *cp)
                    274: {
1.30      schwarze  275:        size_t           rsz;
                    276:        int              skip, sz;
1.26      schwarze  277:
                    278:        /*
                    279:         * Account for escaped sequences within string length
                    280:         * calculations.  This follows the logic in term_strlen() as we
                    281:         * must calculate the width of produced strings.
                    282:         * Assume that characters are always width of "1".  This is
                    283:         * hacky, but it gets the job done for approximation of widths.
                    284:         */
                    285:
                    286:        sz = 0;
1.30      schwarze  287:        skip = 0;
                    288:        while (1) {
                    289:                rsz = strcspn(cp, "\\");
                    290:                if (rsz) {
                    291:                        cp += rsz;
                    292:                        if (skip) {
                    293:                                skip = 0;
                    294:                                rsz--;
                    295:                        }
                    296:                        sz += rsz;
                    297:                }
                    298:                if ('\0' == *cp)
                    299:                        break;
                    300:                cp++;
                    301:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.35      schwarze  302:                case ESCAPE_ERROR:
1.26      schwarze  303:                        return(sz);
1.35      schwarze  304:                case ESCAPE_UNICODE:
1.26      schwarze  305:                        /* FALLTHROUGH */
1.35      schwarze  306:                case ESCAPE_NUMBERED:
1.26      schwarze  307:                        /* FALLTHROUGH */
1.35      schwarze  308:                case ESCAPE_SPECIAL:
1.30      schwarze  309:                        if (skip)
                    310:                                skip = 0;
                    311:                        else
                    312:                                sz++;
                    313:                        break;
1.35      schwarze  314:                case ESCAPE_SKIPCHAR:
1.30      schwarze  315:                        skip = 1;
1.26      schwarze  316:                        break;
                    317:                default:
                    318:                        break;
                    319:                }
                    320:        }
1.30      schwarze  321:        return(sz);
1.26      schwarze  322: }
1.1       schwarze  323:
1.5       schwarze  324: static int
1.38      schwarze  325: print_escape(char c)
                    326: {
                    327:
                    328:        switch (c) {
                    329:        case '<':
                    330:                printf("&lt;");
                    331:                break;
                    332:        case '>':
                    333:                printf("&gt;");
                    334:                break;
                    335:        case '&':
                    336:                printf("&amp;");
                    337:                break;
                    338:        case '"':
                    339:                printf("&quot;");
                    340:                break;
                    341:        case ASCII_NBRSP:
                    342:                putchar('-');
                    343:                break;
                    344:        case ASCII_HYPH:
                    345:                putchar('-');
                    346:                /* FALLTHROUGH */
                    347:        case ASCII_BREAK:
                    348:                break;
                    349:        default:
                    350:                return(0);
                    351:        }
                    352:        return(1);
                    353: }
                    354:
                    355: static int
1.5       schwarze  356: print_encode(struct html *h, const char *p, int norecurse)
1.1       schwarze  357: {
1.4       schwarze  358:        size_t           sz;
1.26      schwarze  359:        int              c, len, nospace;
1.5       schwarze  360:        const char      *seq;
1.26      schwarze  361:        enum mandoc_esc  esc;
1.37      schwarze  362:        static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33      schwarze  363:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  364:
                    365:        nospace = 0;
1.1       schwarze  366:
1.26      schwarze  367:        while ('\0' != *p) {
1.30      schwarze  368:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    369:                        h->flags &= ~HTML_SKIPCHAR;
                    370:                        p++;
                    371:                        continue;
                    372:                }
                    373:
1.9       schwarze  374:                sz = strcspn(p, rejs);
1.4       schwarze  375:
                    376:                fwrite(p, 1, sz, stdout);
1.26      schwarze  377:                p += (int)sz;
1.4       schwarze  378:
1.26      schwarze  379:                if ('\0' == *p)
                    380:                        break;
                    381:
1.38      schwarze  382:                if (print_escape(*p++))
1.33      schwarze  383:                        continue;
1.4       schwarze  384:
1.26      schwarze  385:                esc = mandoc_escape(&p, &seq, &len);
                    386:                if (ESCAPE_ERROR == esc)
                    387:                        break;
1.5       schwarze  388:
1.26      schwarze  389:                switch (esc) {
1.35      schwarze  390:                case ESCAPE_FONT:
1.30      schwarze  391:                        /* FALLTHROUGH */
1.35      schwarze  392:                case ESCAPE_FONTPREV:
1.30      schwarze  393:                        /* FALLTHROUGH */
1.35      schwarze  394:                case ESCAPE_FONTBOLD:
1.30      schwarze  395:                        /* FALLTHROUGH */
1.35      schwarze  396:                case ESCAPE_FONTITALIC:
1.30      schwarze  397:                        /* FALLTHROUGH */
1.35      schwarze  398:                case ESCAPE_FONTBI:
1.31      schwarze  399:                        /* FALLTHROUGH */
1.35      schwarze  400:                case ESCAPE_FONTROMAN:
1.30      schwarze  401:                        if (0 == norecurse)
                    402:                                print_metaf(h, esc);
                    403:                        continue;
1.35      schwarze  404:                case ESCAPE_SKIPCHAR:
1.30      schwarze  405:                        h->flags |= HTML_SKIPCHAR;
                    406:                        continue;
                    407:                default:
                    408:                        break;
                    409:                }
                    410:
                    411:                if (h->flags & HTML_SKIPCHAR) {
                    412:                        h->flags &= ~HTML_SKIPCHAR;
                    413:                        continue;
                    414:                }
                    415:
                    416:                switch (esc) {
1.35      schwarze  417:                case ESCAPE_UNICODE:
1.38      schwarze  418:                        /* Skip past "u" header. */
1.26      schwarze  419:                        c = mchars_num2uc(seq + 1, len - 1);
                    420:                        if ('\0' != c)
                    421:                                printf("&#x%x;", c);
                    422:                        break;
1.35      schwarze  423:                case ESCAPE_NUMBERED:
1.26      schwarze  424:                        c = mchars_num2char(seq, len);
1.38      schwarze  425:                        if ( ! ('\0' == c || print_escape(c)))
1.26      schwarze  426:                                putchar(c);
                    427:                        break;
1.35      schwarze  428:                case ESCAPE_SPECIAL:
1.26      schwarze  429:                        c = mchars_spec2cp(h->symtab, seq, len);
                    430:                        if (c > 0)
                    431:                                printf("&#%d;", c);
1.38      schwarze  432:                        else if (-1 == c && 1 == len &&
                    433:                            !print_escape(*seq))
1.26      schwarze  434:                                putchar((int)*seq);
                    435:                        break;
1.35      schwarze  436:                case ESCAPE_NOSPACE:
1.26      schwarze  437:                        if ('\0' == *p)
                    438:                                nospace = 1;
1.5       schwarze  439:                        break;
                    440:                default:
                    441:                        break;
                    442:                }
1.1       schwarze  443:        }
1.5       schwarze  444:
                    445:        return(nospace);
1.1       schwarze  446: }
                    447:
1.6       schwarze  448: static void
                    449: print_attr(struct html *h, const char *key, const char *val)
                    450: {
                    451:        printf(" %s=\"", key);
                    452:        (void)print_encode(h, val, 1);
                    453:        putchar('\"');
                    454: }
                    455:
1.1       schwarze  456: struct tag *
1.35      schwarze  457: print_otag(struct html *h, enum htmltag tag,
1.1       schwarze  458:                int sz, const struct htmlpair *p)
                    459: {
                    460:        int              i;
                    461:        struct tag      *t;
                    462:
1.6       schwarze  463:        /* Push this tags onto the stack of open scopes. */
                    464:
1.1       schwarze  465:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24      schwarze  466:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  467:                t->tag = tag;
1.2       schwarze  468:                t->next = h->tags.head;
                    469:                h->tags.head = t;
1.1       schwarze  470:        } else
                    471:                t = NULL;
                    472:
                    473:        if ( ! (HTML_NOSPACE & h->flags))
1.12      schwarze  474:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    475:                        /* Manage keeps! */
                    476:                        if ( ! (HTML_KEEP & h->flags)) {
                    477:                                if (HTML_PREKEEP & h->flags)
                    478:                                        h->flags |= HTML_KEEP;
                    479:                                putchar(' ');
                    480:                        } else
                    481:                                printf("&#160;");
                    482:                }
1.1       schwarze  483:
1.13      schwarze  484:        if ( ! (h->flags & HTML_NONOSPACE))
                    485:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  486:        else
                    487:                h->flags |= HTML_NOSPACE;
1.13      schwarze  488:
1.6       schwarze  489:        /* Print out the tag name and attributes. */
                    490:
1.1       schwarze  491:        printf("<%s", htmltags[tag].name);
1.6       schwarze  492:        for (i = 0; i < sz; i++)
                    493:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    494:
                    495:        /* Add non-overridable attributes. */
                    496:
                    497:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    498:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    499:                print_attr(h, "xml:lang", "en");
                    500:                print_attr(h, "lang", "en");
1.1       schwarze  501:        }
1.6       schwarze  502:
1.26      schwarze  503:        /* Accommodate for XML "well-formed" singleton escaping. */
1.6       schwarze  504:
                    505:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    506:                switch (h->type) {
1.35      schwarze  507:                case HTML_XHTML_1_0_STRICT:
1.6       schwarze  508:                        putchar('/');
                    509:                        break;
                    510:                default:
                    511:                        break;
                    512:                }
                    513:
1.4       schwarze  514:        putchar('>');
1.1       schwarze  515:
                    516:        h->flags |= HTML_NOSPACE;
1.18      schwarze  517:
                    518:        if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
                    519:                putchar('\n');
                    520:
1.1       schwarze  521:        return(t);
                    522: }
                    523:
                    524: static void
                    525: print_ctag(struct html *h, enum htmltag tag)
                    526: {
1.35      schwarze  527:
1.1       schwarze  528:        printf("</%s>", htmltags[tag].name);
1.3       schwarze  529:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.1       schwarze  530:                h->flags |= HTML_NOSPACE;
1.4       schwarze  531:                putchar('\n');
1.35      schwarze  532:        }
1.1       schwarze  533: }
                    534:
                    535: void
1.6       schwarze  536: print_gen_decls(struct html *h)
                    537: {
                    538:        const char      *doctype;
                    539:        const char      *dtd;
                    540:        const char      *name;
                    541:
                    542:        switch (h->type) {
1.35      schwarze  543:        case HTML_HTML_4_01_STRICT:
1.6       schwarze  544:                name = "HTML";
                    545:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    546:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    547:                break;
                    548:        default:
1.26      schwarze  549:                puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.6       schwarze  550:                name = "html";
                    551:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    552:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    553:                break;
                    554:        }
                    555:
1.35      schwarze  556:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    557:            name, doctype, dtd);
1.1       schwarze  558: }
                    559:
                    560: void
1.12      schwarze  561: print_text(struct html *h, const char *word)
1.1       schwarze  562: {
                    563:
1.12      schwarze  564:        if ( ! (HTML_NOSPACE & h->flags)) {
                    565:                /* Manage keeps! */
                    566:                if ( ! (HTML_KEEP & h->flags)) {
                    567:                        if (HTML_PREKEEP & h->flags)
                    568:                                h->flags |= HTML_KEEP;
                    569:                        putchar(' ');
                    570:                } else
                    571:                        printf("&#160;");
                    572:        }
1.1       schwarze  573:
1.20      schwarze  574:        assert(NULL == h->metaf);
1.31      schwarze  575:        switch (h->metac) {
1.35      schwarze  576:        case HTMLFONT_ITALIC:
1.31      schwarze  577:                h->metaf = print_otag(h, TAG_I, 0, NULL);
                    578:                break;
1.35      schwarze  579:        case HTMLFONT_BOLD:
1.31      schwarze  580:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    581:                break;
1.35      schwarze  582:        case HTMLFONT_BI:
1.31      schwarze  583:                h->metaf = print_otag(h, TAG_B, 0, NULL);
                    584:                print_otag(h, TAG_I, 0, NULL);
                    585:                break;
                    586:        default:
                    587:                break;
                    588:        }
1.20      schwarze  589:
1.12      schwarze  590:        assert(word);
1.28      schwarze  591:        if ( ! print_encode(h, word, 0)) {
1.13      schwarze  592:                if ( ! (h->flags & HTML_NONOSPACE))
                    593:                        h->flags &= ~HTML_NOSPACE;
1.28      schwarze  594:        } else
                    595:                h->flags |= HTML_NOSPACE;
1.20      schwarze  596:
                    597:        if (h->metaf) {
                    598:                print_tagq(h, h->metaf);
                    599:                h->metaf = NULL;
                    600:        }
1.17      schwarze  601:
                    602:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  603: }
                    604:
                    605: void
                    606: print_tagq(struct html *h, const struct tag *until)
                    607: {
                    608:        struct tag      *tag;
                    609:
1.2       schwarze  610:        while ((tag = h->tags.head) != NULL) {
1.35      schwarze  611:                /*
1.22      schwarze  612:                 * Remember to close out and nullify the current
                    613:                 * meta-font and table, if applicable.
                    614:                 */
1.5       schwarze  615:                if (tag == h->metaf)
                    616:                        h->metaf = NULL;
1.22      schwarze  617:                if (tag == h->tblt)
                    618:                        h->tblt = NULL;
1.1       schwarze  619:                print_ctag(h, tag->tag);
1.2       schwarze  620:                h->tags.head = tag->next;
1.1       schwarze  621:                free(tag);
                    622:                if (until && tag == until)
                    623:                        return;
                    624:        }
                    625: }
                    626:
                    627: void
                    628: print_stagq(struct html *h, const struct tag *suntil)
                    629: {
                    630:        struct tag      *tag;
                    631:
1.2       schwarze  632:        while ((tag = h->tags.head) != NULL) {
1.1       schwarze  633:                if (suntil && tag == suntil)
                    634:                        return;
1.35      schwarze  635:                /*
1.22      schwarze  636:                 * Remember to close out and nullify the current
                    637:                 * meta-font and table, if applicable.
                    638:                 */
1.5       schwarze  639:                if (tag == h->metaf)
                    640:                        h->metaf = NULL;
1.22      schwarze  641:                if (tag == h->tblt)
                    642:                        h->tblt = NULL;
1.1       schwarze  643:                print_ctag(h, tag->tag);
1.2       schwarze  644:                h->tags.head = tag->next;
1.1       schwarze  645:                free(tag);
                    646:        }
                    647: }
                    648:
                    649: void
                    650: bufinit(struct html *h)
                    651: {
                    652:
                    653:        h->buf[0] = '\0';
                    654:        h->buflen = 0;
                    655: }
                    656:
                    657: void
                    658: bufcat_style(struct html *h, const char *key, const char *val)
                    659: {
                    660:
                    661:        bufcat(h, key);
1.26      schwarze  662:        bufcat(h, ":");
1.1       schwarze  663:        bufcat(h, val);
1.26      schwarze  664:        bufcat(h, ";");
1.1       schwarze  665: }
                    666:
                    667: void
                    668: bufcat(struct html *h, const char *p)
                    669: {
1.36      schwarze  670:
                    671:        /*
                    672:         * XXX This is broken and not easy to fix.
                    673:         * When using the -Oincludes option, buffmt_includes()
                    674:         * may pass in strings overrunning BUFSIZ, causing a crash.
                    675:         */
1.1       schwarze  676:
1.26      schwarze  677:        h->buflen = strlcat(h->buf, p, BUFSIZ);
                    678:        assert(h->buflen < BUFSIZ);
1.1       schwarze  679: }
                    680:
                    681: void
1.26      schwarze  682: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1       schwarze  683: {
                    684:        va_list          ap;
                    685:
                    686:        va_start(ap, fmt);
1.35      schwarze  687:        (void)vsnprintf(h->buf + (int)h->buflen,
                    688:            BUFSIZ - h->buflen - 1, fmt, ap);
1.1       schwarze  689:        va_end(ap);
                    690:        h->buflen = strlen(h->buf);
                    691: }
                    692:
1.26      schwarze  693: static void
1.1       schwarze  694: bufncat(struct html *h, const char *p, size_t sz)
                    695: {
                    696:
1.26      schwarze  697:        assert(h->buflen + sz + 1 < BUFSIZ);
                    698:        strncat(h->buf, p, sz);
1.1       schwarze  699:        h->buflen += sz;
                    700: }
                    701:
                    702: void
                    703: buffmt_includes(struct html *h, const char *name)
                    704: {
                    705:        const char      *p, *pp;
                    706:
                    707:        pp = h->base_includes;
1.35      schwarze  708:
1.26      schwarze  709:        bufinit(h);
1.1       schwarze  710:        while (NULL != (p = strchr(pp, '%'))) {
                    711:                bufncat(h, pp, (size_t)(p - pp));
                    712:                switch (*(p + 1)) {
1.35      schwarze  713:                case'I':
1.1       schwarze  714:                        bufcat(h, name);
                    715:                        break;
                    716:                default:
                    717:                        bufncat(h, p, 2);
                    718:                        break;
                    719:                }
                    720:                pp = p + 2;
                    721:        }
                    722:        if (pp)
                    723:                bufcat(h, pp);
                    724: }
                    725:
                    726: void
1.35      schwarze  727: buffmt_man(struct html *h, const char *name, const char *sec)
1.1       schwarze  728: {
                    729:        const char      *p, *pp;
                    730:
                    731:        pp = h->base_man;
1.35      schwarze  732:
1.26      schwarze  733:        bufinit(h);
1.1       schwarze  734:        while (NULL != (p = strchr(pp, '%'))) {
                    735:                bufncat(h, pp, (size_t)(p - pp));
                    736:                switch (*(p + 1)) {
1.35      schwarze  737:                case 'S':
1.1       schwarze  738:                        bufcat(h, sec ? sec : "1");
                    739:                        break;
1.35      schwarze  740:                case 'N':
1.32      schwarze  741:                        bufcat_fmt(h, "%s", name);
1.1       schwarze  742:                        break;
                    743:                default:
                    744:                        bufncat(h, p, 2);
                    745:                        break;
                    746:                }
                    747:                pp = p + 2;
                    748:        }
                    749:        if (pp)
                    750:                bufcat(h, pp);
                    751: }
                    752:
                    753: void
                    754: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    755: {
                    756:        double           v;
                    757:
                    758:        v = su->scale;
1.26      schwarze  759:        if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
                    760:                v = 1.0;
1.1       schwarze  761:
1.26      schwarze  762:        bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1       schwarze  763: }
                    764:
1.3       schwarze  765: void
1.26      schwarze  766: bufcat_id(struct html *h, const char *src)
1.3       schwarze  767: {
                    768:
                    769:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    770:
1.26      schwarze  771:        while ('\0' != *src)
                    772:                bufcat_fmt(h, "%.2x", *src++);
1.3       schwarze  773: }