module BibtexMunge

Constants

VERSION

Public Instance Methods

depunctuate!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 53
def depunctuate!
  each_field :except => @@name_fields do |f|
    f.chomp!(".")
  end
  self
end
each_field(options = {}) { |v| ... } click to toggle source

Run a block on every field in every entry

# File lib/bibtex_munge/bibliography.rb, line 26
def each_field(options = {})
  except   = options[:except]
  except ||= []
  if block_given?
    each_entry do |e|
      e.fields.each do |k,v|
        yield v unless except.include? k
      end
    end
  end
end
extend_braces!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 60
def extend_braces!
  each_field do |f|
    f.gsub! /\ {([^ ])}([^ ]+)/, ' {\1\2}'  # The initial space is to avoid
                                            # fucking up braces after an accent.
                                            # this is *probably* redundant now that we're
                                            # unicodifying everything, but better safe than
                                            # sorry
  end
  self
end
fix_everything!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 126
def fix_everything!
  remove_double_braces!
  depunctuate!
  extend_braces!
  split_fields!
  prune_fields!
  normalize_initials!
  normalize_page_ranges!
end
normalize_initials!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 38
def normalize_initials!
  each_entry do |e|
    @@name_fields.each do |k|
      if e[k]
        e[k].each do |name|
          if name.first
            name.first = name.normalize_initials
          end
        end
      end
    end
  end
  self
end
normalize_page_ranges!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 117
def normalize_page_ranges!
  each_entry do |e|
    if e.pages
      e.pages.gsub! /^(\d+)\s*(–|—|-|--|---)\s*(\d+)$/, '\1 -- \3'
    end
  end
  self
end
prune_fields!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 108
def prune_fields!
  each_entry do |e|
    @@prune_fields.each do |k|
      e.delete(k)
    end
  end
  self
end
remove_double_braces!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 71
def remove_double_braces!
  each_field do |f|
    f.gsub! /{{(.*)}}/, '{\1}'    # Eliminate overt double braces

    f.gsub! /^{(.*)}$/, '\1'      # Eliminate whole-entry braces (which would have been
                                  # double braces in the unparsed BibTeX file, since
                                  #       publisher = {{Blackwell}}
                                  # parses to
                                  #       @bib[:key].publisher = "{Blackwell}"
  end
  self
end
save_and_backup(path) click to toggle source
# File lib/bibtex_munge/save.rb, line 4
def save_and_backup(path)
  if path == @filepath
    File.rename(@filepath, @filepath+".bak")
  end
  f = File.open(path, "w")
  f.write(to_s)
  f.close
end
split_field!(e, oldkey, newsuperkey, newsubkey) click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 84
def split_field!(e, oldkey, newsuperkey, newsubkey)
  if e[oldkey]
    elements = e[oldkey].split(/([.:?]) /,2)    # Split after .:?, retaining the separator
    if elements.length == 3                       
      e[newsubkey] = elements[2]                     
      if elements[1] == "?"                       # Keep the separator as part of the first
        e[newsuperkey] = elements[0..1].join      # field if it is a ?, to handle titles of
                                                  # the form {Question? An Answer}
      else
        e[newsuperkey] = elements[0]
      end
    end
  end
end
split_fields!() click to toggle source
# File lib/bibtex_munge/bibliography.rb, line 99
def split_fields!
  each_entry do |e|
    @@split_fields.each do |oldkey, newsuperkey, newsubkey|
      split_field!(e, oldkey, newsuperkey, newsubkey)
    end
  end
  self
end