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

Annotation of mandoc/html.c, Revision 1.34

1.34    ! kristaps    1: /*     $Id: html.c,v 1.33 2009/09/17 13:17:30 kristaps Exp $ */
1.1       kristaps    2: /*
1.29      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.29      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.29      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.30      kristaps   17: #include <sys/queue.h>
                     18:
1.1       kristaps   19: #include <assert.h>
1.33      kristaps   20: #include <ctype.h>
1.4       kristaps   21: #include <err.h>
1.29      kristaps   22: #include <stdio.h>
1.1       kristaps   23: #include <stdlib.h>
1.33      kristaps   24: #include <string.h>
1.1       kristaps   25:
1.32      kristaps   26: #include "chars.h"
1.29      kristaps   27: #include "mdoc.h"
                     28: #include "man.h"
1.2       kristaps   29:
1.29      kristaps   30: #define        DOCTYPE         "-//W3C//DTD HTML 4.01//EN"
                     31: #define        DTD             "http://www.w3.org/TR/html4/strict.dtd"
1.8       kristaps   32:
1.33      kristaps   33: #define        INDENT           5
                     34: #define        HALFINDENT       3
1.34    ! kristaps   35: #define        PX_MULT          8
1.33      kristaps   36:
1.29      kristaps   37: enum   htmltag {
                     38:        TAG_HTML,
                     39:        TAG_HEAD,
                     40:        TAG_BODY,
                     41:        TAG_META,
                     42:        TAG_TITLE,
                     43:        TAG_DIV,
                     44:        TAG_H1,
                     45:        TAG_H2,
                     46:        TAG_P,
                     47:        TAG_SPAN,
                     48:        TAG_LINK,
1.30      kristaps   49:        TAG_BR,
                     50:        TAG_A,
1.33      kristaps   51:        TAG_TABLE,
                     52:        TAG_COL,
                     53:        TAG_TR,
                     54:        TAG_TD,
1.34    ! kristaps   55:        TAG_LI,
        !            56:        TAG_UL,
        !            57:        TAG_OL,
1.29      kristaps   58:        TAG_MAX
1.7       kristaps   59: };
                     60:
1.29      kristaps   61: enum   htmlattr {
                     62:        ATTR_HTTPEQUIV,
                     63:        ATTR_CONTENT,
                     64:        ATTR_NAME,
                     65:        ATTR_REL,
                     66:        ATTR_HREF,
                     67:        ATTR_TYPE,
                     68:        ATTR_MEDIA,
                     69:        ATTR_CLASS,
1.33      kristaps   70:        ATTR_STYLE,
                     71:        ATTR_WIDTH,
                     72:        ATTR_VALIGN,
1.29      kristaps   73:        ATTR_MAX
1.7       kristaps   74: };
                     75:
1.29      kristaps   76: struct htmldata {
                     77:        char             *name;
                     78:        int               flags;
1.30      kristaps   79: #define        HTML_CLRLINE     (1 << 0)
                     80: #define        HTML_NOSTACK     (1 << 1)
1.29      kristaps   81: };
1.7       kristaps   82:
1.29      kristaps   83: static const struct htmldata htmltags[TAG_MAX] = {
1.30      kristaps   84:        {"html",        HTML_CLRLINE}, /* TAG_HTML */
                     85:        {"head",        HTML_CLRLINE}, /* TAG_HEAD */
                     86:        {"body",        HTML_CLRLINE}, /* TAG_BODY */
                     87:        {"meta",        HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
1.33      kristaps   88:        {"title",       HTML_CLRLINE}, /* TAG_TITLE */
1.30      kristaps   89:        {"div",         HTML_CLRLINE}, /* TAG_DIV */
1.29      kristaps   90:        {"h1",          0}, /* TAG_H1 */
                     91:        {"h2",          0}, /* TAG_H2 */
1.30      kristaps   92:        {"p",           HTML_CLRLINE}, /* TAG_P */
1.29      kristaps   93:        {"span",        0}, /* TAG_SPAN */
1.30      kristaps   94:        {"link",        HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
                     95:        {"br",          HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
                     96:        {"a",           0}, /* TAG_A */
1.33      kristaps   97:        {"table",       HTML_CLRLINE}, /* TAG_TABLE */
                     98:        {"col",         HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
                     99:        {"tr",          HTML_CLRLINE}, /* TAG_TR */
                    100:        {"td",          HTML_CLRLINE}, /* TAG_TD */
1.34    ! kristaps  101:        {"li",          HTML_CLRLINE}, /* TAG_LI */
        !           102:        {"ul",          HTML_CLRLINE}, /* TAG_UL */
        !           103:        {"ol",          HTML_CLRLINE}, /* TAG_OL */
1.29      kristaps  104: };
1.10      kristaps  105:
1.29      kristaps  106: static const char       *const htmlattrs[ATTR_MAX] = {
                    107:        "http-equiv",
                    108:        "content",
                    109:        "name",
                    110:        "rel",
                    111:        "href",
                    112:        "type",
                    113:        "media",
1.33      kristaps  114:        "class",
                    115:        "style",
                    116:        "width",
                    117:        "valign",
1.29      kristaps  118: };
1.10      kristaps  119:
1.29      kristaps  120: struct htmlpair {
                    121:        enum htmlattr     key;
                    122:        char             *val;
                    123: };
1.10      kristaps  124:
1.30      kristaps  125: struct tag {
                    126:        enum htmltag      tag;
                    127:        SLIST_ENTRY(tag)  entry;
                    128: };
                    129:
                    130: SLIST_HEAD(tagq, tag);
                    131:
1.29      kristaps  132: struct html {
                    133:        int               flags;
                    134: #define        HTML_NOSPACE     (1 << 0)
1.30      kristaps  135: #define        HTML_NEWLINE     (1 << 1)
                    136:        struct tagq       stack;
1.32      kristaps  137:        void             *symtab;
1.29      kristaps  138: };
1.10      kristaps  139:
1.29      kristaps  140: #define        MDOC_ARGS         const struct mdoc_meta *m, \
                    141:                          const struct mdoc_node *n, \
                    142:                          struct html *h
                    143: #define        MAN_ARGS          const struct man_meta *m, \
                    144:                          const struct man_node *n, \
                    145:                          struct html *h
                    146: struct htmlmdoc {
                    147:        int             (*pre)(MDOC_ARGS);
                    148:        void            (*post)(MDOC_ARGS);
                    149: };
1.13      kristaps  150:
1.29      kristaps  151: static void              print_gen_doctype(struct html *);
                    152: static void              print_gen_head(struct html *);
                    153: static void              print_mdoc(MDOC_ARGS);
                    154: static void              print_mdoc_head(MDOC_ARGS);
1.30      kristaps  155: static void              print_mdoc_title(MDOC_ARGS);
1.29      kristaps  156: static void              print_mdoc_node(MDOC_ARGS);
                    157: static void              print_man(MAN_ARGS);
                    158: static void              print_man_head(MAN_ARGS);
                    159: static void              print_man_body(MAN_ARGS);
1.30      kristaps  160: static struct tag       *print_otag(struct html *, enum htmltag,
1.29      kristaps  161:                                int, const struct htmlpair *);
1.30      kristaps  162: static void              print_tagq(struct html *, const struct tag *);
                    163: static void              print_stagq(struct html *, const struct tag *);
1.29      kristaps  164: static void              print_ctag(struct html *, enum htmltag);
1.32      kristaps  165: static void              print_encode(struct html *, const char *);
                    166: static void              print_escape(struct html *, const char **);
1.29      kristaps  167: static void              print_text(struct html *, const char *);
1.32      kristaps  168: static void              print_res(struct html *, const char *, int);
                    169: static void              print_spec(struct html *, const char *, int);
1.33      kristaps  170:
                    171: static int               a2width(const char *);
                    172: static int               a2offs(const char *);
                    173:
1.34    ! kristaps  174: static int               mdoc_list_pre(MDOC_ARGS, int);
        !           175: static int               mdoc_listitem_pre(MDOC_ARGS);
1.29      kristaps  176: static int               mdoc_root_pre(MDOC_ARGS);
1.34    ! kristaps  177: static int               mdoc_tbl_pre(MDOC_ARGS, int);
        !           178: static int               mdoc_tbl_block_pre(MDOC_ARGS, int, int, int);
        !           179: static int               mdoc_tbl_body_pre(MDOC_ARGS, int, int);
        !           180: static int               mdoc_tbl_head_pre(MDOC_ARGS, int, int);
1.29      kristaps  181:
1.31      kristaps  182: static int               mdoc_ar_pre(MDOC_ARGS);
1.33      kristaps  183: static int               mdoc_bl_pre(MDOC_ARGS);
1.34    ! kristaps  184: static int               mdoc_d1_pre(MDOC_ARGS);
        !           185: static void              mdoc_dq_post(MDOC_ARGS);
        !           186: static int               mdoc_dq_pre(MDOC_ARGS);
1.30      kristaps  187: static int               mdoc_fl_pre(MDOC_ARGS);
1.34    ! kristaps  188: static int               mdoc_em_pre(MDOC_ARGS);
        !           189: static int               mdoc_ex_pre(MDOC_ARGS);
1.33      kristaps  190: static int               mdoc_it_pre(MDOC_ARGS);
1.29      kristaps  191: static int               mdoc_nd_pre(MDOC_ARGS);
                    192: static int               mdoc_nm_pre(MDOC_ARGS);
1.31      kristaps  193: static int               mdoc_ns_pre(MDOC_ARGS);
1.34    ! kristaps  194: static void              mdoc_op_post(MDOC_ARGS);
1.30      kristaps  195: static int               mdoc_op_pre(MDOC_ARGS);
1.29      kristaps  196: static int               mdoc_pp_pre(MDOC_ARGS);
1.34    ! kristaps  197: static void              mdoc_pq_post(MDOC_ARGS);
        !           198: static int               mdoc_pq_pre(MDOC_ARGS);
1.29      kristaps  199: static int               mdoc_sh_pre(MDOC_ARGS);
1.34    ! kristaps  200: static void              mdoc_sq_post(MDOC_ARGS);
        !           201: static int               mdoc_sq_pre(MDOC_ARGS);
1.29      kristaps  202: static int               mdoc_ss_pre(MDOC_ARGS);
1.34    ! kristaps  203: static int               mdoc_sx_pre(MDOC_ARGS);
1.30      kristaps  204: static int               mdoc_xr_pre(MDOC_ARGS);
1.33      kristaps  205: static int               mdoc_xx_pre(MDOC_ARGS);
                    206:
                    207: #ifdef __linux__
                    208: extern size_t    strlcpy(char *, const char *, size_t);
                    209: extern size_t    strlcat(char *, const char *, size_t);
                    210: #endif
1.29      kristaps  211:
                    212: static const struct htmlmdoc mdocs[MDOC_MAX] = {
                    213:        {NULL, NULL}, /* Ap */
                    214:        {NULL, NULL}, /* Dd */
                    215:        {NULL, NULL}, /* Dt */
                    216:        {NULL, NULL}, /* Os */
1.30      kristaps  217:        {mdoc_sh_pre, NULL }, /* Sh */
                    218:        {mdoc_ss_pre, NULL }, /* Ss */
1.29      kristaps  219:        {mdoc_pp_pre, NULL}, /* Pp */
1.34    ! kristaps  220:        {mdoc_d1_pre, NULL}, /* D1 */
        !           221:        {mdoc_d1_pre, NULL}, /* Dl */
1.29      kristaps  222:        {NULL, NULL}, /* Bd */
                    223:        {NULL, NULL}, /* Ed */
1.33      kristaps  224:        {mdoc_bl_pre, NULL}, /* Bl */
1.29      kristaps  225:        {NULL, NULL}, /* El */
1.33      kristaps  226:        {mdoc_it_pre, NULL}, /* It */
1.29      kristaps  227:        {NULL, NULL}, /* Ad */
                    228:        {NULL, NULL}, /* An */
1.31      kristaps  229:        {mdoc_ar_pre, NULL}, /* Ar */
1.29      kristaps  230:        {NULL, NULL}, /* Cd */
                    231:        {NULL, NULL}, /* Cm */
                    232:        {NULL, NULL}, /* Dv */
                    233:        {NULL, NULL}, /* Er */
                    234:        {NULL, NULL}, /* Ev */
1.34    ! kristaps  235:        {mdoc_ex_pre, NULL}, /* Ex */
1.29      kristaps  236:        {NULL, NULL}, /* Fa */
                    237:        {NULL, NULL}, /* Fd */
1.30      kristaps  238:        {mdoc_fl_pre, NULL}, /* Fl */
1.29      kristaps  239:        {NULL, NULL}, /* Fn */
                    240:        {NULL, NULL}, /* Ft */
                    241:        {NULL, NULL}, /* Ic */
                    242:        {NULL, NULL}, /* In */
                    243:        {NULL, NULL}, /* Li */
                    244:        {mdoc_nd_pre, NULL}, /* Nd */
1.30      kristaps  245:        {mdoc_nm_pre, NULL}, /* Nm */
                    246:        {mdoc_op_pre, mdoc_op_post}, /* Op */
1.29      kristaps  247:        {NULL, NULL}, /* Ot */
                    248:        {NULL, NULL}, /* Pa */
                    249:        {NULL, NULL}, /* Rv */
                    250:        {NULL, NULL}, /* St */
                    251:        {NULL, NULL}, /* Va */
                    252:        {NULL, NULL}, /* Vt */
1.30      kristaps  253:        {mdoc_xr_pre, NULL}, /* Xr */
1.29      kristaps  254:        {NULL, NULL}, /* %A */
                    255:        {NULL, NULL}, /* %B */
                    256:        {NULL, NULL}, /* %D */
                    257:        {NULL, NULL}, /* %I */
                    258:        {NULL, NULL}, /* %J */
                    259:        {NULL, NULL}, /* %N */
                    260:        {NULL, NULL}, /* %O */
                    261:        {NULL, NULL}, /* %P */
                    262:        {NULL, NULL}, /* %R */
                    263:        {NULL, NULL}, /* %T */
                    264:        {NULL, NULL}, /* %V */
                    265:        {NULL, NULL}, /* Ac */
                    266:        {NULL, NULL}, /* Ao */
                    267:        {NULL, NULL}, /* Aq */
                    268:        {NULL, NULL}, /* At */
                    269:        {NULL, NULL}, /* Bc */
                    270:        {NULL, NULL}, /* Bf */
                    271:        {NULL, NULL}, /* Bo */
                    272:        {NULL, NULL}, /* Bq */
1.33      kristaps  273:        {mdoc_xx_pre, NULL}, /* Bsx */
1.29      kristaps  274:        {NULL, NULL}, /* Bx */
                    275:        {NULL, NULL}, /* Db */
                    276:        {NULL, NULL}, /* Dc */
                    277:        {NULL, NULL}, /* Do */
1.34    ! kristaps  278:        {mdoc_dq_pre, mdoc_dq_post}, /* Dq */
1.29      kristaps  279:        {NULL, NULL}, /* Ec */
                    280:        {NULL, NULL}, /* Ef */
1.34    ! kristaps  281:        {mdoc_em_pre, NULL}, /* Em */
1.29      kristaps  282:        {NULL, NULL}, /* Eo */
1.33      kristaps  283:        {mdoc_xx_pre, NULL}, /* Fx */
1.29      kristaps  284:        {NULL, NULL}, /* Ms */
                    285:        {NULL, NULL}, /* No */
1.31      kristaps  286:        {mdoc_ns_pre, NULL}, /* Ns */
1.33      kristaps  287:        {mdoc_xx_pre, NULL}, /* Nx */
                    288:        {mdoc_xx_pre, NULL}, /* Ox */
1.29      kristaps  289:        {NULL, NULL}, /* Pc */
                    290:        {NULL, NULL}, /* Pf */
1.34    ! kristaps  291:        {mdoc_pq_pre, mdoc_pq_post}, /* Po */
        !           292:        {mdoc_pq_pre, mdoc_pq_post}, /* Pq */
1.29      kristaps  293:        {NULL, NULL}, /* Qc */
                    294:        {NULL, NULL}, /* Ql */
                    295:        {NULL, NULL}, /* Qo */
                    296:        {NULL, NULL}, /* Qq */
                    297:        {NULL, NULL}, /* Re */
                    298:        {NULL, NULL}, /* Rs */
                    299:        {NULL, NULL}, /* Sc */
1.34    ! kristaps  300:        {mdoc_sq_pre, mdoc_sq_post}, /* So */
        !           301:        {mdoc_sq_pre, mdoc_sq_post}, /* Sq */
1.29      kristaps  302:        {NULL, NULL}, /* Sm */
1.34    ! kristaps  303:        {mdoc_sx_pre, NULL}, /* Sx */
1.29      kristaps  304:        {NULL, NULL}, /* Sy */
                    305:        {NULL, NULL}, /* Tn */
1.33      kristaps  306:        {mdoc_xx_pre, NULL}, /* Ux */
1.29      kristaps  307:        {NULL, NULL}, /* Xc */
                    308:        {NULL, NULL}, /* Xo */
                    309:        {NULL, NULL}, /* Fo */
                    310:        {NULL, NULL}, /* Fc */
                    311:        {NULL, NULL}, /* Oo */
                    312:        {NULL, NULL}, /* Oc */
                    313:        {NULL, NULL}, /* Bk */
                    314:        {NULL, NULL}, /* Ek */
                    315:        {NULL, NULL}, /* Bt */
                    316:        {NULL, NULL}, /* Hf */
                    317:        {NULL, NULL}, /* Fr */
                    318:        {NULL, NULL}, /* Ud */
                    319:        {NULL, NULL}, /* Lb */
                    320:        {NULL, NULL}, /* Lp */
                    321:        {NULL, NULL}, /* Lk */
                    322:        {NULL, NULL}, /* Mt */
                    323:        {NULL, NULL}, /* Brq */
                    324:        {NULL, NULL}, /* Bro */
                    325:        {NULL, NULL}, /* Brc */
                    326:        {NULL, NULL}, /* %C */
                    327:        {NULL, NULL}, /* Es */
                    328:        {NULL, NULL}, /* En */
1.33      kristaps  329:        {mdoc_xx_pre, NULL}, /* Dx */
1.29      kristaps  330:        {NULL, NULL}, /* %Q */
                    331:        {NULL, NULL}, /* br */
                    332:        {NULL, NULL}, /* sp */
                    333: };
1.10      kristaps  334:
1.33      kristaps  335:
1.30      kristaps  336: void
1.29      kristaps  337: html_mdoc(void *arg, const struct mdoc *m)
1.10      kristaps  338: {
1.29      kristaps  339:        struct html     *h;
1.30      kristaps  340:        struct tag      *t;
1.10      kristaps  341:
1.29      kristaps  342:        h = (struct html *)arg;
1.10      kristaps  343:
1.29      kristaps  344:        print_gen_doctype(h);
1.30      kristaps  345:        t = print_otag(h, TAG_HTML, 0, NULL);
1.29      kristaps  346:        print_mdoc(mdoc_meta(m), mdoc_node(m), h);
1.30      kristaps  347:        print_tagq(h, t);
                    348:
1.29      kristaps  349:        printf("\n");
1.10      kristaps  350: }
                    351:
1.33      kristaps  352:
1.30      kristaps  353: void
1.29      kristaps  354: html_man(void *arg, const struct man *m)
1.10      kristaps  355: {
1.29      kristaps  356:        struct html     *h;
1.30      kristaps  357:        struct tag      *t;
1.10      kristaps  358:
1.29      kristaps  359:        h = (struct html *)arg;
1.10      kristaps  360:
1.29      kristaps  361:        print_gen_doctype(h);
1.30      kristaps  362:        t = print_otag(h, TAG_HTML, 0, NULL);
1.29      kristaps  363:        print_man(man_meta(m), man_node(m), h);
1.30      kristaps  364:        print_tagq(h, t);
                    365:
1.29      kristaps  366:        printf("\n");
1.10      kristaps  367: }
                    368:
1.33      kristaps  369:
1.29      kristaps  370: void *
                    371: html_alloc(void)
1.10      kristaps  372: {
1.30      kristaps  373:        struct html     *h;
                    374:
                    375:        if (NULL == (h = calloc(1, sizeof(struct html))))
                    376:                return(NULL);
1.10      kristaps  377:
1.30      kristaps  378:        SLIST_INIT(&h->stack);
1.32      kristaps  379:        if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
                    380:                free(h);
                    381:                return(NULL);
                    382:        }
1.30      kristaps  383:        return(h);
1.29      kristaps  384: }
1.10      kristaps  385:
1.33      kristaps  386:
1.29      kristaps  387: void
                    388: html_free(void *p)
                    389: {
1.30      kristaps  390:        struct tag      *tag;
                    391:        struct html     *h;
                    392:
                    393:        h = (struct html *)p;
1.10      kristaps  394:
1.30      kristaps  395:        while ( ! SLIST_EMPTY(&h->stack)) {
                    396:                tag = SLIST_FIRST(&h->stack);
                    397:                SLIST_REMOVE_HEAD(&h->stack, entry);
                    398:                free(tag);
                    399:        }
                    400:        free(h);
1.10      kristaps  401: }
1.2       kristaps  402:
1.33      kristaps  403:
1.29      kristaps  404: static void
                    405: print_mdoc(MDOC_ARGS)
1.4       kristaps  406: {
1.30      kristaps  407:        struct tag      *t;
1.4       kristaps  408:
1.30      kristaps  409:        t = print_otag(h, TAG_HEAD, 0, NULL);
1.29      kristaps  410:        print_mdoc_head(m, n, h);
1.30      kristaps  411:        print_tagq(h, t);
                    412:
                    413:        t = print_otag(h, TAG_BODY, 0, NULL);
                    414:        print_mdoc_title(m, n, h);
1.29      kristaps  415:        print_mdoc_node(m, n, h);
1.30      kristaps  416:        print_tagq(h, t);
1.29      kristaps  417: }
1.4       kristaps  418:
1.33      kristaps  419:
1.29      kristaps  420: static void
                    421: print_gen_head(struct html *h)
                    422: {
                    423:        struct htmlpair  meta0[2];
                    424:        struct htmlpair  meta1[2];
                    425:        struct htmlpair  link[4];
                    426:
                    427:        meta0[0].key = ATTR_HTTPEQUIV;
                    428:        meta0[0].val = "Content-Type";
                    429:        meta0[1].key = ATTR_CONTENT;
1.34    ! kristaps  430:        meta0[1].val = "text/html; charset=utf-8";
1.29      kristaps  431:
                    432:        meta1[0].key = ATTR_NAME;
                    433:        meta1[0].val = "resource-type";
                    434:        meta1[1].key = ATTR_CONTENT;
                    435:        meta1[1].val = "document";
                    436:
                    437:        link[0].key = ATTR_REL;
                    438:        link[0].val = "stylesheet";
                    439:        link[1].key = ATTR_HREF;
1.30      kristaps  440:        link[1].val = "style.css"; /* XXX */
1.29      kristaps  441:        link[2].key = ATTR_TYPE;
                    442:        link[2].val = "text/css";
                    443:        link[3].key = ATTR_MEDIA;
                    444:        link[3].val = "all";
                    445:
                    446:        print_otag(h, TAG_META, 2, meta0);
                    447:        print_otag(h, TAG_META, 2, meta1);
                    448:        print_otag(h, TAG_LINK, 4, link);
1.4       kristaps  449: }
                    450:
1.33      kristaps  451:
1.30      kristaps  452: /* ARGSUSED */
1.29      kristaps  453: static void
                    454: print_mdoc_head(MDOC_ARGS)
1.18      kristaps  455: {
                    456:
1.29      kristaps  457:        print_gen_head(h);
                    458:        print_otag(h, TAG_TITLE, 0, NULL);
1.32      kristaps  459:        print_encode(h, m->title);
1.2       kristaps  460: }
                    461:
1.33      kristaps  462:
1.30      kristaps  463: /* ARGSUSED */
1.29      kristaps  464: static void
1.30      kristaps  465: print_mdoc_title(MDOC_ARGS)
1.2       kristaps  466: {
                    467:
1.30      kristaps  468:        /* TODO */
1.2       kristaps  469: }
                    470:
1.33      kristaps  471:
1.29      kristaps  472: static void
                    473: print_mdoc_node(MDOC_ARGS)
1.2       kristaps  474: {
1.29      kristaps  475:        int              child;
1.30      kristaps  476:        struct tag      *t;
1.8       kristaps  477:
1.29      kristaps  478:        child = 1;
1.30      kristaps  479:        t = SLIST_FIRST(&h->stack);
1.8       kristaps  480:
1.29      kristaps  481:        switch (n->type) {
                    482:        case (MDOC_ROOT):
                    483:                child = mdoc_root_pre(m, n, h);
1.7       kristaps  484:                break;
1.29      kristaps  485:        case (MDOC_TEXT):
                    486:                print_text(h, n->string);
1.7       kristaps  487:                break;
1.3       kristaps  488:        default:
1.29      kristaps  489:                if (mdocs[n->tok].pre)
                    490:                        child = (*mdocs[n->tok].pre)(m, n, h);
1.3       kristaps  491:                break;
1.2       kristaps  492:        }
                    493:
1.29      kristaps  494:        if (child && n->child)
                    495:                print_mdoc_node(m, n->child, h);
1.8       kristaps  496:
1.30      kristaps  497:        print_stagq(h, t);
                    498:
1.29      kristaps  499:        switch (n->type) {
                    500:        case (MDOC_ROOT):
1.7       kristaps  501:                break;
1.29      kristaps  502:        case (MDOC_TEXT):
1.7       kristaps  503:                break;
1.3       kristaps  504:        default:
1.29      kristaps  505:                if (mdocs[n->tok].post)
                    506:                        (*mdocs[n->tok].post)(m, n, h);
1.3       kristaps  507:                break;
                    508:        }
1.2       kristaps  509:
1.29      kristaps  510:        if (n->next)
                    511:                print_mdoc_node(m, n->next, h);
1.2       kristaps  512: }
                    513:
1.33      kristaps  514:
1.29      kristaps  515: static void
                    516: print_man(MAN_ARGS)
1.9       kristaps  517: {
1.30      kristaps  518:        struct tag      *t;
1.9       kristaps  519:
1.30      kristaps  520:        t = print_otag(h, TAG_HEAD, 0, NULL);
1.29      kristaps  521:        print_man_head(m, n, h);
1.30      kristaps  522:        print_tagq(h, t);
                    523:
                    524:        t = print_otag(h, TAG_BODY, 0, NULL);
1.29      kristaps  525:        print_man_body(m, n, h);
1.30      kristaps  526:        print_tagq(h, t);
1.9       kristaps  527: }
                    528:
1.33      kristaps  529:
1.30      kristaps  530: /* ARGSUSED */
1.9       kristaps  531: static void
1.29      kristaps  532: print_man_head(MAN_ARGS)
1.9       kristaps  533: {
                    534:
1.29      kristaps  535:        print_gen_head(h);
                    536:        print_otag(h, TAG_TITLE, 0, NULL);
1.32      kristaps  537:        print_encode(h, m->title);
1.29      kristaps  538: }
1.9       kristaps  539:
1.33      kristaps  540:
1.30      kristaps  541: /* ARGSUSED */
1.29      kristaps  542: static void
                    543: print_man_body(MAN_ARGS)
                    544: {
1.30      kristaps  545:
                    546:        /* TODO */
1.9       kristaps  547: }
                    548:
1.33      kristaps  549:
1.32      kristaps  550: static void
                    551: print_spec(struct html *h, const char *p, int len)
                    552: {
                    553:        const char      *rhs;
                    554:        int              i;
                    555:        size_t           sz;
                    556:
                    557:        rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
                    558:
                    559:        if (NULL == rhs)
                    560:                return;
                    561:        for (i = 0; i < (int)sz; i++)
                    562:                putchar(rhs[i]);
                    563: }
                    564:
1.33      kristaps  565:
1.32      kristaps  566: static void
                    567: print_res(struct html *h, const char *p, int len)
                    568: {
                    569:        const char      *rhs;
                    570:        int              i;
                    571:        size_t           sz;
                    572:
                    573:        rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
                    574:
                    575:        if (NULL == rhs)
                    576:                return;
                    577:        for (i = 0; i < (int)sz; i++)
                    578:                putchar(rhs[i]);
                    579: }
                    580:
1.33      kristaps  581:
1.32      kristaps  582: static void
                    583: print_escape(struct html *h, const char **p)
                    584: {
                    585:        int              j, type;
                    586:        const char      *wp;
                    587:
                    588:        wp = *p;
                    589:        type = 1;
                    590:
                    591:        if (0 == *(++wp)) {
                    592:                *p = wp;
                    593:                return;
                    594:        }
                    595:
                    596:        if ('(' == *wp) {
                    597:                wp++;
                    598:                if (0 == *wp || 0 == *(wp + 1)) {
                    599:                        *p = 0 == *wp ? wp : wp + 1;
                    600:                        return;
                    601:                }
                    602:
                    603:                print_spec(h, wp, 2);
                    604:                *p = ++wp;
                    605:                return;
                    606:
                    607:        } else if ('*' == *wp) {
                    608:                if (0 == *(++wp)) {
                    609:                        *p = wp;
                    610:                        return;
                    611:                }
                    612:
                    613:                switch (*wp) {
                    614:                case ('('):
                    615:                        wp++;
                    616:                        if (0 == *wp || 0 == *(wp + 1)) {
                    617:                                *p = 0 == *wp ? wp : wp + 1;
                    618:                                return;
                    619:                        }
                    620:
                    621:                        print_res(h, wp, 2);
                    622:                        *p = ++wp;
                    623:                        return;
                    624:                case ('['):
                    625:                        type = 0;
                    626:                        break;
                    627:                default:
                    628:                        print_res(h, wp, 1);
                    629:                        *p = wp;
                    630:                        return;
                    631:                }
                    632:
                    633:        } else if ('f' == *wp) {
                    634:                if (0 == *(++wp)) {
                    635:                        *p = wp;
                    636:                        return;
                    637:                }
                    638:
                    639:                switch (*wp) {
                    640:                case ('B'):
                    641:                        /* TODO */
                    642:                        break;
                    643:                case ('I'):
                    644:                        /* TODO */
                    645:                        break;
                    646:                case ('P'):
                    647:                        /* FALLTHROUGH */
                    648:                case ('R'):
                    649:                        /* TODO */
                    650:                        break;
                    651:                default:
                    652:                        break;
                    653:                }
                    654:
                    655:                *p = wp;
                    656:                return;
                    657:
                    658:        } else if ('[' != *wp) {
                    659:                print_spec(h, wp, 1);
                    660:                *p = wp;
                    661:                return;
                    662:        }
                    663:
                    664:        wp++;
                    665:        for (j = 0; *wp && ']' != *wp; wp++, j++)
                    666:                /* Loop... */ ;
                    667:
                    668:        if (0 == *wp) {
                    669:                *p = wp;
                    670:                return;
                    671:        }
                    672:
                    673:        if (type)
                    674:                print_spec(h, wp - j, j);
                    675:        else
                    676:                print_res(h, wp - j, j);
                    677:
                    678:        *p = wp;
                    679: }
                    680:
1.9       kristaps  681:
1.29      kristaps  682: static void
1.32      kristaps  683: print_encode(struct html *h, const char *p)
1.29      kristaps  684: {
1.14      kristaps  685:
1.32      kristaps  686:        for (; *p; p++) {
1.34    ! kristaps  687:                if ('\\' == *p) {
        !           688:                        print_escape(h, &p);
        !           689:                        continue;
        !           690:                }
        !           691:                switch (*p) {
        !           692:                case ('<'):
        !           693:                        printf("&lt;");
        !           694:                        break;
        !           695:                case ('>'):
        !           696:                        printf("&gt;");
        !           697:                        break;
        !           698:                case ('&'):
        !           699:                        printf("&amp;");
        !           700:                        break;
        !           701:                default:
1.32      kristaps  702:                        putchar(*p);
1.34    ! kristaps  703:                        break;
1.32      kristaps  704:                }
                    705:        }
1.14      kristaps  706: }
                    707:
                    708:
1.30      kristaps  709: static struct tag *
1.29      kristaps  710: print_otag(struct html *h, enum htmltag tag,
                    711:                int sz, const struct htmlpair *p)
1.14      kristaps  712: {
1.29      kristaps  713:        int              i;
1.30      kristaps  714:        struct tag      *t;
                    715:
                    716:        if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
                    717:                if (NULL == (t = malloc(sizeof(struct tag))))
                    718:                        err(EXIT_FAILURE, "malloc");
                    719:                t->tag = tag;
                    720:                SLIST_INSERT_HEAD(&h->stack, t, entry);
                    721:        } else
                    722:                t = NULL;
1.29      kristaps  723:
                    724:        if ( ! (HTML_NOSPACE & h->flags))
1.30      kristaps  725:                if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29      kristaps  726:                        printf(" ");
                    727:
                    728:        printf("<%s", htmltags[tag].name);
                    729:        for (i = 0; i < sz; i++) {
                    730:                printf(" %s=\"", htmlattrs[p[i].key]);
                    731:                assert(p->val);
1.32      kristaps  732:                print_encode(h, p[i].val);
1.29      kristaps  733:                printf("\"");
                    734:        }
                    735:        printf(">");
1.14      kristaps  736:
1.29      kristaps  737:        h->flags |= HTML_NOSPACE;
1.30      kristaps  738:        if (HTML_CLRLINE & htmltags[tag].flags)
                    739:                h->flags |= HTML_NEWLINE;
                    740:        else
                    741:                h->flags &= ~HTML_NEWLINE;
1.14      kristaps  742:
1.30      kristaps  743:        return(t);
1.14      kristaps  744: }
                    745:
                    746:
                    747: /* ARGSUSED */
1.29      kristaps  748: static void
                    749: print_ctag(struct html *h, enum htmltag tag)
1.14      kristaps  750: {
                    751:
1.29      kristaps  752:        printf("</%s>", htmltags[tag].name);
1.30      kristaps  753:        if (HTML_CLRLINE & htmltags[tag].flags)
1.29      kristaps  754:                h->flags |= HTML_NOSPACE;
1.30      kristaps  755:        if (HTML_CLRLINE & htmltags[tag].flags)
                    756:                h->flags |= HTML_NEWLINE;
                    757:        else
                    758:                h->flags &= ~HTML_NEWLINE;
1.14      kristaps  759: }
                    760:
                    761:
1.29      kristaps  762: /* ARGSUSED */
                    763: static void
                    764: print_gen_doctype(struct html *h)
1.1       kristaps  765: {
1.29      kristaps  766:
                    767:        printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD);
1.1       kristaps  768: }
                    769:
                    770:
