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

Annotation of mandoc/html.c, Revision 1.238

1.238   ! schwarze    1: /*     $Id: html.c,v 1.237 2018/06/25 14:13:54 schwarze Exp $ */
1.1       kristaps    2: /*
1.176     schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.221     schwarze    4:  * Copyright (c) 2011-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.186     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29      kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.92      kristaps   18: #include "config.h"
                     19:
1.41      kristaps   20: #include <sys/types.h>
1.30      kristaps   21:
1.1       kristaps   22: #include <assert.h>
1.68      kristaps   23: #include <ctype.h>
1.76      kristaps   24: #include <stdarg.h>
1.229     schwarze   25: #include <stddef.h>
1.29      kristaps   26: #include <stdio.h>
1.63      kristaps   27: #include <stdint.h>
1.1       kristaps   28: #include <stdlib.h>
1.33      kristaps   29: #include <string.h>
1.45      kristaps   30: #include <unistd.h>
1.1       kristaps   31:
1.210     schwarze   32: #include "mandoc_aux.h"
1.229     schwarze   33: #include "mandoc_ohash.h"
1.100     kristaps   34: #include "mandoc.h"
1.210     schwarze   35: #include "roff.h"
1.58      kristaps   36: #include "out.h"
1.51      kristaps   37: #include "html.h"
1.186     schwarze   38: #include "manconf.h"
1.64      kristaps   39: #include "main.h"
1.63      kristaps   40:
1.29      kristaps   41: struct htmldata {
1.63      kristaps   42:        const char       *name;
1.29      kristaps   43:        int               flags;
1.196     schwarze   44: #define        HTML_NOSTACK     (1 << 0)
                     45: #define        HTML_AUTOCLOSE   (1 << 1)
                     46: #define        HTML_NLBEFORE    (1 << 2)
                     47: #define        HTML_NLBEGIN     (1 << 3)
                     48: #define        HTML_NLEND       (1 << 4)
                     49: #define        HTML_NLAFTER     (1 << 5)
                     50: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     51: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     52: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
                     53: #define        HTML_INDENT      (1 << 6)
                     54: #define        HTML_NOINDENT    (1 << 7)
1.29      kristaps   55: };
1.7       kristaps   56:
1.29      kristaps   57: static const struct htmldata htmltags[TAG_MAX] = {
1.196     schwarze   58:        {"html",        HTML_NLALL},
                     59:        {"head",        HTML_NLALL | HTML_INDENT},
                     60:        {"body",        HTML_NLALL},
                     61:        {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     62:        {"title",       HTML_NLAROUND},
                     63:        {"div",         HTML_NLAROUND},
1.225     schwarze   64:        {"div",         0},
1.196     schwarze   65:        {"h1",          HTML_NLAROUND},
                     66:        {"h2",          HTML_NLAROUND},
                     67:        {"span",        0},
                     68:        {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     69:        {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     70:        {"a",           0},
                     71:        {"table",       HTML_NLALL | HTML_INDENT},
                     72:        {"tr",          HTML_NLALL | HTML_INDENT},
                     73:        {"td",          HTML_NLAROUND},
                     74:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     75:        {"ul",          HTML_NLALL | HTML_INDENT},
                     76:        {"ol",          HTML_NLALL | HTML_INDENT},
                     77:        {"dl",          HTML_NLALL | HTML_INDENT},
                     78:        {"dt",          HTML_NLAROUND},
                     79:        {"dd",          HTML_NLAROUND | HTML_INDENT},
                     80:        {"pre",         HTML_NLALL | HTML_NOINDENT},
1.207     schwarze   81:        {"var",         0},
1.206     schwarze   82:        {"cite",        0},
1.196     schwarze   83:        {"b",           0},
                     84:        {"i",           0},
                     85:        {"code",        0},
                     86:        {"small",       0},
                     87:        {"style",       HTML_NLALL | HTML_INDENT},
                     88:        {"math",        HTML_NLALL | HTML_INDENT},
                     89:        {"mrow",        0},
                     90:        {"mi",          0},
1.215     schwarze   91:        {"mn",          0},
1.196     schwarze   92:        {"mo",          0},
                     93:        {"msup",        0},
                     94:        {"msub",        0},
                     95:        {"msubsup",     0},
                     96:        {"mfrac",       0},
                     97:        {"msqrt",       0},
                     98:        {"mfenced",     0},
                     99:        {"mtable",      0},
                    100:        {"mtr",         0},
                    101:        {"mtd",         0},
                    102:        {"munderover",  0},
                    103:        {"munder",      0},
                    104:        {"mover",       0},
1.90      kristaps  105: };
                    106:
1.229     schwarze  107: /* Avoid duplicate HTML id= attributes. */
                    108: static struct ohash     id_unique;
                    109:
