Skip to content

Commit

Permalink
Support newlines when using a CitationStyle (#2165)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriba authored and koppor committed Oct 16, 2016
1 parent 10897df commit 6903386
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 400 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ dependencies {
// need to use snapshots as the stable version is from 2013 and doesn't support v1.0.1 CitationStyles
compile 'org.citationstyles:styles:1.0.1-SNAPSHOT'
compile 'org.citationstyles:locales:1.0.1-SNAPSHOT'
compile 'de.undercouch:citeproc-java:0.6'
// in Java 8 Rhino was replaced by Nashorn, but 'de.undercouch:citeproc-java:0.6' depends on Rhino
compile 'org.mozilla:rhino:1.7.7.1'
compile 'de.undercouch:citeproc-java:1.0.0-SNAPSHOT'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
Expand Down
5 changes: 0 additions & 5 deletions external-libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,4 @@ Project: Citeproc-Java
URL: http://michel-kraemer.github.io/citeproc-java/
Licence: Apache License, Version 2.0

Id: org.mozilla.rhino
Project: Rhino
URL: https://developer.mozilla.org/de/docs/Rhino
Licence: Mozilla Public License Version 2.0

The last entry has to end with an empty line. Otherwise the entry is not present in About.html.
375 changes: 0 additions & 375 deletions licenses/org.mozilla.rhino.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public CitationStyleWorker(PreviewPanel previewPanel, JEditorPane previewPane) {

Optional<BasePanel> basePanel = previewPanel.getBasePanel();
if (basePanel.isPresent()){
Optional<CitationStyle> citationStyle = basePanel.get().getCitationStyleCache().getCitationStyle();
if (citationStyle.isPresent()) {
previewPane.setText("<i>" + Localization.lang("Processing %0", Localization.lang("Citation Style")) +
": " + citationStyle.get().getTitle() + " ..." + "</i>");
}
CitationStyle citationStyle = basePanel.get().getCitationStyleCache().getCitationStyle();
previewPane.setText("<i>" + Localization.lang("Processing %0", Localization.lang("Citation Style")) +
": " + citationStyle.getTitle() + " ..." + "</i>");
}
previewPane.revalidate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.event.EntryRemovedEvent;
Expand All @@ -19,7 +18,7 @@
*/
public class CitationStyleCache {

private Optional<CitationStyle> citationStyle = Optional.of(CitationStyle.getDefault());
private CitationStyle citationStyle = CitationStyle.getDefault();
private Map<BibEntry, String> citationStylesCache = new HashMap<>();


Expand All @@ -38,21 +37,21 @@ public CitationStyleCache(BibDatabaseContext bibDatabaseContext, CitationStyle c
public String getCitationFor(BibEntry entry) {
String citation = citationStylesCache.get(entry);
if (citation == null) {
citation = CitationStyleGenerator.generateCitation(entry, this.citationStyle.get().getSource());
citation = CitationStyleGenerator.generateCitation(entry, this.citationStyle);
citationStylesCache.put(entry, citation);
}
return citation;
}

public void setCitationStyle(CitationStyle citationStyle) {
Objects.requireNonNull(citationStyle);
if (!this.citationStyle.isPresent() || !this.citationStyle.get().equals(citationStyle)){
this.citationStyle = Optional.of(citationStyle);
if (!this.citationStyle.equals(citationStyle)){
this.citationStyle = citationStyle;
this.citationStylesCache.clear();
}
}

public Optional<CitationStyle> getCitationStyle() {
public CitationStyle getCitationStyle() {
return citationStyle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jbibtex.BibTeXEntry;
import org.jbibtex.DigitStringValue;
import org.jbibtex.Key;
import org.jbibtex.TokenMgrError;
import org.jbibtex.TokenMgrException;


/**
Expand All @@ -28,6 +28,13 @@ public class CitationStyleGenerator {
private static final UnicodeToLatexFormatter UNICODE_TO_LATEX_FORMATTER = new UnicodeToLatexFormatter();
private static final BibTeXConverter BIBTEX_CONVERTER = new BibTeXConverter();

/**
* Generates a Citation based on the given entry and style
* WARNING: the citation is generated with JavaScript which may take some time, better call it in outside the main Thread
*/
protected static String generateCitation(BibEntry entry, CitationStyle style) {
return generateCitation(entry, style.getSource(), CitationStyleOutputFormat.HTML);
}

/**
* Generates a Citation based on the given entry and style
Expand Down Expand Up @@ -55,18 +62,19 @@ protected static String generateCitation(BibEntry entry, String style, CitationS
return bibliography.getEntries()[0];

} catch (IOException | ArrayIndexOutOfBoundsException e) {
LOGGER.error("Could not generate BibEntry Citation", e);
} catch (TokenMgrError e) {
LOGGER.error("Could not generate BibEntry citation", e);
return Localization.lang("Cannot generate preview based on selected citation style.");
} catch (TokenMgrException e) {
LOGGER.error("Bad character inside BibEntry", e);
// sadly one cannot easily retrieve the bad char from the TokenMgrError
return new StringBuilder()
.append(Localization.lang("Cannot generate preview based on selected citation style."))
.append(outputFormat == CitationStyleOutputFormat.HTML ? "<br>" : "\n")
.append(Localization.lang("Bad character inside entry"))
.append(outputFormat == CitationStyleOutputFormat.HTML ? "<br>" : "\n")
.append(e.getLocalizedMessage())
.toString();
}

return "";
}

}
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Indstillinger_gemt.
Preview=Forhåndsvisning
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Einstellungen_gespeichert.
Preview=Vorschau
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Preferences_recorded.
Preview=Preview
Citation_Style=Citation_Style
Current_Preview=Current_Preview
Cannot_generate_preview_based_on_selected_citation_style.=Cannot_generate_preview_based_on_selected_citation_style.
Bad_character_inside_entry=Bad_character_inside_entry
Error_while_generating_citation_style=Error_while_generating_citation_style
Preview_style_changed_to\:_%0=Preview_style_changed_to\:_%0
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Preferencias_guardadas.
Preview=Vista_previa
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=
Preview=
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Préférences_enregistrées.
Preview=Aperçu
Citation_Style=Style_de_citation
Current_Preview=Aperçu_actuel
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=Caractère_érroné_dans_l'entrée
Error_while_generating_citation_style=Erreur_lors_de_la_génération_du_style_de_citation
Preview_style_changed_to\:_%0=Style_d'aperçu_modifié_en_:_%0
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Preferensi_disimpan.
Preview=Pratampilan
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Preferenze_registrate.
Preview=Anteprima
Citation_Style=Stile_delle_citazioni
Current_Preview=Anteprima_di_stampa_corrente
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=Carattere_errato_nella_voce
Error_while_generating_citation_style=Errore_durante_la_generazione_dello_stile_di_citazione
Preview_style_changed_to\:_%0=Stile_di_anteprima_modificato_in\:_%0
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=設定が記録されました。
Preview=プレビュー
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Instellingen_opgeslagen.
Preview=Voorbeeld
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Lagret_oppsett.
Preview=Forh\u00e5ndsvisning
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Preferências_salvas.
Preview=Previsualização
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Выполнена_запись_пользовательс
Preview=Предпросмотр
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Inställningar_sparade.
Preview=Postvisning
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Tercihler_kaydedildi.
Preview=Önizleme
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_vi.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=Các_tùy_thích_được_ghi_lại.
Preview=Xem_trước
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Preferences_recorded.=首选项被记录。
Preview=预览
Citation_Style=
Current_Preview=
Cannot_generate_preview_based_on_selected_citation_style.=
Bad_character_inside_entry=
Error_while_generating_citation_style=
Preview_style_changed_to\:_%0=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.sf.jabref.logic.citationstyle;

import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class CitationStyleGeneratorTest {

@Test
public void testCarriageReturn() {
BibEntry entry = new BibEntry();
entry.setField(FieldName.AUTHOR, "Doe, John and\rDoe, Jane");

// if the default citation style changes this has to be modified
String expected = " <div class=\"csl-entry\">\n" +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">J. Doe and J. Doe, .</div>\n" +
" </div>\n";
String citation = CitationStyleGenerator.generateCitation(entry, CitationStyle.getDefault());

assertEquals(expected, citation);
}

@Test
public void testMissingCitationStyle() {
String expected = Localization.lang("Cannot generate preview based on selected citation style.");
String citation = CitationStyleGenerator.generateCitation(new BibEntry(), "faulty citation style");
assertEquals(expected, citation);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public void getDefault() throws Exception {

@Test
public void testDefaultCitation() {
String citation = CitationStyleGenerator.generateCitation(TestEntry.getTestEntry(), CitationStyle.getDefault().getSource());
String citation = CitationStyleGenerator.generateCitation(TestEntry.getTestEntry(), CitationStyle.getDefault());

// if the default citation style changes this has to be modified
String expected = " <div class=\"csl-entry\">\n" +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">" +
"B. Smith, B. Jones, and J. Williams, “Title of the test entry,” " +
Expand Down

0 comments on commit 6903386

Please sign in to comment.