1.29      kristaps  771: static void
                    772: print_text(struct html *h, const char *p)
1.1       kristaps  773: {
                    774:
1.29      kristaps  775:        if (*p && 0 == *(p + 1))
                    776:                switch (*p) {
                    777:                case('.'):
                    778:                        /* FALLTHROUGH */
                    779:                case(','):
                    780:                        /* FALLTHROUGH */
                    781:                case(';'):
                    782:                        /* FALLTHROUGH */
                    783:                case(':'):
                    784:                        /* FALLTHROUGH */
                    785:                case('?'):
                    786:                        /* FALLTHROUGH */
                    787:                case('!'):
                    788:                        /* FALLTHROUGH */
                    789:                case(')'):
                    790:                        /* FALLTHROUGH */
                    791:                case(']'):
                    792:                        /* FALLTHROUGH */
                    793:                case('}'):
                    794:                        h->flags |= HTML_NOSPACE;
1.30      kristaps  795:                        break;
1.29      kristaps  796:                default:
                    797:                        break;
                    798:                }
1.1       kristaps  799:
1.29      kristaps  800:        if ( ! (h->flags & HTML_NOSPACE))
                    801:                printf(" ");
1.30      kristaps  802:
1.29      kristaps  803:        h->flags &= ~HTML_NOSPACE;
1.30      kristaps  804:        h->flags &= ~HTML_NEWLINE;
1.1       kristaps  805:
1.29      kristaps  806:        if (p)
1.32      kristaps  807:                print_encode(h, p);
1.8       kristaps  808:
1.29      kristaps  809:        if (*p && 0 == *(p + 1))
                    810:                switch (*p) {
                    811:                case('('):
                    812:                        /* FALLTHROUGH */
                    813:                case('['):
                    814:                        /* FALLTHROUGH */
                    815:                case('{'):
                    816:                        h->flags |= HTML_NOSPACE;
1.30      kristaps  817:                        break;
1.29      kristaps  818:                default:
                    819:                        break;
                    820:                }
1.1       kristaps  821: }
1.30      kristaps  822:
                    823:
                    824: static void
                    825: print_tagq(struct html *h, const struct tag *until)
                    826: {
                    827:        struct tag      *tag;
                    828:
                    829:        while ( ! SLIST_EMPTY(&h->stack)) {
                    830:                tag = SLIST_FIRST(&h->stack);
                    831:                print_ctag(h, tag->tag);
                    832:                SLIST_REMOVE_HEAD(&h->stack, entry);
                    833:                free(tag);
                    834:                if (until && tag == until)
                    835:                        return;
                    836:        }
                    837: }
                    838:
                    839:
                    840: static void
                    841: print_stagq(struct html *h, const struct tag *suntil)
                    842: {
                    843:        struct tag      *tag;
                    844:
                    845:        while ( ! SLIST_EMPTY(&h->stack)) {
                    846:                tag = SLIST_FIRST(&h->stack);
                    847:                if (suntil && tag == suntil)
                    848:                        return;
                    849:                print_ctag(h, tag->tag);
                    850:                SLIST_REMOVE_HEAD(&h->stack, entry);
                    851:                free(tag);
                    852:        }
                    853: }
                    854:
                    855:
1.33      kristaps  856: static int
                    857: a2offs(const char *p)
                    858: {
                    859:        int              len, i;
                    860:
                    861:        if (0 == strcmp(p, "left"))
                    862:                return(0);
                    863:        if (0 == strcmp(p, "indent"))
                    864:                return(INDENT + 1);
                    865:        if (0 == strcmp(p, "indent-two"))
                    866:                return((INDENT + 1) * 2);
                    867:
                    868:        if (0 == (len = (int)strlen(p)))
                    869:                return(0);
                    870:
                    871:        for (i = 0; i < len - 1; i++)
                    872:                if ( ! isdigit((u_char)p[i]))
                    873:                        break;
                    874:
                    875:        if (i == len - 1)
                    876:                if ('n' == p[len - 1] || 'm' == p[len - 1])
                    877:                        return(atoi(p));
                    878:
                    879:        return(len);
                    880: }
                    881:
                    882:
                    883: static int
                    884: a2width(const char *p)
                    885: {
                    886:        int              i, len;
                    887:
                    888:        if (0 == (len = (int)strlen(p)))
                    889:                return(0);
                    890:        for (i = 0; i < len - 1; i++)
                    891:                if ( ! isdigit((u_char)p[i]))
                    892:                        break;
                    893:
                    894:        if (i == len - 1)
                    895:                if ('n' == p[len - 1] || 'm' == p[len - 1])
                    896:                        return(atoi(p) + 2);
                    897:
                    898:        return(len + 2);
                    899: }
                    900:
                    901:
                    902:
                    903:
1.30      kristaps  904: /* ARGSUSED */
                    905: static int
                    906: mdoc_root_pre(MDOC_ARGS)
                    907: {
                    908:        struct htmlpair  tag;
                    909:
                    910:        tag.key = ATTR_CLASS;
                    911:        tag.val = "body";
                    912:
                    913:        print_otag(h, TAG_DIV, 1, &tag);
                    914:        return(1);
                    915: }
                    916:
                    917:
                    918: /* ARGSUSED */
                    919: static int
                    920: mdoc_ss_pre(MDOC_ARGS)
                    921: {
1.34    ! kristaps  922:        struct htmlpair tag[2];
        !           923:
        !           924:        tag[0].key = ATTR_CLASS;
        !           925:        tag[0].val = "ssec";
        !           926:
        !           927:        tag[1].key = ATTR_STYLE;
        !           928:        tag[1].val = "margin-left: -20px;";
1.30      kristaps  929:
                    930:        if (MDOC_BODY == n->type)
1.34    ! kristaps  931:                print_otag(h, TAG_DIV, 1, &tag);
1.30      kristaps  932:        if (MDOC_HEAD == n->type)
1.34    ! kristaps  933:                print_otag(h, TAG_SPAN, 1, &tag);
1.30      kristaps  934:        return(1);
                    935: }
                    936:
                    937:
                    938: /* ARGSUSED */
                    939: static int
                    940: mdoc_fl_pre(MDOC_ARGS)
                    941: {
                    942:        struct htmlpair  tag;
                    943:
                    944:        tag.key = ATTR_CLASS;
                    945:        tag.val = "flag";
                    946:
                    947:        print_otag(h, TAG_SPAN, 1, &tag);
                    948:        print_text(h, "\\-");
                    949:        h->flags |= HTML_NOSPACE;
                    950:        return(1);
                    951: }
                    952:
                    953:
                    954: /* ARGSUSED */
                    955: static int
                    956: mdoc_pp_pre(MDOC_ARGS)
                    957: {
1.34    ! kristaps  958:        struct htmlpair tag;
1.30      kristaps  959:
1.34    ! kristaps  960:        tag.key = ATTR_STYLE;
        !           961:        tag.val = "clear: both;";
        !           962:
        !           963:        print_otag(h, TAG_BR, 1, &tag);
        !           964:        print_otag(h, TAG_BR, 1, &tag);
1.30      kristaps  965:        return(0);
                    966: }
                    967:
                    968:
                    969: /* ARGSUSED */
                    970: static int
                    971: mdoc_nd_pre(MDOC_ARGS)
                    972: {
                    973:
                    974:        if (MDOC_BODY == n->type)
1.31      kristaps  975:                print_text(h, "\\(en");
1.30      kristaps  976:        return(1);
                    977: }
                    978:
                    979:
                    980: /* ARGSUSED */
                    981: static int
                    982: mdoc_op_pre(MDOC_ARGS)
                    983: {
                    984:
                    985:        if (MDOC_BODY == n->type) {
                    986:                print_text(h, "\\(lB");
                    987:                h->flags |= HTML_NOSPACE;
                    988:        }
                    989:        return(1);
                    990: }
                    991:
                    992:
                    993: /* ARGSUSED */
                    994: static void
                    995: mdoc_op_post(MDOC_ARGS)
                    996: {
                    997:
                    998:        if (MDOC_BODY != n->type)
                    999:                return;
                   1000:        h->flags |= HTML_NOSPACE;
                   1001:        print_text(h, "\\(rB");
                   1002: }
                   1003:
                   1004:
                   1005: static int
                   1006: mdoc_nm_pre(MDOC_ARGS)
                   1007: {
                   1008:        struct htmlpair class;
                   1009:
                   1010:        if ( ! (HTML_NEWLINE & h->flags))
                   1011:                if (SEC_SYNOPSIS == n->sec)
                   1012:                        print_otag(h, TAG_BR, 0, NULL);
                   1013:
                   1014:        class.key = ATTR_CLASS;
                   1015:        class.val = "name";
                   1016:
                   1017:        print_otag(h, TAG_SPAN, 1, &class);
                   1018:        if (NULL == n->child)
                   1019:                print_text(h, m->name);
                   1020:
                   1021:        return(1);
                   1022: }
                   1023:
                   1024:
                   1025: /* ARGSUSED */
                   1026: static int
                   1027: mdoc_sh_pre(MDOC_ARGS)
                   1028: {
1.34    ! kristaps 1029:        struct htmlpair tag;
        !          1030:
        !          1031:        tag.key = ATTR_CLASS;
        !          1032:        tag.val = "sec";
1.30      kristaps 1033:
                   1034:        if (MDOC_BODY == n->type)
1.34    ! kristaps 1035:                print_otag(h, TAG_DIV, 1, &tag);
1.30      kristaps 1036:        if (MDOC_HEAD == n->type)
1.34    ! kristaps 1037:                print_otag(h, TAG_SPAN, 1, &tag);
1.30      kristaps 1038:        return(1);
                   1039: }
                   1040:
                   1041:
                   1042: /* ARGSUSED */
                   1043: static int
                   1044: mdoc_xr_pre(MDOC_ARGS)
                   1045: {
                   1046:        struct htmlpair tag;
                   1047:
                   1048:        tag.key = ATTR_HREF;
                   1049:        tag.val = "#"; /* TODO */
                   1050:
                   1051:        print_otag(h, TAG_A, 1, &tag);
                   1052:
                   1053:        n = n->child;
                   1054:        print_text(h, n->string);
                   1055:        if (NULL == (n = n->next))
                   1056:                return(0);
                   1057:
                   1058:        h->flags |= HTML_NOSPACE;
                   1059:        print_text(h, "(");
                   1060:        h->flags |= HTML_NOSPACE;
                   1061:        print_text(h, n->string);
                   1062:        h->flags |= HTML_NOSPACE;
                   1063:        print_text(h, ")");
                   1064:
                   1065:        return(0);
                   1066: }
1.31      kristaps 1067:
                   1068:
                   1069: /* ARGSUSED */
                   1070: static int
                   1071: mdoc_ns_pre(MDOC_ARGS)
                   1072: {
                   1073:
                   1074:        h->flags |= HTML_NOSPACE;
                   1075:        return(1);
                   1076: }
                   1077:
                   1078: /* ARGSUSED */
                   1079: static int
                   1080: mdoc_ar_pre(MDOC_ARGS)
                   1081: {
                   1082:        struct htmlpair tag;
                   1083:
                   1084:        tag.key = ATTR_CLASS;
                   1085:        tag.val = "arg";
                   1086:
                   1087:        print_otag(h, TAG_SPAN, 1, &tag);
                   1088:        return(1);
                   1089: }
1.33      kristaps 1090:
                   1091: /* ARGSUSED */
                   1092: static int
                   1093: mdoc_xx_pre(MDOC_ARGS)
                   1094: {
                   1095:        const char      *pp;
                   1096:
                   1097:        switch (n->tok) {
                   1098:        case (MDOC_Bsx):
                   1099:                pp = "BSDI BSD/OS";
                   1100:                break;
                   1101:        case (MDOC_Dx):
                   1102:                pp = "DragonFlyBSD";
                   1103:                break;
                   1104:        case (MDOC_Fx):
                   1105:                pp = "FreeBSD";
                   1106:                break;
                   1107:        case (MDOC_Nx):
                   1108:                pp = "NetBSD";
                   1109:                break;
                   1110:        case (MDOC_Ox):
                   1111:                pp = "OpenBSD";
                   1112:                break;
                   1113:        case (MDOC_Ux):
                   1114:                pp = "UNIX";
                   1115:                break;
                   1116:        default:
                   1117:                return(1);
                   1118:        }
                   1119:
                   1120:        print_text(h, pp);
                   1121:        return(1);
                   1122: }
                   1123:
                   1124:
                   1125: static int
