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

Annotation of mandoc/html.c, Revision 1.218

1.218   ! schwarze    1: /*     $Id: html.c,v 1.217 2017/07/14 16:06:44 schwarze Exp $ */
1.1       kristaps    2: /*
1.176     schwarze    3:  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.194     schwarze    4:  * Copyright (c) 2011-2015, 2017 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.165     kristaps  171:
1.168     kristaps  172:        /*
                    173:         * Print a default style-sheet.
                    174:         */
1.196     schwarze  175:
1.194     schwarze  176:        t = print_otag(h, TAG_STYLE, "");
1.196     schwarze  177:        print_text(h, "table.head, table.foot { width: 100%; }");
1.197     schwarze  178:        print_endline(h);
1.196     schwarze  179:        print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197     schwarze  180:        print_endline(h);
1.196     schwarze  181:        print_text(h, "td.head-vol { text-align: center; }");
1.197     schwarze  182:        print_endline(h);
1.198     schwarze  183:        print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.165     kristaps  184:        print_tagq(h, t);
1.41      kristaps  185:
1.194     schwarze  186:        if (h->style)
                    187:                print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
                    188:                    h->style, "type", "text/css", "media", "all");
1.4       kristaps  189: }
                    190:
1.126     schwarze  191: static void
1.132     kristaps  192: print_metaf(struct html *h, enum mandoc_esc deco)
1.88      kristaps  193: {
1.90      kristaps  194:        enum htmlfont    font;
1.88      kristaps  195:
                    196:        switch (deco) {
1.156     schwarze  197:        case ESCAPE_FONTPREV:
1.90      kristaps  198:                font = h->metal;
1.88      kristaps  199:                break;
1.156     schwarze  200:        case ESCAPE_FONTITALIC:
1.90      kristaps  201:                font = HTMLFONT_ITALIC;
                    202:                break;
1.156     schwarze  203:        case ESCAPE_FONTBOLD:
1.90      kristaps  204:                font = HTMLFONT_BOLD;
1.88      kristaps  205:                break;
1.156     schwarze  206:        case ESCAPE_FONTBI:
1.152     schwarze  207:                font = HTMLFONT_BI;
                    208:                break;
1.156     schwarze  209:        case ESCAPE_FONT:
                    210:        case ESCAPE_FONTROMAN:
1.90      kristaps  211:                font = HTMLFONT_NONE;
1.88      kristaps  212:                break;
                    213:        default:
                    214:                abort();
                    215:        }
                    216:
1.122     kristaps  217:        if (h->metaf) {
                    218:                print_tagq(h, h->metaf);
                    219:                h->metaf = NULL;
                    220:        }
                    221:
                    222:        h->metal = h->metac;
                    223:        h->metac = font;
                    224:
1.152     schwarze  225:        switch (font) {
1.156     schwarze  226:        case HTMLFONT_ITALIC:
1.194     schwarze  227:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  228:                break;
1.156     schwarze  229:        case HTMLFONT_BOLD:
1.194     schwarze  230:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  231:                break;
1.156     schwarze  232:        case HTMLFONT_BI:
1.194     schwarze  233:                h->metaf = print_otag(h, TAG_B, "");
                    234:                print_otag(h, TAG_I, "");
1.152     schwarze  235:                break;
                    236:        default:
                    237:                break;
                    238:        }
1.210     schwarze  239: }
                    240:
                    241: char *
                    242: html_make_id(const struct roff_node *n)
                    243: {
                    244:        const struct roff_node  *nch;
                    245:        char                    *buf, *cp;
                    246:
                    247:        for (nch = n->child; nch != NULL; nch = nch->next)
                    248:                if (nch->type != ROFFT_TEXT)
                    249:                        return NULL;
                    250:
                    251:        buf = NULL;
                    252:        deroff(&buf, n);
                    253:
                    254:        /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
                    255:
                    256:        for (cp = buf; *cp != '\0'; cp++)
                    257:                if (*cp == ' ')
                    258:                        *cp = '_';
                    259:
                    260:        return buf;
1.88      kristaps  261: }
                    262:
1.138     kristaps  263: int
                    264: html_strlen(const char *cp)
                    265: {
1.151     schwarze  266:        size_t           rsz;
                    267:        int              skip, sz;
1.138     kristaps  268:
                    269:        /*
                    270:         * Account for escaped sequences within string length
                    271:         * calculations.  This follows the logic in term_strlen() as we
                    272:         * must calculate the width of produced strings.
                    273:         * Assume that characters are always width of "1".  This is
                    274:         * hacky, but it gets the job done for approximation of widths.
                    275:         */
                    276:
                    277:        sz = 0;
1.151     schwarze  278:        skip = 0;
                    279:        while (1) {
                    280:                rsz = strcspn(cp, "\\");
                    281:                if (rsz) {
                    282:                        cp += rsz;
                    283:                        if (skip) {
                    284:                                skip = 0;
                    285:                                rsz--;
                    286:                        }
                    287:                        sz += rsz;
                    288:                }
                    289:                if ('\0' == *cp)
                    290:                        break;
                    291:                cp++;
                    292:                switch (mandoc_escape(&cp, NULL, NULL)) {
1.156     schwarze  293:                case ESCAPE_ERROR:
1.188     schwarze  294:                        return sz;
1.156     schwarze  295:                case ESCAPE_UNICODE:
                    296:                case ESCAPE_NUMBERED:
                    297:                case ESCAPE_SPECIAL:
1.185     schwarze  298:                case ESCAPE_OVERSTRIKE:
1.151     schwarze  299:                        if (skip)
                    300:                                skip = 0;
                    301:                        else
                    302:                                sz++;
                    303:                        break;
1.156     schwarze  304:                case ESCAPE_SKIPCHAR:
1.151     schwarze  305:                        skip = 1;
1.138     kristaps  306:                        break;
                    307:                default:
                    308:                        break;
                    309:                }
                    310:        }
1.188     schwarze  311:        return sz;
1.138     kristaps  312: }
1.88      kristaps  313:
1.85      kristaps  314: static int
1.197     schwarze  315: print_escape(struct html *h, char c)
1.159     schwarze  316: {
                    317:
                    318:        switch (c) {
                    319:        case '<':
1.197     schwarze  320:                print_word(h, "&lt;");
1.159     schwarze  321:                break;
                    322:        case '>':
1.197     schwarze  323:                print_word(h, "&gt;");
1.159     schwarze  324:                break;
                    325:        case '&':
1.197     schwarze  326:                print_word(h, "&amp;");
1.159     schwarze  327:                break;
                    328:        case '"':
1.197     schwarze  329:                print_word(h, "&quot;");
1.159     schwarze  330:                break;
                    331:        case ASCII_NBRSP:
1.197     schwarze  332:                print_word(h, "&nbsp;");
1.159     schwarze  333:                break;
                    334:        case ASCII_HYPH:
1.197     schwarze  335:                print_byte(h, '-');
1.189     schwarze  336:                break;
1.159     schwarze  337:        case ASCII_BREAK:
                    338:                break;
                    339:        default:
1.188     schwarze  340:                return 0;
1.159     schwarze  341:        }
1.188     schwarze  342:        return 1;
1.159     schwarze  343: }
                    344:
                    345: static int
