Module: BibtexMunge
- Defined in:
- lib/bibtex_munge/save.rb,
lib/bibtex_munge/version.rb,
lib/bibtex_munge/bibliography.rb
Constant Summary collapse
- VERSION =
"0.0.1"
- @@split_fields =
[ # specifying how to handle {PART 1: PART 2} # in fields where it is a frequent issue: [:title, :title, :subtitle], # * if the title contains a separator, # the second part becomes a subtitle [:booktitle, :booktitle, :booksubtitle], # * if the booktitle contains a separator, # the second part becomes a booksubtitle [:publisher, :address, :publisher] # * if the publisher contains a separator, # the first part becomes an address ]
- @@name_fields =
-
if the publisher contains a separator,
the first part becomes an address
-
[:author, :editor]
- @@prune_fields =
specifying which fields shouldn’t have trailing periods removed (because they might be initials)
[:isbn, :issn, :abstract]
Instance Method Summary collapse
- #depunctuate! ⇒ Object
-
#each_field(options = {}) ⇒ Object
Run a block on every field in every entry.
- #extend_braces! ⇒ Object
- #fix_everything! ⇒ Object
- #normalize_initials! ⇒ Object
- #normalize_page_ranges! ⇒ Object
- #prune_fields! ⇒ Object
- #remove_double_braces! ⇒ Object
- #save_and_backup(path) ⇒ Object
- #split_field!(e, oldkey, newsuperkey, newsubkey) ⇒ Object
- #split_fields! ⇒ Object
Instance Method Details
#depunctuate! ⇒ Object
53 54 55 56 57 58 |
# 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 = {}) ⇒ Object
Run a block on every field in every entry
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bibtex_munge/bibliography.rb', line 26 def each_field( = {}) except = [: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! ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# 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! ⇒ Object
126 127 128 129 130 131 132 133 134 |
# 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! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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! ⇒ Object
117 118 119 120 121 122 123 124 |
# 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! ⇒ Object
108 109 110 111 112 113 114 115 |
# 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! ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
4 5 6 7 8 9 10 11 |
# 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) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# 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! ⇒ Object
99 100 101 102 103 104 105 106 |
# 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 |