1.197     schwarze  110: static void     print_byte(struct html *, char);
                    111: static void     print_endword(struct html *);
                    112: static void     print_indent(struct html *);
                    113: static void     print_word(struct html *, const char *);
                    114:
1.184     schwarze  115: static void     print_ctag(struct html *, struct tag *);
1.197     schwarze  116: static int      print_escape(struct html *, char);
1.195     schwarze  117: static int      print_encode(struct html *, const char *, const char *, int);
                    118: static void     print_href(struct html *, const char *, const char *, int);
1.141     kristaps  119: static void     print_metaf(struct html *, enum mandoc_esc);
1.82      kristaps  120:
1.156     schwarze  121:
1.180     schwarze  122: void *
1.191     schwarze  123: html_alloc(const struct manoutput *outopts)
1.10      kristaps  124: {
1.30      kristaps  125:        struct html     *h;
                    126:
1.128     kristaps  127:        h = mandoc_calloc(1, sizeof(struct html));
1.10      kristaps  128:
1.204     schwarze  129:        h->tag = NULL;
1.186     schwarze  130:        h->style = outopts->style;
                    131:        h->base_man = outopts->man;
                    132:        h->base_includes = outopts->includes;
                    133:        if (outopts->fragment)
                    134:                h->oflags |= HTML_FRAGMENT;
1.43      kristaps  135:
1.229     schwarze  136:        mandoc_ohash_init(&id_unique, 4, 0);
                    137:
1.188     schwarze  138:        return h;
1.29      kristaps  139: }
1.10      kristaps  140:
1.29      kristaps  141: void
                    142: html_free(void *p)
                    143: {
1.30      kristaps  144:        struct tag      *tag;
                    145:        struct html     *h;
1.229     schwarze  146:        char            *cp;
                    147:        unsigned int     slot;
1.30      kristaps  148:
                    149:        h = (struct html *)p;
1.204     schwarze  150:        while ((tag = h->tag) != NULL) {
                    151:                h->tag = tag->next;
1.30      kristaps  152:                free(tag);
                    153:        }
1.229     schwarze  154:        free(h);
1.53      kristaps  155:
1.229     schwarze  156:        cp = ohash_first(&id_unique, &slot);
                    157:        while (cp != NULL) {
                    158:                free(cp);
                    159:                cp = ohash_next(&id_unique, &slot);
                    160:        }
                    161:        ohash_delete(&id_unique);
1.10      kristaps  162: }
1.2       kristaps  163:
1.51      kristaps  164: void
1.29      kristaps  165: print_gen_head(struct html *h)
                    166: {
1.165     kristaps  167:        struct tag      *t;
1.41      kristaps  168:
1.194     schwarze  169:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222     schwarze  170:        if (h->style != NULL) {
                    171:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    172:                    h->style, "type", "text/css", "media", "all");
                    173:                return;
                    174:        }
1.165     kristaps  175:
1.168     kristaps  176:        /*
1.222     schwarze  177:         * Print a minimal embedded style sheet.
1.168     kristaps  178:         */
1.196     schwarze  179:
1.194     schwarze  180:        t = print_otag(h, TAG_STYLE, "");
1.196     schwarze  181:        print_text(h, "table.head, table.foot { width: 100%; }");
1.197     schwarze  182:        print_endline(h);
1.196     schwarze  183:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197     schwarze  184:        print_endline(h);
1.196     schwarze  185:        print_text(h, "td.head-vol { text-align: center; }");
1.197     schwarze  186:        print_endline(h);
1.198     schwarze  187:        print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.225     schwarze  188:        print_endline(h);
                    189:        print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.226     schwarze  190:        print_endline(h);
1.227     schwarze  191:        print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.228     schwarze  192:        print_endline(h);
                    193:        print_text(h, "span.Ms { font-weight: bold; }");
1.224     schwarze  194:        print_endline(h);
                    195:        print_text(h, "dl.Bl-diag ");
                    196:        print_byte(h, '>');
                    197:        print_text(h, " dt { font-weight: bold; }");
1.223     schwarze  198:        print_endline(h);
                    199:        print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
                    200:            "code.In, code.Fd, code.Fn,");
                    201:        print_endline(h);
                    202:        print_text(h, "code.Cd { font-weight: bold; "
                    203:            "font-family: inherit; }");
