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 all commits
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
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Configuration is applied in the form of AsciiDoc document attributes, which must
| Attribute Name | Description | Valid Values | Default Value

| bibtex-file
| Bibtex database file
| Bibtex database files (comma separated)
| any string, or empty
| Automatic searching

Expand Down
28 changes: 21 additions & 7 deletions lib/asciidoctor-bibtex/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,39 @@ 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-file').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
bibtex_format = ((document.attr 'bibtex-format') || 'asciidoc').to_sym
bibtex_throw = ((document.attr 'bibtex-throw') || 'false').to_s.downcase
bibtex_citation_template = ((document.attr 'bibtex-citation-template') || '[$id]').to_s

# Fild bibtex file automatically if not supplied.
if bibtex_file.empty?
if bibtex_files.nil?
bibtex_files = []
end
bibtex_files.delete_if {|file_name| file_name.empty? }

# Find bibtex file automatically if not supplied.
if bibtex_files.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile document.base_dir
if !bibtex_file.nil?
bibtex_files += [bibtex_file]
end
end
if bibtex_file.empty?
if bibtex_files.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile '.'
if !bibtex_file.nil?
bibtex_files += [bibtex_file]
end
end
if bibtex_file.empty?
if bibtex_files.empty?
bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile "#{ENV['HOME']}/Documents"
if !bibtex_file.nil?
bibtex_files += [bibtex_file]
end
end
if bibtex_file.empty?
if bibtex_files.empty?
puts 'Error: bibtex-file is not set and automatic search failed'
exit
end
Expand All @@ -93,7 +107,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
39 changes: 30 additions & 9 deletions lib/asciidoctor-bibtex/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ class Processor
def initialize(bibfile, 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
bibfiles = bibfile
if bibfile.is_a? String
bibfiles = [bibfile]
end
bibfiles.each do |bibfile|
raise "File '#{bibfile}' is not found" unless FileTest.file? bibfile
end
@biblio = bibfiles.map {|bibfile| BibTeX.open bibfile, filter: [LatexFilter] }

bibtex = BibTeX.open bibfile, filter: [LatexFilter]
@biblio = bibtex
@links = links
@numeric_in_appearance_order = numeric_in_appearance_order
@style = style
Expand All @@ -73,10 +78,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 +114,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 +171,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 +193,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 +261,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 +330,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
3 changes: 3 additions & 0 deletions samples/multiple-bibfiles/1.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@book{foo,
title = {The legend of Foo}
}
3 changes: 3 additions & 0 deletions samples/multiple-bibfiles/2.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@book{bar,
title = {The legend of Bar}
}
8 changes: 8 additions & 0 deletions samples/multiple-bibfiles/test.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
= Test doc
:bibtex-file: 1.bib, 2.bib

== Hello

To refer to cite:[foo] and cite:[bar]

bibliography::[]
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
8 changes: 8 additions & 0 deletions test/processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,13 @@
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