2017-07-31 19:17:54 +00:00
|
|
|
/* $Id: eqn_html.c,v 1.17 2017/07/14 13:32:35 schwarze Exp $ */
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
2014-12-02 07:34:06 +00:00
|
|
|
* Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
|
2017-01-21 13:01:39 +00:00
|
|
|
* Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
|
2012-10-18 09:55:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
2014-12-02 07:34:06 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2017-07-31 19:17:54 +00:00
|
|
|
#include <ctype.h>
|
2012-10-18 09:55:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mandoc.h"
|
|
|
|
#include "out.h"
|
|
|
|
#include "html.h"
|
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
static void
|
|
|
|
eqn_box(struct html *p, const struct eqn_box *bp)
|
|
|
|
{
|
|
|
|
struct tag *post, *row, *cell, *t;
|
|
|
|
const struct eqn_box *child, *parent;
|
2017-07-31 19:17:54 +00:00
|
|
|
const char *cp;
|
2014-12-02 07:34:06 +00:00
|
|
|
size_t i, j, rows;
|
2017-07-31 19:17:54 +00:00
|
|
|
enum htmltag tag;
|
|
|
|
enum eqn_fontt font;
|
2014-12-02 07:34:06 +00:00
|
|
|
|
|
|
|
if (NULL == bp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
post = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special handling for a matrix, which is presented to us in
|
|
|
|
* column order, but must be printed in row-order.
|
|
|
|
*/
|
|
|
|
if (EQN_MATRIX == bp->type) {
|
|
|
|
if (NULL == bp->first)
|
|
|
|
goto out;
|
2017-07-31 19:17:54 +00:00
|
|
|
if (bp->first->type != EQN_LIST ||
|
|
|
|
bp->first->expectargs == 1) {
|
2014-12-02 07:34:06 +00:00
|
|
|
eqn_box(p, bp->first);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (NULL == (parent = bp->first->first))
|
|
|
|
goto out;
|
|
|
|
/* Estimate the number of rows, first. */
|
|
|
|
if (NULL == (child = parent->first))
|
|
|
|
goto out;
|
|
|
|
for (rows = 0; NULL != child; rows++)
|
|
|
|
child = child->next;
|
|
|
|
/* Print row-by-row. */
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MTABLE, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
for (i = 0; i < rows; i++) {
|
|
|
|
parent = bp->first->first;
|
2017-01-21 13:01:39 +00:00
|
|
|
row = print_otag(p, TAG_MTR, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
while (NULL != parent) {
|
|
|
|
child = parent->first;
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
if (NULL == child)
|
|
|
|
break;
|
|
|
|
child = child->next;
|
|
|
|
}
|
2017-01-21 13:01:39 +00:00
|
|
|
cell = print_otag(p, TAG_MTD, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
/*
|
|
|
|
* If we have no data for this
|
|
|
|
* particular cell, then print a
|
|
|
|
* placeholder and continue--don't puke.
|
|
|
|
*/
|
|
|
|
if (NULL != child)
|
|
|
|
eqn_box(p, child->first);
|
|
|
|
print_tagq(p, cell);
|
|
|
|
parent = parent->next;
|
|
|
|
}
|
|
|
|
print_tagq(p, row);
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (bp->pos) {
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_TO:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MOVER, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_SUP:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MSUP, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_FROM:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MUNDER, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_SUB:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MSUB, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_OVER:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MFRAC, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_FROMTO:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MUNDEROVER, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_SUBSUP:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MSUBSUP, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
2017-07-31 19:17:54 +00:00
|
|
|
case EQNPOS_SQRT:
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MSQRT, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bp->top || bp->bottom) {
|
|
|
|
assert(NULL == post);
|
|
|
|
if (bp->top && NULL == bp->bottom)
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MOVER, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
else if (bp->top && bp->bottom)
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MUNDEROVER, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
else if (bp->bottom)
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MUNDER, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (EQN_PILE == bp->type) {
|
|
|
|
assert(NULL == post);
|
2017-07-31 19:17:54 +00:00
|
|
|
if (bp->first != NULL &&
|
|
|
|
bp->first->type == EQN_LIST &&
|
|
|
|
bp->first->expectargs > 1)
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MTABLE, "");
|
2017-07-31 19:17:54 +00:00
|
|
|
} else if (bp->type == EQN_LIST && bp->expectargs > 1 &&
|
2014-12-02 07:34:06 +00:00
|
|
|
bp->parent && bp->parent->type == EQN_PILE) {
|
|
|
|
assert(NULL == post);
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MTR, "");
|
|
|
|
print_otag(p, TAG_MTD, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 19:17:54 +00:00
|
|
|
if (bp->text != NULL) {
|
|
|
|
assert(post == NULL);
|
|
|
|
tag = TAG_MI;
|
|
|
|
cp = bp->text;
|
|
|
|
if (isdigit((unsigned char)cp[0]) ||
|
|
|
|
(cp[0] == '.' && isdigit((unsigned char)cp[1]))) {
|
|
|
|
tag = TAG_MN;
|
|
|
|
while (*++cp != '\0') {
|
|
|
|
if (*cp != '.' &&
|
|
|
|
isdigit((unsigned char)*cp) == 0) {
|
|
|
|
tag = TAG_MI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (*cp != '\0' && isalpha((unsigned char)*cp) == 0) {
|
|
|
|
tag = TAG_MO;
|
|
|
|
while (*cp != '\0') {
|
|
|
|
if (cp[0] == '\\' && cp[1] != '\0') {
|
|
|
|
cp++;
|
|
|
|
mandoc_escape(&cp, NULL, NULL);
|
|
|
|
} else if (isalnum((unsigned char)*cp)) {
|
|
|
|
tag = TAG_MI;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
font = bp->font;
|
|
|
|
if (bp->text[0] != '\0' &&
|
|
|
|
(((tag == TAG_MN || tag == TAG_MO) &&
|
|
|
|
font == EQNFONT_ROMAN) ||
|
|
|
|
(tag == TAG_MI && font == (bp->text[1] == '\0' ?
|
|
|
|
EQNFONT_ITALIC : EQNFONT_ROMAN))))
|
|
|
|
font = EQNFONT_NONE;
|
|
|
|
switch (font) {
|
|
|
|
case EQNFONT_NONE:
|
|
|
|
post = print_otag(p, tag, "");
|
|
|
|
break;
|
|
|
|
case EQNFONT_ROMAN:
|
|
|
|
post = print_otag(p, tag, "?", "fontstyle", "normal");
|
|
|
|
break;
|
|
|
|
case EQNFONT_BOLD:
|
|
|
|
case EQNFONT_FAT:
|
|
|
|
post = print_otag(p, tag, "?", "fontweight", "bold");
|
|
|
|
break;
|
|
|
|
case EQNFONT_ITALIC:
|
|
|
|
post = print_otag(p, tag, "?", "fontstyle", "italic");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2014-12-02 07:34:06 +00:00
|
|
|
print_text(p, bp->text);
|
|
|
|
} else if (NULL == post) {
|
2017-01-21 13:01:39 +00:00
|
|
|
if (NULL != bp->left || NULL != bp->right)
|
|
|
|
post = print_otag(p, TAG_MFENCED, "??",
|
|
|
|
"open", bp->left == NULL ? "" : bp->left,
|
|
|
|
"close", bp->right == NULL ? "" : bp->right);
|
2014-12-02 07:34:06 +00:00
|
|
|
if (NULL == post)
|
2017-01-21 13:01:39 +00:00
|
|
|
post = print_otag(p, TAG_MROW, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
else
|
2017-01-21 13:01:39 +00:00
|
|
|
print_otag(p, TAG_MROW, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eqn_box(p, bp->first);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (NULL != bp->bottom) {
|
2017-01-21 13:01:39 +00:00
|
|
|
t = print_otag(p, TAG_MO, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
print_text(p, bp->bottom);
|
|
|
|
print_tagq(p, t);
|
|
|
|
}
|
|
|
|
if (NULL != bp->top) {
|
2017-01-21 13:01:39 +00:00
|
|
|
t = print_otag(p, TAG_MO, "");
|
2014-12-02 07:34:06 +00:00
|
|
|
print_text(p, bp->top);
|
|
|
|
print_tagq(p, t);
|
|
|
|
}
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
if (NULL != post)
|
|
|
|
print_tagq(p, post);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2014-12-02 07:34:06 +00:00
|
|
|
eqn_box(p, bp->next);
|
|
|
|
}
|
2014-11-22 18:08:25 +00:00
|
|
|
|
2012-10-18 09:55:16 +00:00
|
|
|
void
|
2017-07-31 19:17:54 +00:00
|
|
|
print_eqn(struct html *p, const struct eqn_box *bp)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
|
|
|
struct tag *t;
|
|
|
|
|
2017-07-31 19:17:54 +00:00
|
|
|
if (bp->first == NULL)
|
|
|
|
return;
|
|
|
|
|
2017-01-21 13:01:39 +00:00
|
|
|
t = print_otag(p, TAG_MATH, "c", "eqn");
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
p->flags |= HTML_NONOSPACE;
|
2017-07-31 19:17:54 +00:00
|
|
|
eqn_box(p, bp);
|
2012-10-18 09:55:16 +00:00
|
|
|
p->flags &= ~HTML_NONOSPACE;
|
|
|
|
|
|
|
|
print_tagq(p, t);
|
|
|
|
}
|