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

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

1.148   ! schwarze    1: /* $OpenBSD: html.c,v 1.147 2022/06/24 11:15:19 schwarze Exp $ */
1.1       schwarze    2: /*
1.42      schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.145     schwarze    4:  * Copyright (c) 2011-2015, 2017-2021 Ingo Schwarze <schwarze@openbsd.org>
1.148   ! schwarze    5:  * Copyright (c) 2022 Anna Vyalkova <cyber@sysrq.in>
1.1       schwarze    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
1.56      schwarze   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.56      schwarze   13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.135     schwarze   18:  *
                     19:  * Common functions for mandoc(1) HTML formatters.
                     20:  * For use by individual formatters and by the main program.
1.1       schwarze   21:  */
                     22: #include <sys/types.h>
1.110     schwarze   23: #include <sys/stat.h>
1.1       schwarze   24:
                     25: #include <assert.h>
1.3       schwarze   26: #include <ctype.h>
1.4       schwarze   27: #include <stdarg.h>
1.99      schwarze   28: #include <stddef.h>
1.1       schwarze   29: #include <stdio.h>
                     30: #include <stdint.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
1.80      schwarze   35: #include "mandoc_aux.h"
1.99      schwarze   36: #include "mandoc_ohash.h"
1.9       schwarze   37: #include "mandoc.h"
1.80      schwarze   38: #include "roff.h"
1.1       schwarze   39: #include "out.h"
                     40: #include "html.h"
1.56      schwarze   41: #include "manconf.h"
1.1       schwarze   42: #include "main.h"
                     43:
                     44: struct htmldata {
                     45:        const char       *name;
                     46:        int               flags;
1.127     schwarze   47: #define        HTML_INPHRASE    (1 << 0)  /* Can appear in phrasing context. */
                     48: #define        HTML_TOPHRASE    (1 << 1)  /* Establishes phrasing context. */
                     49: #define        HTML_NOSTACK     (1 << 2)  /* Does not have an end tag. */
                     50: #define        HTML_NLBEFORE    (1 << 3)  /* Output line break before opening. */
                     51: #define        HTML_NLBEGIN     (1 << 4)  /* Output line break after opening. */
                     52: #define        HTML_NLEND       (1 << 5)  /* Output line break before closing. */
                     53: #define        HTML_NLAFTER     (1 << 6)  /* Output line break after closing. */
1.66      schwarze   54: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     55: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     56: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
1.127     schwarze   57: #define        HTML_INDENT      (1 << 7)  /* Indent content by two spaces. */
                     58: #define        HTML_NOINDENT    (1 << 8)  /* Exception: never indent content. */
1.1       schwarze   59: };
                     60:
                     61: static const struct htmldata htmltags[TAG_MAX] = {
1.66      schwarze   62:        {"html",        HTML_NLALL},
                     63:        {"head",        HTML_NLALL | HTML_INDENT},
1.127     schwarze   64:        {"meta",        HTML_NOSTACK | HTML_NLALL},
                     65:        {"link",        HTML_NOSTACK | HTML_NLALL},
                     66:        {"style",       HTML_NLALL | HTML_INDENT},
                     67:        {"title",       HTML_NLAROUND},
1.66      schwarze   68:        {"body",        HTML_NLALL},
1.148   ! schwarze   69:        {"main",        HTML_NLALL},
1.66      schwarze   70:        {"div",         HTML_NLAROUND},
1.123     schwarze   71:        {"section",     HTML_NLALL},
1.147     schwarze   72:        {"nav",         HTML_NLALL},
1.66      schwarze   73:        {"table",       HTML_NLALL | HTML_INDENT},
                     74:        {"tr",          HTML_NLALL | HTML_INDENT},
                     75:        {"td",          HTML_NLAROUND},
                     76:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     77:        {"ul",          HTML_NLALL | HTML_INDENT},
                     78:        {"ol",          HTML_NLALL | HTML_INDENT},
                     79:        {"dl",          HTML_NLALL | HTML_INDENT},
                     80:        {"dt",          HTML_NLAROUND},
                     81:        {"dd",          HTML_NLAROUND | HTML_INDENT},
1.127     schwarze   82:        {"h1",          HTML_TOPHRASE | HTML_NLAROUND},
                     83:        {"h2",          HTML_TOPHRASE | HTML_NLAROUND},
                     84:        {"p",           HTML_TOPHRASE | HTML_NLAROUND | HTML_INDENT},
1.142     schwarze   85:        {"pre",         HTML_TOPHRASE | HTML_NLAROUND | HTML_NOINDENT},
1.127     schwarze   86:        {"a",           HTML_INPHRASE | HTML_TOPHRASE},
                     87:        {"b",           HTML_INPHRASE | HTML_TOPHRASE},
                     88:        {"cite",        HTML_INPHRASE | HTML_TOPHRASE},
                     89:        {"code",        HTML_INPHRASE | HTML_TOPHRASE},
                     90:        {"i",           HTML_INPHRASE | HTML_TOPHRASE},
                     91:        {"small",       HTML_INPHRASE | HTML_TOPHRASE},
                     92:        {"span",        HTML_INPHRASE | HTML_TOPHRASE},
                     93:        {"var",         HTML_INPHRASE | HTML_TOPHRASE},
                     94:        {"br",          HTML_INPHRASE | HTML_NOSTACK | HTML_NLALL},
1.146     schwarze   95:        {"hr",          HTML_INPHRASE | HTML_NOSTACK},
1.134     schwarze   96:        {"mark",        HTML_INPHRASE },
1.127     schwarze   97:        {"math",        HTML_INPHRASE | HTML_NLALL | HTML_INDENT},
1.66      schwarze   98:        {"mrow",        0},
                     99:        {"mi",          0},
1.85      schwarze  100:        {"mn",          0},
1.66      schwarze  101:        {"mo",          0},
                    102:        {"msup",        0},
                    103:        {"msub",        0},
                    104:        {"msubsup",     0},
                    105:        {"mfrac",       0},
                    106:        {"msqrt",       0},
                    107:        {"mfenced",     0},
                    108:        {"mtable",      0},
                    109:        {"mtr",         0},
                    110:        {"mtd",         0},
                    111:        {"munderover",  0},
                    112:        {"munder",      0},
                    113:        {"mover",       0},
1.5       schwarze  114: };
                    115:
