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>
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"
19 
20 #include <sys/types.h>
21 
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "mandoc.h"
29 #include "eqn.h"
30 #include "out.h"
31 #include "html.h"
32 
33 static void
eqn_box(struct html * p,const struct eqn_box * bp)34 eqn_box(struct html *p, const struct eqn_box *bp)
35 {
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;
42 
43           if (NULL == bp)
44                     return;
45 
46           post = NULL;
47 
48           /*
49            * Special handling for a matrix, which is presented to us in
50            * column order, but must be printed in row-order.
51            */
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;
59                     }
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;
78                                         }
79                                         cell = print_otag(p, TAG_MTD, "");
80                                         /*
81                                          * If we have no data for this
82                                          * particular cell, then print a
83                                          * placeholder and continue--don't puke.
84                                          */
85                                         if (NULL != child)
86                                                   eqn_box(p, child->first);
87                                         print_tagq(p, cell);
88                                         parent = parent->next;
89                               }
90                               print_tagq(p, row);
91                     }
92                     goto out;
93           }
94 
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;
122           }
123 
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, "");
132           }
133 
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, "");
145           }
146 
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;
159                                         }
160                               }
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++;
172                               }
173                     }
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();
197                     }
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, "");
208           }
209 
210           eqn_box(p, bp->first);
211 
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);
217           }
218           if (NULL != bp->top) {
219                     t = print_otag(p, TAG_MO, "");
220                     print_text(p, bp->top);
221                     print_tagq(p, t);
222           }
223 
224           if (NULL != post)
225                     print_tagq(p, post);
226 
227           eqn_box(p, bp->next);
228 }
229 
230 void
print_eqn(struct html * p,const struct eqn_box * bp)231 print_eqn(struct html *p, const struct eqn_box *bp)
232 {
233           struct tag          *t;
234 
235           if (bp->first == NULL)
236                     return;
237 
238           t = print_otag(p, TAG_MATH, "c", "eqn");
239 
240           p->flags |= HTML_NONOSPACE;
241           eqn_box(p, bp);
242           p->flags &= ~HTML_NONOSPACE;
243 
244           print_tagq(p, t);
245 }
246