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

Annotation of mandoc/html.c, Revision 1.114

1.114   ! kristaps    1: /*     $Id: html.c,v 1.113 2010/10/01 21:51:13 schwarze Exp $ */
1.1       kristaps    2: /*
1.106     schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.29      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.92      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.41      kristaps   21: #include <sys/types.h>
1.30      kristaps   22:
1.1       kristaps   23: #include <assert.h>
1.68      kristaps   24: #include <ctype.h>
1.76      kristaps   25: #include <stdarg.h>
1.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.100     kristaps   32: #include "mandoc.h"
1.58      kristaps   33: #include "out.h"
1.32      kristaps   34: #include "chars.h"
1.51      kristaps   35: #include "html.h"
1.64      kristaps   36: #include "main.h"
1.63      kristaps   37:
1.29      kristaps   38: struct htmldata {
1.63      kristaps   39:        const char       *name;
1.29      kristaps   40:        int               flags;
1.30      kristaps   41: #define        HTML_CLRLINE     (1 << 0)
                     42: #define        HTML_NOSTACK     (1 << 1)
1.93      kristaps   43: #define        HTML_AUTOCLOSE   (1 << 2) /* Tag has auto-closure. */
1.29      kristaps   44: };
1.7       kristaps   45:
1.29      kristaps   46: static const struct htmldata htmltags[TAG_MAX] = {
1.30      kristaps   47:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     48:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     49:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
1.93      kristaps   50:        {"meta",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.33      kristaps   51:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
1.30      kristaps   52:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
1.29      kristaps   53:        {"h1",          0}, /* TAG_H1 */
                     54:        {"h2",          0}, /* TAG_H2 */
                     55:        {"span",        0}, /* TAG_SPAN */
1.99      kristaps   56:        {"link",        HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.93      kristaps   57:        {"br",          HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.30      kristaps   58:        {"a",           0}, /* TAG_A */
1.33      kristaps   59:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
1.114   ! kristaps   60:        {"tbody",       HTML_CLRLINE}, /* TAG_TBODY */
1.93      kristaps   61:        {"col",         HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.33      kristaps   62:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                     63:        {"td",          HTML_CLRLINE}, /* TAG_TD */
1.34      kristaps   64:        {"li",          HTML_CLRLINE}, /* TAG_LI */
                     65:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
                     66:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.114   ! kristaps   67:        {"dl",          HTML_CLRLINE}, /* TAG_DL */
        !            68:        {"dt",          HTML_CLRLINE}, /* TAG_DT */
        !            69:        {"dd",          HTML_CLRLINE}, /* TAG_DD */
1.29      kristaps   70: };
1.10      kristaps   71:
1.90      kristaps   72: static const char      *const htmlfonts[HTMLFONT_MAX] = {
                     73:        "roman",
                     74:        "bold",
                     75:        "italic"
                     76: };
                     77:
1.82      kristaps   78: static const char      *const htmlattrs[ATTR_MAX] = {
1.29      kristaps   79:        "http-equiv",
                     80:        "content",
                     81:        "name",
                     82:        "rel",
                     83:        "href",
                     84:        "type",
                     85:        "media",
1.33      kristaps   86:        "class",
                     87:        "style",
                     88:        "width",
                     89:        "valign",
1.54      kristaps   90:        "target",
1.57      kristaps   91:        "id",
1.67      kristaps   92:        "summary",
1.29      kristaps   93: };
1.10      kristaps   94:
1.108     kristaps   95: static void              print_spec(struct html *, enum roffdeco,
                     96:                                const char *, size_t);
1.83      kristaps   97: static void              print_res(struct html *, const char *, size_t);
1.82      kristaps   98: static void              print_ctag(struct html *, enum htmltag);
1.93      kristaps   99: static void              print_doctype(struct html *);
                    100: static void              print_xmltype(struct html *);
1.88      kristaps  101: static int               print_encode(struct html *, const char *, int);
                    102: static void              print_metaf(struct html *, enum roffdeco);
1.94      kristaps  103: static void              print_attr(struct html *,
                    104:                                const char *, const char *);
1.93      kristaps  105: static void             *ml_alloc(char *, enum htmltype);
1.82      kristaps  106:
                    107:
1.93      kristaps  108: static void *
                    109: ml_alloc(char *outopts, enum htmltype type)
1.10      kristaps  110: {
1.30      kristaps  111:        struct html     *h;
1.63      kristaps  112:        const char      *toks[4];
                    113:        char            *v;
1.43      kristaps  114:
                    115:        toks[0] = "style";
1.53      kristaps  116:        toks[1] = "man";
1.54      kristaps  117:        toks[2] = "includes";
                    118:        toks[3] = NULL;
1.30      kristaps  119:
1.72      kristaps  120:        h = calloc(1, sizeof(struct html));
                    121:        if (NULL == h) {
1.75      kristaps  122:                perror(NULL);
1.112     kristaps  123:                exit((int)MANDOCLEVEL_SYSERR);
1.72      kristaps  124:        }
1.10      kristaps  125:
1.93      kristaps  126:        h->type = type;
1.66      kristaps  127:        h->tags.head = NULL;
1.72      kristaps  128:        h->symtab = chars_init(CHARS_HTML);
1.41      kristaps  129:
1.47      kristaps  130:        while (outopts && *outopts)
1.63      kristaps  131:                switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43      kristaps  132:                case (0):
                    133:                        h->style = v;
                    134:                        break;
                    135:                case (1):
1.53      kristaps  136:                        h->base_man = v;
1.43      kristaps  137:                        break;
1.54      kristaps  138:                case (2):
                    139:                        h->base_includes = v;
                    140:                        break;
1.43      kristaps  141:                default:
                    142:                        break;
                    143:                }
                    144:
1.30      kristaps  145:        return(h);
1.29      kristaps  146: }
1.10      kristaps  147:
1.93      kristaps  148: void *
                    149: html_alloc(char *outopts)
                    150: {
                    151:
                    152:        return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
                    153: }
                    154:
                    155:
                    156: void *
                    157: xhtml_alloc(char *outopts)
                    158: {
                    159:
                    160:        return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
                    161: }
                    162:
1.33      kristaps  163:
1.29      kristaps  164: void
                    165: html_free(void *p)
                    166: {
1.30      kristaps  167:        struct tag      *tag;
                    168:        struct html     *h;
                    169:
                    170:        h = (struct html *)p;
1.37      kristaps  171:
1.66      kristaps  172:        while ((tag = h->tags.head) != NULL) {
                    173:                h->tags.head = tag->next;
1.30      kristaps  174:                free(tag);
                    175:        }
1.36      kristaps  176:
                    177:        if (h->symtab)
                    178:                chars_free(h->symtab);
1.53      kristaps  179:
1.30      kristaps  180:        free(h);
1.10      kristaps  181: }
1.2       kristaps  182:
1.33      kristaps  183:
1.51      kristaps  184: void
1.29      kristaps  185: print_gen_head(struct html *h)
                    186: {
1.41      kristaps  187:        struct htmlpair  tag[4];
                    188:
                    189:        tag[0].key = ATTR_HTTPEQUIV;
                    190:        tag[0].val = "Content-Type";
                    191:        tag[1].key = ATTR_CONTENT;
                    192:        tag[1].val = "text/html; charset=utf-8";
                    193:        print_otag(h, TAG_META, 2, tag);
                    194:
                    195:        tag[0].key = ATTR_NAME;
                    196:        tag[0].val = "resource-type";
                    197:        tag[1].key = ATTR_CONTENT;
                    198:        tag[1].val = "document";
                    199:        print_otag(h, TAG_META, 2, tag);
                    200:
                    201:        if (h->style) {
                    202:                tag[0].key = ATTR_REL;
                    203:                tag[0].val = "stylesheet";
                    204:                tag[1].key = ATTR_HREF;
                    205:                tag[1].val = h->style;
                    206:                tag[2].key = ATTR_TYPE;
                    207:                tag[2].val = "text/css";
                    208:                tag[3].key = ATTR_MEDIA;
                    209:                tag[3].val = "all";
                    210:                print_otag(h, TAG_LINK, 4, tag);
                    211:        }
1.4       kristaps  212: }
                    213:
1.33      kristaps  214:
1.29      kristaps  215: static void
1.108     kristaps  216: print_spec(struct html *h, enum roffdeco d, const char *p, size_t len)
1.32      kristaps  217: {
1.107     kristaps  218:        int              cp;
1.32      kristaps  219:        const char      *rhs;
                    220:        size_t           sz;
                    221:
1.107     kristaps  222:        if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
                    223:                printf("&#%d;", cp);
                    224:                return;
1.108     kristaps  225:        } else if (-1 == cp && DECO_SSPECIAL == d) {
                    226:                fwrite(p, 1, len, stdout);
                    227:                return;
1.107     kristaps  228:        } else if (-1 == cp)
                    229:                return;
1.32      kristaps  230:
1.107     kristaps  231:        if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
                    232:                fwrite(rhs, 1, sz, stdout);
1.32      kristaps  233: }
                    234:
1.33      kristaps  235:
1.32      kristaps  236: static void
1.83      kristaps  237: print_res(struct html *h, const char *p, size_t len)
1.32      kristaps  238: {
1.107     kristaps  239:        int              cp;
1.32      kristaps  240:        const char      *rhs;
                    241:        size_t           sz;
                    242:
1.107     kristaps  243:        if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
                    244:                printf("&#%d;", cp);
                    245:                return;
                    246:        } else if (-1 == cp)
                    247:                return;
1.32      kristaps  248:
1.107     kristaps  249:        if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
                    250:                fwrite(rhs, 1, sz, stdout);
1.32      kristaps  251: }
                    252:
1.33      kristaps  253:
1.90      kristaps  254: struct tag *
                    255: print_ofont(struct html *h, enum htmlfont font)
                    256: {
                    257:        struct htmlpair  tag;
                    258:
                    259:        h->metal = h->metac;
                    260:        h->metac = font;
                    261:
                    262:        /* FIXME: DECO_ROMAN should just close out preexisting. */
                    263:
1.91      kristaps  264:        if (h->metaf && h->tags.head == h->metaf)
1.90      kristaps  265:                print_tagq(h, h->metaf);
                    266:
                    267:        PAIR_CLASS_INIT(&tag, htmlfonts[font]);
                    268:        h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
                    269:        return(h->metaf);
                    270: }
                    271:
                    272:
1.88      kristaps  273: static void
                    274: print_metaf(struct html *h, enum roffdeco deco)
                    275: {
1.90      kristaps  276:        enum htmlfont    font;
1.88      kristaps  277:
                    278:        switch (deco) {
1.90      kristaps  279:        case (DECO_PREVIOUS):
                    280:                font = h->metal;
1.88      kristaps  281:                break;
                    282:        case (DECO_ITALIC):
1.90      kristaps  283:                font = HTMLFONT_ITALIC;
                    284:                break;
                    285:        case (DECO_BOLD):
                    286:                font = HTMLFONT_BOLD;
1.88      kristaps  287:                break;
                    288:        case (DECO_ROMAN):
1.90      kristaps  289:                font = HTMLFONT_NONE;
1.88      kristaps  290:                break;
                    291:        default:
                    292:                abort();
                    293:                /* NOTREACHED */
                    294:        }
                    295:
1.90      kristaps  296:        (void)print_ofont(h, font);
1.88      kristaps  297: }
                    298:
                    299:
1.85      kristaps  300: static int
1.88      kristaps  301: print_encode(struct html *h, const char *p, int norecurse)
1.29      kristaps  302: {
1.77      kristaps  303:        size_t           sz;
1.85      kristaps  304:        int              len, nospace;
1.82      kristaps  305:        const char      *seq;
                    306:        enum roffdeco    deco;
1.100     kristaps  307:        static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.14      kristaps  308:
1.85      kristaps  309:        nospace = 0;
                    310:
1.32      kristaps  311:        for (; *p; p++) {
1.100     kristaps  312:                sz = strcspn(p, rejs);
1.77      kristaps  313:
                    314:                fwrite(p, 1, sz, stdout);
1.80      kristaps  315:                p += /* LINTED */
                    316:                        sz;
1.77      kristaps  317:
1.82      kristaps  318:                if ('<' == *p) {
                    319:                        printf("&lt;");
                    320:                        continue;
                    321:                } else if ('>' == *p) {
                    322:                        printf("&gt;");
                    323:                        continue;
                    324:                } else if ('&' == *p) {
                    325:                        printf("&amp;");
1.34      kristaps  326:                        continue;
1.100     kristaps  327:                } else if (ASCII_HYPH == *p) {
                    328:                        /*
                    329:                         * Note: "soft hyphens" aren't graphically
                    330:                         * displayed when not breaking the text; we want
                    331:                         * them to be displayed.
                    332:                         */
                    333:                        /*printf("&#173;");*/
                    334:                        putchar('-');
                    335:                        continue;
1.77      kristaps  336:                } else if ('\0' == *p)
                    337:                        break;
                    338:
1.82      kristaps  339:                seq = ++p;
                    340:                len = a2roffdeco(&deco, &seq, &sz);
                    341:
                    342:                switch (deco) {
                    343:                case (DECO_RESERVED):
                    344:                        print_res(h, seq, sz);
                    345:                        break;
1.108     kristaps  346:                case (DECO_SSPECIAL):
                    347:                        /* FALLTHROUGH */
1.82      kristaps  348:                case (DECO_SPECIAL):
1.108     kristaps  349:                        print_spec(h, deco, seq, sz);
1.82      kristaps  350:                        break;
1.90      kristaps  351:                case (DECO_PREVIOUS):
                    352:                        /* FALLTHROUGH */
1.88      kristaps  353:                case (DECO_BOLD):
                    354:                        /* FALLTHROUGH */
                    355:                case (DECO_ITALIC):
                    356:                        /* FALLTHROUGH */
                    357:                case (DECO_ROMAN):
                    358:                        if (norecurse)
                    359:                                break;
                    360:                        print_metaf(h, deco);
                    361:                        break;
1.82      kristaps  362:                default:
                    363:                        break;
                    364:                }
                    365:
                    366:                p += len - 1;
1.84      kristaps  367:
                    368:                if (DECO_NOSPACE == deco && '\0' == *(p + 1))
1.85      kristaps  369:                        nospace = 1;
1.32      kristaps  370:        }
1.85      kristaps  371:
                    372:        return(nospace);
1.14      kristaps  373: }
                    374:
                    375:
1.94      kristaps  376: static void
                    377: print_attr(struct html *h, const char *key, const char *val)
                    378: {
                    379:        printf(" %s=\"", key);
                    380:        (void)print_encode(h, val, 1);
                    381:        putchar('\"');
                    382: }
                    383:
                    384:
1.51      kristaps  385: struct tag *
1.29      kristaps  386: print_otag(struct html *h, enum htmltag tag,
                    387:                int sz, const struct htmlpair *p)
1.14      kristaps  388: {
1.29      kristaps  389:        int              i;
1.30      kristaps  390:        struct tag      *t;
                    391:
1.94      kristaps  392:        /* Push this tags onto the stack of open scopes. */
                    393:
1.30      kristaps  394:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.72      kristaps  395:                t = malloc(sizeof(struct tag));
                    396:                if (NULL == t) {
1.75      kristaps  397:                        perror(NULL);
1.112     kristaps  398:                        exit((int)MANDOCLEVEL_SYSERR);
1.72      kristaps  399:                }
1.30      kristaps  400:                t->tag = tag;
1.66      kristaps  401:                t->next = h->tags.head;
                    402:                h->tags.head = t;
1.30      kristaps  403:        } else
                    404:                t = NULL;
1.29      kristaps  405:
                    406:        if ( ! (HTML_NOSPACE & h->flags))
1.105     kristaps  407:                if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
                    408:                        /* Manage keeps! */
                    409:                        if ( ! (HTML_KEEP & h->flags)) {
                    410:                                if (HTML_PREKEEP & h->flags)
                    411:                                        h->flags |= HTML_KEEP;
                    412:                                putchar(' ');
                    413:                        } else
                    414:                                printf("&#160;");
                    415:                }
