Skip to content
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
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/asciidoctor-bibtex/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def process(parent, target, attrs)
class CitationProcessor < ::Asciidoctor::Extensions::Treeprocessor
def process(document)
bibtex_file = (document.attr 'bibtex-file').to_s
bibtex_files = (document.attr 'bibtex-files').to_s.split(',').map(&:strip)
bibtex_style = ((document.attr 'bibtex-style') || 'ieee').to_s
bibtex_locale = ((document.attr 'bibtex-locale') || 'en-US').to_s
bibtex_order = ((document.attr 'bibtex-order') || 'appearance').to_sym
Expand All @@ -70,20 +71,22 @@ def process(document)
bibtex_citation_template = ((document.attr 'bibtex-citation-template') || '[$id]').to_s

# Fild bibtex file automatically if not supplied.
if bibtex_file.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile document.base_dir
end
if bibtex_file.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile '.'
end
if bibtex_file.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile "#{ENV['HOME']}/Documents"
end
if bibtex_file.empty?
puts 'Error: bibtex-file is not set and automatic search failed'
exit
if bibtex_files.empty?
if bibtex_file.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile document.base_dir
end
if bibtex_file.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile '.'
end
if bibtex_file.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile "#{ENV['HOME']}/Documents"
end
if bibtex_file.empty?
puts 'Error: bibtex-file is not set and automatic search failed'
exit
end
bibtex_files = [bibtex_file]
end

# Extract all AST nodes that can contain citations.
prose_blocks = document.find_by traverse_documents: true do |b|
(b.content_model == :simple) ||
Expand All @@ -93,7 +96,7 @@ def process(document)
end
return nil if prose_blocks.nil?

processor = Processor.new bibtex_file, true, bibtex_style, bibtex_locale,
processor = Processor.new bibtex_files, true, bibtex_style, bibtex_locale,
bibtex_order == :appearance, bibtex_format,
bibtex_throw == 'true', custom_citation_template: bibtex_citation_template

Expand Down
42 changes: 31 additions & 11 deletions lib/asciidoctor-bibtex/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link

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.

Copy link
Author

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.

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
Copy link

Choose a reason for hiding this comment

The 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] }

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(', ', ' ')
Expand Down
6 changes: 6 additions & 0 deletions test/data/test-multiple.bib
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}
}
2 changes: 1 addition & 1 deletion test/formats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
describe AsciidoctorBibtex do

def check_complete_citation style, line, result, links = false
p = Processor.new 'test/data/test.bib', links, style
p = Processor.new ['test/data/test.bib'], links, style
p.process_citation_macros(line)
_(p.build_citation_text(CitationMacro.extract_macros(line).first)).must_equal "[.citation]##{result}#"
end
Expand Down
19 changes: 14 additions & 5 deletions test/processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,46 @@

describe AsciidoctorBibtex do
it "must return Chicago style references" do
p = Processor.new 'test/data/test.bib', false, 'chicago-author-date'
p = Processor.new ['test/data/test.bib'], false, 'chicago-author-date'
_(p.build_bibliography_item('smith10')).must_equal "Smith, D. 2010. _Book Title_. Mahwah, NJ: Lawrence Erlbaum."
_(p.build_bibliography_item('brown09')).must_equal "Brown, J., ed. 2009. _Book Title_. OUP."
end

it "must return numeric style (IEEE) references" do
p = Processor.new 'test/data/test.bib', false, 'ieee'
p = Processor.new ['test/data/test.bib'], false, 'ieee'
_(p.build_bibliography_item('smith10', 0)).must_equal "[1] D. Smith, _Book title_. Mahwah, NJ: Lawrence Erlbaum, 2010."
_(p.build_bibliography_item('brown09', 1)).must_equal "[2] J. Brown, Ed., _Book title_. OUP, 2009."
end

it "must support custom template in numeric style (IEEE) references" do
p = Processor.new 'test/data/test.bib', false, 'ieee', custom_citation_template: '/$id/'
p = Processor.new ['test/data/test.bib'], false, 'ieee', custom_citation_template: '/$id/'
_(p.build_bibliography_item('smith10', 0)).must_equal "/1/ D. Smith, _Book title_. Mahwah, NJ: Lawrence Erlbaum, 2010."
_(p.build_bibliography_item('brown09', 1)).must_equal "/2/ J. Brown, Ed., _Book title_. OUP, 2009."
end

it "must return harvard style (APA) references" do
p = Processor.new 'test/data/test.bib', false, 'apa'
p = Processor.new ['test/data/test.bib'], false, 'apa'
_(p.build_bibliography_item('smith10')).must_equal "Smith, D. (2010). _Book title_. Lawrence Erlbaum."
_(p.build_bibliography_item('brown09')).must_equal "Brown, J. (Ed.). (2009). _Book title_. OUP."
end

it "must sort citations correctly" do
begin
p = Processor.new 'test/data/sort.bib', true, 'ieee'
p = Processor.new ['test/data/sort.bib'], true, 'ieee'
p.process_citation_macros 'cite:[Morgan2018, Morgan2006]'
p.finalize_macro_processing
rescue
fail 'Should not throw exception when sorting citations'
end
end

it "must handle multiple bibliographies" do
p = Processor.new ['test/data/test.bib', 'test/data/test-multiple.bib'], false, 'ieee'
_(p.build_bibliography_item('brown10', 0)).must_equal "[1] J. B. Jr, Ed., _Other book title_. OUP, 2010."
_(p.build_bibliography_item('smith10', 1)).must_equal "[2] D. Smith, _Book title_. Mahwah, NJ: Lawrence Erlbaum, 2010."
_(p.build_bibliography_item('brown09', 2)).must_equal "[3] J. Brown, Ed., _Book title_. OUP, 2009."

end

end