Class: BibSync::Transformer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/bibsync/transformer.rb

Instance Method Summary collapse

Methods included from Utils

#arxiv_download, #arxiv_id, #fetch, #fetch_xml, #name_without_ext

Instance Method Details

#call(bib) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bibsync/transformer.rb', line 5

def call(bib)
  bib.each do |entry|
    next if entry.comment?

    # Fix math mode of title and abstract
    [:title, :abstract].each do |k|
      next unless v = entry[k]

      parts = v.split('$', -1)
      parts.each_with_index do |part, i|
        if i % 2 == 0 # Not in math mode

          # Fix underscores which are not wrapped by $
          parts[i].gsub!(/(\s|\A)([^\s]*?_[^\s]*?)([:\.,]|(\-\w+))?(\s|\Z)/) do
            "#{$1}$#{$2}$#{$3}#{$5}"
          end
        end
      end
      entry[k] = parts.join('$')
    end

    if entry[:author]
      entry[:author] = entry[:author].gsub(/\{(\w+)\}/, '\\1').gsub(/#/, ' and ')
    end

    if entry[:doi] && entry[:doi] =~ /(PhysRev|RevModPhys).*?\.(\d+)$/
      entry[:publisher] ||= 'American Physical Society'
      entry[:pages] ||= $2
    end

    if entry[:publisher] && entry[:publisher] =~ /American Physical Society/i
      entry[:publisher] = 'American Physical Society'
    end

    if entry[:month]
      entry[:month] = Literal.new(entry[:month].downcase)
    end

    if entry[:journal]
      if entry[:journal] =~ /EPL/
        entry[:year] = $1 if entry[:journal] =~ /\((\d{4})\)/
        entry[:pages] = $1 if entry[:journal] =~ / (\d{5,10})( |\Z)/
        entry[:volume] = $1 if entry[:journal] =~ / (\d{2,4})( |\Z)/
        entry[:journal] = 'Europhysics Letters'
      end

      if entry[:journal] =~ /(Phys\.|Physical) (Rev\.|Review) Lett[^ ]+ /
        entry[:year] = $1 if entry[:journal] =~ /\((\d{4})\)/
        entry[:pages] = $1 if entry[:journal] =~ / (\d{5,10})( |,|\Z)/
        entry[:volume] = $1 if entry[:journal] =~ / (\d{2,4})( |,|\Z)/
        entry[:journal] = 'Physical Review Letters'
      end

      if entry[:journal] =~ /(Phys\.|Physical) (Rev\.|Review) (\w) /
        letter = $3
        entry[:year] = $1 if entry[:journal] =~ /\((\d{4})\)/
        entry[:pages] = $1 if entry[:journal] =~ / (\d{5,10})( |,|\Z)/
        entry[:volume] = $1 if entry[:journal] =~ / (\d{2,4})( |,|\Z)/
        entry[:journal] = "Physical Review #{letter}"
      end

      case entry[:journal]
      when /\APhysical Review (\w)\Z/i
        entry[:shortjournal] = "PR#{$1.upcase}"
      when /\APhysical Review Letters\Z/i
        entry[:shortjournal] = 'PRL'
      when /\AReviews of Modern Physics\Z/i
        entry[:shortjournal] = 'RMP'
      when /\ANew Journal of Physics\Z/i
        entry[:shortjournal] = 'NJP'
      when /\AArXiv e-prints\Z/i
        entry[:shortjournal] = 'arXiv'
      when /\AEurophysics Letters\Z/i
        entry[:shortjournal] = 'EPL'
      else
        entry[:shortjournal] = entry[:journal]
      end
    end
  end
end