/************************************************************************
*
* SectionConverter.java
*
* Copyright: 2002-2014 by Henrik Just
*
* This file is part of Writer2LaTeX.
*
* Writer2LaTeX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Writer2LaTeX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Writer2LaTeX. If not, see
This class creates LaTeX code from OOo sections. *
Sections are converted to multicols environments using multicol.sty
*/
public class SectionConverter extends ConverterHelper {
// Do we need multicols.sty?
private boolean bNeedMulticol = false;
// Display hidden text?
private boolean bDisplayHiddenText = false;
// Filenames for external sections
private ExportNameCollection fileNames = new ExportNameCollection(true);
/**
Constructs a new SectionStyleConverter
.
Process a section (text:section tag)
* @param node The element containing the section * @param ldp theLaTeXDocumentPortion
to which
* LaTeX code should be added
* @param oc the current context
*/
public void handleSection(Element node, LaTeXDocumentPortion ldp, Context oc) {
// Unlike headings, paragraphs and spans, text:display is not attached to the style:
if (!bDisplayHiddenText && "none".equals(Misc.getAttribute(node,XMLString.TEXT_DISPLAY))) {
return;
}
// We may need a hyperlink target, add this first
palette.getFieldCv().addTarget(node,"|region",ldp);
// Create new document, if desired
String sFileName = null;
Element source = Misc.getChildByTagName(node,XMLString.TEXT_SECTION_SOURCE);
if (config.splitLinkedSections() && source!=null) {
sFileName = fileNames.getExportName(Misc.removeExtension(Misc.urlDecode(source.getAttribute(XMLString.XLINK_HREF))));
}
else if (config.splitToplevelSections() && isToplevel(node)) {
//sFileName = fileNames.getExportName(palette.getOutFileName()+node.getAttribute(XMLString.TEXT_NAME));
sFileName = fileNames.getExportName(node.getAttribute(XMLString.TEXT_NAME));
}
LaTeXDocumentPortion sectionLdp = ldp;
if (sFileName!=null) {
LaTeXDocument newDoc = new LaTeXDocument(sFileName,config.getWrapLinesAfter(),false);
if (config.getBackend()!=LaTeXConfig.XETEX) {
newDoc.setEncoding(ClassicI18n.writeJavaEncoding(config.getInputencoding()));
}
else {
newDoc.setEncoding("UTF-8");
}
palette.addDocument(newDoc);
sectionLdp = newDoc.getContents();
}
// Apply the style
String sStyleName = node.getAttribute(XMLString.TEXT_STYLE_NAME);
BeforeAfter ba = new BeforeAfter();
Context ic = (Context) oc.clone();
applySectionStyle(sStyleName,ba,ic);
// Do conversion
ldp.append(ba.getBefore());
if (sFileName!=null) {
ldp.append("\\input{").append(sFileName).append("}").nl();
}
// Zotero or JabRef might have generated this section as a bibliograhy:
if (!handleZoteroBibliography(node,sectionLdp,ic) && !handleJabRefBibliography(node,sectionLdp,ic)) {
palette.getBlockCv().traverseBlockText(node,sectionLdp,ic);
}
if (sectionLdp!=ldp) { sectionLdp.append("\\endinput").nl(); }
ldp.append(ba.getAfter());
}
// Create multicols environment as needed
private void applySectionStyle(String sStyleName, BeforeAfter ba, Context context) {
StyleWithProperties style = ofr.getSectionStyle(sStyleName);
// Don't nest multicols and require at least 2 columns
if (context.isInMulticols() || style==null || style.getColCount()<2) { return; }
int nCols = style.getColCount();
bNeedMulticol = true;
context.setInMulticols(true);
ba.add("\\begin{multicols}{"+(nCols>10 ? 10 : nCols)+"}\n", "\\end{multicols}\n");
}
// return true if this node is *not* contained in a text:section element
private boolean isToplevel(Node node) {
Node parent = node.getParentNode();
if (XMLString.TEXT_SECTION.equals(parent.getNodeName())) {
return false;
}
else if (XMLString.OFFICE_BODY.equals(parent.getNodeName())) {
return true;
}
return isToplevel(parent);
}
}