1.165     kristaps  204:        print_tagq(h, t);
1.4       kristaps  205: }
                    206:
1.126     schwarze  207: static void
1.132     kristaps  208: print_metaf(struct html *h, enum mandoc_esc deco)
1.88      kristaps  209: {
1.90      kristaps  210:        enum htmlfont    font;
1.88      kristaps  211:
                    212:        switch (deco) {
1.156     schwarze  213:        case ESCAPE_FONTPREV:
1.90      kristaps  214:                font = h->metal;
1.88      kristaps  215:                break;
1.156     schwarze  216:        case ESCAPE_FONTITALIC:
1.90      kristaps  217:                font = HTMLFONT_ITALIC;
                    218:                break;
1.156     schwarze  219:        case ESCAPE_FONTBOLD:
1.90      kristaps  220:                font = HTMLFONT_BOLD;
1.88      kristaps  221:                break;
1.156     schwarze  222:        case ESCAPE_FONTBI:
1.152     schwarze  223:                font = HTMLFONT_BI;
                    224:                break;
1.156     schwarze  225:        case ESCAPE_FONT:
                    226:        case ESCAPE_FONTROMAN:
1.90      kristaps  227:                font = HTMLFONT_NONE;
1.88      kristaps  228:                break;
                    229:        default:
                    230:                abort();
                    231:        }
                    232:
1.122     kristaps  233:        if (h->metaf) {
                    234:                print_tagq(h, h->metaf);
                    235:                h->metaf = NULL;
                    236:        }
                    237:
                    238:        h->metal = h->metac;
                    239:        h->metac = font;
                    240:
1.152     schwarze  241:        switch (font) {
1.156     schwarze  242:        case HTMLFONT_ITALIC:
1.194     schwarze  243:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  244:                break;
1.156     schwarze  245:        case HTMLFONT_BOLD:
1.194     schwarze  246:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  247:                break;
1.156     schwarze  248:        case HTMLFONT_BI:
1.194     schwarze  249:                h->metaf = print_otag(h, TAG_B, "");
                    250:                print_otag(h, TAG_I, "");
1.152     schwarze  251:                break;
                    252:        default:
                    253:                break;
                    254:        }
1.210     schwarze  255: }
                    256:
                    257: char *
1.229     schwarze  258: html_make_id(const struct roff_node *n, int unique)
1.210     schwarze  259: {
                    260:        const struct roff_node  *nch;
1.229     schwarze  261:        char                    *buf, *bufs, *cp;
                    262:        unsigned int             slot;
                    263:        int                      suffix;
1.210     schwarze  264:
                    265:        for (nch = n->child; nch != NULL; nch = nch->next)
                    266:                if (nch->type != ROFFT_TEXT)
                    267:                        return NULL;
                    268:
                    269:        buf = NULL;
                    270:        deroff(&buf, n);
1.220     schwarze  271:        if (buf == NULL)
                    272:                return NULL;
1.210     schwarze  273:
1.230     schwarze  274:        /*
                    275:         * In ID attributes, only use ASCII characters that are
                    276:         * permitted in URL-fragment strings according to the
                    277:         * explicit list at:
                    278:         * https://url.spec.whatwg.org/#url-fragment-string
                    279:         */
1.210     schwarze  280:
                    281:        for (cp = buf; *cp != '\0'; cp++)
1.230     schwarze  282:                if (isalnum((unsigned char)*cp) == 0 &&
                    283:                    strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210     schwarze  284:                        *cp = '_';
                    285:
1.229     schwarze  286:        if (unique == 0)
                    287:                return buf;
                    288:
                    289:        /* Avoid duplicate HTML id= attributes. */
                    290:
                    291:        bufs = NULL;
                    292:        suffix = 1;
                    293:        slot = ohash_qlookup(&id_unique, buf);
                    294:        cp = ohash_find(&id_unique, slot);
                    295:        if (cp != NULL) {
                    296:                while (cp != NULL) {
                    297:                        free(bufs);
                    298:                        if (++suffix > 127) {
                    299:                                free(buf);
                    300:                                return NULL;
                    301:                        }
                    302:                        mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
                    303:                        slot = ohash_qlookup(&id_unique, bufs);
                    304:                        cp = ohash_find(&id_unique, slot);
                    305:                }
                    306:                free(buf);
                    307:                buf = bufs;
                    308:        }
                    309:        ohash_insert(&id_unique, slot, buf);
1.210     schwarze  310:        return buf;
1.88      kristaps  311: }
                    312:
1.85      kristaps  313: static int
1.197     schwarze  314: print_escape(struct html *h, char c)
1.159     schwarze  315: {
                    316:
                    317:        switch (c) {
                    318:        case '<':
1.197     schwarze  319:                print_word(h, "&lt;");
1.159     schwarze  320:                break;
                    321:        case '>':
1.197     schwarze  322:                print_word(h, "&gt;");
1.159     schwarze  323:                break;
                    324:        case '&':
1.197     schwarze  325:                print_word(h, "&amp;");
1.159     schwarze  326:                break;
                    327:        case '"':
1.197     schwarze  328:                print_word(h, "&quot;");
1.159     schwarze  329:                break;
                    330:        case ASCII_NBRSP:
1.197     schwarze  331:                print_word(h, "&nbsp;");
1.159     schwarze  332:                break;
                    333:        case ASCII_HYPH:
1.197     schwarze  334:                print_byte(h, '-');
1.189     schwarze  335:                break;
1.159     schwarze  336:        case ASCII_BREAK:
                    337:                break;
                    338:        default:
1.188     schwarze  339:                return 0;
1.159     schwarze  340:        }
1.188     schwarze  341:        return 1;
1.159     schwarze  342: }
                    343:
                    344: static int
1.195     schwarze  345: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29      kristaps  346: {
1.197     schwarze  347:        char             numbuf[16];
1.214     schwarze  348:        struct tag      *t;
                    349:        const char      *seq;
1.77      kristaps  350:        size_t           sz;
1.214     schwarze  351:        int              c, len, breakline, nospace;
1.132     kristaps  352:        enum mandoc_esc  esc;
1.214     schwarze  353:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154     schwarze  354:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  355:
1.195     schwarze  356:        if (pend == NULL)
                    357:                pend = strchr(p, '\0');
                    358:
1.214     schwarze  359:        breakline = 0;
1.85      kristaps  360:        nospace = 0;
                    361:
1.195     schwarze  362:        while (p < pend) {
1.151     schwarze  363:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    364:                        h->flags &= ~HTML_SKIPCHAR;
                    365:                        p++;
                    366:                        continue;
                    367:                }
                    368:
1.197     schwarze  369:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214     schwarze  370:                        print_byte(h, *p);
                    371:
                    372:                if (breakline &&
                    373:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
                    374:                        t = print_otag(h, TAG_DIV, "");
                    375:                        print_text(h, "\\~");
                    376:                        print_tagq(h, t);
                    377:                        breakline = 0;
                    378:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    379:                                p++;
                    380:                        continue;
                    381:                }
1.77      kristaps  382:
1.195     schwarze  383:                if (p >= pend)
1.132     kristaps  384:                        break;
                    385:
1.214     schwarze  386:                if (*p == ' ') {
                    387:                        print_endword(h);
                    388:                        p++;
                    389:                        continue;
                    390:                }
                    391:
1.197     schwarze  392:                if (print_escape(h, *p++))
1.154     schwarze  393:                        continue;
1.77      kristaps  394:
1.132     kristaps  395:                esc = mandoc_escape(&p, &seq, &len);
                    396:                if (ESCAPE_ERROR == esc)
                    397:                        break;
1.82      kristaps  398:
1.132     kristaps  399:                switch (esc) {
1.156     schwarze  400:                case ESCAPE_FONT:
                    401:                case ESCAPE_FONTPREV:
                    402:                case ESCAPE_FONTBOLD:
                    403:                case ESCAPE_FONTITALIC:
                    404:                case ESCAPE_FONTBI:
                    405:                case ESCAPE_FONTROMAN:
1.151     schwarze  406:                        if (0 == norecurse)
                    407:                                print_metaf(h, esc);
                    408:                        continue;
1.156     schwarze  409:                case ESCAPE_SKIPCHAR:
1.151     schwarze  410:                        h->flags |= HTML_SKIPCHAR;
                    411:                        continue;
                    412:                default:
                    413:                        break;
                    414:                }
                    415:
                    416:                if (h->flags & HTML_SKIPCHAR) {
                    417:                        h->flags &= ~HTML_SKIPCHAR;
                    418:                        continue;
                    419:                }
                    420:
                    421:                switch (esc) {
1.156     schwarze  422:                case ESCAPE_UNICODE:
1.159     schwarze  423:                        /* Skip past "u" header. */
1.144     kristaps  424:                        c = mchars_num2uc(seq + 1, len - 1);
                    425:                        break;
1.156     schwarze  426:                case ESCAPE_NUMBERED:
1.141     kristaps  427:                        c = mchars_num2char(seq, len);
1.181     schwarze  428:                        if (c < 0)
                    429:                                continue;
1.82      kristaps  430:                        break;
1.156     schwarze  431:                case ESCAPE_SPECIAL:
1.191     schwarze  432:                        c = mchars_spec2cp(seq, len);
1.181     schwarze  433:                        if (c <= 0)
                    434:                                continue;
1.132     kristaps  435:                        break;
1.214     schwarze  436:                case ESCAPE_BREAK:
                    437:                        breakline = 1;
                    438:                        continue;
1.156     schwarze  439:                case ESCAPE_NOSPACE:
1.132     kristaps  440:                        if ('\0' == *p)
                    441:                                nospace = 1;
1.179     schwarze  442:                        continue;
1.185     schwarze  443:                case ESCAPE_OVERSTRIKE:
                    444:                        if (len == 0)
                    445:                                continue;
                    446:                        c = seq[len - 1];
                    447:                        break;
1.82      kristaps  448:                default:
1.179     schwarze  449:                        continue;
1.82      kristaps  450:                }
1.181     schwarze  451:                if ((c < 0x20 && c != 0x09) ||
                    452:                    (c > 0x7E && c < 0xA0))
1.179     schwarze  453:                        c = 0xFFFD;
1.197     schwarze  454:                if (c > 0x7E) {
1.216     schwarze  455:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197     schwarze  456:                        print_word(h, numbuf);
                    457:                } else if (print_escape(h, c) == 0)
                    458:                        print_byte(h, c);
1.32      kristaps  459:        }
1.85      kristaps  460:
1.188     schwarze  461:        return nospace;
1.14      kristaps  462: }
                    463:
1.94      kristaps  464: static void
1.195     schwarze  465: print_href(struct html *h, const char *name, const char *sec, int man)
1.94      kristaps  466: {
1.195     schwarze  467:        const char      *p, *pp;
                    468:
                    469:        pp = man ? h->base_man : h->base_includes;
                    470:        while ((p = strchr(pp, '%')) != NULL) {
                    471:                print_encode(h, pp, p, 1);
                    472:                if (man && p[1] == 'S') {
                    473:                        if (sec == NULL)
1.197     schwarze  474:                                print_byte(h, '1');
1.195     schwarze  475:                        else
                    476:                                print_encode(h, sec, NULL, 1);
                    477:                } else if ((man && p[1] == 'N') ||
                    478:                    (man == 0 && p[1] == 'I'))
                    479:                        print_encode(h, name, NULL, 1);
                    480:                else
                    481:                        print_encode(h, p, p + 2, 1);
                    482:                pp = p + 2;
                    483:        }
                    484:        if (*pp != '\0')
                    485:                print_encode(h, pp, NULL, 1);
1.94      kristaps  486: }
                    487:
1.51      kristaps  488: struct tag *
1.194     schwarze  489: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14      kristaps  490: {
1.194     schwarze  491:        va_list          ap;
1.30      kristaps  492:        struct tag      *t;
1.195     schwarze  493:        const char      *attr;
1.203     schwarze  494:        char            *arg1, *arg2;
1.238   ! schwarze  495:        int              tflags;
1.196     schwarze  496:
                    497:        tflags = htmltags[tag].flags;
1.30      kristaps  498:
1.204     schwarze  499:        /* Push this tag onto the stack of open scopes. */
1.94      kristaps  500:
1.196     schwarze  501:        if ((tflags & HTML_NOSTACK) == 0) {
1.128     kristaps  502:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  503:                t->tag = tag;
1.204     schwarze  504:                t->next = h->tag;
                    505:                h->tag = t;
1.30      kristaps  506:        } else
                    507:                t = NULL;
1.29      kristaps  508:
1.196     schwarze  509:        if (tflags & HTML_NLBEFORE)
1.197     schwarze  510:                print_endline(h);
                    511:        if (h->col == 0)
                    512:                print_indent(h);
1.196     schwarze  513:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    514:                if (h->flags & HTML_KEEP)
1.216     schwarze  515:                        print_word(h, "&#x00A0;");
1.196     schwarze  516:                else {
                    517:                        if (h->flags & HTML_PREKEEP)
                    518:                                h->flags |= HTML_KEEP;
1.197     schwarze  519:                        print_endword(h);
1.105     kristaps  520:                }
1.196     schwarze  521:        }
1.29      kristaps  522:
1.109     kristaps  523:        if ( ! (h->flags & HTML_NONOSPACE))
                    524:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  525:        else
                    526:                h->flags |= HTML_NOSPACE;
