<commit_before>/* $Id$
 *
 * Copyright 2008 University of Edinburgh.
 * All Rights Reserved
 */
package uk.ac.ed.ph.snuggletex.dombuilding;

import uk.ac.ed.ph.snuggletex.conversion.DOMBuilder;
import uk.ac.ed.ph.snuggletex.conversion.SnuggleParseException;
import uk.ac.ed.ph.snuggletex.definitions.BuiltinEnvironment;
import uk.ac.ed.ph.snuggletex.definitions.GlobalBuiltins;
import uk.ac.ed.ph.snuggletex.tokens.EnvironmentToken;

import org.w3c.dom.DOMException;
import org.w3c.dom.Element;

/**
 * Builds LaTeX math environments. Note that this might be inside an mbox inside
 * an existing math environment.
 * 
 * @author  <NAME>
 * @version $Revision$
 */
public final class MathEnvironmentBuilder implements EnvironmentHandler {

    public void handleEnvironment(DOMBuilder builder, Element parentElement, EnvironmentToken token)
            throws DOMException, SnuggleParseException {
        BuiltinEnvironment environment = token.getEnvironment();
        if (builder.isBuildingMathMLIsland(parentElement)) {
            /* We're putting maths inside maths (e.g. \mbox{$ $}) so make a <mrow/> */
            Element mrow = builder.appendMathMLElement(parentElement, "mrow");
            builder.handleTokens(mrow, token.getContent(), false);
        }
        else {
            /* Create a proper <math>...</math> element with optional annotation */
            Element math = builder.appendMathMLElement(parentElement, "math");
            if (environment==GlobalBuiltins.DISPLAYMATH) {
                math.setAttribute("display", "block");
            }
            if (builder.getOptions().isAddingMathAnnotations()) {
                /* The structure here is <semantics>....<annotation-xml/></semantics> */
                Element semantics = builder.appendMathMLElement(math, "semantics");
                
                builder.handleTokens(semantics, token.getContent(), false);
                
                Element annotation = builder.appendMathMLTextElement(semantics, "annotation-xml",
                        token.getContent().getSlice().extract().toString(), true);
                annotation.setAttribute("encoding", "snuffle-tex");
            }
            else {
                builder.handleTokens(math, token.getContent(), false);
            }
        }
    }
}<commit_msg>Fixed bug when producing MathML annotations - my understanding of what to do here was broken and is now fixed :-)<commit_after>/* $Id$
 *
 * Copyright 2008 University of Edinburgh.
 * All Rights Reserved
 */
package uk.ac.ed.ph.snuggletex.dombuilding;

import uk.ac.ed.ph.snuggletex.conversion.DOMBuilder;
import uk.ac.ed.ph.snuggletex.conversion.SnuggleParseException;
import uk.ac.ed.ph.snuggletex.definitions.BuiltinEnvironment;
import uk.ac.ed.ph.snuggletex.definitions.GlobalBuiltins;
import uk.ac.ed.ph.snuggletex.tokens.EnvironmentToken;

import org.w3c.dom.DOMException;
import org.w3c.dom.Element;

/**
 * Builds LaTeX math environments. Note that this might be inside an mbox inside
 * an existing math environment.
 * 
 * @author  <NAME>
 * @version $Revision$
 */
public final class MathEnvironmentBuilder implements EnvironmentHandler {

    public void handleEnvironment(DOMBuilder builder, Element parentElement, EnvironmentToken token)
            throws DOMException, SnuggleParseException {
        BuiltinEnvironment environment = token.getEnvironment();
        if (builder.isBuildingMathMLIsland(parentElement)) {
            /* We're putting maths inside maths (e.g. \mbox{$ $}) so make a <mrow/> */
            Element mrow = builder.appendMathMLElement(parentElement, "mrow");
            builder.handleTokens(mrow, token.getContent(), false);
        }
        else {
            /* Create a proper <math>...</math> element with optional annotation */
            Element math = builder.appendMathMLElement(parentElement, "math");
            if (environment==GlobalBuiltins.DISPLAYMATH) {
                math.setAttribute("display", "block");
            }
            if (builder.getOptions().isAddingMathAnnotations()) {
                /* The structure here is <semantics>...<annotation/></semantics>
                 * where the first child of <semantics> is the resulting MathML.
                 * (Therefore, we need to wrap the MathML in an <mrow/> if there is
                 * more than one top level element here)
                 */
                Element semantics = builder.appendMathMLElement(math, "semantics");
                builder.handleMathTokensAsSingleElement(semantics, token.getContent());

                /* Create annotation */
                Element annotation = builder.appendMathMLTextElement(semantics, "annotation",
                        token.getContent().getSlice().extract().toString(), true);
                annotation.setAttribute("encoding", "SnuggleTeX");
            }
            else {
                builder.handleTokens(math, token.getContent(), false);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10778803693295864, 'fraction_numerical': 0.0024086712163789645, 'mean_word_length': 2.605643994211288, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3402968', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1291, 'n_words': 310}