eqn_html.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov/* $Id: eqn_html.c,v 1.10 2014/10/12 19:31:41 schwarze Exp $ */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore *
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * copyright notice and this permission notice appear in all copies.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore *
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "config.h"
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov#include <sys/types.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <assert.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <stdio.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <stdlib.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include <string.h>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "mandoc.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "out.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#include "html.h"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovstatic void
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankoveqn_box(struct html *p, const struct eqn_box *bp)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov{
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov struct tag *post, *row, *cell, *t;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov struct htmlpair tag[2];
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov const struct eqn_box *child, *parent;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov size_t i, j, rows;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL == bp)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov return;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = NULL;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * Special handling for a matrix, which is presented to us in
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * column order, but must be printed in row-order.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (EQN_MATRIX == bp->type) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL == bp->first)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov goto out;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (EQN_LIST != bp->first->type) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov eqn_box(p, bp->first);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov goto out;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL == (parent = bp->first->first))
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov goto out;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Estimate the number of rows, first. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL == (child = parent->first))
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov goto out;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov for (rows = 0; NULL != child; rows++)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov child = child->next;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /* Print row-by-row. */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MTABLE, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov for (i = 0; i < rows; i++) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov parent = bp->first->first;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov row = print_otag(p, TAG_MTR, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov while (NULL != parent) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov child = parent->first;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov for (j = 0; j < i; j++) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL == child)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov child = child->next;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov cell = print_otag
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov (p, TAG_MTD, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov /*
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * If we have no data for this
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * particular cell, then print a
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov * placeholder and continue--don't puke.
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov */
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != child)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov eqn_box(p, child->first);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_tagq(p, cell);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov parent = parent->next;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_tagq(p, row);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov goto out;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov switch (bp->pos) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_TO):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MOVER, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_SUP):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MSUP, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_FROM):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MUNDER, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_SUB):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MSUB, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_OVER):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MFRAC, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_FROMTO):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MUNDEROVER, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_SUBSUP):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MSUBSUP, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov case (EQNPOS_SQRT):
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MSQRT, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov default:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov break;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (bp->top || bp->bottom) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov assert(NULL == post);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (bp->top && NULL == bp->bottom)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MOVER, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else if (bp->top && bp->bottom)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MUNDEROVER, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else if (bp->bottom)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MUNDER, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (EQN_PILE == bp->type) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov assert(NULL == post);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (bp->first != NULL && bp->first->type == EQN_LIST)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MTABLE, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else if (bp->type == EQN_LIST &&
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov bp->parent && bp->parent->type == EQN_PILE) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov assert(NULL == post);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MTR, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_otag(p, TAG_MTD, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != bp->text) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov assert(NULL == post);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MI, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_text(p, bp->text);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov } else if (NULL == post) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != bp->left || NULL != bp->right) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov PAIR_INIT(&tag[0], ATTR_OPEN,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov NULL == bp->left ? "" : bp->left);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov PAIR_INIT(&tag[1], ATTR_CLOSE,
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov NULL == bp->right ? "" : bp->right);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MFENCED, 2, tag);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL == post)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov post = print_otag(p, TAG_MROW, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov else
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_otag(p, TAG_MROW, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov eqn_box(p, bp->first);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankovout:
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != bp->bottom) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov t = print_otag(p, TAG_MO, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_text(p, bp->bottom);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_tagq(p, t);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != bp->top) {
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov t = print_otag(p, TAG_MO, 0, NULL);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_text(p, bp->top);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_tagq(p, t);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov }
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov if (NULL != post)
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov print_tagq(p, post);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov eqn_box(p, bp->next);
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov}
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorevoid
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreprint_eqn(struct html *p, const struct eqn *ep)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore{
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore struct htmlpair tag;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore struct tag *t;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore PAIR_CLASS_INIT(&tag, "eqn");
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov t = print_otag(p, TAG_MATH, 1, &tag);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->flags |= HTML_NONOSPACE;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore eqn_box(p, ep->root);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore p->flags &= ~HTML_NONOSPACE;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore print_tagq(p, t);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore}