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

Annotation of mandoc/html.c, Revision 1.224

1.224   ! schwarze    1: /*     $Id: html.c,v 1.223 2018/05/08 17:52:55 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.29      kristaps   25: #include <stdio.h>
1.63      kristaps   26: #include <stdint.h>
1.1       kristaps   27: #include <stdlib.h>
1.33      kristaps   28: #include <string.h>
1.45      kristaps   29: #include <unistd.h>
1.1       kristaps   30:
1.210     schwarze   31: #include "mandoc_aux.h"
1.100     kristaps   32: #include "mandoc.h"
1.210     schwarze   33: #include "roff.h"
1.58      kristaps   34: #include "out.h"
1.51      kristaps   35: #include "html.h"
1.186     schwarze   36: #include "manconf.h"
1.64      kristaps   37: #include "main.h"
1.63      kristaps   38:
1.29      kristaps   39: struct htmldata {
1.63      kristaps   40:        const char       *name;
1.29      kristaps   41:        int               flags;
1.196     schwarze   42: #define        HTML_NOSTACK     (1 << 0)
                     43: #define        HTML_AUTOCLOSE   (1 << 1)
                     44: #define        HTML_NLBEFORE    (1 << 2)
                     45: #define        HTML_NLBEGIN     (1 << 3)
                     46: #define        HTML_NLEND       (1 << 4)
                     47: #define        HTML_NLAFTER     (1 << 5)
                     48: #define        HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
                     49: #define        HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
                     50: #define        HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
                     51: #define        HTML_INDENT      (1 << 6)
                     52: #define        HTML_NOINDENT    (1 << 7)
1.29      kristaps   53: };
1.7       kristaps   54:
1.29      kristaps   55: static const struct htmldata htmltags[TAG_MAX] = {
1.196     schwarze   56:        {"html",        HTML_NLALL},
                     57:        {"head",        HTML_NLALL | HTML_INDENT},
                     58:        {"body",        HTML_NLALL},
                     59:        {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     60:        {"title",       HTML_NLAROUND},
                     61:        {"div",         HTML_NLAROUND},
                     62:        {"h1",          HTML_NLAROUND},
                     63:        {"h2",          HTML_NLAROUND},
                     64:        {"span",        0},
                     65:        {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     66:        {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     67:        {"a",           0},
                     68:        {"table",       HTML_NLALL | HTML_INDENT},
1.205     schwarze   69:        {"colgroup",    HTML_NLALL | HTML_INDENT},
1.196     schwarze   70:        {"col",         HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
                     71:        {"tr",          HTML_NLALL | HTML_INDENT},
                     72:        {"td",          HTML_NLAROUND},
                     73:        {"li",          HTML_NLAROUND | HTML_INDENT},
                     74:        {"ul",          HTML_NLALL | HTML_INDENT},
                     75:        {"ol",          HTML_NLALL | HTML_INDENT},
                     76:        {"dl",          HTML_NLALL | HTML_INDENT},
                     77:        {"dt",          HTML_NLAROUND},
                     78:        {"dd",          HTML_NLAROUND | HTML_INDENT},
                     79:        {"pre",         HTML_NLALL | HTML_NOINDENT},
1.207     schwarze   80:        {"var",         0},
1.206     schwarze   81:        {"cite",        0},
1.196     schwarze   82:        {"b",           0},
                     83:        {"i",           0},
                     84:        {"code",        0},
                     85:        {"small",       0},
                     86:        {"style",       HTML_NLALL | HTML_INDENT},
                     87:        {"math",        HTML_NLALL | HTML_INDENT},
                     88:        {"mrow",        0},
                     89:        {"mi",          0},
1.215     schwarze   90:        {"mn",          0},
1.196     schwarze   91:        {"mo",          0},
                     92:        {"msup",        0},
                     93:        {"msub",        0},
                     94:        {"msubsup",     0},
                     95:        {"mfrac",       0},
                     96:        {"msqrt",       0},
                     97:        {"mfenced",     0},
                     98:        {"mtable",      0},
                     99:        {"mtr",         0},
                    100:        {"mtd",         0},
                    101:        {"munderover",  0},
                    102:        {"munder",      0},
                    103:        {"mover",       0},
1.90      kristaps  104: };
                    105:
1.140     kristaps  106: static const char      *const roffscales[SCALE_MAX] = {
                    107:        "cm", /* SCALE_CM */
                    108:        "in", /* SCALE_IN */
                    109:        "pc", /* SCALE_PC */
                    110:        "pt", /* SCALE_PT */
                    111:        "em", /* SCALE_EM */
                    112:        "em", /* SCALE_MM */
                    113:        "ex", /* SCALE_EN */
                    114:        "ex", /* SCALE_BU */
                    115:        "em", /* SCALE_VS */
                    116:        "ex", /* SCALE_FS */
                    117: };
                    118:
1.194     schwarze  119: static void     a2width(const char *, struct roffsu *);
1.197     schwarze  120: static void     print_byte(struct html *, char);
                    121: static void     print_endword(struct html *);
                    122: static void     print_indent(struct html *);
                    123: static void     print_word(struct html *, const char *);
                    124:
