Annotation of mandoc/eqn_html.c, Revision 1.6
1.6 ! kristaps 1: /* $Id: eqn_html.c,v 1.5 2014/09/28 13:34:15 kristaps 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: }
1.6 ! kristaps 91: if (NULL != bp->parent && bp->parent->type == EQN_MATRIX) {
! 92: pilet = print_otag(p, TAG_MTABLE, 0, NULL);
! 93: print_otag(p, TAG_MTR, 0, NULL);
! 94: print_otag(p, TAG_MTD, 0, NULL);
! 95: }
1.5 kristaps 96:
97: /*
98: * If we're establishing a pile, start the table mode now.
99: * If we've already in a pile row, then don't override "pilet",
100: * because we'll be closed out anyway.
101: */
102: if (bp->pile != EQNPILE_NONE) {
103: tmp = print_otag(p, TAG_MTABLE, 0, NULL);
104: pilet = (NULL == pilet) ? tmp : pilet;
105: }
106:
107: /*
108: * Positioning.
109: * This is the most complicated part, and actually doesn't quite
110: * work (FIXME) because it doesn't account for associativity.
111: * Setting "post" will mean that we're only going to process a
112: * single or double following expression.
113: */
114: switch (bp->pos) {
1.6 ! kristaps 115: case (EQNPOS_TO):
! 116: /* FALLTHROUGH */
1.5 kristaps 117: case (EQNPOS_SUP):
118: post = print_otag(p, TAG_MSUP, 0, NULL);
119: break;
120: case (EQNPOS_FROM):
121: /* FALLTHROUGH */
122: case (EQNPOS_SUB):
123: post = print_otag(p, TAG_MSUB, 0, NULL);
124: break;
125: case (EQNPOS_OVER):
126: post = print_otag(p, TAG_MFRAC, 0, NULL);
127: break;
1.6 ! kristaps 128: case (EQNPOS_FROMTO):
! 129: /* FALLTHROUGH */
1.5 kristaps 130: case (EQNPOS_SUBSUP):
131: /* This requires two elements. */
132: post = print_otag(p, TAG_MSUBSUP, 0, NULL);
133: skiptwo = 1;
134: break;
135: default:
136: break;
137: }
138:
139: /*t = EQNFONT_NONE == bp->font ? NULL :
140: print_otag(p, fontmap[(int)bp->font], 0, NULL);*/
141:
142: if (NULL != bp->text) {
143: assert(NULL == bp->first);
144: /*
145: * We have text.
146: * This can be a number, a function, a variable, or
147: * pretty much anything else.
148: * First, check for some known functions.
149: * If we're going to create a structural node (e.g.,
150: * sqrt), then set the "post" variable only if it's not
151: * already set.
152: */
153: if (0 == strcmp(bp->text, "sqrt")) {
154: tmp = print_otag(p, TAG_MSQRT, 0, NULL);
155: post = (NULL == post) ? tmp : post;
156: } else if (0 == strcmp(bp->text, "+") ||
157: 0 == strcmp(bp->text, "-") ||
158: 0 == strcmp(bp->text, "=") ||
159: 0 == strcmp(bp->text, "(") ||
160: 0 == strcmp(bp->text, ")") ||
161: 0 == strcmp(bp->text, "/")) {
162: tmp = print_otag(p, TAG_MO, 0, NULL);
163: print_text(p, bp->text);
164: print_tagq(p, tmp);
165: } else {
166: tmp = print_otag(p, TAG_MI, 0, NULL);
167: print_text(p, bp->text);
168: print_tagq(p, tmp);
169: }
170: } else if (NULL != bp->first) {
171: assert(NULL == bp->text);
172: /*
173: * If we're a "fenced" component (i.e., having
174: * brackets), then process those brackets now.
175: * Otherwise, introduce a dummy row (if we're not
176: * already in a table context).
177: */
178: tmp = NULL;
179: if (NULL != bp->left || NULL != bp->right) {
180: PAIR_INIT(&tag[0], ATTR_OPEN,
181: NULL != bp->left ? bp->left : "");
182: PAIR_INIT(&tag[1], ATTR_CLOSE,
183: NULL != bp->right ? bp->right : "");
184: tmp = print_otag(p, TAG_MFENCED, 2, tag);
185: print_otag(p, TAG_MROW, 0, NULL);
186: } else if (NULL == pilet)
187: tmp = print_otag(p, TAG_MROW, 0, NULL);
188: eqn_box(p, bp->first, 1);
189: if (NULL != tmp)
190: print_tagq(p, tmp);
191: }
192:
193: /*
194: * If a positional context, invoke the "next" context.
195: * This is recursive and will return the end of the recursive
196: * chain of "next" contexts.
197: */
198: if (NULL != post) {
199: bp = eqn_box(p, bp->next, 0);
200: if (skiptwo)
201: bp = eqn_box(p, bp->next, 0);
202: print_tagq(p, post);
203: }
204:
205: /*
206: * If we're being piled (either directly, in the table, or
207: * indirectly in a table row), then close that out.
208: */
209: if (NULL != pilet)
210: print_tagq(p, pilet);
211:
212: /*
213: * If we're normally processing, then grab the next node.
214: * If we're in a recursive context, then don't seek to the next
215: * node; further recursion has already been handled.
216: */
217: return(next ? eqn_box(p, bp->next, 1) : bp);
1.1 kristaps 218: }
CVSweb