1.29      kristaps  416:
1.109     kristaps  417:        if ( ! (h->flags & HTML_NONOSPACE))
                    418:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  419:        else
                    420:                h->flags |= HTML_NOSPACE;
1.109     kristaps  421:
1.94      kristaps  422:        /* Print out the tag name and attributes. */
                    423:
1.29      kristaps  424:        printf("<%s", htmltags[tag].name);
1.94      kristaps  425:        for (i = 0; i < sz; i++)
                    426:                print_attr(h, htmlattrs[p[i].key], p[i].val);
                    427:
                    428:        /* Add non-overridable attributes. */
                    429:
                    430:        if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
                    431:                print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
                    432:                print_attr(h, "xml:lang", "en");
                    433:                print_attr(h, "lang", "en");
1.29      kristaps  434:        }
1.93      kristaps  435:
1.94      kristaps  436:        /* Accomodate for XML "well-formed" singleton escaping. */
                    437:
1.93      kristaps  438:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
                    439:                switch (h->type) {
                    440:                case (HTML_XHTML_1_0_STRICT):
                    441:                        putchar('/');
                    442:                        break;
                    443:                default:
                    444:                        break;
                    445:                }
                    446:
1.78      kristaps  447:        putchar('>');
1.14      kristaps  448:
1.29      kristaps  449:        h->flags |= HTML_NOSPACE;
1.30      kristaps  450:        return(t);
1.14      kristaps  451: }
                    452:
                    453:
1.29      kristaps  454: static void
                    455: print_ctag(struct html *h, enum htmltag tag)
1.14      kristaps  456: {
                    457:
1.29      kristaps  458:        printf("</%s>", htmltags[tag].name);
1.71      kristaps  459:        if (HTML_CLRLINE & htmltags[tag].flags) {
1.29      kristaps  460:                h->flags |= HTML_NOSPACE;
1.78      kristaps  461:                putchar('\n');
1.87      kristaps  462:        }
1.14      kristaps  463: }
                    464:
                    465:
1.51      kristaps  466: void
1.93      kristaps  467: print_gen_decls(struct html *h)
1.1       kristaps  468: {
1.93      kristaps  469:
                    470:        print_xmltype(h);
                    471:        print_doctype(h);
                    472: }
                    473:
                    474:
                    475: static void
                    476: print_xmltype(struct html *h)
                    477: {
                    478:
1.100     kristaps  479:        if (HTML_XHTML_1_0_STRICT == h->type)
                    480:                printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.93      kristaps  481: }
                    482:
                    483:
                    484: static void
                    485: print_doctype(struct html *h)
                    486: {
                    487:        const char      *doctype;
                    488:        const char      *dtd;
1.96      kristaps  489:        const char      *name;
1.93      kristaps  490:
                    491:        switch (h->type) {
                    492:        case (HTML_HTML_4_01_STRICT):
1.96      kristaps  493:                name = "HTML";
1.93      kristaps  494:                doctype = "-//W3C//DTD HTML 4.01//EN";
                    495:                dtd = "http://www.w3.org/TR/html4/strict.dtd";
                    496:                break;
                    497:        default:
1.96      kristaps  498:                name = "html";
1.93      kristaps  499:                doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
                    500:                dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
                    501:                break;
                    502:        }
                    503:
1.96      kristaps  504:        printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
                    505:                        name, doctype, dtd);