1.184     schwarze  125: static void     print_ctag(struct html *, struct tag *);
1.197     schwarze  126: static int      print_escape(struct html *, char);
1.195     schwarze  127: static int      print_encode(struct html *, const char *, const char *, int);
                    128: static void     print_href(struct html *, const char *, const char *, int);
1.141     kristaps  129: static void     print_metaf(struct html *, enum mandoc_esc);
1.82      kristaps  130:
1.156     schwarze  131:
1.180     schwarze  132: void *
1.191     schwarze  133: html_alloc(const struct manoutput *outopts)
1.10      kristaps  134: {
1.30      kristaps  135:        struct html     *h;
                    136:
1.128     kristaps  137:        h = mandoc_calloc(1, sizeof(struct html));
1.10      kristaps  138:
1.204     schwarze  139:        h->tag = NULL;
1.186     schwarze  140:        h->style = outopts->style;
                    141:        h->base_man = outopts->man;
                    142:        h->base_includes = outopts->includes;
                    143:        if (outopts->fragment)
                    144:                h->oflags |= HTML_FRAGMENT;
1.43      kristaps  145:
1.188     schwarze  146:        return h;
1.29      kristaps  147: }
1.10      kristaps  148:
1.29      kristaps  149: void
                    150: html_free(void *p)
                    151: {
1.30      kristaps  152:        struct tag      *tag;
                    153:        struct html     *h;
                    154:
                    155:        h = (struct html *)p;
1.37      kristaps  156:
1.204     schwarze  157:        while ((tag = h->tag) != NULL) {
                    158:                h->tag = tag->next;
1.30      kristaps  159:                free(tag);
                    160:        }
1.53      kristaps  161:
1.30      kristaps  162:        free(h);
1.10      kristaps  163: }
1.2       kristaps  164:
1.51      kristaps  165: void
1.29      kristaps  166: print_gen_head(struct html *h)
                    167: {
1.165     kristaps  168:        struct tag      *t;
1.41      kristaps  169:
1.194     schwarze  170:        print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222     schwarze  171:        if (h->style != NULL) {
                    172:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    173:                    h->style, "type", "text/css", "media", "all");
                    174:                return;
                    175:        }
1.165     kristaps  176:
1.168     kristaps  177:        /*
1.222     schwarze  178:         * Print a minimal embedded style sheet.
1.168     kristaps  179:         */
1.196     schwarze  180:
1.194     schwarze  181:        t = print_otag(h, TAG_STYLE, "");
1.196     schwarze  182:        print_text(h, "table.head, table.foot { width: 100%; }");
1.197     schwarze  183:        print_endline(h);
1.196     schwarze  184:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197     schwarze  185:        print_endline(h);
1.196     schwarze  186:        print_text(h, "td.head-vol { text-align: center; }");
1.197     schwarze  187:        print_endline(h);
1.198     schwarze  188:        print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.224   ! schwarze  189:        print_endline(h);
        !           190:        print_text(h, "dl.Bl-diag ");
        !           191:        print_byte(h, '>');
        !           192:        print_text(h, " dt { font-weight: bold; }");
1.223     schwarze  193:        print_endline(h);
                    194:        print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
                    195:            "code.In, code.Fd, code.Fn,");
                    196:        print_endline(h);
                    197:        print_text(h, "code.Cd { font-weight: bold; "
                    198:            "font-family: inherit; }");
1.165     kristaps  199:        print_tagq(h, t);
1.4       kristaps  200: }
                    201:
1.126     schwarze  202: static void
1.132     kristaps  203: print_metaf(struct html *h, enum mandoc_esc deco)
1.88      kristaps  204: {
1.90      kristaps  205:        enum htmlfont    font;
1.88      kristaps  206:
                    207:        switch (deco) {
1.156     schwarze  208:        case ESCAPE_FONTPREV:
1.90      kristaps  209:                font = h->metal;
1.88      kristaps  210:                break;
1.156     schwarze  211:        case ESCAPE_FONTITALIC:
1.90      kristaps  212:                font = HTMLFONT_ITALIC;
                    213:                break;
1.156     schwarze  214:        case ESCAPE_FONTBOLD:
1.90      kristaps  215:                font = HTMLFONT_BOLD;
1.88      kristaps  216:                break;
1.156     schwarze  217:        case ESCAPE_FONTBI:
1.152     schwarze  218:                font = HTMLFONT_BI;
                    219:                break;
1.156     schwarze  220:        case ESCAPE_FONT:
                    221:        case ESCAPE_FONTROMAN:
1.90      kristaps  222:                font = HTMLFONT_NONE;
1.88      kristaps  223:                break;
                    224:        default:
                    225:                abort();
                    226:        }
                    227:
1.122     kristaps  228:        if (h->metaf) {
                    229:                print_tagq(h, h->metaf);
                    230:                h->metaf = NULL;
                    231:        }
                    232:
                    233:        h->metal = h->metac;
                    234:        h->metac = font;
                    235:
1.152     schwarze  236:        switch (font) {
1.156     schwarze  237:        case HTMLFONT_ITALIC:
1.194     schwarze  238:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  239:                break;
1.156     schwarze  240:        case HTMLFONT_BOLD:
1.194     schwarze  241:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  242:                break;
1.156     schwarze  243:        case HTMLFONT_BI:
1.194     schwarze  244:                h->metaf = print_otag(h, TAG_B, "");
                    245:                print_otag(h, TAG_I, "");
1.152     schwarze  246:                break;
                    247:        default:
                    248:                break;
                    249:        }
1.210     schwarze  250: }
                    251:
                    252: char *
                    253: html_make_id(const struct roff_node *n)
                    254: {
                    255:        const struct roff_node  *nch;
                    256:        char                    *buf, *cp;
                    257:
                    258:        for (nch = n->child; nch != NULL; nch = nch->next)
                    259:                if (nch->type != ROFFT_TEXT)
                    260:                        return NULL;
                    261:
                    262:        buf = NULL;
                    263:        deroff(&buf, n);
1.220     schwarze  264:        if (buf == NULL)
                    265:                return NULL;
1.210     schwarze  266:
                    267:        /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
                    268:
                    269:        for (cp = buf; *cp != '\0'; cp++)
                    270:                if (*cp == ' ')
                    271:                        *cp = '_';
                    272:
                    273:        return buf;
1.88      kristaps  274: }
                    275:
1.138     kristaps  276: int
                    277: html_strlen(const char *cp)
                    278: {
1.151     schwarze  279:        size_t           rsz;
                    280:        int              skip, sz;
1.138     kristaps  281:
                    282:        /*
                    283:         * Account for escaped sequences within string length
                    284:         * calculations.  This follows the logic in term_strlen() as we
                    285:         * must calculate the width of produced strings.
                    286:         * Assume that characters are always width of "1".  This is
                    287:         * hacky, but it gets the job done for approximation of widths.
                    288:         */
                    289:
                    290:        sz = 0;
1.151     schwarze  291:        skip = 0;
                    292:        while (1) {
                    293:                rsz = strcspn(cp, "\\");
                    294:                if (rsz) {
                    295:                        cp += rsz;
                    296:                        if (skip) {
                    297:                                skip = 0;
                    298:                                rsz--;
                    299:                        }
                    300:                        sz += rsz;
                    301:                }
                    302:                if ('\0' == *cp)
                    303:                        break;
                    304:                cp++;
                    305:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.156     schwarze  306:                case ESCAPE_ERROR:
1.188     schwarze  307:                        return sz;
1.156     schwarze  308:                case ESCAPE_UNICODE:
                    309:                case ESCAPE_NUMBERED:
                    310:                case ESCAPE_SPECIAL:
1.185     schwarze  311:                case ESCAPE_OVERSTRIKE:
1.151     schwarze  312:                        if (skip)
                    313:                                skip = 0;
                    314:                        else
                    315:                                sz++;
                    316:                        break;
1.156     schwarze  317:                case ESCAPE_SKIPCHAR:
1.151     schwarze  318:                        skip = 1;
1.138     kristaps  319:                        break;
                    320:                default:
                    321:                        break;
                    322:                }
                    323:        }
1.188     schwarze  324:        return sz;
1.138     kristaps  325: }
1.88      kristaps  326:
1.85      kristaps  327: static int
1.197     schwarze  328: print_escape(struct html *h, char c)
1.159     schwarze  329: {
                    330:
                    331:        switch (c) {
                    332:        case '<':
1.197     schwarze  333:                print_word(h, "&lt;");
1.159     schwarze  334:                break;
                    335:        case '>':
1.197     schwarze  336:                print_word(h, "&gt;");
1.159     schwarze  337:                break;
                    338:        case '&':
1.197     schwarze  339:                print_word(h, "&amp;");
1.159     schwarze  340:                break;
                    341:        case '"':
1.197     schwarze  342:                print_word(h, "&quot;");
1.159     schwarze  343:                break;
                    344:        case ASCII_NBRSP:
1.197     schwarze  345:                print_word(h, "&nbsp;");
1.159     schwarze  346:                break;
                    347:        case ASCII_HYPH:
1.197     schwarze  348:                print_byte(h, '-');
1.189     schwarze  349:                break;
1.159     schwarze  350:        case ASCII_BREAK:
                    351:                break;
                    352:        default:
1.188     schwarze  353:                return 0;
1.159     schwarze  354:        }
1.188     schwarze  355:        return 1;
1.159     schwarze  356: }
                    357:
                    358: static int
