-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
LO: Fix zotero mode bibliography rendering, optimize performance #16555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
46f61e8
Fix bibliography rendering
subhramit 1e2d805
Wire in bibliography properties
subhramit 8461a1c
Disable irrelvant properties in zotero mode
subhramit 61e12b6
Use standardized bibliography rendering
subhramit a884c57
IntelliJ format
subhramit 9d37258
Restore and refine comment
subhramit 19a2f02
Optimize bulk bibliography generation
subhramit 2c79383
Fix test
subhramit 5ba2936
Refactor, extract common code
subhramit 02652e1
Merge branch 'main' into fix-zotero-mode-bibliography
subhramit c0a14aa
Add changelog entry
subhramit e416fc2
Merge branch 'main' into fix-zotero-mode-bibliography
subhramit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,23 +32,12 @@ | |
|
|
||
| import com.sun.star.beans.XPropertySet; | ||
| import com.sun.star.container.NoSuchElementException; | ||
| import com.sun.star.container.XNameAccess; | ||
| import com.sun.star.container.XNameContainer; | ||
| import com.sun.star.lang.WrappedTargetException; | ||
| import com.sun.star.lang.XMultiServiceFactory; | ||
| import com.sun.star.style.LineSpacing; | ||
| import com.sun.star.style.LineSpacingMode; | ||
| import com.sun.star.style.TabAlign; | ||
| import com.sun.star.style.TabStop; | ||
| import com.sun.star.style.XStyle; | ||
| import com.sun.star.style.XStyleFamiliesSupplier; | ||
| import com.sun.star.text.XTextCursor; | ||
| import com.sun.star.text.XTextDocument; | ||
| import com.sun.star.uno.Exception; | ||
| import com.sun.star.uno.UnoRuntime; | ||
| import com.sun.star.uno.XComponentContext; | ||
| import de.undercouch.citeproc.output.Bibliography; | ||
| import de.undercouch.citeproc.output.SecondFieldAlign; | ||
| import org.jspecify.annotations.NonNull; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -67,15 +56,8 @@ public class CSLCitationOOAdapter { | |
| private static final Logger LOGGER = LoggerFactory.getLogger(CSLCitationOOAdapter.class); | ||
|
|
||
| private static final String CITATION_DELIMITER = ", "; | ||
| private static final String ZOTERO_BIBLIOGRAPHY_PARAGRAPH_STYLE = "Bibliography 1"; | ||
| private static final String PARAGRAPH_STYLES = "ParagraphStyles"; | ||
| private static final CitationStyleOutputFormat HTML_OUTPUT_FORMAT = CitationStyleOutputFormat.HTML; | ||
| private static final Pattern CITATION_NUMBER_PATTERN = Pattern.compile("(\\D*)(\\d+)(\\D*)"); | ||
| private static final Pattern CSL_LEFT_RIGHT_MARGIN_PATTERN = Pattern.compile( | ||
| "<div class=\"csl-left-margin\">(.*?)</div><div class=\"csl-right-inline\">(.*?)</div>"); | ||
| private static final Pattern EMPTY_PARAGRAPHS_AT_BIBLIOGRAPHY_ENTRY_EDGES_PATTERN = Pattern.compile( | ||
| "^(?:\\s*<p(?: oo:ParaStyleName=\"[^\"]*\")?>\\s*</p>)+\\s*|(?:\\s*<p(?: oo:ParaStyleName=\"[^\"]*\")?>\\s*</p>)+\\s*$"); | ||
| private static final double MM_PER_100_TWIP = 25.4 / 1440 * 100; | ||
|
|
||
| private final XComponentContext componentContext; | ||
| private final XTextDocument document; | ||
|
|
@@ -227,46 +209,14 @@ public void insertJabRefBibliography(XTextCursor cursor, CitationStyle selectedS | |
| return; | ||
| } | ||
|
|
||
| boolean isNumericStyle = selectedStyle.isNumericStyle(); | ||
|
|
||
| markManager.setRealTimeNumberUpdateRequired(isNumericStyle); | ||
| markManager.readAndUpdateExistingMarks(); | ||
| updateAllCitationsWithNewStyle(selectedStyle, citationType, selectedDatabases); | ||
| markManager.readAndUpdateExistingMarks(); | ||
| List<BibEntry> bibliographyEntries = prepareBibliographyEntries(selectedStyle, entries, selectedDatabases); | ||
|
|
||
| OOText title = OOFormat.paragraph(OOText.fromString(openOfficePreferences.getCslBibliographyTitle()), openOfficePreferences.getCslBibliographyHeaderFormat()); | ||
| OOTextIntoOO.write(document, cursor, OOText.fromString(title.toString())); | ||
| OOText ooBreak = OOFormat.paragraph(OOText.fromString(""), openOfficePreferences.getCslBibliographyBodyFormat()); | ||
| OOTextIntoOO.write(document, cursor, ooBreak); | ||
|
|
||
| BibDatabaseContext currentEntryContext = new BibDatabaseContext(new BibDatabase(entries)); | ||
| String style = selectedStyle.getSource(); | ||
|
|
||
| if (isNumericStyle) { | ||
| // Sort entries based on their order of appearance in the document | ||
| entries.sort(Comparator.comparingInt(entry -> markManager.getCitationNumber(entry.getCitationKey().orElse("")))); | ||
|
|
||
| for (BibEntry entry : entries) { | ||
| String bibliographyEntry = CitationStyleGenerator.generateBibliography(List.of(entry), style, HTML_OUTPUT_FORMAT, currentEntryContext, bibEntryTypesManager).getFirst(); | ||
| String citationKey = entry.getCitationKey().orElse(""); | ||
| int currentNumber = markManager.getCitationNumber(citationKey); | ||
| String formattedBibliographyEntry = CSLFormatUtils.transformHTML(bibliographyEntry); | ||
| formattedBibliographyEntry = CSLFormatUtils.updateSingleBibliographyNumber(formattedBibliographyEntry, currentNumber); | ||
|
|
||
| OOText ooText = OOFormat.setLocaleNone(OOText.fromString(formattedBibliographyEntry)); | ||
| OOTextIntoOO.write(document, cursor, ooText); | ||
| } | ||
| } else { | ||
| // Ordering will be according to citeproc item data provider (default) | ||
| List<String> bibliographyEntries = CitationStyleGenerator.generateBibliography(entries, style, HTML_OUTPUT_FORMAT, currentEntryContext, bibEntryTypesManager); | ||
|
|
||
| for (String bibliographyEntry : bibliographyEntries) { | ||
| String formattedBibliographyEntry = CSLFormatUtils.transformHTML(bibliographyEntry); | ||
|
|
||
| OOText ooText = OOFormat.setLocaleNone(OOText.fromString(formattedBibliographyEntry)); | ||
| OOTextIntoOO.write(document, cursor, ooText); | ||
| } | ||
| } | ||
| writeBibliographyEntries(cursor, selectedStyle, bibliographyEntries); | ||
| } | ||
|
|
||
| public void insertZoteroBibliography(XTextCursor cursor, CitationStyle selectedStyle, List<BibEntry> entries, List<BibDatabase> selectedDatabases) | ||
|
|
@@ -275,117 +225,54 @@ public void insertZoteroBibliography(XTextCursor cursor, CitationStyle selectedS | |
| return; | ||
| } | ||
|
|
||
| List<BibEntry> bibliographyEntries = prepareBibliographyEntries(selectedStyle, entries, selectedDatabases); | ||
|
|
||
| XPropertySet cursorProperties = UnoRuntime.queryInterface(XPropertySet.class, cursor); | ||
| cursorProperties.setPropertyValue("ParaStyleName", openOfficePreferences.getCslBibliographyBodyFormat()); | ||
| writeBibliographyEntries(cursor, selectedStyle, bibliographyEntries); | ||
| } | ||
|
|
||
| private List<BibEntry> prepareBibliographyEntries(CitationStyle selectedStyle, List<BibEntry> entries, List<BibDatabase> selectedDatabases) | ||
| throws com.sun.star.uno.Exception, CreationException { | ||
| boolean isNumericStyle = selectedStyle.isNumericStyle(); | ||
|
|
||
| markManager.setRealTimeNumberUpdateRequired(isNumericStyle); | ||
| markManager.readAndUpdateExistingMarks(); | ||
| updateAllCitationsWithNewStyle(selectedStyle, citationType, selectedDatabases); | ||
| markManager.readAndUpdateExistingMarks(); | ||
|
|
||
| BibDatabaseContext currentEntryContext = new BibDatabaseContext(new BibDatabase(entries)); | ||
| List<BibEntry> bibliographyEntries = new ArrayList<>(entries); | ||
| if (isNumericStyle) { | ||
| bibliographyEntries.sort( | ||
| Comparator.comparingInt(entry -> markManager.getCitationNumber(entry.getCitationKey().orElse("")))); | ||
| sortBibliographyEntriesByCitationNumber(bibliographyEntries); | ||
| } | ||
| return bibliographyEntries; | ||
| } | ||
|
|
||
| Optional<Bibliography> bibliography = CitationStyleGenerator.generateBibliographyObject( | ||
| private void sortBibliographyEntriesByCitationNumber(List<BibEntry> bibliographyEntries) { | ||
| bibliographyEntries.sort(Comparator.comparingInt(entry -> markManager.getCitationNumber(entry.getCitationKey().orElse("")))); | ||
| } | ||
|
|
||
| /// Writes bibliography entries using JabRef's standard CSL bibliography rendering. | ||
| /// | ||
| /// For numeric styles, callers are expected to provide entries already sorted in order of appearance | ||
| /// in the document so citeproc-java emits matching bibliography numbers. For non-numeric styles, | ||
| /// ordering is left to the citeproc item data provider. | ||
| private void writeBibliographyEntries(XTextCursor cursor, CitationStyle selectedStyle, List<BibEntry> bibliographyEntries) | ||
| throws com.sun.star.uno.Exception, CreationException { | ||
| BibDatabaseContext currentEntryContext = new BibDatabaseContext(new BibDatabase(bibliographyEntries)); | ||
| List<String> bibliographyEntriesText = CitationStyleGenerator.generateBibliography( | ||
| bibliographyEntries, | ||
| selectedStyle.getSource(), | ||
| HTML_OUTPUT_FORMAT, | ||
| currentEntryContext, | ||
| bibEntryTypesManager); | ||
| if (bibliography.isEmpty()) { | ||
| return; | ||
| } | ||
| Bibliography generatedBibliography = bibliography.orElseThrow(); | ||
|
|
||
| XStyleFamiliesSupplier styleFamilies = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, document); | ||
| XNameAccess styleFamilyNames = styleFamilies.getStyleFamilies(); | ||
| XNameAccess paragraphStyles = UnoRuntime.queryInterface( | ||
| XNameAccess.class, | ||
| styleFamilyNames.getByName(PARAGRAPH_STYLES)); | ||
| XPropertySet styleProperties; | ||
|
|
||
| try { | ||
| styleProperties = UnoRuntime.queryInterface( | ||
| XPropertySet.class, | ||
| paragraphStyles.getByName(ZOTERO_BIBLIOGRAPHY_PARAGRAPH_STYLE)); | ||
| } catch (NoSuchElementException e) { | ||
| LOGGER.debug("Bibliography paragraph style not found. Creating it.", e); | ||
| XMultiServiceFactory documentFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, document); | ||
| XStyle style = UnoRuntime.queryInterface( | ||
| XStyle.class, | ||
| documentFactory.createInstance("com.sun.star.style.ParagraphStyle")); | ||
| XNameContainer paragraphStyleNames = UnoRuntime.queryInterface( | ||
| XNameContainer.class, | ||
| paragraphStyles); | ||
| paragraphStyleNames.insertByName(ZOTERO_BIBLIOGRAPHY_PARAGRAPH_STYLE, style); | ||
| style.setParentStyle("Default"); | ||
| styleProperties = UnoRuntime.queryInterface(XPropertySet.class, style); | ||
| } | ||
|
|
||
| int firstLineIndent = 0; | ||
| int indent = 0; | ||
| int maxOffset = Optional.ofNullable(generatedBibliography.getMaxOffset()).orElse(0); | ||
| TabStop[] tabStops = new TabStop[0]; | ||
| SecondFieldAlign secondFieldAlign = Optional.ofNullable(generatedBibliography.getSecondFieldAlign()) | ||
| .orElse(SecondFieldAlign.FALSE); | ||
|
|
||
| if (Boolean.TRUE.equals(generatedBibliography.getHangingIndent())) { | ||
| indent = 720; | ||
| firstLineIndent = -720; | ||
| } else if (secondFieldAlign != SecondFieldAlign.FALSE) { | ||
| int alignAt = 24 + maxOffset * 120; | ||
| firstLineIndent = -alignAt; | ||
| if (secondFieldAlign == SecondFieldAlign.MARGIN) { | ||
| tabStops = createTabStops(0); | ||
| } else { | ||
|
Comment on lines
-335
to
-343
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like JabRef-only mode, we will use LO's natural Hanging Indent formatting instead of these hardcodings, which appear to work in RTF-based mode of zotero but not here. |
||
| indent = alignAt; | ||
| tabStops = createTabStops(alignAt); | ||
| } | ||
| } | ||
|
|
||
| LineSpacing lineSpacing = new LineSpacing(); | ||
| lineSpacing.Mode = LineSpacingMode.MINIMUM; | ||
| lineSpacing.Height = (short) (240 | ||
| * Optional.ofNullable(generatedBibliography.getLineSpacing()).orElse(1) | ||
| * MM_PER_100_TWIP); | ||
|
|
||
| styleProperties.setPropertyValue("ParaFirstLineIndent", (int) (firstLineIndent * MM_PER_100_TWIP)); | ||
| styleProperties.setPropertyValue("ParaLeftMargin", (int) (indent * MM_PER_100_TWIP)); | ||
| styleProperties.setPropertyValue("ParaLineSpacing", lineSpacing); | ||
| styleProperties.setPropertyValue("ParaTopMargin", 0); | ||
| styleProperties.setPropertyValue("ParaBottomMargin", 0); | ||
| styleProperties.setPropertyValue("ParaTabStops", tabStops); | ||
|
|
||
| XPropertySet cursorProperties = UnoRuntime.queryInterface(XPropertySet.class, cursor); | ||
| cursorProperties.setPropertyValue("ParaStyleName", ZOTERO_BIBLIOGRAPHY_PARAGRAPH_STYLE); | ||
| OOTextIntoOO.write(document, cursor, createZoteroBibliographyText(generatedBibliography)); | ||
| } | ||
|
|
||
| static OOText createZoteroBibliographyText(Bibliography bibliography) { | ||
| StringJoiner bibliographyText = new StringJoiner( | ||
| "<p oo:ParaStyleName=\"%s\">".formatted(ZOTERO_BIBLIOGRAPHY_PARAGRAPH_STYLE)); | ||
| for (String bibliographyEntry : bibliography.getEntries()) { | ||
| String formattedBibliographyEntry = CSL_LEFT_RIGHT_MARGIN_PATTERN.matcher(bibliographyEntry) | ||
| .replaceAll("$1\t$2"); | ||
| formattedBibliographyEntry = CSLFormatUtils.transformHTML(formattedBibliographyEntry); | ||
| formattedBibliographyEntry = EMPTY_PARAGRAPHS_AT_BIBLIOGRAPHY_ENTRY_EDGES_PATTERN.matcher(formattedBibliographyEntry) | ||
| .replaceAll(""); | ||
| bibliographyText.add(formattedBibliographyEntry); | ||
| } | ||
| return OOFormat.setLocaleNone(OOText.fromString(bibliographyText.toString())); | ||
| } | ||
| for (String bibliographyEntry : bibliographyEntriesText) { | ||
| String formattedBibliographyEntry = CSLFormatUtils.transformHTML(bibliographyEntry); | ||
|
|
||
| private static TabStop[] createTabStops(int... positions) { | ||
| TabStop[] tabStops = new TabStop[positions.length]; | ||
| for (int i = 0; i < positions.length; i++) { | ||
| tabStops[i] = new TabStop(); | ||
| tabStops[i].Position = (int) (positions[i] * MM_PER_100_TWIP); | ||
| tabStops[i].Alignment = TabAlign.LEFT; | ||
| OOText ooText = OOFormat.setLocaleNone(OOText.fromString(formattedBibliographyEntry)); | ||
| OOTextIntoOO.write(document, cursor, ooText); | ||
| } | ||
| return tabStops; | ||
| } | ||
|
|
||
| /// Inserts references and also adds a space before the citation if not already present ("smart space"). | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A label can also be disabled? Never thought of that