1.109     kristaps  527:
1.94      kristaps  528:        /* Print out the tag name and attributes. */
                    529:
1.197     schwarze  530:        print_byte(h, '<');
                    531:        print_word(h, htmltags[tag].name);
1.194     schwarze  532:
                    533:        va_start(ap, fmt);
                    534:
                    535:        while (*fmt != '\0') {
1.203     schwarze  536:
1.238   ! schwarze  537:                /* Parse attributes and arguments. */
1.203     schwarze  538:
                    539:                arg1 = va_arg(ap, char *);
1.238   ! schwarze  540:                arg2 = NULL;
1.194     schwarze  541:                switch (*fmt++) {
                    542:                case 'c':
1.195     schwarze  543:                        attr = "class";
1.194     schwarze  544:                        break;
                    545:                case 'h':
1.195     schwarze  546:                        attr = "href";
1.194     schwarze  547:                        break;
                    548:                case 'i':
1.195     schwarze  549:                        attr = "id";
1.194     schwarze  550:                        break;
1.238   ! schwarze  551:                case 's':
        !           552:                        attr = "style";
        !           553:                        arg2 = va_arg(ap, char *);
        !           554:                        break;
1.194     schwarze  555:                case '?':
1.203     schwarze  556:                        attr = arg1;
                    557:                        arg1 = va_arg(ap, char *);
1.194     schwarze  558:                        break;
                    559:                default:
                    560:                        abort();
                    561:                }
1.203     schwarze  562:                if (*fmt == 'M')
                    563:                        arg2 = va_arg(ap, char *);
                    564:                if (arg1 == NULL)
                    565:                        continue;
                    566:
1.238   ! schwarze  567:                /* Print the attributes. */
1.203     schwarze  568:
1.197     schwarze  569:                print_byte(h, ' ');
                    570:                print_word(h, attr);
                    571:                print_byte(h, '=');
                    572:                print_byte(h, '"');
1.195     schwarze  573:                switch (*fmt) {
1.208     schwarze  574:                case 'I':
                    575:                        print_href(h, arg1, NULL, 0);
                    576:                        fmt++;
                    577:                        break;
1.195     schwarze  578:                case 'M':
1.203     schwarze  579:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  580:                        fmt++;
                    581:                        break;
1.208     schwarze  582:                case 'R':
                    583:                        print_byte(h, '#');
                    584:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  585:                        fmt++;
                    586:                        break;
1.208     schwarze  587:                case 'T':
                    588:                        print_encode(h, arg1, NULL, 1);
                    589:                        print_word(h, "\" title=\"");
                    590:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  591:                        fmt++;
1.208     schwarze  592:                        break;
1.195     schwarze  593:                default:
1.238   ! schwarze  594:                        if (arg2 == NULL)
        !           595:                                print_encode(h, arg1, NULL, 1);
        !           596:                        else {
        !           597:                                print_word(h, arg1);
        !           598:                                print_byte(h, ':');
        !           599:                                print_byte(h, ' ');
        !           600:                                print_word(h, arg2);
        !           601:                                print_byte(h, ';');
        !           602:                        }
1.195     schwarze  603:                        break;
                    604:                }
1.197     schwarze  605:                print_byte(h, '"');
1.194     schwarze  606:        }
                    607:        va_end(ap);
1.94      kristaps  608:
1.172     kristaps  609:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  610:
1.93      kristaps  611:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  612:                print_byte(h, '/');
1.93      kristaps  613:
1.197     schwarze  614:        print_byte(h, '>');
1.14      kristaps  615:
1.196     schwarze  616:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  617:                print_endline(h);
1.196     schwarze  618:        else
                    619:                h->flags |= HTML_NOSPACE;
1.117     kristaps  620:
1.196     schwarze  621:        if (tflags & HTML_INDENT)
                    622:                h->indent++;
                    623:        if (tflags & HTML_NOINDENT)
                    624:                h->noindent++;
1.117     kristaps  625:
1.188     schwarze  626:        return t;
1.14      kristaps  627: }
                    628:
1.29      kristaps  629: static void
1.184     schwarze  630: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  631: {
1.196     schwarze  632:        int      tflags;
1.156     schwarze  633:
1.184     schwarze  634:        /*
                    635:         * Remember to close out and nullify the current
                    636:         * meta-font and table, if applicable.
                    637:         */
                    638:        if (tag == h->metaf)
                    639:                h->metaf = NULL;
                    640:        if (tag == h->tblt)
                    641:                h->tblt = NULL;
                    642:
1.196     schwarze  643:        tflags = htmltags[tag->tag].flags;
                    644:
                    645:        if (tflags & HTML_INDENT)
                    646:                h->indent--;
                    647:        if (tflags & HTML_NOINDENT)
                    648:                h->noindent--;
                    649:        if (tflags & HTML_NLEND)
1.197     schwarze  650:                print_endline(h);
                    651:        print_indent(h);
                    652:        print_byte(h, '<');
                    653:        print_byte(h, '/');
                    654:        print_word(h, htmltags[tag->tag].name);
                    655:        print_byte(h, '>');
1.196     schwarze  656:        if (tflags & HTML_NLAFTER)
1.197     schwarze  657:                print_endline(h);
1.184     schwarze  658:
1.204     schwarze  659:        h->tag = tag->next;
1.184     schwarze  660:        free(tag);
1.14      kristaps  661: }
                    662:
1.51      kristaps  663: void
1.93      kristaps  664: print_gen_decls(struct html *h)
1.1       kristaps  665: {
1.197     schwarze  666:        print_word(h, "<!DOCTYPE html>");
                    667:        print_endline(h);
1.221     schwarze  668: }
                    669:
                    670: void
                    671: print_gen_comment(struct html *h, struct roff_node *n)
                    672: {
                    673:        int      wantblank;
                    674:
                    675:        print_word(h, "<!-- This is an automatically generated file."
                    676:            "  Do not edit.");
                    677:        h->indent = 1;
                    678:        wantblank = 0;
                    679:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    680:                if (strstr(n->string, "-->") == NULL &&
                    681:                    (wantblank || *n->string != '\0')) {
                    682:                        print_endline(h);
                    683:                        print_indent(h);
                    684:                        print_word(h, n->string);
                    685:                        wantblank = *n->string != '\0';
                    686:                }
                    687:                n = n->next;
                    688:        }
                    689:        if (wantblank)
                    690:                print_endline(h);
                    691:        print_word(h, " -->");
                    692:        print_endline(h);
                    693:        h->indent = 0;
1.1       kristaps  694: }
                    695:
1.51      kristaps  696: void
1.104     kristaps  697: print_text(struct html *h, const char *word)
1.1       kristaps  698: {
1.197     schwarze  699:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  700:                if ( ! (HTML_KEEP & h->flags)) {
                    701:                        if (HTML_PREKEEP & h->flags)
                    702:                                h->flags |= HTML_KEEP;
1.197     schwarze  703:                        print_endword(h);
1.105     kristaps  704:                } else
1.216     schwarze  705:                        print_word(h, "&#x00A0;");
1.105     kristaps  706:        }
1.30      kristaps  707:
1.122     kristaps  708:        assert(NULL == h->metaf);
1.152     schwarze  709:        switch (h->metac) {
1.156     schwarze  710:        case HTMLFONT_ITALIC:
1.194     schwarze  711:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  712:                break;
1.156     schwarze  713:        case HTMLFONT_BOLD:
1.194     schwarze  714:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  715:                break;
1.156     schwarze  716:        case HTMLFONT_BI:
1.194     schwarze  717:                h->metaf = print_otag(h, TAG_B, "");
                    718:                print_otag(h, TAG_I, "");
1.152     schwarze  719:                break;
                    720:        default:
1.197     schwarze  721:                print_indent(h);
1.152     schwarze  722:                break;
                    723:        }
1.122     kristaps  724:
1.104     kristaps  725:        assert(word);
1.195     schwarze  726:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  727:                if ( ! (h->flags & HTML_NONOSPACE))
                    728:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  729:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  730:        } else
1.183     schwarze  731:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  732:
                    733:        if (h->metaf) {
                    734:                print_tagq(h, h->metaf);
                    735:                h->metaf = NULL;
                    736:        }
1.113     schwarze  737:
                    738:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  739: }
1.30      kristaps  740:
1.51      kristaps  741: void
1.30      kristaps  742: print_tagq(struct html *h, const struct tag *until)
                    743: {
                    744:        struct tag      *tag;
                    745:
1.204     schwarze  746:        while ((tag = h->tag) != NULL) {
1.184     schwarze  747:                print_ctag(h, tag);
1.30      kristaps  748:                if (until && tag == until)
                    749:                        return;
                    750:        }
                    751: }
                    752:
1.51      kristaps  753: void
1.30      kristaps  754: print_stagq(struct html *h, const struct tag *suntil)
                    755: {
                    756:        struct tag      *tag;
                    757:
1.204     schwarze  758:        while ((tag = h->tag) != NULL) {
1.30      kristaps  759:                if (suntil && tag == suntil)
                    760:                        return;
1.184     schwarze  761:                print_ctag(h, tag);
1.30      kristaps  762:        }
                    763: }
1.171     kristaps  764:
                    765: void
                    766: print_paragraph(struct html *h)
                    767: {
                    768:        struct tag      *t;
                    769:
1.198     schwarze  770:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.171     kristaps  771:        print_tagq(h, t);
                    772: }
                    773:
1.197     schwarze  774:
                    775: /***********************************************************************
                    776:  * Low level output functions.
                    777:  * They implement line breaking using a short static buffer.
                    778:  ***********************************************************************/
                    779:
                    780: /*
                    781:  * Buffer one HTML output byte.
                    782:  * If the buffer is full, flush and deactivate it and start a new line.
                    783:  * If the buffer is inactive, print directly.
                    784:  */
                    785: static void
                    786: print_byte(struct html *h, char c)
                    787: {
                    788:        if ((h->flags & HTML_BUFFER) == 0) {
                    789:                putchar(c);
                    790:                h->col++;
                    791:                return;
                    792:        }
                    793:
                    794:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    795:                h->buf[h->bufcol++] = c;
                    796:                return;
                    797:        }
                    798:
                    799:        putchar('\n');
                    800:        h->col = 0;
                    801:        print_indent(h);
                    802:        putchar(' ');
                    803:        putchar(' ');
                    804:        fwrite(h->buf, h->bufcol, 1, stdout);
                    805:        putchar(c);
                    806:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    807:        h->bufcol = 0;
                    808:        h->flags &= ~HTML_BUFFER;
                    809: }
                    810:
1.196     schwarze  811: /*
                    812:  * If something was printed on the current output line, end it.
1.197     schwarze  813:  * Not to be called right after print_indent().
1.196     schwarze  814:  */
1.202     schwarze  815: void
1.197     schwarze  816: print_endline(struct html *h)
1.196     schwarze  817: {
1.197     schwarze  818:        if (h->col == 0)
1.196     schwarze  819:                return;
                    820:
1.197     schwarze  821:        if (h->bufcol) {
                    822:                putchar(' ');
                    823:                fwrite(h->buf, h->bufcol, 1, stdout);
                    824:                h->bufcol = 0;
                    825:        }
1.196     schwarze  826:        putchar('\n');
1.197     schwarze  827:        h->col = 0;
                    828:        h->flags |= HTML_NOSPACE;
                    829:        h->flags &= ~HTML_BUFFER;
                    830: }
                    831:
                    832: /*
                    833:  * Flush the HTML output buffer.
                    834:  * If it is inactive, activate it.
                    835:  */
                    836: static void
                    837: print_endword(struct html *h)
                    838: {
                    839:        if (h->noindent) {
                    840:                print_byte(h, ' ');
                    841:                return;
                    842:        }
                    843:
                    844:        if ((h->flags & HTML_BUFFER) == 0) {
                    845:                h->col++;
                    846:                h->flags |= HTML_BUFFER;
                    847:        } else if (h->bufcol) {
                    848:                putchar(' ');
                    849:                fwrite(h->buf, h->bufcol, 1, stdout);
                    850:                h->col += h->bufcol + 1;
                    851:        }
                    852:        h->bufcol = 0;
1.196     schwarze  853: }
                    854:
                    855: /*
                    856:  * If at the beginning of a new output line,
                    857:  * perform indentation and mark the line as containing output.
                    858:  * Make sure to really produce some output right afterwards,
                    859:  * but do not use print_otag() for producing it.
                    860:  */
                    861: static void
1.197     schwarze  862: print_indent(struct html *h)
1.196     schwarze  863: {
1.197     schwarze  864:        size_t   i;
1.196     schwarze  865:
1.197     schwarze  866:        if (h->col)
1.196     schwarze  867:                return;
                    868:
1.197     schwarze  869:        if (h->noindent == 0) {
                    870:                h->col = h->indent * 2;
                    871:                for (i = 0; i < h->col; i++)
1.196     schwarze  872:                        putchar(' ');
1.197     schwarze  873:        }
                    874:        h->flags &= ~HTML_NOSPACE;
                    875: }
                    876:
                    877: /*
                    878:  * Print or buffer some characters
                    879:  * depending on the current HTML output buffer state.
                    880:  */
                    881: static void
                    882: print_word(struct html *h, const char *cp)
                    883: {
                    884:        while (*cp != '\0')
                    885:                print_byte(h, *cp++);
1.68      kristaps  886: }

CVSweb