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

Annotation of mandoc/html.c, Revision 1.119

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

CVSweb