1.34    ! kristaps 1126: mdoc_tbl_block_pre(MDOC_ARGS, int w, int o, int c)
        !          1127: {
        !          1128:        struct htmlpair  tag;
        !          1129:        char             buf[BUFSIZ];
        !          1130:
        !          1131:        buf[BUFSIZ - 1] = 0;
        !          1132:
        !          1133:        snprintf(buf, BUFSIZ - 1, "margin-left: %dpx; "
        !          1134:                        "clear: both;", w + o);
        !          1135:
        !          1136:        if ( ! c)
        !          1137:                (void)strlcat(buf, " padding-top: 1em;", BUFSIZ);
        !          1138:
        !          1139:        tag.key = ATTR_STYLE;
        !          1140:        tag.val = buf;
        !          1141:
        !          1142:        print_otag(h, TAG_DIV, 1, &tag);
        !          1143:        return(1);
        !          1144: }
        !          1145:
        !          1146:
        !          1147: static int
        !          1148: mdoc_tbl_body_pre(MDOC_ARGS, int t, int w)
        !          1149: {
        !          1150:        struct htmlpair  tag;
        !          1151:        char             buf[BUFSIZ];
        !          1152:        int              i;
        !          1153:
        !          1154:        buf[BUFSIZ - 1] = 0;
        !          1155:        i = 0;
        !          1156:
        !          1157:        switch (t) {
        !          1158:        case (MDOC_Tag):
        !          1159:                i++;
        !          1160:                (void)snprintf(buf, BUFSIZ - 1,
        !          1161:                                "clear: right; float: left; "
        !          1162:                                "width: 100%%;");
        !          1163:                tag.key = ATTR_STYLE;
        !          1164:                tag.val = buf;
        !          1165:                break;
        !          1166:        default:
        !          1167:                break;
        !          1168:        }
        !          1169:
        !          1170:        print_otag(h, TAG_DIV, i, &tag);
        !          1171:        return(1);
        !          1172: }
        !          1173:
        !          1174:
        !          1175: static int
        !          1176: mdoc_tbl_head_pre(MDOC_ARGS, int type, int w)
