w2x: Recognize display equations + add MathJax support

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@148 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2012-04-08 09:42:52 +00:00
parent ac7f03f44f
commit e330b3dfab
15 changed files with 254 additions and 40 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2012-04-03)
* Version 1.4 (2012-04-07)
*
*/
@ -349,6 +349,23 @@ public class Converter extends ConverterBase {
}
}
// Load MathJax
// TODO: Should we support different configurations of MathJax?
if (nType==XhtmlDocument.HTML5 && config.useMathJax()) {
for (int i=0; i<=nOutFileIndex; i++) {
if (outFiles.get(i).hasMath()) {
XhtmlDocument doc = outFiles.get(i);
Element head = doc.getHeadNode();
if (head!=null) {
Element script = doc.getContentDOM().createElement("script");
head.appendChild(script);
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=MML_HTMLorMML");
}
}
}
}
// Create headers & footers (if nodes are available)
if (ofr.isSpreadsheet()) {
for (int i=0; i<=nOutFileIndex; i++) {
@ -486,6 +503,7 @@ public class Converter extends ConverterBase {
converterResult.addDocument(cssDoc);
}
}
}
private void addNavigationLink(Document dom, Node node, String s, int nIndex) {
@ -600,7 +618,7 @@ public class Converter extends ConverterBase {
public boolean outFileHasContent() {
return htmlDoc.getContentNode().hasChildNodes();
}
// Use another document. TODO: This is very ugly; clean it up!!!
public void changeOutFile(int nIndex) {
nOutFileIndex = nIndex;

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2012-04-03)
* Version 1.4 (2012-04-07)
*
*/
@ -399,7 +399,7 @@ public class DrawConverter extends ConverterHelper {
}
}
else { // flat xml format
Node formula = Misc.getChildByTagName(onode,XMLString.MATH); // Since OOo 3.2
Element formula = Misc.getChildByTagName(onode,XMLString.MATH); // Since OOo 3.2
if (formula==null) {
formula = Misc.getChildByTagName(onode,XMLString.MATH_MATH);
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2010 by Henrik Just
* Copyright: 2002-2012 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2010-03-15)
* Version 1.4 (2012-04-07)
*
*/
@ -70,8 +70,11 @@ 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, Node onode, Node hnode) {
public void convert(Node image, Element onode, Node hnode) {
if (bSupportMathML) {
if (converter.getTextCv().isDisplayEquation()) {
onode.setAttribute("display", "block");
}
convertAsMathML(onode,hnode);
}
else {

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.2 (2012-03-07)
* Version 1.4 (2012-04-07)
*
*/
@ -140,6 +140,9 @@ public class TextConverter extends ConverterHelper {
// (labels for footnotes and endnotes)
// We put it here and insert it in the first paragraph/heading to come:
private Node asapNode = null;
// Are we within a display equation?
private boolean bDisplayEquation = false;
// When generating toc, a few things should be done differently
private boolean bInToc = false;
@ -357,6 +360,10 @@ public class TextConverter extends ConverterHelper {
p.appendChild(inline);
return inline;
}
public boolean isDisplayEquation() {
return bDisplayEquation;
}
////////////////////////////////////////////////////////////////////////
// BLOCK TEXT (returns current html node at end of block)
@ -813,7 +820,9 @@ public class TextConverter extends ConverterHelper {
insertListLabel(currentListStyle, nCurrentListLevel, "ItemNumber", null, sCurrentListLabel, par);
}
sCurrentListLabel = null;
bDisplayEquation=converter.parseDisplayEquation(onode);
traverseInlineText(onode,par);
bDisplayEquation=false;
}
else {
// An empty paragraph (this includes paragraphs that only contains

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2012-04-03)
* Version 1.4 (2012-04-07)
*
*/
@ -41,7 +41,7 @@ import writer2latex.util.Misc;
public class XhtmlConfig extends writer2latex.base.ConfigBase {
// Implement configuration methods
protected int getOptionCount() { return 57; }
protected int getOptionCount() { return 58; }
protected String getDefaultConfigPath() { return "/writer2latex/xhtml/config/"; }
// Override setOption: To be backwards compatible, we must accept options
@ -138,19 +138,20 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
private static final int IMAGE_SPLIT = 41;
private static final int COVER_IMAGE = 42;
private static final int USE_SVG = 43;
private static final int CALC_SPLIT = 44;
private static final int DISPLAY_HIDDEN_SHEETS = 45;
private static final int DISPLAY_HIDDEN_ROWS_COLS = 46;
private static final int DISPLAY_FILTERED_ROWS_COLS = 47;
private static final int APPLY_PRINT_RANGES = 48;
private static final int USE_TITLE_AS_HEADING = 49;
private static final int USE_SHEET_NAMES_AS_HEADINGS = 50;
private static final int XSLT_PATH = 51;
private static final int SAVE_IMAGES_IN_SUBDIR = 52;
private static final int UPLINK = 53;
private static final int DIRECTORY_ICON = 54;
private static final int DOCUMENT_ICON = 55;
private static final int ZEN_HACK = 56; // temporary hack for ePub Zen Garden styles
private static final int USE_MATHJAX = 44;
private static final int CALC_SPLIT = 45;
private static final int DISPLAY_HIDDEN_SHEETS = 46;
private static final int DISPLAY_HIDDEN_ROWS_COLS = 47;
private static final int DISPLAY_FILTERED_ROWS_COLS = 48;
private static final int APPLY_PRINT_RANGES = 49;
private static final int USE_TITLE_AS_HEADING = 50;
private static final int USE_SHEET_NAMES_AS_HEADINGS = 51;
private static final int XSLT_PATH = 52;
private static final int SAVE_IMAGES_IN_SUBDIR = 53;
private static final int UPLINK = 54;
private static final int DIRECTORY_ICON = 55;
private static final int DOCUMENT_ICON = 56;
private static final int ZEN_HACK = 57; // temporary hack for ePub Zen Garden styles
protected ComplexOption xheading = addComplexOption("heading-map");
protected ComplexOption xpar = addComplexOption("paragraph-map");
@ -262,6 +263,7 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
options[IMAGE_SPLIT] = new Option("image_split","none");
options[COVER_IMAGE] = new BooleanOption("cover_image","false");
options[USE_SVG] = new BooleanOption("use_svg","false");
options[USE_MATHJAX] = new BooleanOption("use_mathjax","false");
options[CALC_SPLIT] = new BooleanOption("calc_split","false");
options[DISPLAY_HIDDEN_SHEETS] = new BooleanOption("display_hidden_sheets", "false");
options[DISPLAY_HIDDEN_ROWS_COLS] = new BooleanOption("display_hidden_rows_cols","false");
@ -389,6 +391,7 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
public String imageSplit() { return options[IMAGE_SPLIT].getString(); }
public boolean coverImage() { return ((BooleanOption) options[COVER_IMAGE]).getValue(); }
public boolean useSVG() { return ((BooleanOption) options[USE_SVG]).getValue(); }
public boolean useMathJax() { return ((BooleanOption) options[USE_MATHJAX]).getValue(); }
public boolean xhtmlCalcSplit() { return ((BooleanOption) options[CALC_SPLIT]).getValue(); }
public boolean xhtmlDisplayHiddenSheets() { return ((BooleanOption) options[DISPLAY_HIDDEN_SHEETS]).getValue(); }
public boolean displayHiddenRowsCols() { return ((BooleanOption) options[DISPLAY_HIDDEN_ROWS_COLS]).getValue(); }

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2012-04-01)
* Version 1.4 (2012-04-07)
*
*/
@ -44,6 +44,7 @@ import javax.xml.parsers.DocumentBuilder;
//import javax.xml.parsers.ParserConfigurationException;
import writer2latex.api.MIMETypes;
import writer2latex.office.XMLString;
import writer2latex.xmerge.DOMDocument;
import java.io.InputStream;
@ -351,6 +352,31 @@ public class XhtmlDocument extends DOMDocument {
super.setContentDOM(doc);
collectNodes();
}
/** Does this document contain any math nodes?
*
* @return true if so
*/
public boolean hasMath() {
return hasMath(getContentDOM().getDocumentElement());
}
private boolean hasMath(Element node) {
// Check this element
if (node.getTagName().equals(XMLString.MATH)) {
return true;
}
// Check children
Node child = node.getFirstChild();
while (child!=null) {
if (child.getNodeType()==Node.ELEMENT_NODE && hasMath((Element)child)) {
return true;
}
child = child.getNextSibling();
}
// Found nothing
return false;
}
public void read(InputStream is) throws IOException {
super.read(is);