1.99      schwarze  116: /* Avoid duplicate HTML id= attributes. */
1.140     schwarze  117:
                    118: struct id_entry {
                    119:        int      ord;   /* Ordinal number of the latest occurrence. */
                    120:        char     id[];  /* The id= attribute without any ordinal suffix. */
                    121: };
1.99      schwarze  122: static struct ohash     id_unique;
                    123:
1.124     schwarze  124: static void     html_reset_internal(struct html *);
1.67      schwarze  125: static void     print_byte(struct html *, char);
                    126: static void     print_endword(struct html *);
                    127: static void     print_indent(struct html *);
                    128: static void     print_word(struct html *, const char *);
                    129:
1.54      schwarze  130: static void     print_ctag(struct html *, struct tag *);
1.67      schwarze  131: static int      print_escape(struct html *, char);
1.65      schwarze  132: static int      print_encode(struct html *, const char *, const char *, int);
                    133: static void     print_href(struct html *, const char *, const char *, int);
1.125     schwarze  134: static void     print_metaf(struct html *);
1.5       schwarze  135:
1.35      schwarze  136:
1.50      schwarze  137: void *
1.61      schwarze  138: html_alloc(const struct manoutput *outopts)
1.1       schwarze  139: {
                    140:        struct html     *h;
                    141:
1.24      schwarze  142:        h = mandoc_calloc(1, sizeof(struct html));
1.1       schwarze  143:
1.74      schwarze  144:        h->tag = NULL;
1.143     schwarze  145:        h->metac = h->metal = ESCAPE_FONTROMAN;
1.56      schwarze  146:        h->style = outopts->style;
1.110     schwarze  147:        if ((h->base_man1 = outopts->man) == NULL)
                    148:                h->base_man2 = NULL;
                    149:        else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
                    150:                *h->base_man2++ = '\0';
1.56      schwarze  151:        h->base_includes = outopts->includes;
                    152:        if (outopts->fragment)
                    153:                h->oflags |= HTML_FRAGMENT;
1.111     schwarze  154:        if (outopts->toc)
                    155:                h->oflags |= HTML_TOC;
1.1       schwarze  156:
1.140     schwarze  157:        mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id));
1.99      schwarze  158:
1.58      schwarze  159:        return h;
1.1       schwarze  160: }
                    161:
1.124     schwarze  162: static void
                    163: html_reset_internal(struct html *h)
1.1       schwarze  164: {
                    165:        struct tag      *tag;
1.140     schwarze  166:        struct id_entry *entry;
1.99      schwarze  167:        unsigned int     slot;
1.1       schwarze  168:
1.74      schwarze  169:        while ((tag = h->tag) != NULL) {
                    170:                h->tag = tag->next;
1.1       schwarze  171:                free(tag);
                    172:        }
1.140     schwarze  173:        entry = ohash_first(&id_unique, &slot);
                    174:        while (entry != NULL) {
                    175:                free(entry);
                    176:                entry = ohash_next(&id_unique, &slot);
1.99      schwarze  177:        }
                    178:        ohash_delete(&id_unique);
1.124     schwarze  179: }
                    180:
                    181: void
                    182: html_reset(void *p)
                    183: {
                    184:        html_reset_internal(p);
1.140     schwarze  185:        mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id));
1.124     schwarze  186: }
                    187:
                    188: void
                    189: html_free(void *p)
                    190: {
                    191:        html_reset_internal(p);
                    192:        free(p);
1.1       schwarze  193: }
                    194:
                    195: void
                    196: print_gen_head(struct html *h)
                    197: {
1.42      schwarze  198:        struct tag      *t;
                    199:
1.64      schwarze  200:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.144     anton     201:        print_otag(h, TAG_META, "??", "name", "viewport",
                    202:            "content", "width=device-width, initial-scale=1.0");
1.92      schwarze  203:        if (h->style != NULL) {
                    204:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    205:                    h->style, "type", "text/css", "media", "all");
                    206:                return;
                    207:        }
1.1       schwarze  208:
1.42      schwarze  209:        /*
1.92      schwarze  210:         * Print a minimal embedded style sheet.
1.42      schwarze  211:         */
1.66      schwarze  212:
1.64      schwarze  213:        t = print_otag(h, TAG_STYLE, "");
1.66      schwarze  214:        print_text(h, "table.head, table.foot { width: 100%; }");
1.67      schwarze  215:        print_endline(h);
1.66      schwarze  216:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.67      schwarze  217:        print_endline(h);
1.66      schwarze  218:        print_text(h, "td.head-vol { text-align: center; }");
1.67      schwarze  219:        print_endline(h);
1.126     schwarze  220:        print_text(h, ".Nd, .Bf, .Op { display: inline; }");
1.95      schwarze  221:        print_endline(h);
1.126     schwarze  222:        print_text(h, ".Pa, .Ad { font-style: italic; }");
1.96      schwarze  223:        print_endline(h);
1.126     schwarze  224:        print_text(h, ".Ms { font-weight: bold; }");
1.98      schwarze  225:        print_endline(h);
1.126     schwarze  226:        print_text(h, ".Bl-diag ");
1.94      schwarze  227:        print_byte(h, '>');
                    228:        print_text(h, " dt { font-weight: bold; }");
1.93      schwarze  229:        print_endline(h);
1.126     schwarze  230:        print_text(h, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
                    231:            "{ font-weight: bold; font-family: inherit; }");
1.42      schwarze  232:        print_tagq(h, t);
1.1       schwarze  233: }
                    234:
1.125     schwarze  235: int
                    236: html_setfont(struct html *h, enum mandoc_esc font)