1.33      kristaps 1177: {
1.34    ! kristaps 1178:        struct htmlpair  tag;
        !          1179:        char             buf[BUFSIZ];
        !          1180:        int              i;
        !          1181:
        !          1182:        buf[BUFSIZ - 1] = 0;
        !          1183:        i = 0;
1.33      kristaps 1184:
1.34    ! kristaps 1185:        switch (type) {
        !          1186:        case (MDOC_Tag):
        !          1187:                i++;
        !          1188:                (void)snprintf(buf, BUFSIZ - 1,
        !          1189:                                "clear: left; float: left; "
        !          1190:                                "padding-right: 1em; "
        !          1191:                                "margin-left: -%dpx;", w);
        !          1192:                tag.key = ATTR_STYLE;
        !          1193:                tag.val = buf;
        !          1194:                break;
        !          1195:        default:
        !          1196:                i++;
        !          1197:                (void)snprintf(buf, BUFSIZ - 1,
        !          1198:                                "clear: left; float: left; "
        !          1199:                                "margin-left: -%dpx; "
        !          1200:                                "padding-right: 1em;", w);
        !          1201:                tag.key = ATTR_STYLE;
        !          1202:                tag.val = buf;
        !          1203:                break;
1.33      kristaps 1204:        }
                   1205:
1.34    ! kristaps 1206:        print_otag(h, TAG_DIV, i, &tag);
        !          1207:        return(1);
        !          1208: }
        !          1209:
        !          1210:
        !          1211: static int
        !          1212: mdoc_tbl_pre(MDOC_ARGS, int type)
        !          1213: {
        !          1214:        int                      i, w, o, c;
        !          1215:        const struct mdoc_node  *bl;
        !          1216:
1.33      kristaps 1217:        bl = n->parent->parent;
                   1218:        if (MDOC_BLOCK != n->type)
                   1219:                bl = bl->parent;
                   1220:
1.34    ! kristaps 1221:        /* FIXME: fmt_vspace() equivalent. */
        !          1222:
1.33      kristaps 1223:        assert(bl->args);
                   1224:
1.34    ! kristaps 1225:        w = o = c = 0;
        !          1226:
        !          1227:        for (i = 0; i < (int)bl->args->argc; i++)
1.33      kristaps 1228:                if (MDOC_Width == bl->args->argv[i].arg) {
                   1229:                        assert(bl->args->argv[i].sz);
1.34    ! kristaps 1230:                        w = a2width(bl->args->argv[i].value[0]);
        !          1231:                } else if (MDOC_Offset == bl->args->argv[i].arg) {
        !          1232:                        assert(bl->args->argv[i].sz);
        !          1233:                        o = a2offs(bl->args->argv[i].value[0]);
        !          1234:                } else if (MDOC_Compact == bl->args->argv[i].arg)
        !          1235:                        c = 1;
        !          1236:
        !          1237:        if (0 == w)
        !          1238:                w = 10;
        !          1239:
        !          1240:        w *= PX_MULT;
        !          1241:        o *= PX_MULT;
        !          1242:
        !          1243:        switch (n->type) {
        !          1244:        case (MDOC_BLOCK):
        !          1245:                break;
        !          1246:        case (MDOC_HEAD):
        !          1247:                return(mdoc_tbl_head_pre(m, n, h, type, w));
        !          1248:        case (MDOC_BODY):
        !          1249:                return(mdoc_tbl_body_pre(m, n, h, type, w));
        !          1250:        default:
        !          1251:                abort();
        !          1252:                /* NOTREACHED */
        !          1253:        }
        !          1254:
        !          1255:        return(mdoc_tbl_block_pre(m, n, h, w, o, c));
        !          1256: }
        !          1257:
        !          1258:
        !          1259: /* ARGSUSED */
        !          1260: static int
        !          1261: mdoc_listitem_pre(MDOC_ARGS)
        !          1262: {
        !          1263:        int                      i, w, o, c;
        !          1264:        const struct mdoc_node  *bl;
        !          1265:        struct htmlpair          tag;
        !          1266:        char                     buf[BUFSIZ];
        !          1267:
        !          1268:        /* FIXME: fmt_vspace() equivalent. */
        !          1269:
        !          1270:        if (MDOC_BLOCK != n->type)
        !          1271:                return(1);
        !          1272:
        !          1273:        bl = n->parent->parent;
        !          1274:        assert(bl);
        !          1275:
        !          1276:        w = o = c = 0;
        !          1277:
        !          1278:        for (i = 0; i < (int)bl->args->argc; i++)
        !          1279:                if (MDOC_Width == bl->args->argv[i].arg) {
        !          1280:                        assert(bl->args->argv[i].sz);
        !          1281:                        w = a2width(bl->args->argv[i].value[0]);
1.33      kristaps 1282:                } else if (MDOC_Offset == bl->args->argv[i].arg) {
                   1283:                        assert(bl->args->argv[i].sz);
1.34    ! kristaps 1284:                        o = a2offs(bl->args->argv[i].value[0]);
        !          1285:                } else if (MDOC_Compact == bl->args->argv[i].arg)
        !          1286:                        c = 1;
        !          1287:
        !          1288:        o *= PX_MULT;
        !          1289:        w *= PX_MULT;
1.33      kristaps 1290:
1.34    ! kristaps 1291:        buf[BUFSIZ - 1] = 0;
1.33      kristaps 1292:
1.34    ! kristaps 1293:        snprintf(buf, BUFSIZ - 1, "margin-left: %dpx;", o);
1.33      kristaps 1294:
1.34    ! kristaps 1295:        if ( ! c)
        !          1296:                (void)strlcat(buf, " padding-top: 1em;", BUFSIZ);
1.33      kristaps 1297:
                   1298:        tag.key = ATTR_STYLE;
                   1299:        tag.val = buf;
                   1300:
1.34    ! kristaps 1301:        print_otag(h, TAG_LI, 1, &tag);
1.33      kristaps 1302:        return(1);
                   1303: }
                   1304:
                   1305:
1.34    ! kristaps 1306: /* ARGSUSED */
1.33      kristaps 1307: static int
1.34    ! kristaps 1308: mdoc_list_pre(MDOC_ARGS, int type)
1.33      kristaps 1309: {
                   1310:
1.34    ! kristaps 1311:        switch (type) {
        !          1312:        case (MDOC_Enum):
        !          1313:                print_otag(h, TAG_OL, 0, NULL);
        !          1314:                break;
        !          1315:        case (MDOC_Bullet):
        !          1316:                print_otag(h, TAG_UL, 0, NULL);
        !          1317:                break;
        !          1318:        default:
        !          1319:                break;
        !          1320:        }
        !          1321:
        !          1322:        return(1);
1.33      kristaps 1323: }
                   1324:
                   1325:
                   1326: static int
1.34    ! kristaps 1327: mdoc_bl_pre(MDOC_ARGS)
1.33      kristaps 1328: {
1.34    ! kristaps 1329:        int             i, len, type;
1.33      kristaps 1330:
1.34    ! kristaps 1331:        if (MDOC_BLOCK != n->type)
        !          1332:                return(1);
1.33      kristaps 1333:
1.34    ! kristaps 1334:        assert(n->args);
        !          1335:        len = (int)n->args->argc;
1.33      kristaps 1336:
                   1337:        for (i = 0; i < len; i++)
1.34    ! kristaps 1338:                switch ((type = n->args->argv[i].arg)) {
        !          1339:                case (MDOC_Enum):
        !          1340:                        /* FALLTHROUGH */
1.33      kristaps 1341:                case (MDOC_Bullet):
1.34    ! kristaps 1342:                        return(mdoc_list_pre(m, n, h, type));
        !          1343:                case (MDOC_Tag):
        !          1344:                        /* FALLTHROUGH */
        !          1345:                case (MDOC_Hang):
1.33      kristaps 1346:                        /* FALLTHROUGH */
                   1347:                case (MDOC_Dash):
                   1348:                        /* FALLTHROUGH */
1.34    ! kristaps 1349:                case (MDOC_Hyphen):
1.33      kristaps 1350:                        /* FALLTHROUGH */
                   1351:                case (MDOC_Inset):
                   1352:                        /* FALLTHROUGH */
                   1353:                case (MDOC_Diag):
                   1354:                        /* FALLTHROUGH */
                   1355:                case (MDOC_Item):
                   1356:                        /* FALLTHROUGH */
                   1357:                case (MDOC_Column):
                   1358:                        /* FALLTHROUGH */
                   1359:                case (MDOC_Ohang):
1.34    ! kristaps 1360:                        return(1);
1.33      kristaps 1361:                default:
1.34    ! kristaps 1362:                        break;
1.33      kristaps 1363:                }
                   1364:
                   1365:        abort();
                   1366:        /* NOTREACHED */
                   1367: }
                   1368:
                   1369:
                   1370: static int
