Annotation of mandoc/eqn_html.c, Revision 1.5
1.5 ! kristaps 1: /* $Id: eqn_html.c,v 1.4 2014/08/10 23:54:41 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
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.
16: */
17: #include "config.h"
1.4 schwarze 18:
19: #include <sys/types.h>
1.1 kristaps 20:
21: #include <assert.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25:
26: #include "mandoc.h"
27: #include "out.h"
28: #include "html.h"
29:
1.2 kristaps 30: static const enum htmltag fontmap[EQNFONT__MAX] = {
31: TAG_SPAN, /* EQNFONT_NONE */
32: TAG_SPAN, /* EQNFONT_ROMAN */
33: TAG_B, /* EQNFONT_BOLD */
34: TAG_B, /* EQNFONT_FAT */
35: TAG_I /* EQNFONT_ITALIC */
36: };
37:
1.5 ! kristaps 38: static const struct eqn_box *
! 39: eqn_box(struct html *, const struct eqn_box *, int);
1.2 kristaps 40:
1.1 kristaps 41:
42: void
43: print_eqn(struct html *p, const struct eqn *ep)
44: {
45: struct htmlpair tag;
46: struct tag *t;
47:
48: PAIR_CLASS_INIT(&tag, "eqn");
1.5 ! kristaps 49: t = print_otag(p, TAG_MATH, 1, &tag);
1.1 kristaps 50:
51: p->flags |= HTML_NONOSPACE;
1.5 ! kristaps 52: eqn_box(p, ep->root, 1);
1.1 kristaps 53: p->flags &= ~HTML_NONOSPACE;
54:
55: print_tagq(p, t);
56: }
57:
1.5 ! kristaps 58: /*
! 59: * This function is fairly brittle.
! 60: * This is because the eqn syntax doesn't play so nicely with recusive
! 61: * formats, e.g.,
! 62: * foo sub bar sub baz
! 63: * ...needs to resolve into
! 64: * <msub> foo <msub> bar, baz </msub> </msub>
! 65: * In other words, we need to embed some recursive work.
! 66: * FIXME: this does NOT handle right-left associativity or precedence!
! 67: */
! 68: static const struct eqn_box *
! 69: eqn_box(struct html *p, const struct eqn_box *bp, int next)
1.1 kristaps 70: {
1.5 ! kristaps 71: struct tag *post, *pilet, *tmp;
! 72: struct htmlpair tag[2];
! 73: int skiptwo;
! 74:
! 75: if (NULL == bp)
! 76: return(NULL);
! 77:
! 78: post = pilet = NULL;
! 79: skiptwo = 0;
! 80:
! 81: /*
! 82: * If we're a "row" under a pile, then open up the piling
! 83: * context here.
! 84: * We do this first because the pile surrounds the content of
! 85: * the contained expression.
! 86: */
! 87: if (NULL != bp->parent && bp->parent->pile != EQNPILE_NONE) {
! 88: pilet = print_otag(p, TAG_MTR, 0, NULL);
! 89: print_otag(p, TAG_MTD, 0, NULL);
! 90: }
! 91:
! 92: /*
! 93: * If we're establishing a pile, start the table mode now.
! 94: * If we've already in a pile row, then don't override "pilet",
! 95: * because we'll be closed out anyway.
! 96: */
! 97: if (bp->pile != EQNPILE_NONE) {
! 98: tmp = print_otag(p, TAG_MTABLE, 0, NULL);
! 99: pilet = (NULL == pilet) ? tmp : pilet;
! 100: }
! 101:
! 102: /*
! 103: * Positioning.
! 104: * This is the most complicated part, and actually doesn't quite
! 105: * work (FIXME) because it doesn't account for associativity.
! 106: * Setting "post" will mean that we're only going to process a
! 107: * single or double following expression.
! 108: */
! 109: switch (bp->pos) {
! 110: case (EQNPOS_SUP):
! 111: post = print_otag(p, TAG_MSUP, 0, NULL);
! 112: break;
! 113: case (EQNPOS_FROM):
! 114: /* FALLTHROUGH */
! 115: case (EQNPOS_SUB):
! 116: post = print_otag(p, TAG_MSUB, 0, NULL);
! 117: break;
! 118: case (EQNPOS_OVER):
! 119: post = print_otag(p, TAG_MFRAC, 0, NULL);
! 120: break;
! 121: case (EQNPOS_SUBSUP):
! 122: /* This requires two elements. */
! 123: post = print_otag(p, TAG_MSUBSUP, 0, NULL);
! 124: skiptwo = 1;
! 125: break;
! 126: default:
! 127: break;
! 128: }
! 129:
! 130: /*t = EQNFONT_NONE == bp->font ? NULL :
! 131: print_otag(p, fontmap[(int)bp->font], 0, NULL);*/
! 132:
! 133: if (NULL != bp->text) {
! 134: assert(NULL == bp->first);
! 135: /*
! 136: * We have text.
! 137: * This can be a number, a function, a variable, or
! 138: * pretty much anything else.
! 139: * First, check for some known functions.
! 140: * If we're going to create a structural node (e.g.,
! 141: * sqrt), then set the "post" variable only if it's not
! 142: * already set.
! 143: */
! 144: if (0 == strcmp(bp->text, "sqrt")) {
! 145: tmp = print_otag(p, TAG_MSQRT, 0, NULL);
! 146: post = (NULL == post) ? tmp : post;
! 147: } else if (0 == strcmp(bp->text, "+") ||
! 148: 0 == strcmp(bp->text, "-") ||
! 149: 0 == strcmp(bp->text, "=") ||
! 150: 0 == strcmp(bp->text, "(") ||
! 151: 0 == strcmp(bp->text, ")") ||
! 152: 0 == strcmp(bp->text, "/")) {
! 153: tmp = print_otag(p, TAG_MO, 0, NULL);
! 154: print_text(p, bp->text);
! 155: print_tagq(p, tmp);
! 156: } else {
! 157: tmp = print_otag(p, TAG_MI, 0, NULL);
! 158: print_text(p, bp->text);
! 159: print_tagq(p, tmp);
! 160: }
! 161: } else if (NULL != bp->first) {
! 162: assert(NULL == bp->text);
! 163: /*
! 164: * If we're a "fenced" component (i.e., having
! 165: * brackets), then process those brackets now.
! 166: * Otherwise, introduce a dummy row (if we're not
! 167: * already in a table context).
! 168: */
! 169: tmp = NULL;
! 170: if (NULL != bp->left || NULL != bp->right) {
! 171: PAIR_INIT(&tag[0], ATTR_OPEN,
! 172: NULL != bp->left ? bp->left : "");
! 173: PAIR_INIT(&tag[1], ATTR_CLOSE,
! 174: NULL != bp->right ? bp->right : "");
! 175: tmp = print_otag(p, TAG_MFENCED, 2, tag);
! 176: print_otag(p, TAG_MROW, 0, NULL);
! 177: } else if (NULL == pilet)
! 178: tmp = print_otag(p, TAG_MROW, 0, NULL);
! 179: eqn_box(p, bp->first, 1);
! 180: if (NULL != tmp)
! 181: print_tagq(p, tmp);
! 182: }
! 183:
! 184: /*
! 185: * If a positional context, invoke the "next" context.
! 186: * This is recursive and will return the end of the recursive
! 187: * chain of "next" contexts.
! 188: */
! 189: if (NULL != post) {
! 190: bp = eqn_box(p, bp->next, 0);
! 191: if (skiptwo)
! 192: bp = eqn_box(p, bp->next, 0);
! 193: print_tagq(p, post);
! 194: }
! 195:
! 196: /*
! 197: * If we're being piled (either directly, in the table, or
! 198: * indirectly in a table row), then close that out.
! 199: */
! 200: if (NULL != pilet)
! 201: print_tagq(p, pilet);
! 202:
! 203: /*
! 204: * If we're normally processing, then grab the next node.
! 205: * If we're in a recursive context, then don't seek to the next
! 206: * node; further recursion has already been handled.
! 207: */
! 208: return(next ? eqn_box(p, bp->next, 1) : bp);
1.1 kristaps 209: }
CVSweb