1.195     schwarze  359: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29      kristaps  360: {
1.197     schwarze  361:        char             numbuf[16];
1.214     schwarze  362:        struct tag      *t;
                    363:        const char      *seq;
1.77      kristaps  364:        size_t           sz;
1.214     schwarze  365:        int              c, len, breakline, nospace;
1.132     kristaps  366:        enum mandoc_esc  esc;
1.214     schwarze  367:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154     schwarze  368:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  369:
1.195     schwarze  370:        if (pend == NULL)
                    371:                pend = strchr(p, '\0');
                    372:
1.214     schwarze  373:        breakline = 0;
1.85      kristaps  374:        nospace = 0;
                    375:
1.195     schwarze  376:        while (p < pend) {
1.151     schwarze  377:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    378:                        h->flags &= ~HTML_SKIPCHAR;
                    379:                        p++;
                    380:                        continue;
                    381:                }
                    382:
1.197     schwarze  383:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214     schwarze  384:                        print_byte(h, *p);
                    385:
                    386:                if (breakline &&
                    387:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
                    388:                        t = print_otag(h, TAG_DIV, "");
                    389:                        print_text(h, "\\~");
                    390:                        print_tagq(h, t);
                    391:                        breakline = 0;
                    392:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    393:                                p++;
                    394:                        continue;
                    395:                }
1.77      kristaps  396:
1.195     schwarze  397:                if (p >= pend)
1.132     kristaps  398:                        break;
                    399:
1.214     schwarze  400:                if (*p == ' ') {
                    401:                        print_endword(h);
                    402:                        p++;
                    403:                        continue;
                    404:                }
                    405:
1.197     schwarze  406:                if (print_escape(h, *p++))
1.154     schwarze  407:                        continue;
1.77      kristaps  408:
1.132     kristaps  409:                esc = mandoc_escape(&p, &seq, &len);
                    410:                if (ESCAPE_ERROR == esc)
                    411:                        break;
1.82      kristaps  412:
1.132     kristaps  413:                switch (esc) {
1.156     schwarze  414:                case ESCAPE_FONT:
                    415:                case ESCAPE_FONTPREV:
                    416:                case ESCAPE_FONTBOLD:
                    417:                case ESCAPE_FONTITALIC:
                    418:                case ESCAPE_FONTBI:
                    419:                case ESCAPE_FONTROMAN:
1.151     schwarze  420:                        if (0 == norecurse)
                    421:                                print_metaf(h, esc);
                    422:                        continue;
1.156     schwarze  423:                case ESCAPE_SKIPCHAR:
1.151     schwarze  424:                        h->flags |= HTML_SKIPCHAR;
                    425:                        continue;
                    426:                default:
                    427:                        break;
                    428:                }
                    429:
                    430:                if (h->flags & HTML_SKIPCHAR) {
                    431:                        h->flags &= ~HTML_SKIPCHAR;
                    432:                        continue;
                    433:                }
                    434:
                    435:                switch (esc) {
1.156     schwarze  436:                case ESCAPE_UNICODE:
1.159     schwarze  437:                        /* Skip past "u" header. */
1.144     kristaps  438:                        c = mchars_num2uc(seq + 1, len - 1);
                    439:                        break;
1.156     schwarze  440:                case ESCAPE_NUMBERED:
1.141     kristaps  441:                        c = mchars_num2char(seq, len);
1.181     schwarze  442:                        if (c < 0)
                    443:                                continue;
1.82      kristaps  444:                        break;
1.156     schwarze  445:                case ESCAPE_SPECIAL:
1.191     schwarze  446:                        c = mchars_spec2cp(seq, len);
1.181     schwarze  447:                        if (c <= 0)
                    448:                                continue;
1.132     kristaps  449:                        break;
1.214     schwarze  450:                case ESCAPE_BREAK:
                    451:                        breakline = 1;
                    452:                        continue;
1.156     schwarze  453:                case ESCAPE_NOSPACE:
1.132     kristaps  454:                        if ('\0' == *p)
                    455:                                nospace = 1;
1.179     schwarze  456:                        continue;
1.185     schwarze  457:                case ESCAPE_OVERSTRIKE:
                    458:                        if (len == 0)
                    459:                                continue;
                    460:                        c = seq[len - 1];
                    461:                        break;
1.82      kristaps  462:                default:
1.179     schwarze  463:                        continue;
1.82      kristaps  464:                }
1.181     schwarze  465:                if ((c < 0x20 && c != 0x09) ||
                    466:                    (c > 0x7E && c < 0xA0))
1.179     schwarze  467:                        c = 0xFFFD;
1.197     schwarze  468:                if (c > 0x7E) {
1.216     schwarze  469:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197     schwarze  470:                        print_word(h, numbuf);
                    471:                } else if (print_escape(h, c) == 0)
                    472:                        print_byte(h, c);
1.32      kristaps  473:        }
1.85      kristaps  474:
1.188     schwarze  475:        return nospace;
1.14      kristaps  476: }
                    477:
1.94      kristaps  478: static void
1.195     schwarze  479: print_href(struct html *h, const char *name, const char *sec, int man)
1.94      kristaps  480: {
1.195     schwarze  481:        const char      *p, *pp;
                    482:
                    483:        pp = man ? h->base_man : h->base_includes;
                    484:        while ((p = strchr(pp, '%')) != NULL) {
                    485:                print_encode(h, pp, p, 1);
                    486:                if (man && p[1] == 'S') {
                    487:                        if (sec == NULL)
1.197     schwarze  488:                                print_byte(h, '1');
1.195     schwarze  489:                        else
                    490:                                print_encode(h, sec, NULL, 1);
                    491:                } else if ((man && p[1] == 'N') ||
                    492:                    (man == 0 && p[1] == 'I'))
                    493:                        print_encode(h, name, NULL, 1);
                    494:                else
                    495:                        print_encode(h, p, p + 2, 1);
                    496:                pp = p + 2;
                    497:        }
                    498:        if (*pp != '\0')
                    499:                print_encode(h, pp, NULL, 1);
1.94      kristaps  500: }
                    501:
1.51      kristaps  502: struct tag *
1.194     schwarze  503: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14      kristaps  504: {
1.194     schwarze  505:        va_list          ap;
                    506:        struct roffsu    mysu, *su;
1.197     schwarze  507:        char             numbuf[16];
1.30      kristaps  508:        struct tag      *t;
1.195     schwarze  509:        const char      *attr;
1.203     schwarze  510:        char            *arg1, *arg2;
1.195     schwarze  511:        double           v;
1.196     schwarze  512:        int              i, have_style, tflags;
                    513:
                    514:        tflags = htmltags[tag].flags;
1.30      kristaps  515:
1.204     schwarze  516:        /* Push this tag onto the stack of open scopes. */
1.94      kristaps  517:
1.196     schwarze  518:        if ((tflags & HTML_NOSTACK) == 0) {
1.128     kristaps  519:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  520:                t->tag = tag;
1.204     schwarze  521:                t->next = h->tag;
                    522:                h->tag = t;
1.30      kristaps  523:        } else
                    524:                t = NULL;
1.29      kristaps  525:
1.196     schwarze  526:        if (tflags & HTML_NLBEFORE)
1.197     schwarze  527:                print_endline(h);
                    528:        if (h->col == 0)
                    529:                print_indent(h);
1.196     schwarze  530:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    531:                if (h->flags & HTML_KEEP)
1.216     schwarze  532:                        print_word(h, "&#x00A0;");
1.196     schwarze  533:                else {
                    534:                        if (h->flags & HTML_PREKEEP)
                    535:                                h->flags |= HTML_KEEP;
1.197     schwarze  536:                        print_endword(h);
1.105     kristaps  537:                }
1.196     schwarze  538:        }
1.29      kristaps  539:
1.109     kristaps  540:        if ( ! (h->flags & HTML_NONOSPACE))
                    541:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  542:        else
                    543:                h->flags |= HTML_NOSPACE;
1.109     kristaps  544:
1.94      kristaps  545:        /* Print out the tag name and attributes. */
                    546:
1.197     schwarze  547:        print_byte(h, '<');
                    548:        print_word(h, htmltags[tag].name);