1.34    ! kristaps 1371: mdoc_it_pre(MDOC_ARGS)
1.33      kristaps 1372: {
1.34    ! kristaps 1373:        int                      i, len, type;
        !          1374:        const struct mdoc_node  *bl;
1.33      kristaps 1375:
1.34    ! kristaps 1376:        if (MDOC_BLOCK == n->type)
        !          1377:                bl = n->parent->parent;
        !          1378:        else
        !          1379:                bl = n->parent->parent->parent;
1.33      kristaps 1380:
1.34    ! kristaps 1381:        assert(bl->args);
        !          1382:        len = (int)bl->args->argc;
1.33      kristaps 1383:
                   1384:        for (i = 0; i < len; i++)
1.34    ! kristaps 1385:                switch ((type = bl->args->argv[i].arg)) {
        !          1386:                case (MDOC_Tag):
        !          1387:                        /* FALLTHROUGH */
        !          1388:                case (MDOC_Hang):
        !          1389:                        return(mdoc_tbl_pre(m, n, h, type));
        !          1390:                case (MDOC_Enum):
        !          1391:                        /* FALLTHROUGH */
1.33      kristaps 1392:                case (MDOC_Bullet):
1.34    ! kristaps 1393:                        return(mdoc_listitem_pre(m, n, h));
1.33      kristaps 1394:                case (MDOC_Dash):
                   1395:                        /* FALLTHROUGH */
1.34    ! kristaps 1396:                case (MDOC_Hyphen):
1.33      kristaps 1397:                        /* FALLTHROUGH */
                   1398:                case (MDOC_Inset):
                   1399:                        /* FALLTHROUGH */
                   1400:                case (MDOC_Diag):
                   1401:                        /* FALLTHROUGH */
                   1402:                case (MDOC_Item):
                   1403:                        /* FALLTHROUGH */
                   1404:                case (MDOC_Column):
                   1405:                        /* FALLTHROUGH */
                   1406:                case (MDOC_Ohang):
1.34    ! kristaps 1407:                        return(0);
1.33      kristaps 1408:                default:
1.34    ! kristaps 1409:                        break;
1.33      kristaps 1410:                }
                   1411:
                   1412:        abort();
                   1413:        /* NOTREACHED */
                   1414: }
1.34    ! kristaps 1415:
        !          1416:
        !          1417: /* ARGSUSED */
        !          1418: static int
        !          1419: mdoc_ex_pre(MDOC_ARGS)
        !          1420: {
        !          1421:        const struct mdoc_node  *nn;
        !          1422:        struct tag              *t;
        !          1423:        struct htmlpair          tag;
        !          1424:
        !          1425:        print_text(h, "The");
        !          1426:
        !          1427:        tag.key = ATTR_CLASS;
        !          1428:        tag.val = "utility";
        !          1429:
        !          1430:        for (nn = n->child; nn; nn = nn->next) {
        !          1431:                t = print_otag(h, TAG_SPAN, 1, &tag);
        !          1432:                print_text(h, nn->string);
        !          1433:                print_tagq(h, t);
        !          1434:
        !          1435:                h->flags |= HTML_NOSPACE;
        !          1436:
        !          1437:                if (nn->next && NULL == nn->next->next)
        !          1438:                        print_text(h, ", and");
        !          1439:                else if (nn->next)
        !          1440:                        print_text(h, ",");
        !          1441:                else
        !          1442:                        h->flags &= ~HTML_NOSPACE;
        !          1443:        }
        !          1444:
        !          1445:        if (n->child->next)
        !          1446:                print_text(h, "utilities exit");
        !          1447:        else
        !          1448:                print_text(h, "utility exits");
        !          1449:
        !          1450:                print_text(h, "0 on success, and >0 if an error occurs.");
        !          1451:        return(0);
        !          1452: }
        !          1453:
        !          1454:
        !          1455: /* ARGSUSED */
        !          1456: static int
        !          1457: mdoc_dq_pre(MDOC_ARGS)
        !          1458: {
        !          1459:
        !          1460:        if (MDOC_BODY != n->type)
        !          1461:                return(1);
        !          1462:        print_text(h, "\\(lq");
        !          1463:        h->flags |= HTML_NOSPACE;
        !          1464:        return(1);
        !          1465: }
        !          1466:
        !          1467:
        !          1468: /* ARGSUSED */
        !          1469: static void
        !          1470: mdoc_dq_post(MDOC_ARGS)
        !          1471: {
        !          1472:
        !          1473:        if (MDOC_BODY != n->type)
        !          1474:                return;
        !          1475:        h->flags |= HTML_NOSPACE;
        !          1476:        print_text(h, "\\(rq");
        !          1477: }
        !          1478:
        !          1479:
        !          1480: /* ARGSUSED */
        !          1481: static int
        !          1482: mdoc_pq_pre(MDOC_ARGS)
        !          1483: {
        !          1484:
        !          1485:        if (MDOC_BODY != n->type)
        !          1486:                return(1);
        !          1487:        print_text(h, "\\&(");
        !          1488:        h->flags |= HTML_NOSPACE;
        !          1489:        return(1);
        !          1490: }
        !          1491:
        !          1492:
        !          1493: /* ARGSUSED */
        !          1494: static void
        !          1495: mdoc_pq_post(MDOC_ARGS)
        !          1496: {
        !          1497:
        !          1498:        if (MDOC_BODY != n->type)
        !          1499:                return;
        !          1500:        print_text(h, ")");
        !          1501: }
        !          1502:
        !          1503:
        !          1504: /* ARGSUSED */
        !          1505: static int
        !          1506: mdoc_sq_pre(MDOC_ARGS)
        !          1507: {
        !          1508:
        !          1509:        if (MDOC_BODY != n->type)
        !          1510:                return(1);
        !          1511:        print_text(h, "\\(oq");
        !          1512:        h->flags |= HTML_NOSPACE;
        !          1513:        return(1);
        !          1514: }
        !          1515:
        !          1516:
        !          1517: /* ARGSUSED */
        !          1518: static void
        !          1519: mdoc_sq_post(MDOC_ARGS)
        !          1520: {
        !          1521:
        !          1522:        if (MDOC_BODY != n->type)
        !          1523:                return;
        !          1524:        h->flags |= HTML_NOSPACE;
        !          1525:        print_text(h, "\\(aq");
        !          1526: }
        !          1527:
        !          1528:
        !          1529: /* ARGSUSED */
        !          1530: static int
        !          1531: mdoc_em_pre(MDOC_ARGS)
        !          1532: {
        !          1533:        struct htmlpair tag;
        !          1534:
        !          1535:        tag.key = ATTR_CLASS;
        !          1536:        tag.val = "emph";
        !          1537:
        !          1538:        print_otag(h, TAG_SPAN, 1, &tag);
        !          1539:        return(1);
        !          1540: }
        !          1541:
        !          1542:
        !          1543: /* ARGSUSED */
        !          1544: static int
        !          1545: mdoc_d1_pre(MDOC_ARGS)
        !          1546: {
        !          1547:        struct htmlpair tag;
        !          1548:        char            buf[BUFSIZ];
        !          1549:
        !          1550:        if (MDOC_BLOCK != n->type)
        !          1551:                return(1);
        !          1552:
        !          1553:        (void)snprintf(buf, BUFSIZ - 1, "margin-left: %dpx",
        !          1554:                        INDENT * PX_MULT);
        !          1555:
        !          1556:        tag.key = ATTR_STYLE;
        !          1557:        tag.val = buf;
        !          1558:
        !          1559:        print_otag(h, TAG_DIV, 1, &tag);
        !          1560:        return(1);
        !          1561: }
        !          1562:
        !          1563:
        !          1564: /* ARGSUSED */
        !          1565: static int
        !          1566: mdoc_sx_pre(MDOC_ARGS)
        !          1567: {
        !          1568:        struct htmlpair tag;
        !          1569:
        !          1570:        tag.key = ATTR_HREF;
        !          1571:        tag.val = "#";
        !          1572:
        !          1573:        print_otag(h, TAG_A, 1, &tag);
        !          1574:        return(1);
        !          1575: }

CVSweb