1.5       schwarze  237: {
1.125     schwarze  238:        switch (font) {
1.35      schwarze  239:        case ESCAPE_FONTPREV:
1.5       schwarze  240:                font = h->metal;
                    241:                break;
1.35      schwarze  242:        case ESCAPE_FONTITALIC:
                    243:        case ESCAPE_FONTBOLD:
                    244:        case ESCAPE_FONTBI:
1.125     schwarze  245:        case ESCAPE_FONTROMAN:
1.145     schwarze  246:        case ESCAPE_FONTCR:
                    247:        case ESCAPE_FONTCB:
                    248:        case ESCAPE_FONTCI:
1.112     schwarze  249:                break;
1.35      schwarze  250:        case ESCAPE_FONT:
1.125     schwarze  251:                font = ESCAPE_FONTROMAN;
1.5       schwarze  252:                break;
                    253:        default:
1.125     schwarze  254:                return 0;
1.1       schwarze  255:        }
1.125     schwarze  256:        h->metal = h->metac;
                    257:        h->metac = font;
                    258:        return 1;
                    259: }
1.1       schwarze  260:
1.125     schwarze  261: static void
                    262: print_metaf(struct html *h)
                    263: {
1.20      schwarze  264:        if (h->metaf) {
                    265:                print_tagq(h, h->metaf);
                    266:                h->metaf = NULL;
                    267:        }
1.125     schwarze  268:        switch (h->metac) {
                    269:        case ESCAPE_FONTITALIC:
1.64      schwarze  270:                h->metaf = print_otag(h, TAG_I, "");
1.31      schwarze  271:                break;
1.125     schwarze  272:        case ESCAPE_FONTBOLD:
1.64      schwarze  273:                h->metaf = print_otag(h, TAG_B, "");
1.31      schwarze  274:                break;
1.125     schwarze  275:        case ESCAPE_FONTBI:
1.64      schwarze  276:                h->metaf = print_otag(h, TAG_B, "");
                    277:                print_otag(h, TAG_I, "");
1.31      schwarze  278:                break;
1.145     schwarze  279:        case ESCAPE_FONTCR:
1.112     schwarze  280:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
                    281:                break;
1.145     schwarze  282:        case ESCAPE_FONTCB:
                    283:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
                    284:                print_otag(h, TAG_B, "");
                    285:                break;
                    286:        case ESCAPE_FONTCI:
                    287:                h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
                    288:                print_otag(h, TAG_I, "");
                    289:                break;
1.31      schwarze  290:        default:
                    291:                break;
                    292:        }
1.118     schwarze  293: }
                    294:
1.119     schwarze  295: void
                    296: html_close_paragraph(struct html *h)
                    297: {
1.129     schwarze  298:        struct tag      *this, *next;
1.130     schwarze  299:        int              flags;
1.119     schwarze  300:
1.129     schwarze  301:        this = h->tag;
                    302:        for (;;) {
                    303:                next = this->next;
1.130     schwarze  304:                flags = htmltags[this->tag].flags;
                    305:                if (flags & (HTML_INPHRASE | HTML_TOPHRASE))
1.129     schwarze  306:                        print_ctag(h, this);
1.130     schwarze  307:                if ((flags & HTML_INPHRASE) == 0)
1.119     schwarze  308:                        break;
1.129     schwarze  309:                this = next;
1.119     schwarze  310:        }
                    311: }
                    312:
1.118     schwarze  313: /*
                    314:  * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
                    315:  * TOKEN_NONE does not switch.  The old mode is returned.
                    316:  */
                    317: enum roff_tok
                    318: html_fillmode(struct html *h, enum roff_tok want)
                    319: {
                    320:        struct tag      *t;
                    321:        enum roff_tok    had;
                    322:
                    323:        for (t = h->tag; t != NULL; t = t->next)
                    324:                if (t->tag == TAG_PRE)
                    325:                        break;
                    326:
                    327:        had = t == NULL ? ROFF_fi : ROFF_nf;
                    328:
                    329:        if (want != had) {
                    330:                switch (want) {
                    331:                case ROFF_fi:
                    332:                        print_tagq(h, t);
                    333:                        break;
                    334:                case ROFF_nf:
1.119     schwarze  335:                        html_close_paragraph(h);
1.118     schwarze  336:                        print_otag(h, TAG_PRE, "");
                    337:                        break;
                    338:                case TOKEN_NONE:
                    339:                        break;
                    340:                default:
                    341:                        abort();
                    342:                }
                    343:        }
                    344:        return had;
1.80      schwarze  345: }
                    346:
1.135     schwarze  347: /*
                    348:  * Allocate a string to be used for the "id=" attribute of an HTML
                    349:  * element and/or as a segment identifier for a URI in an <a> element.
                    350:  * The function may fail and return NULL if the node lacks text data
                    351:  * to create the attribute from.
1.140     schwarze  352:  * The caller is responsible for free(3)ing the returned string.
                    353:  *
1.135     schwarze  354:  * If the "unique" argument is non-zero, the "id_unique" ohash table
1.140     schwarze  355:  * is used for de-duplication.  If the "unique" argument is 1,
                    356:  * it is the first time the function is called for this tag and
                    357:  * location, so if an ordinal suffix is needed, it is incremented.
                    358:  * If the "unique" argument is 2, it is the second time the function
                    359:  * is called for this tag and location, so the ordinal suffix
                    360:  * remains unchanged.
1.135     schwarze  361:  */
1.80      schwarze  362: char *
1.99      schwarze  363: html_make_id(const struct roff_node *n, int unique)
1.80      schwarze  364: {
                    365:        const struct roff_node  *nch;
1.140     schwarze  366:        struct id_entry         *entry;
                    367:        char                    *buf, *cp;
                    368:        size_t                   len;
1.99      schwarze  369:        unsigned int             slot;
1.80      schwarze  370:
1.138     schwarze  371:        if (n->tag != NULL)
                    372:                buf = mandoc_strdup(n->tag);
1.135     schwarze  373:        else {
                    374:                switch (n->tok) {
                    375:                case MDOC_Sh:
                    376:                case MDOC_Ss:
                    377:                case MDOC_Sx:
                    378:                case MAN_SH:
                    379:                case MAN_SS:
                    380:                        for (nch = n->child; nch != NULL; nch = nch->next)
                    381:                                if (nch->type != ROFFT_TEXT)
                    382:                                        return NULL;
                    383:                        buf = NULL;
                    384:                        deroff(&buf, n);
                    385:                        if (buf == NULL)
                    386:                                return NULL;
                    387:                        break;
                    388:                default:
1.136     schwarze  389:                        if (n->child == NULL || n->child->type != ROFFT_TEXT)
1.135     schwarze  390:                                return NULL;
                    391:                        buf = mandoc_strdup(n->child->string);
                    392:                        break;
                    393:                }
                    394:        }
1.80      schwarze  395:
1.100     schwarze  396:        /*
                    397:         * In ID attributes, only use ASCII characters that are
                    398:         * permitted in URL-fragment strings according to the
                    399:         * explicit list at:
                    400:         * https://url.spec.whatwg.org/#url-fragment-string
1.141     schwarze  401:         * In addition, reserve '~' for ordinal suffixes.
1.100     schwarze  402:         */
1.80      schwarze  403:
                    404:        for (cp = buf; *cp != '\0'; cp++)
1.100     schwarze  405:                if (isalnum((unsigned char)*cp) == 0 &&
1.141     schwarze  406:                    strchr("!$&'()*+,-./:;=?@_", *cp) == NULL)
1.80      schwarze  407:                        *cp = '_';
                    408:
1.99      schwarze  409:        if (unique == 0)
                    410:                return buf;
                    411:
                    412:        /* Avoid duplicate HTML id= attributes. */
                    413:
                    414:        slot = ohash_qlookup(&id_unique, buf);
1.140     schwarze  415:        if ((entry = ohash_find(&id_unique, slot)) == NULL) {
                    416:                len = strlen(buf) + 1;
                    417:                entry = mandoc_malloc(sizeof(*entry) + len);
                    418:                entry->ord = 1;
                    419:                memcpy(entry->id, buf, len);
                    420:                ohash_insert(&id_unique, slot, entry);
                    421:        } else if (unique == 1)
                    422:                entry->ord++;
                    423:
                    424:        if (entry->ord > 1) {
                    425:                cp = buf;
1.141     schwarze  426:                mandoc_asprintf(&buf, "%s~%d", cp, entry->ord);
1.140     schwarze  427:                free(cp);
1.99      schwarze  428:        }
1.80      schwarze  429:        return buf;
1.1       schwarze  430: }
                    431:
1.5       schwarze  432: static int
1.67      schwarze  433: print_escape(struct html *h, char c)
1.38      schwarze  434: {
                    435:
                    436:        switch (c) {
                    437:        case '<':
1.67      schwarze  438:                print_word(h, "&lt;");
1.38      schwarze  439:                break;
                    440:        case '>':
1.67      schwarze  441:                print_word(h, "&gt;");
1.38      schwarze  442:                break;
                    443:        case '&':
1.67      schwarze  444:                print_word(h, "&amp;");
1.38      schwarze  445:                break;
                    446:        case '"':
1.67      schwarze  447:                print_word(h, "&quot;");
1.38      schwarze  448:                break;
                    449:        case ASCII_NBRSP:
1.67      schwarze  450:                print_word(h, "&nbsp;");
1.38      schwarze  451:                break;
                    452:        case ASCII_HYPH:
1.67      schwarze  453:                print_byte(h, '-');
1.59      schwarze  454:                break;
1.38      schwarze  455:        case ASCII_BREAK:
                    456:                break;
                    457:        default:
1.58      schwarze  458:                return 0;
1.38      schwarze  459:        }
1.58      schwarze  460:        return 1;
1.38      schwarze  461: }
                    462:
                    463: static int
1.65      schwarze  464: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.1       schwarze  465: {
1.67      schwarze  466:        char             numbuf[16];
1.84      schwarze  467:        const char      *seq;
1.4       schwarze  468:        size_t           sz;
1.84      schwarze  469:        int              c, len, breakline, nospace;
1.26      schwarze  470:        enum mandoc_esc  esc;
1.84      schwarze  471:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.33      schwarze  472:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5       schwarze  473:
1.65      schwarze  474:        if (pend == NULL)
                    475:                pend = strchr(p, '\0');
                    476:
1.84      schwarze  477:        breakline = 0;
1.5       schwarze  478:        nospace = 0;
1.1       schwarze  479:
1.65      schwarze  480:        while (p < pend) {
1.30      schwarze  481:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    482:                        h->flags &= ~HTML_SKIPCHAR;
                    483:                        p++;
                    484:                        continue;
                    485:                }
                    486:
1.67      schwarze  487:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.84      schwarze  488:                        print_byte(h, *p);
                    489:
                    490:                if (breakline &&
                    491:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.115     schwarze  492:                        print_otag(h, TAG_BR, "");
1.84      schwarze  493:                        breakline = 0;
                    494:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    495:                                p++;
                    496:                        continue;
                    497:                }
1.4       schwarze  498:
1.65      schwarze  499:                if (p >= pend)
1.26      schwarze  500:                        break;
                    501:
1.84      schwarze  502:                if (*p == ' ') {
                    503:                        print_endword(h);
                    504:                        p++;
                    505:                        continue;
                    506:                }
                    507:
1.67      schwarze  508:                if (print_escape(h, *p++))
1.33      schwarze  509:                        continue;
1.4       schwarze  510:
1.26      schwarze  511:                esc = mandoc_escape(&p, &seq, &len);
                    512:                switch (esc) {
1.35      schwarze  513:                case ESCAPE_FONT:
                    514:                case ESCAPE_FONTPREV:
                    515:                case ESCAPE_FONTBOLD:
                    516:                case ESCAPE_FONTITALIC:
                    517:                case ESCAPE_FONTBI:
                    518:                case ESCAPE_FONTROMAN:
1.145     schwarze  519:                case ESCAPE_FONTCR:
                    520:                case ESCAPE_FONTCB:
                    521:                case ESCAPE_FONTCI:
1.113     schwarze  522:                        if (0 == norecurse) {
                    523:                                h->flags |= HTML_NOSPACE;
1.125     schwarze  524:                                if (html_setfont(h, esc))
                    525:                                        print_metaf(h);
1.113     schwarze  526:                                h->flags &= ~HTML_NOSPACE;
                    527:                        }
1.30      schwarze  528:                        continue;
1.35      schwarze  529:                case ESCAPE_SKIPCHAR:
1.30      schwarze  530:                        h->flags |= HTML_SKIPCHAR;
                    531:                        continue;
1.116     schwarze  532:                case ESCAPE_ERROR:
                    533:                        continue;
1.30      schwarze  534:                default:
                    535:                        break;
                    536:                }
                    537:
                    538:                if (h->flags & HTML_SKIPCHAR) {
                    539:                        h->flags &= ~HTML_SKIPCHAR;
                    540:                        continue;
                    541:                }
                    542:
                    543:                switch (esc) {
1.35      schwarze  544:                case ESCAPE_UNICODE:
1.38      schwarze  545:                        /* Skip past "u" header. */
1.26      schwarze  546:                        c = mchars_num2uc(seq + 1, len - 1);
                    547:                        break;
1.35      schwarze  548:                case ESCAPE_NUMBERED:
1.26      schwarze  549:                        c = mchars_num2char(seq, len);
1.51      schwarze  550:                        if (c < 0)
                    551:                                continue;
1.26      schwarze  552:                        break;
1.35      schwarze  553:                case ESCAPE_SPECIAL:
1.61      schwarze  554:                        c = mchars_spec2cp(seq, len);
1.51      schwarze  555:                        if (c <= 0)
                    556:                                continue;
1.116     schwarze  557:                        break;
                    558:                case ESCAPE_UNDEF:
                    559:                        c = *seq;
1.26      schwarze  560:                        break;
1.109     schwarze  561:                case ESCAPE_DEVICE:
                    562:                        print_word(h, "html");
                    563:                        continue;
1.84      schwarze  564:                case ESCAPE_BREAK:
                    565:                        breakline = 1;
                    566:                        continue;
1.35      schwarze  567:                case ESCAPE_NOSPACE:
1.26      schwarze  568:                        if ('\0' == *p)
                    569:                                nospace = 1;
1.49      schwarze  570:                        continue;
1.55      schwarze  571:                case ESCAPE_OVERSTRIKE:
                    572:                        if (len == 0)
                    573:                                continue;
                    574:                        c = seq[len - 1];
                    575:                        break;
1.5       schwarze  576:                default:
1.49      schwarze  577:                        continue;
1.5       schwarze  578:                }
1.51      schwarze  579:                if ((c < 0x20 && c != 0x09) ||
                    580:                    (c > 0x7E && c < 0xA0))
1.49      schwarze  581:                        c = 0xFFFD;
1.67      schwarze  582:                if (c > 0x7E) {
1.86      bentley   583:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.67      schwarze  584:                        print_word(h, numbuf);
                    585:                } else if (print_escape(h, c) == 0)
                    586:                        print_byte(h, c);
1.1       schwarze  587:        }
1.5       schwarze  588:
1.58      schwarze  589:        return nospace;
1.1       schwarze  590: }
                    591:
1.6       schwarze  592: static void
1.65      schwarze  593: print_href(struct html *h, const char *name, const char *sec, int man)
1.6       schwarze  594: {
1.110     schwarze  595:        struct stat      sb;
1.65      schwarze  596:        const char      *p, *pp;
1.110     schwarze  597:        char            *filename;
                    598:
                    599:        if (man) {
                    600:                pp = h->base_man1;
                    601:                if (h->base_man2 != NULL) {
                    602:                        mandoc_asprintf(&filename, "%s.%s", name, sec);
                    603:                        if (stat(filename, &sb) == -1)
                    604:                                pp = h->base_man2;
                    605:                        free(filename);
                    606:                }
                    607:        } else
                    608:                pp = h->base_includes;
1.65      schwarze  609:
                    610:        while ((p = strchr(pp, '%')) != NULL) {
                    611:                print_encode(h, pp, p, 1);
                    612:                if (man && p[1] == 'S') {
                    613:                        if (sec == NULL)
1.67      schwarze  614:                                print_byte(h, '1');
1.65      schwarze  615:                        else
                    616:                                print_encode(h, sec, NULL, 1);
                    617:                } else if ((man && p[1] == 'N') ||
                    618:                    (man == 0 && p[1] == 'I'))
                    619:                        print_encode(h, name, NULL, 1);
                    620:                else
                    621:                        print_encode(h, p, p + 2, 1);
                    622:                pp = p + 2;
                    623:        }
                    624:        if (*pp != '\0')
                    625:                print_encode(h, pp, NULL, 1);
1.6       schwarze  626: }
                    627:
1.1       schwarze  628: struct tag *
1.64      schwarze  629: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.1       schwarze  630: {
1.64      schwarze  631:        va_list          ap;
1.1       schwarze  632:        struct tag      *t;
1.65      schwarze  633:        const char      *attr;
1.73      schwarze  634:        char            *arg1, *arg2;
1.114     schwarze  635:        int              style_written, tflags;
1.66      schwarze  636:
                    637:        tflags = htmltags[tag].flags;
1.1       schwarze  638:
1.127     schwarze  639:        /* Flow content is not allowed in phrasing context. */
                    640:
                    641:        if ((tflags & HTML_INPHRASE) == 0) {
                    642:                for (t = h->tag; t != NULL; t = t->next) {
                    643:                        if (t->closed)
                    644:                                continue;
                    645:                        assert((htmltags[t->tag].flags & HTML_TOPHRASE) == 0);
                    646:                        break;
                    647:                }
1.131     schwarze  648:
                    649:        /*
                    650:         * Always wrap phrasing elements in a paragraph
                    651:         * unless already contained in some flow container;
                    652:         * never put them directly into a section.
                    653:         */
                    654:
                    655:        } else if (tflags & HTML_TOPHRASE && h->tag->tag == TAG_SECTION)
                    656:                print_otag(h, TAG_P, "c", "Pp");
1.127     schwarze  657:
1.74      schwarze  658:        /* Push this tag onto the stack of open scopes. */
1.6       schwarze  659:
1.66      schwarze  660:        if ((tflags & HTML_NOSTACK) == 0) {
1.24      schwarze  661:                t = mandoc_malloc(sizeof(struct tag));
1.1       schwarze  662:                t->tag = tag;
1.74      schwarze  663:                t->next = h->tag;
1.122     schwarze  664:                t->refcnt = 0;
                    665:                t->closed = 0;
1.74      schwarze  666:                h->tag = t;
1.1       schwarze  667:        } else
                    668:                t = NULL;
                    669:
1.66      schwarze  670:        if (tflags & HTML_NLBEFORE)
1.67      schwarze  671:                print_endline(h);
                    672:        if (h->col == 0)
                    673:                print_indent(h);
1.66      schwarze  674:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    675:                if (h->flags & HTML_KEEP)
1.86      bentley   676:                        print_word(h, "&#x00A0;");
1.66      schwarze  677:                else {
                    678:                        if (h->flags & HTML_PREKEEP)
                    679:                                h->flags |= HTML_KEEP;
1.67      schwarze  680:                        print_endword(h);
1.12      schwarze  681:                }
1.66      schwarze  682:        }
1.1       schwarze  683:
1.13      schwarze  684:        if ( ! (h->flags & HTML_NONOSPACE))
                    685:                h->flags &= ~HTML_NOSPACE;