1.194     schwarze  549:
                    550:        va_start(ap, fmt);
                    551:
                    552:        have_style = 0;
                    553:        while (*fmt != '\0') {
                    554:                if (*fmt == 's') {
                    555:                        have_style = 1;
                    556:                        fmt++;
                    557:                        break;
                    558:                }
1.203     schwarze  559:
                    560:                /* Parse a non-style attribute and its arguments. */
                    561:
                    562:                arg1 = va_arg(ap, char *);
1.194     schwarze  563:                switch (*fmt++) {
                    564:                case 'c':
1.195     schwarze  565:                        attr = "class";
1.194     schwarze  566:                        break;
                    567:                case 'h':
1.195     schwarze  568:                        attr = "href";
1.194     schwarze  569:                        break;
                    570:                case 'i':
1.195     schwarze  571:                        attr = "id";
1.194     schwarze  572:                        break;
                    573:                case '?':
1.203     schwarze  574:                        attr = arg1;
                    575:                        arg1 = va_arg(ap, char *);
1.194     schwarze  576:                        break;
                    577:                default:
                    578:                        abort();
                    579:                }
1.203     schwarze  580:                arg2 = NULL;
                    581:                if (*fmt == 'M')
                    582:                        arg2 = va_arg(ap, char *);
                    583:                if (arg1 == NULL)
                    584:                        continue;
                    585:
                    586:                /* Print the non-style attributes. */
                    587:
1.197     schwarze  588:                print_byte(h, ' ');
                    589:                print_word(h, attr);
                    590:                print_byte(h, '=');
                    591:                print_byte(h, '"');
1.195     schwarze  592:                switch (*fmt) {
1.208     schwarze  593:                case 'I':
                    594:                        print_href(h, arg1, NULL, 0);
                    595:                        fmt++;
                    596:                        break;
1.195     schwarze  597:                case 'M':
1.203     schwarze  598:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  599:                        fmt++;
                    600:                        break;
1.208     schwarze  601:                case 'R':
                    602:                        print_byte(h, '#');
                    603:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  604:                        fmt++;
                    605:                        break;
1.208     schwarze  606:                case 'T':
                    607:                        print_encode(h, arg1, NULL, 1);
                    608:                        print_word(h, "\" title=\"");
                    609:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  610:                        fmt++;
1.208     schwarze  611:                        break;
1.195     schwarze  612:                default:
1.203     schwarze  613:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  614:                        break;
                    615:                }
1.197     schwarze  616:                print_byte(h, '"');
1.194     schwarze  617:        }
                    618:
                    619:        /* Print out styles. */
                    620:
                    621:        while (*fmt != '\0') {
1.203     schwarze  622:                arg1 = NULL;
                    623:                su = NULL;
1.194     schwarze  624:
                    625:                /* First letter: input argument type. */
                    626:
                    627:                switch (*fmt++) {
                    628:                case 'h':
                    629:                        i = va_arg(ap, int);
1.203     schwarze  630:                        su = &mysu;
1.194     schwarze  631:                        SCALE_HS_INIT(su, i);
                    632:                        break;
                    633:                case 's':
1.203     schwarze  634:                        arg1 = va_arg(ap, char *);
1.194     schwarze  635:                        break;
                    636:                case 'u':
                    637:                        su = va_arg(ap, struct roffsu *);
                    638:                        break;
                    639:                case 'w':
1.219     schwarze  640:                        if ((arg2 = va_arg(ap, char *)) != NULL) {
                    641:                                su = &mysu;
                    642:                                a2width(arg2, su);
                    643:                        }
                    644:                        if (*fmt == '*') {
                    645:                                if (su != NULL && su->unit == SCALE_EN &&
                    646:                                    su->scale > 5.9 && su->scale < 6.1)
                    647:                                        su = NULL;
                    648:                                fmt++;
1.218     schwarze  649:                        }
1.211     schwarze  650:                        if (*fmt == '+') {
1.219     schwarze  651:                                if (su != NULL) {
                    652:                                        /* Make even bold text fit. */
                    653:                                        su->scale *= 1.2;
                    654:                                        /* Add padding. */
                    655:                                        su->scale += 3.0;
                    656:                                }
1.211     schwarze  657:                                fmt++;
                    658:                        }
                    659:                        if (*fmt == '-') {
1.219     schwarze  660:                                if (su != NULL)
                    661:                                        su->scale *= -1.0;
1.211     schwarze  662:                                fmt++;
                    663:                        }
1.194     schwarze  664:                        break;
                    665:                default:
                    666:                        abort();
                    667:                }
                    668:
                    669:                /* Second letter: style name. */
                    670:
                    671:                switch (*fmt++) {
                    672:                case 'h':
1.195     schwarze  673:                        attr = "height";
1.194     schwarze  674:                        break;
                    675:                case 'i':
1.195     schwarze  676:                        attr = "text-indent";
1.194     schwarze  677:                        break;
                    678:                case 'l':
1.195     schwarze  679:                        attr = "margin-left";
1.194     schwarze  680:                        break;
                    681:                case 'w':
1.195     schwarze  682:                        attr = "width";
1.194     schwarze  683:                        break;
                    684:                case 'W':
1.195     schwarze  685:                        attr = "min-width";
1.194     schwarze  686:                        break;
                    687:                case '?':
1.203     schwarze  688:                        attr = arg1;
                    689:                        arg1 = va_arg(ap, char *);
                    690:                        break;
1.194     schwarze  691:                default:
                    692:                        abort();
                    693:                }
1.203     schwarze  694:                if (su == NULL && arg1 == NULL)
                    695:                        continue;
                    696:
                    697:                if (have_style == 1)
                    698:                        print_word(h, " style=\"");
                    699:                else
                    700:                        print_byte(h, ' ');
1.197     schwarze  701:                print_word(h, attr);
                    702:                print_byte(h, ':');
                    703:                print_byte(h, ' ');
1.203     schwarze  704:                if (su != NULL) {
                    705:                        v = su->scale;
                    706:                        if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
                    707:                                v = 1.0;
                    708:                        else if (su->unit == SCALE_BU)
                    709:                                v /= 24.0;
                    710:                        (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
                    711:                        print_word(h, numbuf);
                    712:                        print_word(h, roffscales[su->unit]);
                    713:                } else
                    714:                        print_word(h, arg1);
1.197     schwarze  715:                print_byte(h, ';');
1.203     schwarze  716:                have_style = 2;
1.194     schwarze  717:        }
1.203     schwarze  718:        if (have_style == 2)
1.197     schwarze  719:                print_byte(h, '"');
1.194     schwarze  720:
                    721:        va_end(ap);
1.94      kristaps  722:
1.172     kristaps  723:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  724:
1.93      kristaps  725:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  726:                print_byte(h, '/');
1.93      kristaps  727:
1.197     schwarze  728:        print_byte(h, '>');
1.14      kristaps  729:
1.196     schwarze  730:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  731:                print_endline(h);
1.196     schwarze  732:        else
                    733:                h->flags |= HTML_NOSPACE;
1.117     kristaps  734:
1.196     schwarze  735:        if (tflags & HTML_INDENT)
                    736:                h->indent++;
                    737:        if (tflags & HTML_NOINDENT)
                    738:                h->noindent++;
1.117     kristaps  739:
1.188     schwarze  740:        return t;
1.14      kristaps  741: }
                    742:
1.29      kristaps  743: static void
1.184     schwarze  744: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  745: {
1.196     schwarze  746:        int      tflags;
1.156     schwarze  747:
1.184     schwarze  748:        /*
                    749:         * Remember to close out and nullify the current
                    750:         * meta-font and table, if applicable.
                    751:         */
                    752:        if (tag == h->metaf)
                    753:                h->metaf = NULL;
                    754:        if (tag == h->tblt)
                    755:                h->tblt = NULL;
                    756:
1.196     schwarze  757:        tflags = htmltags[tag->tag].flags;
                    758:
                    759:        if (tflags & HTML_INDENT)
                    760:                h->indent--;
                    761:        if (tflags & HTML_NOINDENT)
                    762:                h->noindent--;
                    763:        if (tflags & HTML_NLEND)
1.197     schwarze  764:                print_endline(h);
                    765:        print_indent(h);
                    766:        print_byte(h, '<');
                    767:        print_byte(h, '/');
                    768:        print_word(h, htmltags[tag->tag].name);
                    769:        print_byte(h, '>');
1.196     schwarze  770:        if (tflags & HTML_NLAFTER)
1.197     schwarze  771:                print_endline(h);
1.184     schwarze  772:
1.204     schwarze  773:        h->tag = tag->next;
1.184     schwarze  774:        free(tag);
1.14      kristaps  775: }
                    776:
1.51      kristaps  777: void
1.93      kristaps  778: print_gen_decls(struct html *h)
1.1       kristaps  779: {
1.197     schwarze  780:        print_word(h, "<!DOCTYPE html>");
                    781:        print_endline(h);
1.221     schwarze  782: }
                    783:
                    784: void
                    785: print_gen_comment(struct html *h, struct roff_node *n)
                    786: {
                    787:        int      wantblank;
                    788:
                    789:        print_word(h, "<!-- This is an automatically generated file."
                    790:            "  Do not edit.");
                    791:        h->indent = 1;
                    792:        wantblank = 0;
                    793:        while (n != NULL && n->type == ROFFT_COMMENT) {
                    794:                if (strstr(n->string, "-->") == NULL &&
                    795:                    (wantblank || *n->string != '\0')) {
                    796:                        print_endline(h);
                    797:                        print_indent(h);
                    798:                        print_word(h, n->string);
                    799:                        wantblank = *n->string != '\0';
                    800:                }
                    801:                n = n->next;
                    802:        }
                    803:        if (wantblank)
                    804:                print_endline(h);
                    805:        print_word(h, " -->");
                    806:        print_endline(h);
                    807:        h->indent = 0;
1.1       kristaps  808: }
                    809:
1.51      kristaps  810: void
1.104     kristaps  811: print_text(struct html *h, const char *word)
1.1       kristaps  812: {
1.197     schwarze  813:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  814:                if ( ! (HTML_KEEP & h->flags)) {
                    815:                        if (HTML_PREKEEP & h->flags)
                    816:                                h->flags |= HTML_KEEP;
1.197     schwarze  817:                        print_endword(h);
1.105     kristaps  818:                } else
1.216     schwarze  819:                        print_word(h, "&#x00A0;");
1.105     kristaps  820:        }
1.30      kristaps  821:
1.122     kristaps  822:        assert(NULL == h->metaf);
1.152     schwarze  823:        switch (h->metac) {
1.156     schwarze  824:        case HTMLFONT_ITALIC:
1.194     schwarze  825:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  826:                break;
1.156     schwarze  827:        case HTMLFONT_BOLD:
1.194     schwarze  828:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  829:                break;
1.156     schwarze  830:        case HTMLFONT_BI:
1.194     schwarze  831:                h->metaf = print_otag(h, TAG_B, "");
                    832:                print_otag(h, TAG_I, "");
1.152     schwarze  833:                break;
                    834:        default:
1.197     schwarze  835:                print_indent(h);
1.152     schwarze  836:                break;
                    837:        }
1.122     kristaps  838:
1.104     kristaps  839:        assert(word);
1.195     schwarze  840:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  841:                if ( ! (h->flags & HTML_NONOSPACE))
                    842:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  843:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  844:        } else
1.183     schwarze  845:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  846:
                    847:        if (h->metaf) {
                    848:                print_tagq(h, h->metaf);
                    849:                h->metaf = NULL;
                    850:        }
1.113     schwarze  851:
                    852:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  853: }
1.30      kristaps  854:
1.51      kristaps  855: void
1.30      kristaps  856: print_tagq(struct html *h, const struct tag *until)
                    857: {
                    858:        struct tag      *tag;
                    859:
1.204     schwarze  860:        while ((tag = h->tag) != NULL) {
1.184     schwarze  861:                print_ctag(h, tag);
1.30      kristaps  862:                if (until && tag == until)
                    863:                        return;
                    864:        }
                    865: }
                    866:
1.51      kristaps  867: void
1.30      kristaps  868: print_stagq(struct html *h, const struct tag *suntil)
                    869: {
                    870:        struct tag      *tag;
                    871:
1.204     schwarze  872:        while ((tag = h->tag) != NULL) {
1.30      kristaps  873:                if (suntil && tag == suntil)
                    874:                        return;
1.184     schwarze  875:                print_ctag(h, tag);
1.30      kristaps  876:        }
                    877: }
