Finishing work on MathML, TexMaths and MathJax
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@162 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
fbd7e66412
commit
799d832339
13 changed files with 164 additions and 44 deletions
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2012-08-13)
|
||||
* Version 1.4 (2012-08-20)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class ConverterFactory {
|
|||
|
||||
// Version information
|
||||
private static final String VERSION = "1.3.1";
|
||||
private static final String DATE = "2014-08-13";
|
||||
private static final String DATE = "2014-08-20";
|
||||
|
||||
/** Return the Writer2LaTeX version in the form
|
||||
* (major version).(minor version).(patch level)<br/>
|
||||
|
|
|
@ -20,13 +20,15 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2014-08-13)
|
||||
* Version 1.4 (2014-08-18)
|
||||
*
|
||||
*/
|
||||
|
||||
package writer2latex.latex;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
@ -57,6 +59,7 @@ public final class MathConverter extends ConverterHelper {
|
|||
private boolean bContainsFormulas = false;
|
||||
private boolean bAddParAfterDisplay = false;
|
||||
|
||||
private boolean bNeedTexMathsPreamble = false;
|
||||
private boolean bNeedOOoLaTeXPreamble = false;
|
||||
|
||||
private Element theEquation = null;
|
||||
|
@ -77,6 +80,14 @@ public final class MathConverter extends ConverterHelper {
|
|||
smc.appendDeclarations(pack,decl);
|
||||
}
|
||||
}
|
||||
if (bNeedTexMathsPreamble) {
|
||||
// The preamble may be stored as a user defined property (newline is represented as paragraph sign)
|
||||
Map<String,String> props = palette.getMetaData().getUserDefinedMetaData();
|
||||
if (props.containsKey("TexMathsPreamble")) {
|
||||
decl.append("% TexMaths preamble\n")
|
||||
.append(props.get("TexMathsPreamble").replace('\u00a7', '\n'));
|
||||
}
|
||||
}
|
||||
if (bNeedOOoLaTeXPreamble) {
|
||||
// The preamble may be stored in the description
|
||||
String sDescription = palette.getMetaData().getDescription();
|
||||
|
@ -145,12 +156,14 @@ public final class MathConverter extends ConverterHelper {
|
|||
Element equation = palette.getTexMathsEquation(node);
|
||||
if (equation!=null) {
|
||||
sLaTeX = Misc.getPCDATA(equation);
|
||||
if (sLaTeX!=null) { bNeedTexMathsPreamble = true; }
|
||||
}
|
||||
else { // Try OOoLaTeX
|
||||
// The LaTeX code is embedded in a custom style attribute:
|
||||
StyleWithProperties style = ofr.getFrameStyle(Misc.getAttribute(node, XMLString.DRAW_STYLE_NAME));
|
||||
if (style!=null) {
|
||||
sLaTeX = style.getProperty("OOoLatexArgs");
|
||||
sLaTeX = style.getProperty("OOoLatexArgs");
|
||||
if (sLaTeX!=null) { bNeedOOoLaTeXPreamble = true; }
|
||||
}
|
||||
}
|
||||
if (sLaTeX!=null) {
|
||||
|
@ -225,13 +238,15 @@ public final class MathConverter extends ConverterHelper {
|
|||
// TeXMaths equation
|
||||
sLaTeX = palette.getTexMathsEquation(Misc.getPCDATA(equation));
|
||||
style = palette.getTexMathsStyle(Misc.getPCDATA(equation));
|
||||
if (sLaTeX!=null) { bNeedTexMathsPreamble = true; }
|
||||
}
|
||||
else {
|
||||
// MathML equation
|
||||
sLaTeX = convert(null,equation);
|
||||
}
|
||||
if (!" ".equals(sLaTeX)) { // ignore empty formulas
|
||||
if (sLaTeX!=null && !" ".equals(sLaTeX)) { // ignore empty formulas
|
||||
if (!bTexMaths || style!=TexMathsStyle.latex) {
|
||||
// Unfortunately we can't do numbered equations for TexMaths equations with latex style
|
||||
if (sequence!=null) {
|
||||
// Numbered equation
|
||||
ldp.append("\\begin{equation}");
|
||||
|
|
|
@ -16,11 +16,11 @@
|
|||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2012 by Henrik Just
|
||||
* Copyright: 2002-2014 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.2 (2012-03-05)
|
||||
* Version 1.4 (2014-08-20)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -85,7 +85,21 @@ public class OfficeReader {
|
|||
node.getNodeName().equals(XMLString.TEXT_FOOTNOTE) ||
|
||||
node.getNodeName().equals(XMLString.TEXT_ENDNOTE) );
|
||||
}
|
||||
|
||||
|
||||
/** Get the paragraph or heading containing a node
|
||||
*
|
||||
* @param node the node in question
|
||||
* @return the paragraph or heading
|
||||
*/
|
||||
public static Element getParagraph(Element node) {
|
||||
Element parent = (Element) node.getParentNode();
|
||||
if (parent.getTagName().equals(XMLString.TEXT_P) || parent.getTagName().equals(XMLString.TEXT_H)) {
|
||||
return parent;
|
||||
}
|
||||
return getParagraph(parent);
|
||||
}
|
||||
|
||||
|
||||
/** Checks, if this node contains at most one element, and that this is a
|
||||
* paragraph.
|
||||
* @param node the node to check
|
||||
|
@ -109,6 +123,28 @@ public class OfficeReader {
|
|||
return bFoundPar;
|
||||
}
|
||||
|
||||
/** Checks, if the only text content of this node is whitespace.
|
||||
* Other (draw) content is allowed.
|
||||
* @param node the node to check (should be a paragraph node or a child
|
||||
* of a paragraph node)
|
||||
* @return true if the node contains whitespace only
|
||||
*/
|
||||
public static boolean isNoTextPar(Node node) {
|
||||
Node child = node.getFirstChild();
|
||||
while (child!=null) {
|
||||
if (child.getNodeType()==Node.ELEMENT_NODE) {
|
||||
if (isTextElement(child)) {
|
||||
if (!isWhitespaceContent(child)) { return false; }
|
||||
}
|
||||
}
|
||||
else if (child.getNodeType()==Node.TEXT_NODE) {
|
||||
if (!isWhitespace(child.getNodeValue())) { return false; }
|
||||
}
|
||||
child = child.getNextSibling();
|
||||
}
|
||||
return true; // found nothing!
|
||||
}
|
||||
|
||||
/** <p>Checks, if the only text content of this node is whitespace</p>
|
||||
* @param node the node to check (should be a paragraph node or a child
|
||||
* of a paragraph node)
|
||||
|
@ -1073,14 +1109,6 @@ public class OfficeReader {
|
|||
}*/
|
||||
}
|
||||
|
||||
private Element getParagraph(Element node) {
|
||||
Element parent = (Element) node.getParentNode();
|
||||
if (parent.getTagName().equals(XMLString.TEXT_P) || parent.getTagName().equals(XMLString.TEXT_H)) {
|
||||
return parent;
|
||||
}
|
||||
return getParagraph(parent);
|
||||
}
|
||||
|
||||
private void traverseContent(Element node, String sListStyleName, int nListLevel, int nParLevel) {
|
||||
// Handle this node first
|
||||
String sName = node.getTagName();
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2014-08-12)
|
||||
* Version 1.4 (2014-08-20)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -360,6 +360,8 @@ public class DrawConverter extends ConverterHelper {
|
|||
hnodeBlock.appendChild(div);
|
||||
hnode = div;
|
||||
}
|
||||
|
||||
boolean bNoTextPar = OfficeReader.isNoTextPar(OfficeReader.getParagraph(onode));
|
||||
|
||||
String sHref = Misc.getAttribute(onode, XMLString.XLINK_HREF);
|
||||
if (sHref!=null) { // Embedded object in package or linked object
|
||||
|
@ -376,7 +378,7 @@ public class DrawConverter extends ConverterHelper {
|
|||
}
|
||||
try {
|
||||
hnode.appendChild(converter.createTextNode(" "));
|
||||
getMathCv().convert(replacementImage,xmlObject.getContentDOM().getDocumentElement(),hnode);
|
||||
getMathCv().convert(replacementImage,xmlObject.getContentDOM().getDocumentElement(),hnode,bNoTextPar);
|
||||
hnode.appendChild(converter.createTextNode(" "));
|
||||
}
|
||||
catch (SAXException e) {
|
||||
|
@ -415,7 +417,7 @@ public class DrawConverter extends ConverterHelper {
|
|||
replacementImage = Misc.getChildByTagName(getFrame(onode),XMLString.DRAW_IMAGE);
|
||||
}
|
||||
hnode.appendChild(converter.createTextNode(" "));
|
||||
getMathCv().convert(replacementImage,formula,hnode);
|
||||
getMathCv().convert(replacementImage,formula,hnode,bNoTextPar);
|
||||
hnode.appendChild(converter.createTextNode(" "));
|
||||
}
|
||||
else { // unsupported object
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2014-08-13)
|
||||
* Version 1.4 (2014-08-20)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -70,9 +70,9 @@ public class MathConverter extends ConverterHelper {
|
|||
* @param onode the math node
|
||||
* @param hnode the xhtml node to which content should be added
|
||||
*/
|
||||
public void convert(Node image, Element onode, Node hnode) {
|
||||
public void convert(Node image, Element onode, Node hnode, boolean bAllowDisplayStyle) {
|
||||
if (bSupportMathML) {
|
||||
convertAsMathML(onode,hnode);
|
||||
convertAsMathML(onode,hnode,bAllowDisplayStyle);
|
||||
}
|
||||
else {
|
||||
convertAsImageOrText(image,onode,hnode);
|
||||
|
@ -182,15 +182,33 @@ public class MathConverter extends ConverterHelper {
|
|||
}
|
||||
|
||||
// For xhtml+mathml: Insert the mathml, removing the namespace (if any) and the annotation
|
||||
public void convertAsMathML(Node onode, Node hnode) {
|
||||
private void convertAsMathML(Element onode, Node hnode, boolean bAllowDisplay) {
|
||||
Element math = converter.createElement("math");
|
||||
if (onode.hasAttribute("xmlns:math")) {
|
||||
math.setAttribute("xmlns", onode.getAttribute("xmlns:math"));
|
||||
}
|
||||
else if (onode.hasAttribute("xmlns")) {
|
||||
math.setAttribute("xmlns", onode.getAttribute("xmlns"));
|
||||
}
|
||||
if (bAllowDisplay && onode.hasAttribute("display")) {
|
||||
// Starting with version 4.2, LO exports display="block" for display equations
|
||||
// This is a good thing, but in XHTML we can unfortunately only allow this for
|
||||
// paragraphs with no other text content
|
||||
math.setAttribute("display", onode.getAttribute("display"));
|
||||
}
|
||||
hnode.appendChild(math);
|
||||
convertMathMLNodeList(onode.getChildNodes(), math);
|
||||
}
|
||||
|
||||
private void convertElementAsMathML(Node onode, Node hnode) {
|
||||
if (onode.getNodeType()==Node.ELEMENT_NODE) {
|
||||
if (onode.getNodeName().equals(XMLString.SEMANTICS)) { // Since OOo 3.2
|
||||
// ignore this construction
|
||||
convertNodeList(onode.getChildNodes(),hnode);
|
||||
convertMathMLNodeList(onode.getChildNodes(),hnode);
|
||||
}
|
||||
else if (onode.getNodeName().equals(XMLString.MATH_SEMANTICS)) {
|
||||
// ignore this construction
|
||||
convertNodeList(onode.getChildNodes(),hnode);
|
||||
convertMathMLNodeList(onode.getChildNodes(),hnode);
|
||||
}
|
||||
else if (onode.getNodeName().equals(XMLString.ANNOTATION)) { // Since OOo 3.2
|
||||
// ignore the annotation (StarMath) completely
|
||||
|
@ -202,20 +220,18 @@ public class MathConverter extends ConverterHelper {
|
|||
}
|
||||
else {
|
||||
String sElementName = stripNamespace(onode.getNodeName());
|
||||
Element newNode = hnode.getOwnerDocument().createElement(sElementName);
|
||||
Element newNode = converter.createElement(sElementName);
|
||||
hnode.appendChild(newNode);
|
||||
if (onode.hasAttributes()) {
|
||||
NamedNodeMap attr = onode.getAttributes();
|
||||
int nLen = attr.getLength();
|
||||
for (int i=0; i<nLen; i++) {
|
||||
String sName = attr.item(i).getNodeName();
|
||||
if (sName.equals("xmlns:math")) { sName="xmlns"; }
|
||||
else { sName = stripNamespace(sName); }
|
||||
String sName = stripNamespace(attr.item(i).getNodeName());
|
||||
String sValue = attr.item(i).getNodeValue();
|
||||
newNode.setAttribute(sName,replacePrivateChars(sValue));
|
||||
}
|
||||
}
|
||||
convertNodeList(onode.getChildNodes(),newNode);
|
||||
convertMathMLNodeList(onode.getChildNodes(),newNode);
|
||||
}
|
||||
}
|
||||
else if (onode.getNodeType()==Node.TEXT_NODE) {
|
||||
|
@ -224,11 +240,11 @@ public class MathConverter extends ConverterHelper {
|
|||
}
|
||||
}
|
||||
|
||||
private void convertNodeList(NodeList list, Node hnode) {
|
||||
private void convertMathMLNodeList(NodeList list, Node hnode) {
|
||||
if (list==null) { return; }
|
||||
int nLen = list.getLength();
|
||||
for (int i=0; i<nLen; i++) {
|
||||
convertAsMathML(list.item(i),hnode);
|
||||
convertElementAsMathML(list.item(i),hnode);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue