Skip to content

Commit

Permalink
Added binom and choose handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ktisha committed Sep 12, 2014
1 parent 7e15f33 commit f07c3c2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,26 @@
*/
package uk.ac.ed.ph.snuggletex.definitions;

import static uk.ac.ed.ph.snuggletex.definitions.Globals.ALL_MODES;
import static uk.ac.ed.ph.snuggletex.definitions.Globals.MATH_MODE_ONLY;
import static uk.ac.ed.ph.snuggletex.definitions.Globals.PARA_MODE_ONLY;
import static uk.ac.ed.ph.snuggletex.definitions.Globals.TEXT_MODE_ONLY;
import static uk.ac.ed.ph.snuggletex.definitions.LaTeXMode.LR;
import static uk.ac.ed.ph.snuggletex.definitions.LaTeXMode.MATH;
import static uk.ac.ed.ph.snuggletex.definitions.LaTeXMode.PARAGRAPH;
import static uk.ac.ed.ph.snuggletex.definitions.LaTeXMode.VERBATIM;
import static uk.ac.ed.ph.snuggletex.definitions.TextFlowContext.ALLOW_INLINE;
import static uk.ac.ed.ph.snuggletex.definitions.TextFlowContext.IGNORE;
import static uk.ac.ed.ph.snuggletex.definitions.TextFlowContext.START_NEW_XHTML_BLOCK;

import uk.ac.ed.ph.snuggletex.SnugglePackage;
import uk.ac.ed.ph.snuggletex.SnuggleRuntimeException;
import uk.ac.ed.ph.snuggletex.dombuilding.AccentHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.AnchorHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.ArrayHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.BoxHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.CharacterCommandHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.DoNothingHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.EnsureMathHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.EqnArrayHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.GetVarHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.HSpaceHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.HrefHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.InsertUnicodeHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.InterpretableSimpleMathHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.LineBreakHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.ListEnvironmentHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.LiteralHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathComplexCommandHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathEnvironmentHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathFenceHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathLimitsHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathNotHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathRootHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathUnderOrOverHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MathVariantMapHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MatrixHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.ModeDelegatingHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.MrowHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.ParagraphHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.SetVarHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.SimpleXHTMLContainerBuildingHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.SpaceHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.StyleHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.TabularHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.TextClassHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.TextSafeInterpretableMathIdentifierHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.UnitsHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.VerbatimHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.XMLAttrHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.XMLBlockElementHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.XMLInlineElementHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.XMLNameOrIdHandler;
import uk.ac.ed.ph.snuggletex.dombuilding.XMLUnparseHandler;
import uk.ac.ed.ph.snuggletex.semantics.Interpretation;
import uk.ac.ed.ph.snuggletex.semantics.InterpretationType;
import uk.ac.ed.ph.snuggletex.semantics.MathBracketInterpretation;
import uk.ac.ed.ph.snuggletex.dombuilding.*;
import uk.ac.ed.ph.snuggletex.semantics.*;
import uk.ac.ed.ph.snuggletex.semantics.MathBracketInterpretation.BracketType;
import uk.ac.ed.ph.snuggletex.semantics.MathIdentifierInterpretation;
import uk.ac.ed.ph.snuggletex.semantics.MathOperatorInterpretation;
import uk.ac.ed.ph.snuggletex.semantics.StyleDeclarationInterpretation;
import uk.ac.ed.ph.snuggletex.tokens.FlowToken;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import static uk.ac.ed.ph.snuggletex.definitions.Globals.*;
import static uk.ac.ed.ph.snuggletex.definitions.LaTeXMode.*;
import static uk.ac.ed.ph.snuggletex.definitions.TextFlowContext.*;