1.171     kristaps  878:
                    879: void
                    880: print_paragraph(struct html *h)
                    881: {
                    882:        struct tag      *t;
                    883:
1.198     schwarze  884:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.171     kristaps  885:        print_tagq(h, t);
                    886: }
                    887:
1.197     schwarze  888:
                    889: /***********************************************************************
                    890:  * Low level output functions.
                    891:  * They implement line breaking using a short static buffer.
                    892:  ***********************************************************************/
                    893:
                    894: /*
                    895:  * Buffer one HTML output byte.
                    896:  * If the buffer is full, flush and deactivate it and start a new line.
                    897:  * If the buffer is inactive, print directly.
                    898:  */
                    899: static void
                    900: print_byte(struct html *h, char c)
                    901: {
                    902:        if ((h->flags & HTML_BUFFER) == 0) {
                    903:                putchar(c);
                    904:                h->col++;
                    905:                return;
                    906:        }
                    907:
                    908:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    909:                h->buf[h->bufcol++] = c;
                    910:                return;
                    911:        }
                    912:
                    913:        putchar('\n');
                    914:        h->col = 0;
                    915:        print_indent(h);
                    916:        putchar(' ');
                    917:        putchar(' ');
                    918:        fwrite(h->buf, h->bufcol, 1, stdout);
                    919:        putchar(c);
                    920:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    921:        h->bufcol = 0;
                    922:        h->flags &= ~HTML_BUFFER;
                    923: }
                    924:
1.196     schwarze  925: /*
                    926:  * If something was printed on the current output line, end it.
1.197     schwarze  927:  * Not to be called right after print_indent().
1.196     schwarze  928:  */
1.202     schwarze  929: void
1.197     schwarze  930: print_endline(struct html *h)
1.196     schwarze  931: {
1.197     schwarze  932:        if (h->col == 0)
1.196     schwarze  933:                return;
                    934:
1.197     schwarze  935:        if (h->bufcol) {
                    936:                putchar(' ');
                    937:                fwrite(h->buf, h->bufcol, 1, stdout);
                    938:                h->bufcol = 0;
                    939:        }
1.196     schwarze  940:        putchar('\n');
1.197     schwarze  941:        h->col = 0;
                    942:        h->flags |= HTML_NOSPACE;
                    943:        h->flags &= ~HTML_BUFFER;
                    944: }
                    945:
                    946: /*
                    947:  * Flush the HTML output buffer.
                    948:  * If it is inactive, activate it.
                    949:  */
                    950: static void
                    951: print_endword(struct html *h)
                    952: {
                    953:        if (h->noindent) {
                    954:                print_byte(h, ' ');
                    955:                return;
                    956:        }
                    957:
                    958:        if ((h->flags & HTML_BUFFER) == 0) {
                    959:                h->col++;
                    960:                h->flags |= HTML_BUFFER;
                    961:        } else if (h->bufcol) {
                    962:                putchar(' ');
                    963:                fwrite(h->buf, h->bufcol, 1, stdout);
                    964:                h->col += h->bufcol + 1;
                    965:        }
                    966:        h->bufcol = 0;
1.196     schwarze  967: }
                    968:
                    969: /*
                    970:  * If at the beginning of a new output line,
                    971:  * perform indentation and mark the line as containing output.
                    972:  * Make sure to really produce some output right afterwards,
                    973:  * but do not use print_otag() for producing it.
                    974:  */
                    975: static void
1.197     schwarze  976: print_indent(struct html *h)
1.196     schwarze  977: {
1.197     schwarze  978:        size_t   i;
1.196     schwarze  979:
1.197     schwarze  980:        if (h->col)
1.196     schwarze  981:                return;
                    982:
1.197     schwarze  983:        if (h->noindent == 0) {
                    984:                h->col = h->indent * 2;
                    985:                for (i = 0; i < h->col; i++)
1.196     schwarze  986:                        putchar(' ');
1.197     schwarze  987:        }
                    988:        h->flags &= ~HTML_NOSPACE;
                    989: }
                    990:
                    991: /*
                    992:  * Print or buffer some characters
                    993:  * depending on the current HTML output buffer state.
                    994:  */
                    995: static void
                    996: print_word(struct html *h, const char *cp)
                    997: {
                    998:        while (*cp != '\0')
                    999:                print_byte(h, *cp++);
1.196     schwarze 1000: }
1.194     schwarze 1001:
                   1002: /*
                   1003:  * Calculate the scaling unit passed in a `-width' argument.  This uses
                   1004:  * either a native scaling unit (e.g., 1i, 2m) or the string length of
                   1005:  * the value.
                   1006:  */
                   1007: static void
                   1008: a2width(const char *p, struct roffsu *su)
                   1009: {
1.213     schwarze 1010:        const char      *end;
                   1011:
                   1012:        end = a2roffsu(p, su, SCALE_MAX);
                   1013:        if (end == NULL || *end != '\0') {
1.194     schwarze 1014:                su->unit = SCALE_EN;
                   1015:                su->scale = html_strlen(p);
                   1016:        } else if (su->scale < 0.0)
                   1017:                su->scale = 0.0;
1.68      kristaps 1018: }

CVSweb