1.195     schwarze  346: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29      kristaps  347: {
1.197     schwarze  348:        char             numbuf[16];
1.214     schwarze  349:        struct tag      *t;
                    350:        const char      *seq;
1.77      kristaps  351:        size_t           sz;
1.214     schwarze  352:        int              c, len, breakline, nospace;
1.132     kristaps  353:        enum mandoc_esc  esc;
1.214     schwarze  354:        static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154     schwarze  355:                ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14      kristaps  356:
1.195     schwarze  357:        if (pend == NULL)
                    358:                pend = strchr(p, '\0');
                    359:
1.214     schwarze  360:        breakline = 0;
1.85      kristaps  361:        nospace = 0;
                    362:
1.195     schwarze  363:        while (p < pend) {
1.151     schwarze  364:                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
                    365:                        h->flags &= ~HTML_SKIPCHAR;
                    366:                        p++;
                    367:                        continue;
                    368:                }
                    369:
1.197     schwarze  370:                for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214     schwarze  371:                        print_byte(h, *p);
                    372:
                    373:                if (breakline &&
                    374:                    (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
                    375:                        t = print_otag(h, TAG_DIV, "");
                    376:                        print_text(h, "\\~");
                    377:                        print_tagq(h, t);
                    378:                        breakline = 0;
                    379:                        while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
                    380:                                p++;
                    381:                        continue;
                    382:                }
1.77      kristaps  383:
1.195     schwarze  384:                if (p >= pend)
1.132     kristaps  385:                        break;
                    386:
1.214     schwarze  387:                if (*p == ' ') {
                    388:                        print_endword(h);
                    389:                        p++;
                    390:                        continue;
                    391:                }
                    392:
1.197     schwarze  393:                if (print_escape(h, *p++))
1.154     schwarze  394:                        continue;
1.77      kristaps  395:
1.132     kristaps  396:                esc = mandoc_escape(&p, &seq, &len);
                    397:                if (ESCAPE_ERROR == esc)
                    398:                        break;
1.82      kristaps  399:
1.132     kristaps  400:                switch (esc) {
1.156     schwarze  401:                case ESCAPE_FONT:
                    402:                case ESCAPE_FONTPREV:
                    403:                case ESCAPE_FONTBOLD:
                    404:                case ESCAPE_FONTITALIC:
                    405:                case ESCAPE_FONTBI:
                    406:                case ESCAPE_FONTROMAN:
1.151     schwarze  407:                        if (0 == norecurse)
                    408:                                print_metaf(h, esc);
                    409:                        continue;
1.156     schwarze  410:                case ESCAPE_SKIPCHAR:
1.151     schwarze  411:                        h->flags |= HTML_SKIPCHAR;
                    412:                        continue;
                    413:                default:
                    414:                        break;
                    415:                }
                    416:
                    417:                if (h->flags & HTML_SKIPCHAR) {
                    418:                        h->flags &= ~HTML_SKIPCHAR;
                    419:                        continue;
                    420:                }
                    421:
                    422:                switch (esc) {
1.156     schwarze  423:                case ESCAPE_UNICODE:
1.159     schwarze  424:                        /* Skip past "u" header. */
1.144     kristaps  425:                        c = mchars_num2uc(seq + 1, len - 1);
                    426:                        break;
1.156     schwarze  427:                case ESCAPE_NUMBERED:
1.141     kristaps  428:                        c = mchars_num2char(seq, len);
1.181     schwarze  429:                        if (c < 0)
                    430:                                continue;
1.82      kristaps  431:                        break;
1.156     schwarze  432:                case ESCAPE_SPECIAL:
1.191     schwarze  433:                        c = mchars_spec2cp(seq, len);
1.181     schwarze  434:                        if (c <= 0)
                    435:                                continue;
1.132     kristaps  436:                        break;
1.214     schwarze  437:                case ESCAPE_BREAK:
                    438:                        breakline = 1;
                    439:                        continue;
1.156     schwarze  440:                case ESCAPE_NOSPACE:
1.132     kristaps  441:                        if ('\0' == *p)
                    442:                                nospace = 1;
1.179     schwarze  443:                        continue;
1.185     schwarze  444:                case ESCAPE_OVERSTRIKE:
                    445:                        if (len == 0)
                    446:                                continue;
                    447:                        c = seq[len - 1];
                    448:                        break;
1.82      kristaps  449:                default:
1.179     schwarze  450:                        continue;
1.82      kristaps  451:                }
1.181     schwarze  452:                if ((c < 0x20 && c != 0x09) ||
                    453:                    (c > 0x7E && c < 0xA0))
1.179     schwarze  454:                        c = 0xFFFD;
1.197     schwarze  455:                if (c > 0x7E) {
1.216     schwarze  456:                        (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197     schwarze  457:                        print_word(h, numbuf);
                    458:                } else if (print_escape(h, c) == 0)
                    459:                        print_byte(h, c);
1.32      kristaps  460:        }
1.85      kristaps  461:
1.188     schwarze  462:        return nospace;
1.14      kristaps  463: }
                    464:
1.94      kristaps  465: static void
1.195     schwarze  466: print_href(struct html *h, const char *name, const char *sec, int man)
1.94      kristaps  467: {
1.195     schwarze  468:        const char      *p, *pp;
                    469:
                    470:        pp = man ? h->base_man : h->base_includes;
                    471:        while ((p = strchr(pp, '%')) != NULL) {
                    472:                print_encode(h, pp, p, 1);
                    473:                if (man && p[1] == 'S') {
                    474:                        if (sec == NULL)
1.197     schwarze  475:                                print_byte(h, '1');
1.195     schwarze  476:                        else
                    477:                                print_encode(h, sec, NULL, 1);
                    478:                } else if ((man && p[1] == 'N') ||
                    479:                    (man == 0 && p[1] == 'I'))
                    480:                        print_encode(h, name, NULL, 1);
                    481:                else
                    482:                        print_encode(h, p, p + 2, 1);
                    483:                pp = p + 2;
                    484:        }
                    485:        if (*pp != '\0')
                    486:                print_encode(h, pp, NULL, 1);
1.94      kristaps  487: }
                    488:
1.51      kristaps  489: struct tag *
1.194     schwarze  490: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14      kristaps  491: {
1.194     schwarze  492:        va_list          ap;
                    493:        struct roffsu    mysu, *su;
1.197     schwarze  494:        char             numbuf[16];
1.30      kristaps  495:        struct tag      *t;
1.195     schwarze  496:        const char      *attr;
1.203     schwarze  497:        char            *arg1, *arg2;
1.195     schwarze  498:        double           v;
1.196     schwarze  499:        int              i, have_style, tflags;
                    500:
                    501:        tflags = htmltags[tag].flags;
1.30      kristaps  502:
1.204     schwarze  503:        /* Push this tag onto the stack of open scopes. */
1.94      kristaps  504:
1.196     schwarze  505:        if ((tflags & HTML_NOSTACK) == 0) {
1.128     kristaps  506:                t = mandoc_malloc(sizeof(struct tag));
1.30      kristaps  507:                t->tag = tag;
1.204     schwarze  508:                t->next = h->tag;
                    509:                h->tag = t;
1.30      kristaps  510:        } else
                    511:                t = NULL;
1.29      kristaps  512:
1.196     schwarze  513:        if (tflags & HTML_NLBEFORE)
1.197     schwarze  514:                print_endline(h);
                    515:        if (h->col == 0)
                    516:                print_indent(h);
1.196     schwarze  517:        else if ((h->flags & HTML_NOSPACE) == 0) {
                    518:                if (h->flags & HTML_KEEP)
1.216     schwarze  519:                        print_word(h, "&#x00A0;");
1.196     schwarze  520:                else {
                    521:                        if (h->flags & HTML_PREKEEP)
                    522:                                h->flags |= HTML_KEEP;
1.197     schwarze  523:                        print_endword(h);
1.105     kristaps  524:                }
1.196     schwarze  525:        }
1.29      kristaps  526:
1.109     kristaps  527:        if ( ! (h->flags & HTML_NONOSPACE))
                    528:                h->flags &= ~HTML_NOSPACE;
1.110     kristaps  529:        else
                    530:                h->flags |= HTML_NOSPACE;
1.109     kristaps  531:
1.94      kristaps  532:        /* Print out the tag name and attributes. */
                    533:
1.197     schwarze  534:        print_byte(h, '<');
                    535:        print_word(h, htmltags[tag].name);
1.194     schwarze  536:
                    537:        va_start(ap, fmt);
                    538:
                    539:        have_style = 0;
                    540:        while (*fmt != '\0') {
                    541:                if (*fmt == 's') {
                    542:                        have_style = 1;
                    543:                        fmt++;
                    544:                        break;
                    545:                }
1.203     schwarze  546:
                    547:                /* Parse a non-style attribute and its arguments. */
                    548:
                    549:                arg1 = va_arg(ap, char *);
1.194     schwarze  550:                switch (*fmt++) {
                    551:                case 'c':
1.195     schwarze  552:                        attr = "class";
1.194     schwarze  553:                        break;
                    554:                case 'h':
1.195     schwarze  555:                        attr = "href";
1.194     schwarze  556:                        break;
                    557:                case 'i':
1.195     schwarze  558:                        attr = "id";
1.194     schwarze  559:                        break;
                    560:                case '?':
1.203     schwarze  561:                        attr = arg1;
                    562:                        arg1 = va_arg(ap, char *);
1.194     schwarze  563:                        break;
                    564:                default:
                    565:                        abort();
                    566:                }
1.203     schwarze  567:                arg2 = NULL;
                    568:                if (*fmt == 'M')
                    569:                        arg2 = va_arg(ap, char *);
                    570:                if (arg1 == NULL)
                    571:                        continue;
                    572:
                    573:                /* Print the non-style attributes. */
                    574:
1.197     schwarze  575:                print_byte(h, ' ');
                    576:                print_word(h, attr);
                    577:                print_byte(h, '=');
                    578:                print_byte(h, '"');
1.195     schwarze  579:                switch (*fmt) {
1.208     schwarze  580:                case 'I':
                    581:                        print_href(h, arg1, NULL, 0);
                    582:                        fmt++;
                    583:                        break;
1.195     schwarze  584:                case 'M':
1.203     schwarze  585:                        print_href(h, arg1, arg2, 1);
1.195     schwarze  586:                        fmt++;
                    587:                        break;
1.208     schwarze  588:                case 'R':
                    589:                        print_byte(h, '#');
                    590:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  591:                        fmt++;
                    592:                        break;
1.208     schwarze  593:                case 'T':
                    594:                        print_encode(h, arg1, NULL, 1);
                    595:                        print_word(h, "\" title=\"");
                    596:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  597:                        fmt++;
1.208     schwarze  598:                        break;
1.195     schwarze  599:                default:
1.203     schwarze  600:                        print_encode(h, arg1, NULL, 1);
1.195     schwarze  601:                        break;
                    602:                }
1.197     schwarze  603:                print_byte(h, '"');
1.194     schwarze  604:        }
                    605:
                    606:        /* Print out styles. */
                    607:
                    608:        while (*fmt != '\0') {
1.203     schwarze  609:                arg1 = NULL;
                    610:                su = NULL;
1.194     schwarze  611:
                    612:                /* First letter: input argument type. */
                    613:
                    614:                switch (*fmt++) {
                    615:                case 'h':
                    616:                        i = va_arg(ap, int);
1.203     schwarze  617:                        su = &mysu;
1.194     schwarze  618:                        SCALE_HS_INIT(su, i);
                    619:                        break;
                    620:                case 's':
1.203     schwarze  621:                        arg1 = va_arg(ap, char *);
1.194     schwarze  622:                        break;
                    623:                case 'u':
                    624:                        su = va_arg(ap, struct roffsu *);
                    625:                        break;
                    626:                case 'w':
1.218   ! schwarze  627:                        if ((arg2 = va_arg(ap, char *)) == NULL) {
        !           628:                                if (*fmt == '+')
        !           629:                                        fmt++;
        !           630:                                if (*fmt == '-')
        !           631:                                        fmt++;
1.203     schwarze  632:                                break;
1.218   ! schwarze  633:                        }
1.203     schwarze  634:                        su = &mysu;
                    635:                        a2width(arg2, su);
1.211     schwarze  636:                        if (*fmt == '+') {
                    637:                                /* Increase to make even bold text fit. */
1.212     schwarze  638:                                su->scale *= 1.2;
1.211     schwarze  639:                                /* Add padding. */
                    640:                                su->scale += 3.0;
                    641:                                fmt++;
                    642:                        }
                    643:                        if (*fmt == '-') {
1.201     schwarze  644:                                su->scale *= -1.0;
1.211     schwarze  645:                                fmt++;
                    646:                        }
1.194     schwarze  647:                        break;
                    648:                default:
                    649:                        abort();
                    650:                }
                    651:
                    652:                /* Second letter: style name. */
                    653:
                    654:                switch (*fmt++) {
                    655:                case 'h':
1.195     schwarze  656:                        attr = "height";
1.194     schwarze  657:                        break;
                    658:                case 'i':
1.195     schwarze  659:                        attr = "text-indent";
1.194     schwarze  660:                        break;
                    661:                case 'l':
1.195     schwarze  662:                        attr = "margin-left";
1.194     schwarze  663:                        break;
                    664:                case 'w':
1.195     schwarze  665:                        attr = "width";
1.194     schwarze  666:                        break;
                    667:                case 'W':
1.195     schwarze  668:                        attr = "min-width";
1.194     schwarze  669:                        break;
                    670:                case '?':
1.203     schwarze  671:                        attr = arg1;
                    672:                        arg1 = va_arg(ap, char *);
                    673:                        break;
1.194     schwarze  674:                default:
                    675:                        abort();
                    676:                }
1.203     schwarze  677:                if (su == NULL && arg1 == NULL)
                    678:                        continue;
                    679:
                    680:                if (have_style == 1)
                    681:                        print_word(h, " style=\"");
                    682:                else
                    683:                        print_byte(h, ' ');
1.197     schwarze  684:                print_word(h, attr);
                    685:                print_byte(h, ':');
                    686:                print_byte(h, ' ');
1.203     schwarze  687:                if (su != NULL) {
                    688:                        v = su->scale;
                    689:                        if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
                    690:                                v = 1.0;
                    691:                        else if (su->unit == SCALE_BU)
                    692:                                v /= 24.0;
                    693:                        (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
                    694:                        print_word(h, numbuf);
                    695:                        print_word(h, roffscales[su->unit]);
                    696:                } else
                    697:                        print_word(h, arg1);
1.197     schwarze  698:                print_byte(h, ';');
1.203     schwarze  699:                have_style = 2;
1.194     schwarze  700:        }
1.203     schwarze  701:        if (have_style == 2)
1.197     schwarze  702:                print_byte(h, '"');
1.194     schwarze  703:
                    704:        va_end(ap);
1.94      kristaps  705:
1.172     kristaps  706:        /* Accommodate for "well-formed" singleton escaping. */
1.94      kristaps  707:
1.93      kristaps  708:        if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197     schwarze  709:                print_byte(h, '/');
1.93      kristaps  710:
1.197     schwarze  711:        print_byte(h, '>');
1.14      kristaps  712:
1.196     schwarze  713:        if (tflags & HTML_NLBEGIN)
1.197     schwarze  714:                print_endline(h);
1.196     schwarze  715:        else
                    716:                h->flags |= HTML_NOSPACE;
1.117     kristaps  717:
1.196     schwarze  718:        if (tflags & HTML_INDENT)
                    719:                h->indent++;
                    720:        if (tflags & HTML_NOINDENT)
                    721:                h->noindent++;
1.117     kristaps  722:
1.188     schwarze  723:        return t;
1.14      kristaps  724: }
                    725:
1.29      kristaps  726: static void
1.184     schwarze  727: print_ctag(struct html *h, struct tag *tag)
1.14      kristaps  728: {
1.196     schwarze  729:        int      tflags;
1.156     schwarze  730:
1.184     schwarze  731:        /*
                    732:         * Remember to close out and nullify the current
                    733:         * meta-font and table, if applicable.
                    734:         */
                    735:        if (tag == h->metaf)
                    736:                h->metaf = NULL;
                    737:        if (tag == h->tblt)
                    738:                h->tblt = NULL;
                    739:
1.196     schwarze  740:        tflags = htmltags[tag->tag].flags;
                    741:
                    742:        if (tflags & HTML_INDENT)
                    743:                h->indent--;
                    744:        if (tflags & HTML_NOINDENT)
                    745:                h->noindent--;
                    746:        if (tflags & HTML_NLEND)
1.197     schwarze  747:                print_endline(h);
                    748:        print_indent(h);
                    749:        print_byte(h, '<');
                    750:        print_byte(h, '/');
                    751:        print_word(h, htmltags[tag->tag].name);
                    752:        print_byte(h, '>');
1.196     schwarze  753:        if (tflags & HTML_NLAFTER)
1.197     schwarze  754:                print_endline(h);
1.184     schwarze  755:
1.204     schwarze  756:        h->tag = tag->next;
1.184     schwarze  757:        free(tag);
1.14      kristaps  758: }
                    759:
1.51      kristaps  760: void
1.93      kristaps  761: print_gen_decls(struct html *h)
1.1       kristaps  762: {
1.197     schwarze  763:        print_word(h, "<!DOCTYPE html>");
                    764:        print_endline(h);
1.1       kristaps  765: }
                    766:
1.51      kristaps  767: void
1.104     kristaps  768: print_text(struct html *h, const char *word)
1.1       kristaps  769: {
1.197     schwarze  770:        if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105     kristaps  771:                if ( ! (HTML_KEEP & h->flags)) {
                    772:                        if (HTML_PREKEEP & h->flags)
                    773:                                h->flags |= HTML_KEEP;
1.197     schwarze  774:                        print_endword(h);
1.105     kristaps  775:                } else
1.216     schwarze  776:                        print_word(h, "&#x00A0;");
1.105     kristaps  777:        }
1.30      kristaps  778:
1.122     kristaps  779:        assert(NULL == h->metaf);
1.152     schwarze  780:        switch (h->metac) {
1.156     schwarze  781:        case HTMLFONT_ITALIC:
1.194     schwarze  782:                h->metaf = print_otag(h, TAG_I, "");
1.152     schwarze  783:                break;
1.156     schwarze  784:        case HTMLFONT_BOLD:
1.194     schwarze  785:                h->metaf = print_otag(h, TAG_B, "");
1.152     schwarze  786:                break;
1.156     schwarze  787:        case HTMLFONT_BI:
1.194     schwarze  788:                h->metaf = print_otag(h, TAG_B, "");
                    789:                print_otag(h, TAG_I, "");
1.152     schwarze  790:                break;
                    791:        default:
1.197     schwarze  792:                print_indent(h);
1.152     schwarze  793:                break;
                    794:        }
1.122     kristaps  795:
1.104     kristaps  796:        assert(word);
1.195     schwarze  797:        if ( ! print_encode(h, word, NULL, 0)) {
1.109     kristaps  798:                if ( ! (h->flags & HTML_NONOSPACE))
                    799:                        h->flags &= ~HTML_NOSPACE;
1.183     schwarze  800:                h->flags &= ~HTML_NONEWLINE;
1.149     kristaps  801:        } else
1.183     schwarze  802:                h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122     kristaps  803:
                    804:        if (h->metaf) {
                    805:                print_tagq(h, h->metaf);
                    806:                h->metaf = NULL;
                    807:        }
1.113     schwarze  808:
                    809:        h->flags &= ~HTML_IGNDELIM;
1.1       kristaps  810: }
1.30      kristaps  811:
1.51      kristaps  812: void
1.30      kristaps  813: print_tagq(struct html *h, const struct tag *until)
                    814: {
                    815:        struct tag      *tag;
                    816:
1.204     schwarze  817:        while ((tag = h->tag) != NULL) {
1.184     schwarze  818:                print_ctag(h, tag);
1.30      kristaps  819:                if (until && tag == until)
                    820:                        return;
                    821:        }
                    822: }
                    823:
1.51      kristaps  824: void
1.30      kristaps  825: print_stagq(struct html *h, const struct tag *suntil)
                    826: {
                    827:        struct tag      *tag;
                    828:
1.204     schwarze  829:        while ((tag = h->tag) != NULL) {
1.30      kristaps  830:                if (suntil && tag == suntil)
                    831:                        return;
1.184     schwarze  832:                print_ctag(h, tag);
1.30      kristaps  833:        }
                    834: }
1.171     kristaps  835:
                    836: void
                    837: print_paragraph(struct html *h)
                    838: {
                    839:        struct tag      *t;
                    840:
1.198     schwarze  841:        t = print_otag(h, TAG_DIV, "c", "Pp");
1.171     kristaps  842:        print_tagq(h, t);
                    843: }
                    844:
1.197     schwarze  845:
                    846: /***********************************************************************
                    847:  * Low level output functions.
                    848:  * They implement line breaking using a short static buffer.
                    849:  ***********************************************************************/
                    850:
                    851: /*
                    852:  * Buffer one HTML output byte.
                    853:  * If the buffer is full, flush and deactivate it and start a new line.
                    854:  * If the buffer is inactive, print directly.
                    855:  */
                    856: static void
                    857: print_byte(struct html *h, char c)
                    858: {
                    859:        if ((h->flags & HTML_BUFFER) == 0) {
                    860:                putchar(c);
                    861:                h->col++;
                    862:                return;
                    863:        }
                    864:
                    865:        if (h->col + h->bufcol < sizeof(h->buf)) {
                    866:                h->buf[h->bufcol++] = c;
                    867:                return;
                    868:        }
                    869:
                    870:        putchar('\n');
                    871:        h->col = 0;
                    872:        print_indent(h);
                    873:        putchar(' ');
                    874:        putchar(' ');
                    875:        fwrite(h->buf, h->bufcol, 1, stdout);
                    876:        putchar(c);
                    877:        h->col = (h->indent + 1) * 2 + h->bufcol + 1;
                    878:        h->bufcol = 0;
                    879:        h->flags &= ~HTML_BUFFER;
                    880: }
                    881:
1.196     schwarze  882: /*
                    883:  * If something was printed on the current output line, end it.
1.197     schwarze  884:  * Not to be called right after print_indent().
1.196     schwarze  885:  */
1.202     schwarze  886: void
1.197     schwarze  887: print_endline(struct html *h)
1.196     schwarze  888: {
1.197     schwarze  889:        if (h->col == 0)
1.196     schwarze  890:                return;
                    891:
1.197     schwarze  892:        if (h->bufcol) {
                    893:                putchar(' ');
                    894:                fwrite(h->buf, h->bufcol, 1, stdout);
                    895:                h->bufcol = 0;
                    896:        }
1.196     schwarze  897:        putchar('\n');
1.197     schwarze  898:        h->col = 0;
                    899:        h->flags |= HTML_NOSPACE;
                    900:        h->flags &= ~HTML_BUFFER;
                    901: }
                    902:
                    903: /*
                    904:  * Flush the HTML output buffer.
                    905:  * If it is inactive, activate it.
                    906:  */
                    907: static void
                    908: print_endword(struct html *h)
                    909: {
                    910:        if (h->noindent) {
                    911:                print_byte(h, ' ');
                    912:                return;
                    913:        }
                    914:
                    915:        if ((h->flags & HTML_BUFFER) == 0) {
                    916:                h->col++;
                    917:                h->flags |= HTML_BUFFER;
                    918:        } else if (h->bufcol) {
                    919:                putchar(' ');
                    920:                fwrite(h->buf, h->bufcol, 1, stdout);
                    921:                h->col += h->bufcol + 1;
                    922:        }
                    923:        h->bufcol = 0;
1.196     schwarze  924: }
                    925:
                    926: /*
                    927:  * If at the beginning of a new output line,
                    928:  * perform indentation and mark the line as containing output.
                    929:  * Make sure to really produce some output right afterwards,
                    930:  * but do not use print_otag() for producing it.
                    931:  */
                    932: static void
1.197     schwarze  933: print_indent(struct html *h)
1.196     schwarze  934: {
1.197     schwarze  935:        size_t   i;
1.196     schwarze  936:
1.197     schwarze  937:        if (h->col)
1.196     schwarze  938:                return;
                    939:
1.197     schwarze  940:        if (h->noindent == 0) {
                    941:                h->col = h->indent * 2;
                    942:                for (i = 0; i < h->col; i++)
1.196     schwarze  943:                        putchar(' ');
1.197     schwarze  944:        }
                    945:        h->flags &= ~HTML_NOSPACE;
                    946: }
                    947:
                    948: /*
                    949:  * Print or buffer some characters
                    950:  * depending on the current HTML output buffer state.
                    951:  */
                    952: static void
                    953: print_word(struct html *h, const char *cp)
                    954: {
                    955:        while (*cp != '\0')
                    956:                print_byte(h, *cp++);
1.196     schwarze  957: }
1.194     schwarze  958:
                    959: /*
                    960:  * Calculate the scaling unit passed in a `-width' argument.  This uses
                    961:  * either a native scaling unit (e.g., 1i, 2m) or the string length of
                    962:  * the value.
                    963:  */
                    964: static void
                    965: a2width(const char *p, struct roffsu *su)
                    966: {
1.213     schwarze  967:        const char      *end;
                    968:
                    969:        end = a2roffsu(p, su, SCALE_MAX);
                    970:        if (end == NULL || *end != '\0') {
1.194     schwarze  971:                su->unit = SCALE_EN;
                    972:                su->scale = html_strlen(p);
                    973:        } else if (su->scale < 0.0)
                    974:                su->scale = 0.0;
1.68      kristaps  975: }

CVSweb