Zotero support

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@72 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2010-10-06 08:58:31 +00:00
parent f6ce71c6a5
commit 4949e6a995
32 changed files with 6492 additions and 68 deletions

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2008 by Henrik Just
* Copyright: 2002-2010 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-11-23)
* Version 1.2 (2010-10-06)
*
*/
@ -30,7 +30,11 @@ package writer2latex.latex;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -51,7 +55,10 @@ import writer2latex.util.SimpleInputBuffer;
*/
public class FieldConverter extends ConverterHelper {
// Identify Zotero items
private static final String ZOTERO_ITEM = "ZOTERO_ITEM";
// Links & references
private ExportNameCollection targets = new ExportNameCollection(true);
private ExportNameCollection refnames = new ExportNameCollection(true);
@ -71,11 +78,14 @@ public class FieldConverter extends ConverterHelper {
private boolean bUsesPageCount = false;
private boolean bUsesTitleref = false;
private boolean bUsesOooref = false;
private boolean bConvertZotero = false;
private boolean bNeedNatbib = false;
public FieldConverter(OfficeReader ofr, LaTeXConfig config, ConverterPalette palette) {
super(ofr,config,palette);
// hyperref.sty is not compatible with titleref.sty and oooref.sty:
bUseHyperref = config.useHyperref() && !config.useTitleref() && !config.useOooref();
bConvertZotero = config.useBibtex() && config.zoteroBibtexFiles().length()>0;
}
/** <p>Append declarations needed by the <code>FieldConverter</code> to
@ -122,7 +132,12 @@ public class FieldConverter extends ConverterHelper {
}
}
pack.append("}").nl();
}
}
// Use natbib
if (bNeedNatbib) {
pack.append("\\usepackage{natbib}").nl();
}
// Export sequence declarations
// The number format is fetched from the first occurence of the
@ -422,10 +437,197 @@ public class FieldConverter extends ConverterHelper {
palette.getInlineCv().traversePCDATA(node,ldp,oc);
}
}
}
}
// Try to handle this reference name as a Zotero reference, return true on success
private boolean handleZoteroReferenceName(String sName, LaTeXDocumentPortion ldp, Context oc) {
// First parse the reference name:
// A Zotero reference name has the form ZOTERO_ITEM <json object> <identifier> with a single space separating the items
// The identifier is a unique identifier for the reference and is not used here
if (sName.startsWith(ZOTERO_ITEM)) {
int nObjectStart = sName.indexOf('{');
int nObjectEnd = sName.lastIndexOf('}');
if (nObjectStart>-1 && nObjectEnd>-1 && nObjectStart<nObjectEnd) {
String sJsonObject = sName.substring(nObjectStart, nObjectEnd+1);
JSONObject jo = null;
try {
jo = new JSONObject(sJsonObject);
} catch (JSONException e) {
return false;
}
// Successfully parsed the reference, now generate the code
// (we don't expect any errors and ignore them, if they happen anyway)
// Sort key (purpose? currently ignored)
boolean bSort = true;
try {
bSort = jo.getBoolean("sort");
}
catch (JSONException e) {
}
/** <p>Process a reference mark (text:reference-mark tag)</p>
JSONArray citationItemsArray = null;
try { // The value is an array of objects, one for each source in this citation
citationItemsArray = jo.getJSONArray("citationItems");
}
catch (JSONException e) {
}
if (citationItemsArray!=null) {
int nCitationCount = citationItemsArray.length();
if (nCitationCount>1) {
// For multiple citations, use \citetext, otherwise we cannot add individual prefixes and suffixes
// TODO: If no prefixes or suffixes exist, it's safe to combine the citations
ldp.append("\\citetext{");
}
for (int nIndex=0; nIndex<nCitationCount; nIndex++) {
JSONObject citationItems = null;
try { // Each citation is represented as an object
citationItems = citationItemsArray.getJSONObject(nIndex);
}
catch (JSONException e) {
}
if (citationItems!=null) {
if (nIndex>0) {
ldp.append("; "); // Separate multiple citations in this reference
}
// Citation items
String sURI = "";
boolean bSuppressAuthor = false;
String sPrefix = "";
String sSuffix = "";
String sLocator = "";
String sLocatorType = "";
try { // The URI seems to be an array with a single string value(?)
sURI = citationItems.getJSONArray("uri").getString(0);
}
catch (JSONException e) {
}
try { // SuppressAuthor is a boolean value
bSuppressAuthor = citationItems.getBoolean("suppressAuthor");
}
catch (JSONException e) {
}
try { // Prefix is a string value
sPrefix = citationItems.getString("prefix");
}
catch (JSONException e) {
}
try { // Suffix is a string value
sSuffix = citationItems.getString("suffix");
}
catch (JSONException e) {
}
try { // Locator is a string value, e.g. a page number
sLocator = citationItems.getString("locator");
}
catch (JSONException e) {
}
try {
// LocatorType is a string value, e.g. book, verse, page (missing locatorType means page)
sLocatorType = citationItems.getString("locatorType");
}
catch (JSONException e) {
}
// Adjust locator type (empty locator type means "page")
// TODO: Handle other locator types (localize and abbreviate): Currently the internal name (e.g. book) is used.
if (sLocator.length()>0 && sLocatorType.length()==0) {
// A locator of the form <number><other characters><number> is interpreted as several pages
if (Pattern.compile("[0-9]+[^0-9]+[0-9]+").matcher(sLocator).find()) {
sLocatorType = "pp.";
}
else {
sLocatorType = "p.";
}
}
// Insert command. TODO: Evaluate this
if (nCitationCount>1) { // Use commands without parentheses
if (bSuppressAuthor) { ldp.append("\\citeyear"); }
else { ldp.append("\\citet"); }
}
else {
if (bSuppressAuthor) { ldp.append("\\citeyearpar"); }
else { ldp.append("\\citep"); }
}
if (sPrefix.length()>0) {
ldp.append("[").append(palette.getI18n().convert(sPrefix,true,oc.getLang())).append("]");
}
if (sPrefix.length()>0 || sSuffix.length()>0 || sLocatorType.length()>0 || sLocator.length()>0) {
// Note that we need to include an empty suffix if there's a prefix!
ldp.append("[")
.append(palette.getI18n().convert(sSuffix,true,oc.getLang()))
.append(palette.getI18n().convert(sLocatorType,true,oc.getLang()));
if (sLocatorType.length()>0 && sLocator.length()>0) {
ldp.append("~");
}
ldp.append(palette.getI18n().convert(sLocator,true,oc.getLang()))
.append("]");
}
ldp.append("{");
int nSlash = sURI.lastIndexOf('/');
if (nSlash>0) {
ldp.append(sURI.substring(nSlash+1));
}
else {
ldp.append(sURI);
}
ldp.append("}");
}
}
if (nCitationCount>1) { // End the \citetext command
ldp.append("}");
}
oc.setInZoteroText(true);
bNeedNatbib = true;
return true;
}
}
}
return false;
}
private String shortenRefname(String s) {
// For Zotero items, use the trailing unique identifier
if (s.startsWith(ZOTERO_ITEM)) {
int nLast = s.lastIndexOf(' ');
if (nLast>0) {
return s.substring(nLast+1);
}
}
return s;
}
/** <p>Process a reference mark end (text:reference-mark-end tag)</p>
* @param node The element containing the reference mark
* @param ldp the <code>LaTeXDocumentPortion</code> to which
* LaTeX code should be added
* @param oc the current context
*/
public void handleReferenceMarkEnd(Element node, LaTeXDocumentPortion ldp, Context oc) {
// Nothing to do, except to mark that this ends any Zotero citation
oc.setInZoteroText(false);
}
/** <p>Process a reference mark (text:reference-mark or text:reference-mark-start tag)</p>
* @param node The element containing the reference mark
* @param ldp the <code>LaTeXDocumentPortion</code> to which
* LaTeX code should be added
@ -433,10 +635,12 @@ public class FieldConverter extends ConverterHelper {
*/
public void handleReferenceMark(Element node, LaTeXDocumentPortion ldp, Context oc) {
if (!oc.isInSection() && !oc.isInCaption() && !oc.isVerbatim()) {
// Note: Always include \label here, even when it's not used
String sName = node.getAttribute(XMLString.TEXT_NAME);
if (sName!=null) {
ldp.append("\\label{ref:"+refnames.getExportName(sName)+"}");
// Zotero (mis)uses reference marks to store citations, so check this first
if (sName!=null && (!bConvertZotero || !handleZoteroReferenceName(sName, ldp, oc))) {
// Plain reference mark
// Note: Always include \label here, even when it's not used
ldp.append("\\label{ref:"+refnames.getExportName(shortenRefname(sName))+"}");
}
}
else {
@ -455,11 +659,11 @@ public class FieldConverter extends ConverterHelper {
String sFormat = node.getAttribute(XMLString.TEXT_REFERENCE_FORMAT);
String sName = node.getAttribute(XMLString.TEXT_REF_NAME);
if (("page".equals(sFormat) || "".equals(sFormat)) && sName!=null) {
ldp.append("\\pageref{ref:"+refnames.getExportName(sName)+"}");
ldp.append("\\pageref{ref:"+refnames.getExportName(shortenRefname(sName))+"}");
}
else if ("chapter".equals(sFormat) && ofr.referenceMarkInHeading(sName)) {
// This is safe if the reference mark is contained in a heading
ldp.append("\\ref{ref:"+refnames.getExportName(sName)+"}");
ldp.append("\\ref{ref:"+refnames.getExportName(shortenRefname(sName))+"}");
}
else { // use current value
palette.getInlineCv().traversePCDATA(node,ldp,oc);

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2008 by Henrik Just
* Copyright: 2002-2010 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-11-22)
* Version 1.2 (2010-10-04)
*
*/
@ -195,7 +195,13 @@ public class InlineConverter extends ConverterHelper {
case Node.TEXT_NODE:
String s = childNode.getNodeValue();
if (s.length() > 0) {
if (oc.isInZoteroText()) { // Comment out Zotero citations
ldp.append("%");
}
ldp.append(palette.getI18n().convert(s, false, oc.getLang()));
if (oc.isInZoteroText()) { // End comment out
ldp.nl();
}
}
break;
@ -319,6 +325,9 @@ public class InlineConverter extends ConverterHelper {
else if (sName.equals(XMLString.TEXT_REFERENCE_MARK_START)) {
palette.getFieldCv().handleReferenceMark(child,ldp,oc);
}
else if (sName.equals(XMLString.TEXT_REFERENCE_MARK_END)) {
palette.getFieldCv().handleReferenceMarkEnd(child,ldp,oc);
}
else if (sName.equals(XMLString.TEXT_REFERENCE_REF)) {
palette.getFieldCv().handleReferenceRef(child,ldp,oc);
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2009 by Henrik Just
* Copyright: 2002-2010 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2009-11-02)
* Version 1.2 (2010-10-04)
*
*/
@ -48,7 +48,7 @@ public class LaTeXConfig extends writer2latex.base.ConfigBase {
/////////////////////////////////////////////////////////////////////////
// I. Define items needed by ConfigBase
protected int getOptionCount() { return 65; }
protected int getOptionCount() { return 66; }
protected String getDefaultConfigPath() { return "/writer2latex/latex/config/"; }
/////////////////////////////////////////////////////////////////////////
@ -144,37 +144,38 @@ public class LaTeXConfig extends writer2latex.base.ConfigBase {
private static final int USE_BIBTEX = 31;
private static final int BIBTEX_STYLE = 32;
private static final int EXTERNAL_BIBTEX_FILES = 33;
private static final int FORMATTING = 34;
private static final int PAGE_FORMATTING = 35;
private static final int OTHER_STYLES = 36;
private static final int IMAGE_CONTENT = 37;
private static final int TABLE_CONTENT = 38;
private static final int TABLE_FIRST_HEAD_STYLE = 39;
private static final int TABLE_HEAD_STYLE = 40;
private static final int TABLE_FOOT_STYLE = 41;
private static final int TABLE_LAST_FOOT_STYLE = 42;
private static final int IGNORE_HARD_PAGE_BREAKS = 43;
private static final int IGNORE_HARD_LINE_BREAKS = 44;
private static final int IGNORE_EMPTY_PARAGRAPHS = 45;
private static final int IGNORE_DOUBLE_SPACES = 46;
private static final int ALIGN_FRAMES = 47;
private static final int FLOAT_FIGURES = 48;
private static final int FLOAT_TABLES = 49;
private static final int FLOAT_OPTIONS = 50;
private static final int FIGURE_SEQUENCE_NAME = 51;
private static final int TABLE_SEQUENCE_NAME = 52;
private static final int IMAGE_OPTIONS = 53;
private static final int REMOVE_GRAPHICS_EXTENSION = 54;
private static final int ORIGINAL_IMAGE_SIZE = 55;
private static final int SIMPLE_TABLE_LIMIT = 56;
private static final int NOTES = 57;
private static final int METADATA = 58;
private static final int TABSTOP = 59;
private static final int WRAP_LINES_AFTER = 60;
private static final int SPLIT_LINKED_SECTIONS = 61;
private static final int SPLIT_TOPLEVEL_SECTIONS = 62;
private static final int SAVE_IMAGES_IN_SUBDIR = 63;
private static final int DEBUG = 64;
private static final int ZOTERO_BIBTEX_FILES = 34;
private static final int FORMATTING = 35;
private static final int PAGE_FORMATTING = 36;
private static final int OTHER_STYLES = 37;
private static final int IMAGE_CONTENT = 38;
private static final int TABLE_CONTENT = 39;
private static final int TABLE_FIRST_HEAD_STYLE = 40;
private static final int TABLE_HEAD_STYLE = 41;
private static final int TABLE_FOOT_STYLE = 42;
private static final int TABLE_LAST_FOOT_STYLE = 43;
private static final int IGNORE_HARD_PAGE_BREAKS = 44;
private static final int IGNORE_HARD_LINE_BREAKS = 45;
private static final int IGNORE_EMPTY_PARAGRAPHS = 46;
private static final int IGNORE_DOUBLE_SPACES = 47;
private static final int ALIGN_FRAMES = 48;
private static final int FLOAT_FIGURES = 49;
private static final int FLOAT_TABLES = 50;
private static final int FLOAT_OPTIONS = 51;
private static final int FIGURE_SEQUENCE_NAME = 52;
private static final int TABLE_SEQUENCE_NAME = 53;
private static final int IMAGE_OPTIONS = 54;
private static final int REMOVE_GRAPHICS_EXTENSION = 55;
private static final int ORIGINAL_IMAGE_SIZE = 56;
private static final int SIMPLE_TABLE_LIMIT = 57;
private static final int NOTES = 58;
private static final int METADATA = 59;
private static final int TABSTOP = 60;
private static final int WRAP_LINES_AFTER = 61;
private static final int SPLIT_LINKED_SECTIONS = 62;
private static final int SPLIT_TOPLEVEL_SECTIONS = 63;
private static final int SAVE_IMAGES_IN_SUBDIR = 64;
private static final int DEBUG = 65;
/////////////////////////////////////////////////////////////////////////
// IV. Our options data
@ -247,6 +248,7 @@ public class LaTeXConfig extends writer2latex.base.ConfigBase {
options[USE_BIBTEX] = new BooleanOption("use_bibtex","false");
options[BIBTEX_STYLE] = new Option("bibtex_style","plain");
options[EXTERNAL_BIBTEX_FILES] = new Option("external_bibtex_files","");
options[ZOTERO_BIBTEX_FILES] = new Option("zotero_bibtex_files","");
options[FORMATTING] = new IntegerOption("formatting","convert_basic") {
public void setString(String sValue) {
super.setString(sValue);
@ -656,6 +658,7 @@ public class LaTeXConfig extends writer2latex.base.ConfigBase {
public boolean useBibtex() { return ((BooleanOption) options[USE_BIBTEX]).getValue(); }
public String bibtexStyle() { return options[BIBTEX_STYLE].getString(); }
public String externalBibtexFiles() { return options[EXTERNAL_BIBTEX_FILES].getString(); }
public String zoteroBibtexFiles() { return options[ZOTERO_BIBTEX_FILES].getString(); }
// Formatting options
public int formatting() { return ((IntegerOption) options[FORMATTING]).getValue(); }

View file

@ -20,12 +20,15 @@
*
* All Rights Reserved.
*
* Version 1.2 (2010-03-28)
* Version 1.2 (2010-10-06)
*
*/
package writer2latex.latex;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -56,6 +59,68 @@ public class SectionConverter extends ConverterHelper {
public void appendDeclarations(LaTeXDocumentPortion pack, LaTeXDocumentPortion decl) {
if (bNeedMulticol) { pack.append("\\usepackage{multicol}").nl(); }
}
// Handle a section as a Zotero bibliography
private boolean handleZoteroBibliography(Element node, LaTeXDocumentPortion ldp, Context oc) {
String sName = node.getAttribute(XMLString.TEXT_NAME);
if (config.useBibtex() && config.zoteroBibtexFiles().length()>0 && sName.startsWith("ZOTERO_BIBL")) {
// This section is a Zotero bibliography, and the user wishes to handle it as such
// A Zotero bibliography name has the form ZOTERO_BIBL <json object> <identifier> with a single space separating the items
// The identifier is a unique identifier for the bibliography and is not used here
if (!config.noIndex()) {
// Parse the name (errors are ignored) and add \nocite commands as appropriate
int nObjectStart = sName.indexOf('{');
int nObjectEnd = sName.lastIndexOf('}');
if (nObjectStart>-1 && nObjectEnd>-1 && nObjectStart<nObjectEnd) {
String sJsonObject = sName.substring(nObjectStart, nObjectEnd+1);
JSONObject jo = null;
try {
jo = new JSONObject(sJsonObject);
} catch (JSONException e) {
}
if (jo!=null) {
JSONArray uncited = null;
try {
uncited = jo.getJSONArray("uncited");
}
catch (JSONException e) {
}
if (uncited!=null) {
int nCount = uncited.length();
if (nCount>0) {
ldp.append("\\nocite{");
for (int nIndex=0; nIndex<nCount; nIndex++) {
if (nIndex>0) {
ldp.append(",");
}
String sURI = null;
try { // Each item is an array containing a single string
sURI = uncited.getJSONArray(nIndex).getString(0);
}
catch (JSONException e) {
}
if (sURI!=null) {
int nSlash = sURI.lastIndexOf('/');
if (nSlash>0) { ldp.append(sURI.substring(nSlash+1)); }
else { ldp.append(sURI); }
}
}
ldp.append("}").nl();
}
}
}
}
// Use the BibTeX style and files given in the configuration
ldp.append("\\bibliographystyle{").append(config.bibtexStyle()).append("}").nl()
.append("\\bibliography{").append(config.zoteroBibtexFiles()).append("}").nl();
}
return true;
}
else {
return false;
}
}
/** <p> Process a section (text:section tag)</p>
* @param node The element containing the section
@ -102,7 +167,10 @@ public class SectionConverter extends ConverterHelper {
if (sFileName!=null) {
ldp.append("\\input{").append(sFileName).append("}").nl();
}
palette.getBlockCv().traverseBlockText(node,sectionLdp,ic);
// Zotero might have generated this section as a bibliograhy:
if (!handleZoteroBibliography(node,sectionLdp,ic)) {
palette.getBlockCv().traverseBlockText(node,sectionLdp,ic);
}
if (sectionLdp!=ldp) { sectionLdp.append("\\endinput").nl(); }
ldp.append(ba.getAfter());
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2009 by Henrik Just
* Copyright: 2002-2010 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2009-04-30)
* Version 1.2 (2010-10-04)
*
*/
@ -77,11 +77,14 @@ public class Context {
// within a caption
private boolean bInCaption = false;
// within a Zotero citation
private boolean bInZoteroText = false;
// within a floating figure (figure environment)
private boolean bInFigureFloat = false;
// within a floating table (table envrionment)
// within a floating table (table environment)
private boolean bInTableFloat = false;
// within a minipage environment
@ -190,6 +193,10 @@ public class Context {
public void setInCaption(boolean bInCaption) { this.bInCaption = bInCaption; }
public boolean isInCaption() { return bInCaption; }
public void setInZoteroText(boolean bInZoteroText) { this.bInZoteroText = bInZoteroText; }
public boolean isInZoteroText() { return bInZoteroText; }
public void setInFigureFloat(boolean bInFigureFloat) { this.bInFigureFloat = bInFigureFloat; }