require "nokogiri" module BibTeX class Bibliography attr_accessor :filepath class << self def trim_unnecessary_entries!(bib, citekeys) bib.entries.each do |k,v| if !citekeys.include? k bib.delete(k) end end end def from_bcf(path) File.open(path, "r") do |f| @bcf = Nokogiri::XML(f).remove_namespaces! end filepath = File.dirname(path) + "/" + @bcf.at_xpath("//datasource").content citekeys = @bcf.xpath("//citekey", 'bcf'=>'URI').map(&:content) bib = open(filepath, :filter => 'latex') bib.filepath = filepath trim_unnecessary_entries!(bib, citekeys) bib.sort! { |a,b| a.author <=> b.author } bib.extend(BibtexMunge) bib.fix_everything! return bib end end end end