public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ x+2=3 $$"); session.parseInput(input); /* Convert the results to an XML String, which in this case will * be a single MathML <math>...</math> element. */ String xmlString = session.buildXMLString(); System.out.println("Input " + input.getString() + " was converted to:\n" + xmlString); } }
@Override public String toString() { if (stringRepresentation==null) { stringRepresentation = buildStringRepresentation(); } return stringRepresentation; }
private StringBuilder readInputData() throws IOException { switch (input.getType()) { case STRING: return new StringBuilder(input.getString()); case FILE: return readCharacterStream(createReader(new FileInputStream(input.getFile()), input.getEncoding())); case INPUT_STREAM: return readCharacterStream(createReader(input.getInputStream(), input.getEncoding())); case READER: return readCharacterStream(input.getReader()); default: throw new SnuggleLogicException("Unexpected switch case: " + input.getType()); } }
private Node latexToNode(String latex){ SnuggleEngine engine = new SnuggleEngine(); try { SnuggleSession createSession = engine.createSession(); createSession.parseInput(new SnuggleInput(latex)); NodeList dst = createSession.buildDOMSubtree(); return dst.item(0); } catch (IOException e) { throw new RuntimeException(e); } }
public URI resolveLink(final Element parentElement, final Token token, final String rawHref) throws SnuggleParseException { URI result = validateURI(parentElement, token, rawHref); if (result==null) { return null; } LinkResolver linkResolver = options.getLinkResolver(); if (linkResolver!=null) { result = linkResolver.mapLink(result, token.getSlice().getDocument().getInput().getURI()); } return result; }
private static void appendFrame(StringBuffer messageBuilder, CharacterSource source, int offsetInSource) { SourceContext context = source.context; if (context instanceof SnuggleInputReader) { SnuggleInputReader inputContext = (SnuggleInputReader) context; int[] location = inputContext.getLineAndColumn(offsetInSource); appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("input_context")).format(new Object[] { location[0], /* Line */ location[1], /* Column */ inputContext.getInput().getIdentifier() /* Input description */ }, messageBuilder, null); } else if (context instanceof WorkingDocument.SubstitutionContext) { WorkingDocument.SubstitutionContext substitutionContext = (WorkingDocument.SubstitutionContext) context; appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("subs_context")).format(new Object[] { offsetInSource, /* Character index */ formatText(source.substitutedText), /* Before subs */ formatText(substitutionContext.replacement) /* After subs */ }, messageBuilder, null); } else { throw new SnuggleLogicException("Unexpected SourceContext " + context.getClass().getName()); } if (source.substitutedSource!=null) { appendFrame(messageBuilder, source.substitutedSource, source.substitutionOffset); } }
void convert(Element latexElement) throws IOException { String rawInputLaTeX = latexElement.getTextContent(); String inputLaTeX = rawInputLaTeX.replaceAll("(\r\n|\r|\n)", "\n"); SnuggleEngine engine = createSnuggleEngine(); SnuggleSession session = engine.createSession(); SnuggleInput input = new SnuggleInput(inputLaTeX, "LaTeX Element"); try { session.parseInput(input); } catch (Exception e) { throw new IOException("Error while parsing: " + rawInputLaTeX + ": " + e.getMessage(), e); } while (latexElement.getChildNodes().getLength() != 0) latexElement.removeChild(latexElement.getFirstChild()); DOMOutputOptions options = new DOMOutputOptions(); options.setErrorOutputOptions(DOMOutputOptions.ErrorOutputOptions.XHTML); try { session.buildDOMSubtree(latexElement, options); } catch (Exception e) { throw new IOException("Error while building DOM for: " + rawInputLaTeX + ": " + e.getMessage(), e); } }
public URI resolveLink(final Element parentElement, final Token token, final String rawHref) throws SnuggleParseException { URI result = validateURI(parentElement, token, rawHref); if (result==null) { return null; } LinkResolver linkResolver = options.getLinkResolver(); if (linkResolver!=null) { result = linkResolver.mapLink(result, token.getSlice().getDocument().getInput().getURI()); } return result; }
private static void appendFrame(StringBuffer messageBuilder, CharacterSource source, int offsetInSource) { SourceContext context = source.context; if (context instanceof SnuggleInputReader) { SnuggleInputReader inputContext = (SnuggleInputReader) context; int[] location = inputContext.getLineAndColumn(offsetInSource); appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("input_context")).format(new Object[] { location[0], /* Line */ location[1], /* Column */ inputContext.getInput().getIdentifier() /* Input description */ }, messageBuilder, null); } else if (context instanceof WorkingDocument.SubstitutionContext) { WorkingDocument.SubstitutionContext substitutionContext = (WorkingDocument.SubstitutionContext) context; appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("subs_context")).format(new Object[] { offsetInSource, /* Character index */ formatText(source.substitutedText), /* Before subs */ formatText(substitutionContext.replacement) /* After subs */ }, messageBuilder, null); } else { throw new SnuggleLogicException("Unexpected SourceContext " + context.getClass().getName()); } if (source.substitutedSource!=null) { appendFrame(messageBuilder, source.substitutedSource, source.substitutionOffset); } }
private StringBuilder readInputData() throws IOException { switch (input.getType()) { case STRING: return new StringBuilder(input.getString()); case FILE: /* (Assumes platform default encoding) */ return readCharacterStream(new InputStreamReader(new FileInputStream(input.getFile()))); case INPUT_STREAM: /* (Assumes platform default encoding) */ return readCharacterStream(new InputStreamReader(input.getInputStream())); case READER: return readCharacterStream(input.getReader()); default: throw new SnuggleLogicException("Unexpected switch case: " + input.getType()); } }
private Node latexToNode(String latex){ SnuggleEngine engine = new SnuggleEngine(); try { SnuggleSession createSession = engine.createSession(); createSession.parseInput(new SnuggleInput(latex)); NodeList dst = createSession.buildDOMSubtree(); return dst.item(0); } catch (IOException e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ x+2=3 $$"); session.parseInput(input); /* Convert the results to an XML String, which in this case will * be a single MathML <math>...</math> element. */ String xmlString = session.buildXMLString(); System.out.println("Input " + input.getString() + " was converted to:\n" + xmlString); } }
@Override public String toString() { if (stringRepresentation==null) { stringRepresentation = buildStringRepresentation(); } return stringRepresentation; }
public URI resolveLink(final Element parentElement, final Token token, final String rawHref) throws SnuggleParseException { URI result = validateURI(parentElement, token, rawHref); if (result==null) { return null; } LinkResolver linkResolver = options.getLinkResolver(); if (linkResolver!=null) { result = linkResolver.mapLink(result, token.getSlice().getDocument().getInput().getURI()); } return result; }
private static void appendFrame(StringBuffer messageBuilder, CharacterSource source, int offsetInSource) { SourceContext context = source.context; if (context instanceof SnuggleInputReader) { SnuggleInputReader inputContext = (SnuggleInputReader) context; int[] location = inputContext.getLineAndColumn(offsetInSource); appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("input_context")).format(new Object[] { location[0], /* Line */ location[1], /* Column */ inputContext.getInput().getIdentifier() /* Input description */ }, messageBuilder, null); } else if (context instanceof WorkingDocument.SubstitutionContext) { WorkingDocument.SubstitutionContext substitutionContext = (WorkingDocument.SubstitutionContext) context; appendNewlineIfRequired(messageBuilder); new MessageFormat(GENERAL_MESSAGE_BUNDLE.getString("subs_context")).format(new Object[] { offsetInSource, /* Character index */ formatText(source.substitutedText), /* Before subs */ formatText(substitutionContext.replacement) /* After subs */ }, messageBuilder, null); } else { throw new SnuggleLogicException("Unexpected SourceContext " + context.getClass().getName()); } if (source.substitutedSource!=null) { appendFrame(messageBuilder, source.substitutedSource, source.substitutionOffset); } }
private StringBuilder readInputData() throws IOException { switch (input.getType()) { case STRING: return new StringBuilder(input.getString()); case FILE: /* (Assumes platform default encoding) */ return readCharacterStream(new InputStreamReader(new FileInputStream(input.getFile()))); case INPUT_STREAM: /* (Assumes platform default encoding) */ return readCharacterStream(new InputStreamReader(input.getInputStream())); case READER: return readCharacterStream(input.getReader()); default: throw new SnuggleLogicException("Unexpected switch case: " + input.getType()); } }
public Element doMathInput(String mathModeInput, DOMOutputOptions domOutputOptions) { ConstraintUtilities.ensureNotNull(mathModeInput, "Math Mode Input"); String latexInput = "\\[" + mathModeInput + "\\]"; session.reset(); try { session.parseInput(new SnuggleInput(latexInput, "Math Mode Input")); } catch (IOException e) { throw new SnuggleLogicException("Unexpected IOException"); } NodeList nodeList = session.buildDOMSubtree(domOutputOptions); if (!session.getErrors().isEmpty()) { return null; } /* Extract and check <math> element */ Element result = null; if (nodeList.getLength()==1) { result = (Element) nodeList.item(0); if (!MathMLUtilities.isMathMLElement(result, "math")) { throw new IllegalArgumentException("Input did not yield exactly 1 <math> element"); } } else { throw new IllegalArgumentException("Input did not yield exactly 1 result element"); } return result; }
public static void main(String[] args) throws IOException { /* Create vanilla SnuggleEngine and new SnuggleSession */ SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); /* Parse some very basic Math Mode input */ SnuggleInput input = new SnuggleInput("$$ x+2=3 $$"); session.parseInput(input); /* Convert the results to an XML String, which in this case will * be a single MathML <math>...</math> element. */ String xmlString = session.buildXMLString(); System.out.println("Input " + input.getString() + " was converted to:\n" + xmlString); } }
@Override public String toString() { if (stringRepresentation==null) { stringRepresentation = buildStringRepresentation(); } return stringRepresentation; }
if (inputFile.equals("-")) { input = new SnuggleInput(System.in); input = new SnuggleInput(new File(inputFile));