1.1       kristaps  506: }
                    507:
                    508:
1.51      kristaps  509: void
1.104     kristaps  510: print_text(struct html *h, const char *word)
1.1       kristaps  511: {
                    512:
1.104     kristaps  513:        if (word[0] && '\0' == word[1])
                    514:                switch (word[0]) {
1.29      kristaps  515:                case('.'):
                    516:                        /* FALLTHROUGH */
                    517:                case(','):
                    518:                        /* FALLTHROUGH */
                    519:                case(';'):
                    520:                        /* FALLTHROUGH */
                    521:                case(':'):
                    522:                        /* FALLTHROUGH */
                    523:                case('?'):
                    524:                        /* FALLTHROUGH */
                    525:                case('!'):
                    526:                        /* FALLTHROUGH */
                    527:                case(')'):
                    528:                        /* FALLTHROUGH */
                    529:                case(']'):
1.52      kristaps  530:                        if ( ! (HTML_IGNDELIM & h->flags))
                    531:                                h->flags |= HTML_NOSPACE;
1.30      kristaps  532:                        break;
1.29      kristaps  533:                default:
                    534:                        break;
                    535:                }
1.1       kristaps  536:
1.105     kristaps  537:        if ( ! (HTML_NOSPACE & h->flags)) {
                    538:                /* Manage keeps! */
                    539:                if ( ! (HTML_KEEP & h->flags)) {
                    540:                        if (HTML_PREKEEP & h->flags)
                    541:                                h->flags |= HTML_KEEP;
                    542:                        putchar(' ');
                    543:                } else
                    544:                        printf("&#160;");
                    545:        }
1.30      kristaps  546:
1.104     kristaps  547:        assert(word);
                    548:        if ( ! print_encode(h, word, 0))
1.109     kristaps  549:                if ( ! (h->flags & HTML_NONOSPACE))
                    550:                        h->flags &= ~HTML_NOSPACE;
1.113     schwarze  551:
                    552:        h->flags &= ~HTML_IGNDELIM;
1.8       kristaps  553:
1.98      kristaps  554:        /*
                    555:         * Note that we don't process the pipe: the parser sees it as
                    556:         * punctuation, but we don't in terms of typography.
                    557:         */
1.104     kristaps  558:        if (word[0] && '\0' == word[1])
                    559:                switch (word[0]) {
1.29      kristaps  560:                case('('):
                    561:                        /* FALLTHROUGH */
                    562:                case('['):
                    563:                        h->flags |= HTML_NOSPACE;
1.30      kristaps  564:                        break;
1.29      kristaps  565:                default:
                    566:                        break;
                    567:                }
1.1       kristaps  568: }
1.30      kristaps  569:
                    570:
1.51      kristaps  571: void
1.30      kristaps  572: print_tagq(struct html *h, const struct tag *until)
                    573: {
                    574:        struct tag      *tag;
                    575:
1.66      kristaps  576:        while ((tag = h->tags.head) != NULL) {
1.89      kristaps  577:                if (tag == h->metaf)
                    578:                        h->metaf = NULL;
1.30      kristaps  579:                print_ctag(h, tag->tag);
1.66      kristaps  580:                h->tags.head = tag->next;
1.30      kristaps  581:                free(tag);
                    582:                if (until && tag == until)
                    583:                        return;
                    584:        }
                    585: }
                    586:
                    587:
1.51      kristaps  588: void
1.30      kristaps  589: print_stagq(struct html *h, const struct tag *suntil)
                    590: {
                    591:        struct tag      *tag;
                    592:
1.66      kristaps  593:        while ((tag = h->tags.head) != NULL) {
1.30      kristaps  594:                if (suntil && tag == suntil)
                    595:                        return;
1.89      kristaps  596:                if (tag == h->metaf)
                    597:                        h->metaf = NULL;
1.30      kristaps  598:                print_ctag(h, tag->tag);
1.66      kristaps  599:                h->tags.head = tag->next;
1.30      kristaps  600:                free(tag);
                    601:        }
                    602: }
1.55      kristaps  603:
                    604:
                    605: void
                    606: bufinit(struct html *h)
                    607: {
                    608:
                    609:        h->buf[0] = '\0';
                    610:        h->buflen = 0;
                    611: }
                    612:
                    613:
                    614: void
1.58      kristaps  615: bufcat_style(struct html *h, const char *key, const char *val)
                    616: {
                    617:
                    618:        bufcat(h, key);
                    619:        bufncat(h, ":", 1);
                    620:        bufcat(h, val);
                    621:        bufncat(h, ";", 1);
                    622: }
                    623:
                    624:
                    625: void
1.55      kristaps  626: bufcat(struct html *h, const char *p)
                    627: {
                    628:
                    629:        bufncat(h, p, strlen(p));
                    630: }
                    631:
                    632:
                    633: void
                    634: buffmt(struct html *h, const char *fmt, ...)
                    635: {
                    636:        va_list          ap;
                    637:
                    638:        va_start(ap, fmt);
1.56      kristaps  639:        (void)vsnprintf(h->buf + (int)h->buflen,
1.55      kristaps  640:                        BUFSIZ - h->buflen - 1, fmt, ap);
                    641:        va_end(ap);
                    642:        h->buflen = strlen(h->buf);
                    643: }
                    644:
                    645:
                    646: void
                    647: bufncat(struct html *h, const char *p, size_t sz)
                    648: {
                    649:
                    650:        if (h->buflen + sz > BUFSIZ - 1)
                    651:                sz = BUFSIZ - 1 - h->buflen;
                    652:
                    653:        (void)strncat(h->buf, p, sz);
                    654:        h->buflen += sz;
                    655: }
                    656:
                    657:
                    658: void
                    659: buffmt_includes(struct html *h, const char *name)
                    660: {
                    661:        const char      *p, *pp;
                    662:
                    663:        pp = h->base_includes;
1.61      kristaps  664:
                    665:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  666:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  667:                switch (*(p + 1)) {
                    668:                case('I'):
                    669:                        bufcat(h, name);
                    670:                        break;
                    671:                default:
                    672:                        bufncat(h, p, 2);
                    673:                        break;
                    674:                }
                    675:                pp = p + 2;
                    676:        }
                    677:        if (pp)
                    678:                bufcat(h, pp);
                    679: }
                    680:
                    681:
                    682: void
                    683: buffmt_man(struct html *h,
                    684:                const char *name, const char *sec)
                    685: {
                    686:        const char      *p, *pp;
                    687:
                    688:        pp = h->base_man;
1.61      kristaps  689:
                    690:        /* LINTED */
                    691:        while (NULL != (p = strchr(pp, '%'))) {
1.56      kristaps  692:                bufncat(h, pp, (size_t)(p - pp));
1.55      kristaps  693:                switch (*(p + 1)) {
                    694:                case('S'):
1.58      kristaps  695:                        bufcat(h, sec ? sec : "1");
1.55      kristaps  696:                        break;
                    697:                case('N'):
1.58      kristaps  698:                        buffmt(h, name);
1.55      kristaps  699:                        break;
                    700:                default:
                    701:                        bufncat(h, p, 2);
                    702:                        break;
                    703:                }
                    704:                pp = p + 2;
                    705:        }
                    706:        if (pp)
                    707:                bufcat(h, pp);
                    708: }
