Unleashed v1.4
[unleashed.git] / bin / mandoc / eqn_html.c
blob1fe41ecbfac542916544c0d3b3d36978b1b27d30
1 /* $Id: eqn_html.c,v 1.18 2018/12/13 05:23:38 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
18 #include "config.h"
20 #include <sys/types.h>
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "mandoc.h"
29 #include "eqn.h"
30 #include "out.h"
31 #include "html.h"
33 static void
34 eqn_box(struct html *p, const struct eqn_box *bp)
36 struct tag *post, *row, *cell, *t;
37 const struct eqn_box *child, *parent;
38 const char *cp;
39 size_t i, j, rows;
40 enum htmltag tag;
41 enum eqn_fontt font;
43 if (NULL == bp)
44 return;
46 post = NULL;
49 * Special handling for a matrix, which is presented to us in
50 * column order, but must be printed in row-order.
52 if (EQN_MATRIX == bp->type) {
53 if (NULL == bp->first)
54 goto out;
55 if (bp->first->type != EQN_LIST ||
56 bp->first->expectargs == 1) {
57 eqn_box(p, bp->first);
58 goto out;
60 if (NULL == (parent = bp->first->first))
61 goto out;
62 /* Estimate the number of rows, first. */
63 if (NULL == (child = parent->first))
64 goto out;
65 for (rows = 0; NULL != child; rows++)
66 child = child->next;
67 /* Print row-by-row. */
68 post = print_otag(p, TAG_MTABLE, "");
69 for (i = 0; i < rows; i++) {
70 parent = bp->first->first;
71 row = print_otag(p, TAG_MTR, "");
72 while (NULL != parent) {
73 child = parent->first;
74 for (j = 0; j < i; j++) {
75 if (NULL == child)
76 break;
77 child = child->next;
79 cell = print_otag(p, TAG_MTD, "");
81 * If we have no data for this
82 * particular cell, then print a
83 * placeholder and continue--don't puke.
85 if (NULL != child)
86 eqn_box(p, child->first);
87 print_tagq(p, cell);
88 parent = parent->next;
90 print_tagq(p, row);
92 goto out;
95 switch (bp->pos) {
96 case EQNPOS_TO:
97 post = print_otag(p, TAG_MOVER, "");
98 break;
99 case EQNPOS_SUP:
100 post = print_otag(p, TAG_MSUP, "");
101 break;
102 case EQNPOS_FROM:
103 post = print_otag(p, TAG_MUNDER, "");
104 break;
105 case EQNPOS_SUB:
106 post = print_otag(p, TAG_MSUB, "");
107 break;
108 case EQNPOS_OVER:
109 post = print_otag(p, TAG_MFRAC, "");
110 break;
111 case EQNPOS_FROMTO:
112 post = print_otag(p, TAG_MUNDEROVER, "");
113 break;
114 case EQNPOS_SUBSUP:
115 post = print_otag(p, TAG_MSUBSUP, "");
116 break;
117 case EQNPOS_SQRT:
118 post = print_otag(p, TAG_MSQRT, "");
119 break;
120 default:
121 break;
124 if (bp->top || bp->bottom) {
125 assert(NULL == post);
126 if (bp->top && NULL == bp->bottom)
127 post = print_otag(p, TAG_MOVER, "");
128 else if (bp->top && bp->bottom)
129 post = print_otag(p, TAG_MUNDEROVER, "");
130 else if (bp->bottom)
131 post = print_otag(p, TAG_MUNDER, "");
134 if (EQN_PILE == bp->type) {
135 assert(NULL == post);
136 if (bp->first != NULL &&
137 bp->first->type == EQN_LIST &&
138 bp->first->expectargs > 1)
139 post = print_otag(p, TAG_MTABLE, "");
140 } else if (bp->type == EQN_LIST && bp->expectargs > 1 &&
141 bp->parent && bp->parent->type == EQN_PILE) {
142 assert(NULL == post);
143 post = print_otag(p, TAG_MTR, "");
144 print_otag(p, TAG_MTD, "");
147 if (bp->text != NULL) {
148 assert(post == NULL);
149 tag = TAG_MI;
150 cp = bp->text;
151 if (isdigit((unsigned char)cp[0]) ||
152 (cp[0] == '.' && isdigit((unsigned char)cp[1]))) {
153 tag = TAG_MN;
154 while (*++cp != '\0') {
155 if (*cp != '.' &&
156 isdigit((unsigned char)*cp) == 0) {
157 tag = TAG_MI;
158 break;
161 } else if (*cp != '\0' && isalpha((unsigned char)*cp) == 0) {
162 tag = TAG_MO;
163 while (*cp != '\0') {
164 if (cp[0] == '\\' && cp[1] != '\0') {
165 cp++;
166 mandoc_escape(&cp, NULL, NULL);
167 } else if (isalnum((unsigned char)*cp)) {
168 tag = TAG_MI;
169 break;
170 } else
171 cp++;
174 font = bp->font;
175 if (bp->text[0] != '\0' &&
176 (((tag == TAG_MN || tag == TAG_MO) &&
177 font == EQNFONT_ROMAN) ||
178 (tag == TAG_MI && font == (bp->text[1] == '\0' ?
179 EQNFONT_ITALIC : EQNFONT_ROMAN))))
180 font = EQNFONT_NONE;
181 switch (font) {
182 case EQNFONT_NONE:
183 post = print_otag(p, tag, "");
184 break;
185 case EQNFONT_ROMAN:
186 post = print_otag(p, tag, "?", "fontstyle", "normal");
187 break;
188 case EQNFONT_BOLD:
189 case EQNFONT_FAT:
190 post = print_otag(p, tag, "?", "fontweight", "bold");
191 break;
192 case EQNFONT_ITALIC:
193 post = print_otag(p, tag, "?", "fontstyle", "italic");
194 break;
195 default:
196 abort();
198 print_text(p, bp->text);
199 } else if (NULL == post) {
200 if (NULL != bp->left || NULL != bp->right)
201 post = print_otag(p, TAG_MFENCED, "??",
202 "open", bp->left == NULL ? "" : bp->left,
203 "close", bp->right == NULL ? "" : bp->right);
204 if (NULL == post)
205 post = print_otag(p, TAG_MROW, "");
206 else
207 print_otag(p, TAG_MROW, "");
210 eqn_box(p, bp->first);
212 out:
213 if (NULL != bp->bottom) {
214 t = print_otag(p, TAG_MO, "");
215 print_text(p, bp->bottom);
216 print_tagq(p, t);
218 if (NULL != bp->top) {
219 t = print_otag(p, TAG_MO, "");
220 print_text(p, bp->top);
221 print_tagq(p, t);
224 if (NULL != post)
225 print_tagq(p, post);
227 eqn_box(p, bp->next);
230 void
231 print_eqn(struct html *p, const struct eqn_box *bp)
233 struct tag *t;
235 if (bp->first == NULL)
236 return;
238 t = print_otag(p, TAG_MATH, "c", "eqn");
240 p->flags |= HTML_NONOSPACE;
241 eqn_box(p, bp);
242 p->flags &= ~HTML_NONOSPACE;
244 print_tagq(p, t);