forked from percyliang/refdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-bib.rb
executable file
·50 lines (45 loc) · 1.2 KB
/
migrate-bib.rb
1
2
3
4
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
#!/usr/bin/ruby
# Suppose you have done an import of your old bib file (./import.rb < old.bib)
# and fixed up data/entries.rb to your heart's content. Then run this script
# (./migrate-bib.rb old.bib all.bib) to generate a set of shell commands which
# you should run to update the latex.
if ARGV.size != 2
puts 'Usage: <old bib file> <new bib file>'
puts 'Matches IDs between old and new based on title'
puts 'Output: sed commands to run to replace old citations with new ones.'
exit 1
end
old_bib, new_bib = ARGV
def read_mapping(path)
map = {}
id = nil
title = nil
IO.foreach(path) { |line|
if line =~ /@\w+{([^,]+)/
id = $1
elsif line =~ /\btitle[= ]+{(.+)}/
title = $1
elsif line =~ /\btitle[= ]+"(.+)"/
title = $1
end
if title
map[id] = title.downcase.gsub(/[^a-z]/, '')
#p [id, map[id]]
id = title = nil
end
}
map
end
old_map = read_mapping(old_bib)
new_map = read_mapping(new_bib)
inverse_new_map = {}
new_map.each { |k,v| inverse_new_map[v] = k }
old_map.each { |k,v|
l = inverse_new_map[v]
if not l
$stderr.puts "No entry in #{new_bib}: #{k} => #{v}"
next
end
next if k == l
puts "sed -ie 's/#{k}/#{l}/g' *.tex"
}