/**
* This defines the default {@link SnugglePackage} (containing {@link BuiltinCommand} and
* {@link BuiltinEnvironment}) supported by SnuggleTeX.
*
*
* <h2>Notes</h2>
*
*
* <ul>
* <li>This is defined in the static constructor(!)</li>
* <li>A subset of commands and environments are made available as constants.</li>
Expand All @@ -89,23 +35,24 @@
* @version $Revision: 742 $
*/
public final class CorePackageDefinitions {

/** Name of the core package */
public static final String CORE_PACKAGE_NAME = "Core";

/** Location of {@link ResourceBundle} providing error messages for this bundle */
public static final String CORE_ERROR_MESSAGES_PROPERTIES_BASENAME = "uk/ac/ed/ph/snuggletex/core-error-messages";

public static final String CORE_MATH_CHARACTER_DEFS_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/core-math-characters.txt";
public static final String ALL_MATH_CHARACTER_DEFS_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/all-math-characters.txt";
public static final String MATH_CHARACTER_BRACKETS_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/math-character-brackets.txt";
public static final String MATH_CHARACTER_ALIASES_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/math-character-aliases.txt";
public static final String MATH_CHARACTER_NEGATIONS_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/math-character-negations.txt";
public static final String MATH_CHARACTER_BIG_LIMITS_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/math-character-big-limits.txt";
public static final String MATH_FUNCTION_DEFINITIONS_RESOURCE_NAME = "uk/ac/ed/ph/snuggletex/math-function-definitions.txt";

public static final BuiltinCommand CMD_CHAR_BACKSLASH;
public static final BuiltinCommand CMD_FRAC;
public static final BuiltinCommand CMD_BINOM;
public static final BuiltinCommand CMD_ITEM;
public static final BuiltinCommand CMD_LIST_ITEM;
public static final BuiltinCommand CMD_LEFT;
Expand All @@ -119,6 +66,7 @@ public final class CorePackageDefinitions {
public static final BuiltinCommand CMD_NEWENVIRONMENT;
public static final BuiltinCommand CMD_RENEWENVIRONMENT;
public static final BuiltinCommand CMD_OVER;
public static final BuiltinCommand CMD_CHOOSE;
public static final BuiltinCommand CMD_PAR;
public static final BuiltinCommand CMD_PARAGRAPH;
public static final BuiltinCommand CMD_TABLE_ROW;
Expand All @@ -135,15 +83,15 @@ public final class CorePackageDefinitions {
public static final BuiltinEnvironment ENV_DISPLAYMATH;
public static final BuiltinEnvironment ENV_BRACKETED;
public static final BuiltinEnvironment ENV_STYLE;

private static final SnugglePackage corePackage;


public static final SnugglePackage getPackage() {

public static SnugglePackage getPackage() {
return corePackage;
}

static {
corePackage = new SnugglePackage(CORE_PACKAGE_NAME);

Expand Down Expand Up @@ -180,7 +128,7 @@ public static final SnugglePackage getPackage() {

/* Load in function definitions */
corePackage.loadMathFunctionDefinitions(MATH_FUNCTION_DEFINITIONS_RESOURCE_NAME);

//------------------------------------------------------------
// Single (funny) character commands. These do not eat trailing whitespace
//
Expand All @@ -193,11 +141,11 @@ public static final SnugglePackage getPackage() {
corePackage.addSimpleCommand("_", ALL_MODES, new CharacterCommandHandler("_"), ALLOW_INLINE);
corePackage.addSimpleCommand("{", ALL_MODES, new Interpretation[] {
new MathOperatorInterpretation("{"),
new MathBracketInterpretation(corePackage.getBuiltinCommandByTeXName("lbrace").getMathCharacter(), BracketType.OPENER, true),
new MathBracketInterpretation(corePackage.getBuiltinCommandByTeXName("lbrace").getMathCharacter(), BracketType.OPENER, true),
}, new ModeDelegatingHandler(new CharacterCommandHandler("{"), new InterpretableSimpleMathHandler()), null);
corePackage.addSimpleCommand("}", ALL_MODES, new Interpretation[] {
new MathOperatorInterpretation("}"),
new MathBracketInterpretation(corePackage.getBuiltinCommandByTeXName("rbrace").getMathCharacter(), BracketType.CLOSER, true),
new MathBracketInterpretation(corePackage.getBuiltinCommandByTeXName("rbrace").getMathCharacter(), BracketType.CLOSER, true),
}, new ModeDelegatingHandler(new CharacterCommandHandler("}"), new InterpretableSimpleMathHandler()), null);
corePackage.addSimpleCommand(",", ALL_MODES, new SpaceHandler("\u2009", "0.167em"), ALLOW_INLINE); /* Thin space, all modes */
corePackage.addSimpleCommand(":", MATH_MODE_ONLY, new SpaceHandler(null, "0.222em"), null); /* Medium space, math only */
Expand Down Expand Up @@ -225,7 +173,7 @@ public static final SnugglePackage getPackage() {
/* TODO: Is there an equivalent of the following in LaTeX for doing "literal" input, sort of like \verb
* but output using a normal typeface???
*/
corePackage.addComplexCommandOneArg("literal", false, TEXT_MODE_ONLY, VERBATIM, new LiteralHandler(), null);
corePackage.addComplexCommandOneArg("literal", false, TEXT_MODE_ONLY, VERBATIM, new LiteralHandler(), null);

/* Tree version of a paragraph. The {@link TokenFixer} will create these, removing any
* instances of {@link #PAR} and {@link TokenType#NEW_PARAGRAPH}.
Expand Down Expand Up @@ -322,7 +270,7 @@ public static final SnugglePackage getPackage() {

/* Other symbols valid in all modes */
corePackage.addSimpleCommand("pounds", ALL_MODES, new MathIdentifierInterpretation("\u00a3"), new TextSafeInterpretableMathIdentifierHandler(), ALLOW_INLINE);

//---------------------------------------------------------------
// Math Mode stuff (see LaTeX Companion pp39-52)

Expand Down Expand Up @@ -367,7 +315,7 @@ public static final SnugglePackage getPackage() {
CombinerTargetMatcher notTargetMatcher = new CombinerTargetMatcher() {
public boolean isAllowed(FlowToken target) {
return target.hasInterpretationType(InterpretationType.MATH_NEGATABLE);
}
}
};
corePackage.addCombinerCommand("not", MATH_MODE_ONLY, notTargetMatcher, new MathNotHandler(), null);

Expand All @@ -381,8 +329,10 @@ public boolean isAllowed(FlowToken target) {
/* Complex math macros */
corePackage.addComplexCommandSameArgMode("sqrt", true, 1, MATH_MODE_ONLY, new MathRootHandler(), null);
CMD_FRAC = corePackage.addComplexCommandSameArgMode("frac", false, 2, MATH_MODE_ONLY, new MathComplexCommandHandler("mfrac"), null);
CMD_OVER = corePackage.addSimpleCommand("over", MATH_MODE_ONLY, null, null); /* TeX style fractions {... \over ...}, replaced during fixing *;
CMD_OVER = corePackage.addSimpleCommand("over", MATH_MODE_ONLY, null, null); /* TeX style fractions {... \over ...}, replaced during fixing */
CMD_CHOOSE = corePackage.addSimpleCommand("choose", MATH_MODE_ONLY, null, null); /* TeX style fractions {... \choose ...}, replaced during fixing */
CMD_BINOM = corePackage.addComplexCommandSameArgMode("binom", false, 2, Globals.MATH_MODE_ONLY, new BinomHandler(), null);

/* Spacing */
corePackage.addSimpleCommand("quad", ALL_MODES, new SpaceHandler("\u00a0", "1em"), null);
corePackage.addSimpleCommand("qquad", ALL_MODES, new SpaceHandler("\u00a0\u00a0", "2em"), null);
Expand Down Expand Up @@ -446,13 +396,13 @@ public boolean isAllowed(FlowToken target) {
corePackage.addComplexCommandSameArgMode("setvar", true, 2, ALL_MODES, new SetVarHandler(), IGNORE);

/* =================================== ENVIRONMENTS ================================= */

ENV_MATH = corePackage.addEnvironment("math", TEXT_MODE_ONLY, MATH, (Interpretation) null, new MathEnvironmentHandler(), ALLOW_INLINE);
ENV_DISPLAYMATH = corePackage.addEnvironment("displaymath", TEXT_MODE_ONLY, MATH, (Interpretation) null, new MathEnvironmentHandler(), ALLOW_INLINE);
ENV_VERBATIM = corePackage.addEnvironment("verbatim", PARA_MODE_ONLY, VERBATIM, (Interpretation) null, new VerbatimHandler(false), START_NEW_XHTML_BLOCK);
ENV_ITEMIZE = corePackage.addEnvironment("itemize", PARA_MODE_ONLY, null, new Interpretation[] { Interpretation.LIST, Interpretation.STYLE_SENTINEL }, listEnvironmentHandler, START_NEW_XHTML_BLOCK);
ENV_ENUMERATE = corePackage.addEnvironment("enumerate", PARA_MODE_ONLY, null, new Interpretation[] { Interpretation.LIST, Interpretation.STYLE_SENTINEL }, listEnvironmentHandler, START_NEW_XHTML_BLOCK);

corePackage.addEnvironment("tabular", false, 1, PARA_MODE_ONLY, PARAGRAPH, new Interpretation[] { Interpretation.STYLE_SENTINEL, Interpretation.TABULAR }, tabularHandler, START_NEW_XHTML_BLOCK);
corePackage.addEnvironment("array", false, 1, MATH_MODE_ONLY, MATH, Interpretation.TABULAR, new ArrayHandler(), null);
corePackage.addEnvironment("cases", MATH_MODE_ONLY, MATH, Interpretation.TABULAR, new MatrixHandler(2, "{", ""), null);
Expand Down Expand Up @@ -486,7 +436,7 @@ public boolean isAllowed(FlowToken target) {
corePackage.addEnvironment("sc", TEXT_MODE_ONLY, null, StyleDeclarationInterpretation.SC, doNothingHandler, ALLOW_INLINE);
corePackage.addEnvironment("sl", TEXT_MODE_ONLY, null, StyleDeclarationInterpretation.SL, doNothingHandler, ALLOW_INLINE);
corePackage.addEnvironment("sf", TEXT_MODE_ONLY, null, StyleDeclarationInterpretation.SF, doNothingHandler, ALLOW_INLINE);

corePackage.addEnvironment("tiny", TEXT_MODE_ONLY, null, StyleDeclarationInterpretation.TINY, doNothingHandler, ALLOW_INLINE);
corePackage.addEnvironment("scriptsize", TEXT_MODE_ONLY, null, StyleDeclarationInterpretation.SCRIPTSIZE, doNothingHandler, ALLOW_INLINE);
corePackage.addEnvironment("footnotesize", TEXT_MODE_ONLY, null, StyleDeclarationInterpretation.FOOTNOTESIZE, doNothingHandler, ALLOW_INLINE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.ac.ed.ph.snuggletex.dombuilding;

import org.w3c.dom.Element;
import uk.ac.ed.ph.snuggletex.internal.DOMBuilder;
import uk.ac.ed.ph.snuggletex.internal.SnuggleParseException;
import uk.ac.ed.ph.snuggletex.tokens.ArgumentContainerToken;
import uk.ac.ed.ph.snuggletex.tokens.CommandToken;

/**
* Handles \binom{...}{...} and creates <mfrac>
*/
public class BinomHandler implements CommandHandler {

public void handleCommand(DOMBuilder builder, Element parentElement, CommandToken token) throws SnuggleParseException {
builder.appendMathMLOperatorElement(parentElement, "(");

Element result = builder.appendMathMLElement(parentElement, "mfrac");
result.setAttribute("linethickness", "0");
for (ArgumentContainerToken argument : token.getArguments()) {
builder.handleMathTokensAsSingleElement(result, argument);
}
builder.appendMathMLOperatorElement(parentElement, ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ private void visitSiblingsMathMode(Token parentToken, List<FlowToken> tokens)
fixLeadingNegativeNumber(tokens);
fencePairedParentheses(parentToken, tokens); /* (Want to get parentheses first) */
fixOverInstances(parentToken, tokens);
fixChooseInstances(parentToken, tokens);
inferParenthesisFences(parentToken, tokens);
fixSubscriptAndSuperscripts(parentToken, tokens);
fixPrimes(tokens);
Expand Down Expand Up @@ -646,7 +647,39 @@ private void fixOverInstances(Token parentToken, List<FlowToken> tokens) throws
replaceTokens(tokens, 0, tokens.size(), replacementToken);
}
}


/**
* This handles "... \choose ..." by refactoring the tokens into a \binom{...}{...}.
*/
private void fixChooseInstances(Token parentToken, List<FlowToken> tokens) throws SnuggleParseException {
int index = -1;
FlowToken token;
for (int i = 0; i < tokens.size(); i++) { /* Note: size() may change here */
token = tokens.get(i);
if (token.isCommand(CorePackageDefinitions.CMD_CHOOSE)) {
if (index != -1) {
tokens.clear();
return;
}
index = i;
}
}
if (index != -1) {
List<FlowToken> beforeTokens = new ArrayList<FlowToken>(tokens.subList(0, index));
List<FlowToken> afterTokens = new ArrayList<FlowToken>(tokens.subList(index + 1, tokens.size()));
ComputedStyle beforeStyle = tokens.get(0).getComputedStyle();
CommandToken replacementToken = new CommandToken(parentToken.getSlice(),
LaTeXMode.MATH,
CorePackageDefinitions.CMD_BINOM,
null, /* No optional arg */
new ArgumentContainerToken[]{
ArgumentContainerToken.createFromContiguousTokens(parentToken, LaTeXMode.MATH, beforeTokens, beforeStyle),
ArgumentContainerToken.createFromContiguousTokens(parentToken, LaTeXMode.MATH, afterTokens, beforeStyle)
});
replaceTokens(tokens, 0, tokens.size(), replacementToken);
}
}

/**
* Hunts through tokens for occurrences of primes used as <tt>f'</tt>, which are converted
* to a superscript by binding to the preceding token
Expand Down

0 comments on commit f07c3c2

Please sign in to comment.