Module: Vendor::BibtexRuby

Defined in:
lib/vendor/bibtex_ruby.rb

Constant Summary collapse

TAXONWORKS_STYLE_ROOT =
File.expand_path(Rails.root.to_s + '/lib/vendor/styles/')
CSL_STYLE_ROOT =
CSL::Style.root

Class Method Summary collapse

Class Method Details

.bibliography(sources) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/vendor/bibtex_ruby.rb', line 45

def self.bibliography(sources)
  b = BibTeX::Bibliography.new
  sources.each do |s|
    next unless s.is_bibtex?
    e = s.to_bibtex
    e.year = '0000' if (e.year.nil? || e.year == '')
    b.add(e)
  end
  b
end

.get_style(style) ⇒ Object

Searches first through CSL/Styles

Returns:

  • a CSL::Style object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vendor/bibtex_ruby.rb', line 12

def self.get_style(style)
  return nil if style.nil?
  begin
    if s = CSL::Style.load(style)
      return s
    end
  rescue CSL::ParseError
  end

  toggle_style_root # set to TaxonWorks path

  begin
    if s = CSL::Style.load(style)
      return s
    end
  rescue CSL::ParseError
    return nil
  ensure
    toggle_style_root # set it back to rubygems
  end
end

.namecase_bibtex_entry(bibtex_entry) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/vendor/bibtex_ruby.rb', line 34

def self.namecase_bibtex_entry(bibtex_entry)
  bibtex_entry.parse_names
  bibtex_entry.names.each do |n|
    n.first = NameCase(n.first) if n.first
    n.last = NameCase(n.last) if n.last
    n.prefix = NameCase(n.prefix) if n.prefix
    n.suffix = NameCase(n.suffix) if n.suffix
  end
  bibtex_entry
end

.styled(sources = [], style_id = 'http://www.zotero.org/styles/vancouver') ⇒ Object

Returns Array of styled sources. Allows and annotates non-BibTeX sources to be passed.

Returns:

  • Array of styled sources. Allows and annotates non-BibTeX sources to be passed.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vendor/bibtex_ruby.rb', line 58

def self.styled(sources = [], style_id = 'http://www.zotero.org/styles/vancouver')
  return [] if ::CSL_STYLES[style_id].nil?
  r = []
  sources.each do |s|
    if s.type == 'Source::Bibtex'
      r.push s.render_with_style(style_id)
    elsif s.type == 'Source::Verbatim'
      r.push '!!Verbatim!!: ' + s.cached
    else # Source::Human
      r.push '!!People!!: ' + s.cached
    end
  end
  r.sort
end

.toggle_style_rootObject (private)



75
76
77
78
79
80
81
# File 'lib/vendor/bibtex_ruby.rb', line 75

def self.toggle_style_root
  if CSL::Style.root == CSL_STYLE_ROOT
    CSL::Style.root = TAXONWORKS_STYLE_ROOT
  else
    CSL::Style.root = CSL_STYLE_ROOT
  end
end