w2l use thebibliography environment if not using BibTeX

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@257 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2015-06-22 10:59:48 +00:00
parent 6f46ed3177
commit 729e088fa2
10 changed files with 318 additions and 257 deletions

View file

@ -16,37 +16,33 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2011 by Henrik Just
* Copyright: 2002-2015 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2011-01-27)
* Version 1.6 (2015-06-22)
*
*/
package writer2latex.latex;
import java.util.Collection;
import org.w3c.dom.Element;
import writer2latex.base.BibliographyGenerator;
import writer2latex.bibtex.BibTeXDocument;
import writer2latex.latex.util.BeforeAfter;
import writer2latex.latex.util.Context;
import writer2latex.office.BibMark;
import writer2latex.office.OfficeReader;
import writer2latex.office.StyleWithProperties;
import writer2latex.office.XMLString;
import writer2latex.util.Misc;
/**
* This class handles the bibliography. The result depends on these
* configuration options. The citations will be treated like this:
* <ul>
* <li><code>use_bibtex</code>: If true, citations will be exported as \cite
* commands. If false, citations will be exported as static text</li>
* </ul>
* The bibliography will be treated like this:
* <ul>
/** This class handles bibliographic citations and the bibliography. The result depends on these
* configuration options:
* <li><code>use_index</code>: If false, the bibliography will be omitted</li>
* <li><code>use_bibtex</code> true and <code>external_bibtex_files</code>
* empty: The citations will be exported to a BibTeX file, which will be used
@ -55,111 +51,187 @@ import writer2latex.util.Misc;
* non-empty: The citations will be not be exported to a BibTeX file, the
* files referred to by the option will be used instead</li>
* <li><code>use_bibtex</code> false: The bibliography will be exported as
* static text.
* a thebibliography environment
* <li><code>bibtex_style</code> If BibTeX is used, this style will be applied
* </ul>
* The citations will always be exported as \cite commands.
*/
public class BibConverter extends ConverterHelper {
private BibTeXDocument bibDoc;
class BibConverter extends ConverterHelper {
private BibTeXDocument bibDoc = null;
/** Construct a new BibConverter.
*
* @param config the configuration to use
* @param palette the ConverterPalette to use
*/
public BibConverter(OfficeReader ofr,LaTeXConfig config, ConverterPalette palette) {
BibConverter(OfficeReader ofr,LaTeXConfig config, ConverterPalette palette) {
super(ofr,config,palette);
}
/** Append declarations needed by the <code>BibConverter</code> to
* the preamble.
* @param pack the LaTeXDocumentPortion to which
* declarations of packages should be added (\\usepackage).
* @param decl the LaTeXDocumentPortion to which
* other declarations should be added.
*/
public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
// Use natbib
if (config.useBibtex() && config.useNatbib()) {
pack.append("\\usepackage");
if (config.getNatbibOptions().length()>0) {
pack.append("[").append(config.getNatbibOptions()).append("]");
}
pack.append("{natbib}").nl();
// We need to create a BibTeX document except if we are using external BibTeX files
if (!(config.useBibtex() && config.externalBibtexFiles().length()>0)) {
bibDoc = new BibTeXDocument(palette.getOutFileName(),false,ofr);
}
}
/** Process a bibliography (text:bibliography tag)
* @param node The element containing the Bibliography
/** Export the bibliography directly as a thebibliography environment (as an alternative to using BibTeX)
*/
private class ThebibliographyGenerator extends BibliographyGenerator {
// The bibliography is the be inserted in this LaTeX document portion with that context
private LaTeXDocumentPortion ldp;
private Context context;
// The current bibliography item is to formatted with this before/after pair with that context
private BeforeAfter itemBa = null;
private Context itemContext = null;
ThebibliographyGenerator(OfficeReader ofr) {
super(ofr,true);
}
void handleBibliography(Element bibliography, LaTeXDocumentPortion ldp, Context context) {
this.ldp = ldp;
this.context = context;
String sWidestLabel = "";
Collection<String> labels = getLabels();
for (String sLabel : labels) {
if (sLabel.length()>=sWidestLabel.length()) {
sWidestLabel = sLabel;
}
}
ldp.append("\\begin{thebibliography}{").append(sWidestLabel).append("}\n");
generateBibliography(bibliography);
endBibliographyItem();
ldp.append("\\end{thebibliography}\n");
}
@Override protected void insertBibliographyItem(String sStyleName, String sKey) {
endBibliographyItem();
itemBa = new BeforeAfter();
itemContext = (Context) context.clone();
// Apply paragraph style (character formatting only)
StyleWithProperties style = ofr.getParStyle(sStyleName);
if (style!=null) {
palette.getI18n().applyLanguage(style,true,true,itemBa);
palette.getCharSc().applyFont(style,true,true,itemBa,itemContext);
if (itemBa.getBefore().length()>0) {
itemBa.add(" ","");
itemBa.enclose("{", "}");
}
}
// Convert item
ldp.append(itemBa.getBefore());
palette.getI18n().pushSpecialTable(palette.getCharSc().getFontName(style));
ldp.append("\\bibitem");
if (!isNumberedEntries()) {
ldp.append("[").append(bibDoc.getExportName(sKey)).append("]");
}
ldp.append("{").append(bibDoc.getExportName(sKey)).append("} ");
}
private void endBibliographyItem() {
if (itemBa!=null) {
palette.getI18n().popSpecialTable();
ldp.append(itemBa.getAfter()).append("\n");
itemBa = null;
}
}
@Override protected void insertBibliographyItemElement(String sStyleName, String sText) {
BeforeAfter ba = new BeforeAfter();
Context elementContext = (Context) itemContext.clone();
// Apply character style
StyleWithProperties style = ofr.getTextStyle(sStyleName);
palette.getCharSc().applyTextStyle(sStyleName,ba,elementContext);
// Convert text
ldp.append(ba.getBefore());
palette.getI18n().pushSpecialTable(palette.getCharSc().getFontName(style));
ldp.append(palette.getI18n().convert(sText, false, elementContext.getLang()));
palette.getI18n().popSpecialTable();
ldp.append(ba.getAfter());
}
}
/** Append declarations needed by the <code>BibConverter</code> to the preamble.
*
* @param pack the LaTeXDocumentPortion to which declarations of packages (\\usepackage) should be added.
* @param decl the LaTeXDocumentPortion to which other declarations should be added.
*/
void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
// Currently nothing
}
/** Process a bibliography
*
* @param node A text:bibliography element
* @param ldp the LaTeXDocumentPortion to which LaTeX code should be added
* @param oc the current context
*/
public void handleBibliography (Element node, LaTeXDocumentPortion ldp, Context oc) {
if (config.noIndex()) { return; }
if (config.useBibtex()) {
// Use the style given in the configuration
// TODO: Create a bst file from the settings of the text:bibliography
ldp.append("\\bibliographystyle{")
.append(config.bibtexStyle())
.append("}").nl();
// Use BibTeX file from configuration, or exported BibTeX file
if (config.externalBibtexFiles().length()>0) {
ldp.append("\\bibliography{")
.append(config.externalBibtexFiles())
.append("}").nl();
}
else {
if (bibDoc==null) { bibDoc = new BibTeXDocument(palette.getOutFileName(),false); }
ldp.append("\\bibliography{")
.append(bibDoc.getName())
.append("}").nl();
}
}
else { // typeset current content
Element body = Misc.getChildByTagName(node,XMLString.TEXT_INDEX_BODY);
if (body!=null) {
Element title = Misc.getChildByTagName(body,XMLString.TEXT_INDEX_TITLE);
if (title!=null) { palette.getBlockCv().traverseBlockText(title,ldp,oc); }
palette.getBlockCv().traverseBlockText(body,ldp,oc);
}
}
}
/** Process a Bibliography Mark (text:bibliography-mark tag)
* @param node The element containing the Mark
* @param ldp the LaTeXDocumentPortion to which LaTeX code should be added
* @param oc the current context
*/
public void handleBibliographyMark(Element node, LaTeXDocumentPortion ldp, Context oc) {
if (config.useBibtex()) {
String sIdentifier = node.getAttribute(XMLString.TEXT_IDENTIFIER);
if (sIdentifier!=null) {
if (config.externalBibtexFiles().length()==0) {
if (bibDoc==null) { bibDoc = new BibTeXDocument(palette.getOutFileName(),false); }
if (!bibDoc.containsKey(sIdentifier)) {
bibDoc.put(new BibMark(node));
}
}
// Insert citation: Original if using external files; stripped if exporting BibTeX
ldp.append("\\cite{")
.append(config.externalBibtexFiles().length()==0 ? bibDoc.getExportName(sIdentifier) : sIdentifier)
.append("}");
}
}
else { // use current value
palette.getInlineCv().traverseInlineText(node,ldp,oc);
void handleBibliography(Element node, LaTeXDocumentPortion ldp, Context oc) {
if (!config.noIndex()) {
if (config.useBibtex()) { // Export using BibTeX
handleBibliographyAsBibTeX(ldp);
}
else { // Export as thebibliography environment
ThebibliographyGenerator bibCv = new ThebibliographyGenerator(ofr);
Element source = Misc.getChildByTagName(node,XMLString.TEXT_BIBLIOGRAPHY_SOURCE);
bibCv.handleBibliography(source, ldp, oc);
}
}
}
/** Get the BibTeX document, if any (the document is only created if it's
* specified in the configuration *and* the document contains bibliographic
* data *and* the configuration does not specify external BibTeX files
* @return the BiBTeXDocument, or null if it does not exist).
*/
public BibTeXDocument getBibTeXDocument () {
return bibDoc;
}
private void handleBibliographyAsBibTeX(LaTeXDocumentPortion ldp) {
// Use the style given in the configuration
ldp.append("\\bibliographystyle{")
.append(config.bibtexStyle())
.append("}").nl();
// Use BibTeX file from configuration, or exported BibTeX file
if (config.externalBibtexFiles().length()>0) {
ldp.append("\\bibliography{")
.append(config.externalBibtexFiles())
.append("}").nl();
}
else {
ldp.append("\\bibliography{")
.append(bibDoc.getName())
.append("}").nl();
}
}
/** Process a Bibliography Mark
* @param node a text:bibliography-mark element
* @param ldp the LaTeXDocumentPortion to which LaTeX code should be added
* @param oc the current context
*/
void handleBibliographyMark(Element node, LaTeXDocumentPortion ldp, Context oc) {
String sIdentifier = node.getAttribute(XMLString.TEXT_IDENTIFIER);
if (sIdentifier!=null) {
// Use original citation if using external files; stripped if exporting BibTeX
ldp.append("\\cite{")
.append(config.externalBibtexFiles().length()==0 ? bibDoc.getExportName(sIdentifier) : sIdentifier)
.append("}");
}
}
/** Get the BibTeX document, if any (that is if the document contains bibliographic data <em>and</em>
* the configuration does not specify external BibTeX files)
*
* @return the BiBTeXDocument, or null if no BibTeX file is needed
*/
BibTeXDocument getBibTeXDocument() {
if (bibDoc!=null && !bibDoc.isEmpty()) {
return bibDoc;
}
return null;
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-04-15)
* Version 1.6 (2015-06-20)
*
*/
@ -31,18 +31,18 @@ import writer2latex.office.OfficeReader;
/**
* <p>This is an abstract superclass for converter helpers.</p>
*/
public abstract class ConverterHelper {
abstract class ConverterHelper {
protected OfficeReader ofr;
protected LaTeXConfig config;
protected ConverterPalette palette;
OfficeReader ofr;
LaTeXConfig config;
ConverterPalette palette;
protected ConverterHelper(OfficeReader ofr, LaTeXConfig config, ConverterPalette palette) {
ConverterHelper(OfficeReader ofr, LaTeXConfig config, ConverterPalette palette) {
this.ofr = ofr;
this.config = config;
this.palette = palette;
}
public abstract void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl);
abstract void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl);
}

View file

@ -37,7 +37,6 @@ import writer2latex.latex.i18n.ClassicI18n;
import writer2latex.latex.i18n.I18n;
import writer2latex.latex.i18n.XeTeXI18n;
import writer2latex.latex.util.Context;
import writer2latex.latex.util.Info;
import writer2latex.util.CSVList;
import writer2latex.util.ExportNameCollection;
import writer2latex.util.Misc;

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-04-15)
* Version 1.6 (2015-06-22)
*
*/
@ -104,6 +104,14 @@ public class FieldConverter extends ConverterHelper {
* other declarations should be added.
*/
public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
// Use natbib
if (config.useBibtex() && config.useNatbib()) {
pack.append("\\usepackage");
if (config.getNatbibOptions().length()>0) {
pack.append("[").append(config.getNatbibOptions()).append("]");
}
pack.append("{natbib}").nl();
}
// use lastpage.sty
if (bUsesPageCount) {
pack.append("\\usepackage{lastpage}").nl();

View file

@ -20,38 +20,33 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-04-15)
* Version 1.6 (2015-06-21)
*
*/
package writer2latex.latex.util;
package writer2latex.latex;
import org.w3c.dom.Element;
import writer2latex.latex.LaTeXConfig;
import writer2latex.util.Misc;
import writer2latex.office.OfficeReader;
import writer2latex.office.StyleWithProperties;
import writer2latex.office.XMLString;
import writer2latex.latex.LaTeXDocumentPortion;
import writer2latex.latex.ConverterHelper;
import writer2latex.latex.ConverterPalette;
/**
* <p>This class creates various information to the user about the conversion.</p>
/** This class creates various information to the user about the conversion.
*/
public class Info extends ConverterHelper {
class Info extends ConverterHelper {
@Override public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
// Currently nothing
}
public Info(OfficeReader ofr, LaTeXConfig config, ConverterPalette palette) {
Info(OfficeReader ofr, LaTeXConfig config, ConverterPalette palette) {
super(ofr,config,palette);
}
public void addDebugInfo(Element node, LaTeXDocumentPortion ldp) {
void addDebugInfo(Element node, LaTeXDocumentPortion ldp) {
if (config.debug()) {
ldp.append("% ").append(node.getNodeName());
addDebugInfo(node,ldp,XMLString.TEXT_ID);
@ -69,7 +64,7 @@ public class Info extends ConverterHelper {
}
}
private void addDebugInfo(Element node, LaTeXDocumentPortion ldp, String sAttribute) {
void addDebugInfo(Element node, LaTeXDocumentPortion ldp, String sAttribute) {
String sValue = Misc.getAttribute(node,sAttribute);
if (sValue!=null) {
ldp.append(" ").append(sAttribute).append("=\"").append(sValue).append("\"");