1.14      schwarze  686:        else
                    687:                h->flags |= HTML_NOSPACE;
1.13      schwarze  688:
1.6       schwarze  689:        /* Print out the tag name and attributes. */
                    690:
1.67      schwarze  691:        print_byte(h, '<');
                    692:        print_word(h, htmltags[tag].name);
1.64      schwarze  693:
                    694:        va_start(ap, fmt);
                    695:
1.114     schwarze  696:        while (*fmt != '\0' && *fmt != 's') {
1.73      schwarze  697:
1.108     schwarze  698:                /* Parse attributes and arguments. */
1.73      schwarze  699:
                    700:                arg1 = va_arg(ap, char *);
1.108     schwarze  701:                arg2 = NULL;
1.64      schwarze  702:                switch (*fmt++) {
                    703:                case 'c':
1.65      schwarze  704:                        attr = "class";
1.64      schwarze  705:                        break;
                    706:                case 'h':
1.65      schwarze  707:                        attr = "href";
1.64      schwarze  708:                        break;
                    709:                case 'i':
1.65      schwarze  710:                        attr = "id";
1.147     schwarze  711:                        break;
                    712:                case 'r':
                    713:                        attr = "role";
1.64      schwarze  714:                        break;
                    715:                case '?':
1.73      schwarze  716:                        attr = arg1;
                    717:                        arg1 = va_arg(ap, char *);
1.64      schwarze  718:                        break;
                    719:                default:
                    720:                        abort();
                    721:                }
1.73      schwarze  722:                if (*fmt == 'M')
                    723:                        arg2 = va_arg(ap, char *);
                    724:                if (arg1 == NULL)
                    725:                        continue;
                    726:
1.108     schwarze  727:                /* Print the attributes. */
1.73      schwarze  728:
1.67      schwarze  729:                print_byte(h, ' ');
                    730:                print_word(h, attr);
                    731:                print_byte(h, '=');
                    732:                print_byte(h, '"');
1.65      schwarze  733:                switch (*fmt) {
1.78      schwarze  734:                case 'I':
                    735:                        print_href(h, arg1, NULL, 0);
                    736:                        fmt++;
                    737:                        break;
1.65      schwarze  738:                case 'M':
1.73      schwarze  739:                        print_href(h, arg1, arg2, 1);
1.65      schwarze  740:                        fmt++;
                    741:                        break;
1.78      schwarze  742:                case 'R':
                    743:                        print_byte(h, '#');
                    744:                        print_encode(h, arg1, NULL, 1);
1.65      schwarze  745:                        fmt++;
1.78      schwarze  746:                        break;
1.65      schwarze  747:                default:
1.114     schwarze  748:                        print_encode(h, arg1, NULL, 1);
1.65      schwarze  749:                        break;
                    750:                }
1.67      schwarze  751:                print_byte(h, '"');
1.64      schwarze  752:        }
1.114     schwarze  753:
                    754:        style_written = 0;
                    755:        while (*fmt++ == 's') {
                    756:                arg1 = va_arg(ap, char *);
                    757:                arg2 = va_arg(ap, char *);
                    758:                if (arg2 == NULL)
                    759:                        continue;
                    760:                print_byte(h, ' ');
                    761:                if (style_written == 0) {
                    762:                        print_word(h, "style=\"");
                    763:                        style_written = 1;
                    764:                }
                    765:                print_word(h, arg1);
                    766:                print_byte(h, ':');
                    767:                print_byte(h, ' ');
                    768:                print_word(h, arg2);
                    769:                print_byte(h, ';');
                    770:        }
                    771:        if (style_written)
                    772:                print_byte(h, '"');
                    773:
1.64      schwarze  774:        va_end(ap);
1.6       schwarze  775:
1.42      schwarze  776:        /* Accommodate for "well-formed" singleton escaping. */
1.6       schwarze  777:
1.127     schwarze  778:        if (htmltags[tag].flags & HTML_NOSTACK)
1.67      schwarze  779:                print_byte(h, '/');
1.6       schwarze  780:
1.67      schwarze  781:        print_byte(h, '>');
1.1       schwarze  782:
1.66      schwarze  783:        if (tflags & HTML_NLBEGIN)
1.67      schwarze  784:                print_endline(h);
1.66      schwarze  785:        else
                    786:                h->flags |= HTML_NOSPACE;
1.18      schwarze  787:
1.66      schwarze  788:        if (tflags & HTML_INDENT)
                    789:                h->indent++;
                    790:        if (tflags & HTML_NOINDENT)
                    791:                h->noindent++;
1.18      schwarze  792:
1.58      schwarze  793:        return t;
1.135     schwarze  794: }
                    795:
                    796: /*
                    797:  * Print an element with an optional "id=" attribute.
1.136     schwarze  798:  * If the element has phrasing content and an "id=" attribute,
                    799:  * also add a permalink: outside if it can be in phrasing context,
                    800:  * inside otherwise.
1.135     schwarze  801:  */
                    802: struct tag *
                    803: print_otag_id(struct html *h, enum htmltag elemtype, const char *cattr,
                    804:     struct roff_node *n)
                    805: {
1.136     schwarze  806:        struct roff_node *nch;
1.135     schwarze  807:        struct tag      *ret, *t;
1.137     schwarze  808:        char            *id, *href;
1.135     schwarze  809:
                    810:        ret = NULL;
1.137     schwarze  811:        id = href = NULL;
1.135     schwarze  812:        if (n->flags & NODE_ID)
                    813:                id = html_make_id(n, 1);
1.137     schwarze  814:        if (n->flags & NODE_HREF)
1.140     schwarze  815:                href = id == NULL ? html_make_id(n, 2) : id;
1.137     schwarze  816:        if (href != NULL && htmltags[elemtype].flags & HTML_INPHRASE)
                    817:                ret = print_otag(h, TAG_A, "chR", "permalink", href);
1.135     schwarze  818:        t = print_otag(h, elemtype, "ci", cattr, id);
                    819:        if (ret == NULL) {
                    820:                ret = t;
1.137     schwarze  821:                if (href != NULL && (nch = n->child) != NULL) {
1.136     schwarze  822:                        /* man(7) is safe, it tags phrasing content only. */
                    823:                        if (n->tok > MDOC_MAX ||
                    824:                            htmltags[elemtype].flags & HTML_TOPHRASE)
                    825:                                nch = NULL;
                    826:                        else  /* For mdoc(7), beware of nested blocks. */
                    827:                                while (nch != NULL && nch->type == ROFFT_TEXT)
                    828:                                        nch = nch->next;
                    829:                        if (nch == NULL)
1.137     schwarze  830:                                print_otag(h, TAG_A, "chR", "permalink", href);
1.136     schwarze  831:                }
1.135     schwarze  832:        }
1.140     schwarze  833:        free(id);
1.137     schwarze  834:        if (id == NULL)
                    835:                free(href);
1.135     schwarze  836:        return ret;
1.1       schwarze  837: }
                    838:
                    839: static void
1.54      schwarze  840: print_ctag(struct html *h, struct tag *tag)
1.1       schwarze  841: {
1.66      schwarze  842:        int      tflags;
1.35      schwarze  843:
1.122     schwarze  844:        if (tag->closed == 0) {
                    845:                tag->closed = 1;
                    846:                if (tag == h->metaf)
                    847:                        h->metaf = NULL;
                    848:                if (tag == h->tblt)
                    849:                        h->tblt = NULL;
                    850:
                    851:                tflags = htmltags[tag->tag].flags;
                    852:                if (tflags & HTML_INDENT)
                    853:                        h->indent--;
                    854:                if (tflags & HTML_NOINDENT)
                    855:                        h->noindent--;
                    856:                if (tflags & HTML_NLEND)
                    857:                        print_endline(h);
                    858:                print_indent(h);
                    859:                print_byte(h, '<');
                    860:                print_byte(h, '/');
                    861:                print_word(h, htmltags[tag->tag].name);
                    862:                print_byte(h, '>');
                    863:                if (tflags & HTML_NLAFTER)
                    864:                        print_endline(h);
                    865:        }
                    866:        if (tag->refcnt == 0) {
                    867:                h->tag = tag->next;
                    868:                free(tag);
                    869:        }
1.1       schwarze  870: }
                    871:
                    872: void
1.6       schwarze  873: print_gen_decls(struct html *h)
                    874: {
1.67      schwarze  875:        print_word(h, "<!DOCTYPE html>");
                    876:        print_endline(h);
1.91      schwarze  877: }
                    878:
                    879: void
                    880: print_gen_comment(struct html *h, struct roff_node *n)
                    881: {
                    882:        int      wantblank;
                    883:
                    884:        print_word(h, "<!-- This is an automatically generated file."
                    885:            "  Do not edit.");
                    886:        h->indent = 1;
                    887:        wantblank = 0;
                    888:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    889:                if (strstr(n->string, "-->") == NULL &&
                    890:                    (wantblank || *n->string != '\0')) {
                    891:                        print_endline(h);
                    892:                        print_indent(h);
                    893:                        print_word(h, n->string);
                    894:                        wantblank = *n->string != '\0';
                    895:                }
                    896:                n = n->next;
                    897:        }
                    898:        if (wantblank)
                    899:                print_endline(h);
                    900:        print_word(h, " -->");
                    901:        print_endline(h);
                    902:        h->indent = 0;
1.1       schwarze  903: }
                    904:
                    905: void
1.12      schwarze  906: print_text(struct html *h, const char *word)
1.1       schwarze  907: {
1.139     schwarze  908:        print_tagged_text(h, word, NULL);
                    909: }
                    910:
                    911: void
                    912: print_tagged_text(struct html *h, const char *word, struct roff_node *n)
                    913: {
                    914:        struct tag      *t;
                    915:        char            *href;
                    916:
1.131     schwarze  917:        /*
                    918:         * Always wrap text in a paragraph unless already contained in
                    919:         * some flow container; never put it directly into a section.
                    920:         */
                    921:
                    922:        if (h->tag->tag == TAG_SECTION)
                    923:                print_otag(h, TAG_P, "c", "Pp");
                    924:
                    925:        /* Output whitespace before this text? */
                    926:
1.67      schwarze  927:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.12      schwarze  928:                if ( ! (HTML_KEEP & h->flags)) {
                    929:                        if (HTML_PREKEEP & h->flags)
                    930:                                h->flags |= HTML_KEEP;
1.67      schwarze  931:                        print_endword(h);
1.12      schwarze  932:                } else
1.86      bentley   933:                        print_word(h, "&#x00A0;");
1.12      schwarze  934:        }
1.131     schwarze  935:
                    936:        /*
1.139     schwarze  937:         * Optionally switch fonts, optionally write a permalink, then
                    938:         * print the text, optionally surrounded by HTML whitespace.
1.131     schwarze  939:         */
1.1       schwarze  940:
1.125     schwarze  941:        assert(h->metaf == NULL);
                    942:        print_metaf(h);
                    943:        print_indent(h);
1.139     schwarze  944:
1.140     schwarze  945:        if (n != NULL && (href = html_make_id(n, 2)) != NULL) {
1.139     schwarze  946:                t = print_otag(h, TAG_A, "chR", "permalink", href);
                    947:                free(href);
                    948:        } else
                    949:                t = NULL;
                    950:
1.65      schwarze  951:        if ( ! print_encode(h, word, NULL, 0)) {
1.13      schwarze  952:                if ( ! (h->flags & HTML_NONOSPACE))
                    953:                        h->flags &= ~HTML_NOSPACE;
1.53      schwarze  954:                h->flags &= ~HTML_NONEWLINE;
1.28      schwarze  955:        } else
1.53      schwarze  956:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.20      schwarze  957:
1.125     schwarze  958:        if (h->metaf != NULL) {
1.20      schwarze  959:                print_tagq(h, h->metaf);
                    960:                h->metaf = NULL;
1.139     schwarze  961:        } else if (t != NULL)
                    962:                print_tagq(h, t);
1.17      schwarze  963:
                    964:        h->flags &= ~HTML_IGNDELIM;
1.1       schwarze  965: }
                    966:
                    967: void
                    968: print_tagq(struct html *h, const struct tag *until)
                    969: {
1.122     schwarze  970:        struct tag      *this, *next;
1.1       schwarze  971:
1.122     schwarze  972:        for (this = h->tag; this != NULL; this = next) {
                    973:                next = this == until ? NULL : this->next;
                    974:                print_ctag(h, this);
1.1       schwarze  975:        }
                    976: }
                    977:
1.120     schwarze  978: /*
                    979:  * Close out all open elements up to but excluding suntil.
                    980:  * Note that a paragraph just inside stays open together with it
                    981:  * because paragraphs include subsequent phrasing content.
                    982:  */
1.1       schwarze  983: void
                    984: print_stagq(struct html *h, const struct tag *suntil)
                    985: {
1.122     schwarze  986:        struct tag      *this, *next;
1.1       schwarze  987:
1.122     schwarze  988:        for (this = h->tag; this != NULL; this = next) {
                    989:                next = this->next;
                    990:                if (this == suntil || (next == suntil &&
                    991:                    (this->tag == TAG_P || this->tag == TAG_PRE)))
                    992:                        break;
                    993:                print_ctag(h, this);
1.1       schwarze  994:        }
1.42      schwarze  995: }
                    996:
1.67      schwarze  997:
                    998: /***********************************************************************
                    999:  * Low level output functions.
                   1000:  * They implement line breaking using a short static buffer.
                   1001:  ***********************************************************************/
                   1002:
                   1003: /*
                   1004:  * Buffer one HTML output byte.
                   1005:  * If the buffer is full, flush and deactivate it and start a new line.
                   1006:  * If the buffer is inactive, print directly.
                   1007:  */
                   1008: static void
                   1009: print_byte(struct html *h, char c)
                   1010: {
                   1011:        if ((h->flags & HTML_BUFFER) == 0) {
                   1012:                putchar(c);
                   1013:                h->col++;
                   1014:                return;
                   1015:        }
                   1016:
                   1017:        if (h->col + h->bufcol < sizeof(h->buf)) {
                   1018:                h->buf[h->bufcol++] = c;
                   1019:                return;
                   1020:        }
                   1021:
                   1022:        putchar('\n');
                   1023:        h->col = 0;
                   1024:        print_indent(h);
                   1025:        putchar(' ');
                   1026:        putchar(' ');
                   1027:        fwrite(h->buf, h->bufcol, 1, stdout);
                   1028:        putchar(c);
                   1029:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                   1030:        h->bufcol = 0;
                   1031:        h->flags &= ~HTML_BUFFER;
                   1032: }
                   1033:
1.66      schwarze 1034: /*
                   1035:  * If something was printed on the current output line, end it.
1.67      schwarze 1036:  * Not to be called right after print_indent().
1.66      schwarze 1037:  */
1.72      schwarze 1038: void
1.67      schwarze 1039: print_endline(struct html *h)
1.66      schwarze 1040: {
1.67      schwarze 1041:        if (h->col == 0)
1.66      schwarze 1042:                return;
                   1043:
1.67      schwarze 1044:        if (h->bufcol) {
                   1045:                putchar(' ');
                   1046:                fwrite(h->buf, h->bufcol, 1, stdout);
                   1047:                h->bufcol = 0;
                   1048:        }
1.66      schwarze 1049:        putchar('\n');
1.67      schwarze 1050:        h->col = 0;
                   1051:        h->flags |= HTML_NOSPACE;
                   1052:        h->flags &= ~HTML_BUFFER;
                   1053: }
                   1054:
                   1055: /*
                   1056:  * Flush the HTML output buffer.
                   1057:  * If it is inactive, activate it.
                   1058:  */
                   1059: static void
                   1060: print_endword(struct html *h)
                   1061: {
                   1062:        if (h->noindent) {
                   1063:                print_byte(h, ' ');
                   1064:                return;
                   1065:        }
                   1066:
                   1067:        if ((h->flags & HTML_BUFFER) == 0) {
                   1068:                h->col++;
                   1069:                h->flags |= HTML_BUFFER;
                   1070:        } else if (h->bufcol) {
                   1071:                putchar(' ');
                   1072:                fwrite(h->buf, h->bufcol, 1, stdout);
                   1073:                h->col += h->bufcol + 1;
                   1074:        }
                   1075:        h->bufcol = 0;
1.66      schwarze 1076: }
                   1077:
                   1078: /*
                   1079:  * If at the beginning of a new output line,
                   1080:  * perform indentation and mark the line as containing output.
                   1081:  * Make sure to really produce some output right afterwards,
                   1082:  * but do not use print_otag() for producing it.
                   1083:  */
                   1084: static void
1.67      schwarze 1085: print_indent(struct html *h)
1.66      schwarze 1086: {
1.67      schwarze 1087:        size_t   i;
1.66      schwarze 1088:
1.132     schwarze 1089:        if (h->col || h->noindent)
1.66      schwarze 1090:                return;
                   1091:
1.132     schwarze 1092:        h->col = h->indent * 2;
                   1093:        for (i = 0; i < h->col; i++)
                   1094:                putchar(' ');
1.67      schwarze 1095: }
                   1096:
                   1097: /*
                   1098:  * Print or buffer some characters
                   1099:  * depending on the current HTML output buffer state.
                   1100:  */
                   1101: static void
                   1102: print_word(struct html *h, const char *cp)
                   1103: {
                   1104:        while (*cp != '\0')
                   1105:                print_byte(h, *cp++);
1.3       schwarze 1106: }