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

Annotation of mandoc/html.c, Revision 1.117

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

CVSweb