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

Annotation of mandoc/eqn_html.c, Revision 1.14

1.14    ! schwarze    1: /*     $Id: eqn_html.c,v 1.13 2017/06/23 02:32:12 schwarze Exp $ */
1.1       kristaps    2: /*
1.9       schwarze    3:  * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.11      schwarze    4:  * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     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.
                     17:  */
                     18: #include "config.h"
1.4       schwarze   19:
                     20: #include <sys/types.h>
1.1       kristaps   21:
                     22: #include <assert.h>
1.13      schwarze   23: #include <ctype.h>
1.1       kristaps   24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
                     28: #include "mandoc.h"
                     29: #include "out.h"
                     30: #include "html.h"
                     31:
1.8       kristaps   32: static void
                     33: eqn_box(struct html *p, const struct eqn_box *bp)
1.1       kristaps   34: {
1.8       kristaps   35:        struct tag      *post, *row, *cell, *t;
                     36:        const struct eqn_box *child, *parent;
1.14    ! schwarze   37:        const char      *cp;
1.8       kristaps   38:        size_t           i, j, rows;
1.13      schwarze   39:        enum htmltag     tag;
                     40:        enum eqn_fontt   font;
1.5       kristaps   41:
                     42:        if (NULL == bp)
1.8       kristaps   43:                return;
1.5       kristaps   44:
1.8       kristaps   45:        post = NULL;
1.5       kristaps   46:
                     47:        /*
1.8       kristaps   48:         * Special handling for a matrix, which is presented to us in
                     49:         * column order, but must be printed in row-order.
1.5       kristaps   50:         */
1.8       kristaps   51:        if (EQN_MATRIX == bp->type) {
                     52:                if (NULL == bp->first)
                     53:                        goto out;
1.10      schwarze   54:                if (EQN_LIST != bp->first->type) {
                     55:                        eqn_box(p, bp->first);
                     56:                        goto out;
                     57:                }
1.8       kristaps   58:                if (NULL == (parent = bp->first->first))
                     59:                        goto out;
                     60:                /* Estimate the number of rows, first. */
                     61:                if (NULL == (child = parent->first))
                     62:                        goto out;
1.9       schwarze   63:                for (rows = 0; NULL != child; rows++)
1.8       kristaps   64:                        child = child->next;
                     65:                /* Print row-by-row. */
1.11      schwarze   66:                post = print_otag(p, TAG_MTABLE, "");
1.8       kristaps   67:                for (i = 0; i < rows; i++) {
                     68:                        parent = bp->first->first;
1.11      schwarze   69:                        row = print_otag(p, TAG_MTR, "");
1.8       kristaps   70:                        while (NULL != parent) {
                     71:                                child = parent->first;
                     72:                                for (j = 0; j < i; j++) {
                     73:                                        if (NULL == child)
                     74:                                                break;
                     75:                                        child = child->next;
                     76:                                }
1.11      schwarze   77:                                cell = print_otag(p, TAG_MTD, "");
1.9       schwarze   78:                                /*
1.8       kristaps   79:                                 * If we have no data for this
                     80:                                 * particular cell, then print a
                     81:                                 * placeholder and continue--don't puke.
                     82:                                 */
1.9       schwarze   83:                                if (NULL != child)
1.8       kristaps   84:                                        eqn_box(p, child->first);
                     85:                                print_tagq(p, cell);
                     86:                                parent = parent->next;
                     87:                        }
                     88:                        print_tagq(p, row);
                     89:                }
                     90:                goto out;
1.6       kristaps   91:        }
1.5       kristaps   92:
                     93:        switch (bp->pos) {
1.12      schwarze   94:        case EQNPOS_TO:
1.11      schwarze   95:                post = print_otag(p, TAG_MOVER, "");
1.7       kristaps   96:                break;
1.12      schwarze   97:        case EQNPOS_SUP:
1.11      schwarze   98:                post = print_otag(p, TAG_MSUP, "");
1.5       kristaps   99:                break;
1.12      schwarze  100:        case EQNPOS_FROM:
1.11      schwarze  101:                post = print_otag(p, TAG_MUNDER, "");
1.7       kristaps  102:                break;
1.12      schwarze  103:        case EQNPOS_SUB:
1.11      schwarze  104:                post = print_otag(p, TAG_MSUB, "");
1.5       kristaps  105:                break;
1.12      schwarze  106:        case EQNPOS_OVER:
1.11      schwarze  107:                post = print_otag(p, TAG_MFRAC, "");
1.5       kristaps  108:                break;
1.12      schwarze  109:        case EQNPOS_FROMTO:
1.11      schwarze  110:                post = print_otag(p, TAG_MUNDEROVER, "");
1.7       kristaps  111:                break;
1.12      schwarze  112:        case EQNPOS_SUBSUP:
1.11      schwarze  113:                post = print_otag(p, TAG_MSUBSUP, "");
1.8       kristaps  114:                break;
1.12      schwarze  115:        case EQNPOS_SQRT:
1.11      schwarze  116:                post = print_otag(p, TAG_MSQRT, "");
1.5       kristaps  117:                break;
                    118:        default:
                    119:                break;
                    120:        }
                    121:
1.8       kristaps  122:        if (bp->top || bp->bottom) {
                    123:                assert(NULL == post);
                    124:                if (bp->top && NULL == bp->bottom)
1.11      schwarze  125:                        post = print_otag(p, TAG_MOVER, "");
1.8       kristaps  126:                else if (bp->top && bp->bottom)
1.11      schwarze  127:                        post = print_otag(p, TAG_MUNDEROVER, "");
1.8       kristaps  128:                else if (bp->bottom)
1.11      schwarze  129:                        post = print_otag(p, TAG_MUNDER, "");
1.8       kristaps  130:        }
                    131:
                    132:        if (EQN_PILE == bp->type) {
                    133:                assert(NULL == post);
1.10      schwarze  134:                if (bp->first != NULL && bp->first->type == EQN_LIST)
1.11      schwarze  135:                        post = print_otag(p, TAG_MTABLE, "");
1.10      schwarze  136:        } else if (bp->type == EQN_LIST &&
                    137:            bp->parent && bp->parent->type == EQN_PILE) {
1.8       kristaps  138:                assert(NULL == post);
1.11      schwarze  139:                post = print_otag(p, TAG_MTR, "");
                    140:                print_otag(p, TAG_MTD, "");
1.8       kristaps  141:        }
1.5       kristaps  142:
1.13      schwarze  143:        if (bp->text != NULL) {
                    144:                assert(post == NULL);
                    145:                tag = TAG_MI;
1.14    ! schwarze  146:                cp = bp->text;
        !           147:                if (isdigit((unsigned char)cp[0]) ||
        !           148:                    (cp[0] == '.' && isdigit((unsigned char)cp[1]))) {
1.13      schwarze  149:                        tag = TAG_MN;
                    150:                        while (*++cp != '\0') {
1.14    ! schwarze  151:                                if (*cp != '.' &&
        !           152:                                    isdigit((unsigned char)*cp) == 0) {
1.13      schwarze  153:                                        tag = TAG_MI;
                    154:                                        break;
                    155:                                }
                    156:                        }
1.14    ! schwarze  157:                } else if (*cp != '\0' && isalpha((unsigned char)*cp) == 0) {
1.13      schwarze  158:                        tag = TAG_MO;
1.14    ! schwarze  159:                        while (*cp != '\0') {
        !           160:                                if (cp[0] == '\\' && cp[1] != '\0') {
        !           161:                                        cp++;
        !           162:                                        mandoc_escape(&cp, NULL, NULL);
        !           163:                                } else if (isalnum((unsigned char)*cp)) {
1.13      schwarze  164:                                        tag = TAG_MI;
                    165:                                        break;
1.14    ! schwarze  166:                                } else
        !           167:                                        cp++;
1.13      schwarze  168:                        }
                    169:                }
                    170:                font = bp->font;
                    171:                if (bp->text[0] != '\0' &&
                    172:                    (((tag == TAG_MN || tag == TAG_MO) &&
                    173:                      font == EQNFONT_ROMAN) ||
                    174:                     (tag == TAG_MI && font == (bp->text[1] == '\0' ?
                    175:                      EQNFONT_ITALIC : EQNFONT_ROMAN))))
                    176:                        font = EQNFONT_NONE;
                    177:                switch (font) {
                    178:                case EQNFONT_NONE:
                    179:                        post = print_otag(p, tag, "");
                    180:                        break;
                    181:                case EQNFONT_ROMAN:
                    182:                        post = print_otag(p, tag, "?", "fontstyle", "normal");
                    183:                        break;
                    184:                case EQNFONT_BOLD:
                    185:                case EQNFONT_FAT:
                    186:                        post = print_otag(p, tag, "?", "fontweight", "bold");
                    187:                        break;
                    188:                case EQNFONT_ITALIC:
                    189:                        post = print_otag(p, tag, "?", "fontstyle", "italic");
                    190:                        break;
                    191:                default:
                    192:                        abort();
                    193:                }
1.8       kristaps  194:                print_text(p, bp->text);
1.9       schwarze  195:        } else if (NULL == post) {
1.11      schwarze  196:                if (NULL != bp->left || NULL != bp->right)
                    197:                        post = print_otag(p, TAG_MFENCED, "??",
                    198:                            "open", bp->left == NULL ? "" : bp->left,
                    199:                            "close", bp->right == NULL ? "" : bp->right);
1.8       kristaps  200:                if (NULL == post)
1.11      schwarze  201:                        post = print_otag(p, TAG_MROW, "");
1.8       kristaps  202:                else
1.11      schwarze  203:                        print_otag(p, TAG_MROW, "");
1.5       kristaps  204:        }
                    205:
1.8       kristaps  206:        eqn_box(p, bp->first);
                    207:
                    208: out:
                    209:        if (NULL != bp->bottom) {
1.11      schwarze  210:                t = print_otag(p, TAG_MO, "");
1.8       kristaps  211:                print_text(p, bp->bottom);
                    212:                print_tagq(p, t);
1.9       schwarze  213:        }
1.8       kristaps  214:        if (NULL != bp->top) {
1.11      schwarze  215:                t = print_otag(p, TAG_MO, "");
1.8       kristaps  216:                print_text(p, bp->top);
                    217:                print_tagq(p, t);
                    218:        }
                    219:
                    220:        if (NULL != post)
1.5       kristaps  221:                print_tagq(p, post);
                    222:
1.8       kristaps  223:        eqn_box(p, bp->next);
                    224: }
                    225:
                    226: void
                    227: print_eqn(struct html *p, const struct eqn *ep)
                    228: {
                    229:        struct tag      *t;
                    230:
1.11      schwarze  231:        t = print_otag(p, TAG_MATH, "c", "eqn");
1.8       kristaps  232:
                    233:        p->flags |= HTML_NONOSPACE;
                    234:        eqn_box(p, ep->root);
                    235:        p->flags &= ~HTML_NONOSPACE;
1.5       kristaps  236:
1.8       kristaps  237:        print_tagq(p, t);
1.1       kristaps  238: }

CVSweb