Sha256: 35a94182c5b79e12e2e2ba22bbd17164a1cd5c9e5439bc3f72408a73ee173dfe

Contents?: true

Size: 825 Bytes

Versions: 3

Compression:

Stored size: 825 Bytes

Contents

require 'rexml/document'

class BibTeX::Entry::BibTeXMLConverter
  def self.convert(bibtex, options = {})
    new(bibtex, options).convert!
  end

  def initialize(bibtex, options = {})
    @bibtex = bibtex
    @options = options
  end

  def convert!
    xml = REXML::Element.new('bibtex:entry')
    xml.attributes['id'] = bibtex.key

    fields

    xml.add_element(entry)
    xml
  end

  def fields
    bibtex.fields.each do |key, value|
      field = REXML::Element.new("bibtex:#{key}")

      if options[:extended] && value.name?
        value.each { |n| entry.add_element(n.to_xml) }
      else
        field.text = value.to_s(options)
      end

      entry.add_element(field)
    end
  end

  private

  attr_reader :bibtex, :options

  def entry
    @entry ||= REXML::Element.new("bibtex:#{bibtex.type}")
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bibtex-ruby-3.1.2 lib/bibtex/entry/bibtexml_converter.rb
bibtex-ruby-3.1.1 lib/bibtex/entry/bibtexml_converter.rb
bibtex-ruby-3.1.0 lib/bibtex/entry/bibtexml_converter.rb