-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add support for multiple bibliography files #94
Open
lorrden
wants to merge
6
commits into
asciidoctor:main
Choose a base branch
from
lorrden:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b50c65f
Add support for multiple bibliography files
lorrden a7ac3a6
Update README
lorrden 877b743
Add example of multiple bibliographies
lorrden 6e296e1
Get rid of bibtex-files attribute
lorrden 9ca0329
Address comment about compacting for-each loop
lorrden 8aa38c6
Restore ability to pass strings to Processor.new
lorrden 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 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 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 |
---|---|---|
|
@@ -48,13 +48,17 @@ def apply(value) | |
# current document, and run the different steps to add the citations | ||
# and bibliography | ||
class Processor | ||
def initialize(bibfile, links = false, style = 'ieee', locale = 'en-US', | ||
def initialize(bibfiles, links = false, style = 'ieee', locale = 'en-US', | ||
numeric_in_appearance_order = false, output = :asciidoc, | ||
throw_on_unknown = false, custom_citation_template: '[$id]') | ||
raise "File '#{bibfile}' is not found" unless FileTest.file? bibfile | ||
|
||
bibtex = BibTeX.open bibfile, filter: [LatexFilter] | ||
@biblio = bibtex | ||
bibfiles.each do |bibfile| | ||
raise "File '#{bibfile}' is not found" unless FileTest.file? bibfile | ||
end | ||
@biblio = [] | ||
bibfiles.each do |bibfile| | ||
bibtex = BibTeX.open bibfile, filter: [LatexFilter] | ||
@biblio += [bibtex] | ||
end | ||
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. Maybe something more compact like: @biblio = bibfiles.map { |bibfile| BibTeX.open bibfile, filter: [LatexFilter] } 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. Cleaned up in latest patches |
||
@links = links | ||
@numeric_in_appearance_order = numeric_in_appearance_order | ||
@style = style | ||
|
@@ -73,10 +77,22 @@ def initialize(bibfile, links = false, style = 'ieee', locale = 'en-US', | |
|
||
if (output != :latex) && (output != :bibtex) && (output != :biblatex) | ||
@citeproc = CiteProc::Processor.new style: @style, format: :html, locale: @locale | ||
@citeproc.import @biblio.to_citeproc | ||
@biblio.each do | biblio_item | | ||
@citeproc.import biblio_item.to_citeproc | ||
end | ||
end | ||
end | ||
|
||
def get_bibitem(key) | ||
bibitem = nil | ||
@biblio.each do |bib| | ||
bibitem = bib[key] | ||
if !bibitem.nil? | ||
break | ||
end | ||
end | ||
return bibitem | ||
end | ||
# Scan a line and process citation macros. | ||
# | ||
# As this function being called iteratively on the lines of the document, | ||
|
@@ -97,7 +113,8 @@ def finalize_macro_processing | |
return if StyleUtils.is_numeric?(@style) && @numeric_in_appearance_order | ||
|
||
@citations = @citations.sort_by do |ref| | ||
bibitem = @biblio[ref] | ||
bibitem = get_bibitem(ref) | ||
|
||
if bibitem.nil? | ||
[ref] | ||
else | ||
|
@@ -153,7 +170,9 @@ def build_bibliography_list | |
# Build the asciidoc text for a single bibliography item | ||
def build_bibitem_text(key) | ||
begin | ||
if @biblio[key].nil? | ||
bibitem = get_bibitem(key) | ||
|
||
if bibitem.nil? | ||
puts "Unknown reference: #{key}" | ||
cptext = key | ||
else | ||
|
@@ -173,7 +192,7 @@ def build_bibliography_item(key, index = 0) | |
result = '' | ||
|
||
begin | ||
cptext = if @biblio[key].nil? | ||
cptext = if get_bibitem(key).nil? | ||
nil | ||
else | ||
@citeproc.render :bibliography, id: key | ||
|
@@ -241,7 +260,7 @@ def build_citation_text(macro) | |
result << "<<#{cite.key}," if @links | ||
|
||
# if found, insert reference information | ||
if @biblio[cite.key].nil? | ||
if get_bibitem(cite.key).nil? | ||
if @throw_on_unknown | ||
raise "Unknown reference: #{cite.key}" | ||
else | ||
|
@@ -310,7 +329,8 @@ def citation_text(macro, cite) | |
cite_text = cite_text.gsub('(', '') | ||
cite_text = cite_text.gsub(')', '') | ||
cite_text += format_locator(cite) | ||
year = @biblio[cite.key].year | ||
|
||
year = get_bibitem(cite.key).year | ||
if !year.nil? && macro.type == 'citenp' | ||
segs = cite_text.partition(year.to_s) | ||
head = segs[0].gsub(', ', ' ') | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@book{brown10, | ||
editor = {J. Brown Jr}, | ||
title = {Other book title}, | ||
publisher = {OUP}, | ||
year = {2010} | ||
} |
This file contains 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 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
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.
For backwards compatibility it would be better to accept both a single string and an array of strings here. This would also simplify the patch in the tests since single-bib could still be handled with the old syntax, and only the new test using an array would have to be introduced.
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.
String arguments restored in latest patch series
Also restored the old tests to pass strings instead.