1.58      kristaps  709:
                    710:
                    711: void
                    712: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
                    713: {
1.62      kristaps  714:        double           v;
1.63      kristaps  715:        const char      *u;
1.58      kristaps  716:
                    717:        v = su->scale;
                    718:
                    719:        switch (su->unit) {
                    720:        case (SCALE_CM):
                    721:                u = "cm";
                    722:                break;
                    723:        case (SCALE_IN):
                    724:                u = "in";
                    725:                break;
                    726:        case (SCALE_PC):
                    727:                u = "pc";
                    728:                break;
                    729:        case (SCALE_PT):
                    730:                u = "pt";
                    731:                break;
1.59      kristaps  732:        case (SCALE_EM):
                    733:                u = "em";
                    734:                break;
1.58      kristaps  735:        case (SCALE_MM):
                    736:                if (0 == (v /= 100))
                    737:                        v = 1;
                    738:                u = "em";
                    739:                break;
1.59      kristaps  740:        case (SCALE_EN):
                    741:                u = "ex";
                    742:                break;
                    743:        case (SCALE_BU):
                    744:                u = "ex";
                    745:                break;
1.58      kristaps  746:        case (SCALE_VS):
                    747:                u = "em";
                    748:                break;
                    749:        default:
                    750:                u = "ex";
                    751:                break;
                    752:        }
                    753:
1.103     kristaps  754:        /*
                    755:         * XXX: the CSS spec isn't clear as to which types accept
                    756:         * integer or real numbers, so we just make them all decimals.
                    757:         */
                    758:        buffmt(h, "%s: %.2f%s;", p, v, u);
1.58      kristaps  759: }
1.65      kristaps  760:
1.68      kristaps  761:
                    762: void
1.70      kristaps  763: html_idcat(char *dst, const char *src, int sz)
1.68      kristaps  764: {
1.70      kristaps  765:        int              ssz;
1.68      kristaps  766:
                    767:        assert(sz);
                    768:
                    769:        /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
                    770:
1.70      kristaps  771:        for ( ; *dst != '\0' && sz; dst++, sz--)
1.68      kristaps  772:                /* Jump to end. */ ;
                    773:
1.70      kristaps  774:        assert(sz > 2);
1.68      kristaps  775:
1.70      kristaps  776:        /* We can't start with a number (bah). */
1.68      kristaps  777:
1.70      kristaps  778:        *dst++ = 'x';
1.68      kristaps  779:        *dst = '\0';
1.70      kristaps  780:        sz--;
                    781:
                    782:        for ( ; *src != '\0' && sz > 1; src++) {
1.73      kristaps  783:                ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70      kristaps  784:                sz -= ssz;
                    785:                dst += ssz;
                    786:        }
1.68      kristaps  787: }

CVSweb