forked from percyliang/refdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.rb
executable file
·195 lines (176 loc) · 4.82 KB
/
import.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/ruby
# Reads in a bib file and prints out Ruby entries.
# This script is a bit hacky, so double check the added entries before importing.
$: << '.'
require 'refdb'
# Figure out wher to write
usernamePath = File.expand_path(File.dirname(__FILE__)) + '/username'
if File.exists?(usernamePath)
username = IO.readlines(usernamePath)[0].strip
else
while true
print 'Enter username: '
username = gets.strip
break if username =~ /^\w+$/
puts 'Invalid username, please try again.'
end
out = open(usernamePath, 'w')
out.puts username
out.close
end
$stderr.puts 'Enter bibtex:'
# Build up a map to macros (so we can standardize names)
$venues = [] # list of [name, macros]
IO.foreach('data/venues.rb') { |line|
# Example: def focs(year); inproceedings(Name.new('Foundations of Computer Science (FOCS)', 'FOCS'), year) end
if line =~ /^def (\w+)\(year(, volume)?\);/
macroName, volume = $1, $2
args = eval(volume ? "#{macroName}(0, 0)" : "#{macroName}(0)")
args = args.flatten
e = Entry.new(args)
names = []
[e.getFirst('journal'), e.getFirst('booktitle')].compact.each { |n|
if n.is_a?(String)
names << n
elsif
names << n.to_s
names << n.to_short_s
end
}
names.each { |n|
$venues << [n, macroName]
}
end
}
# Sort by decreasing size so that we match 'ICML' before 'ML'
$venues.sort! { |a,b| b[0].size <=> a[0].size }
# Returns a list of Ruby strings ['entry!(...)', 'entry!(...)', ...]
def bibtexToRuby(bibtexLines)
def quote(s); "'" + s + "'" end
type = nil
id = nil
unprocessed = []
map = {}
output = []
flush = lambda {
# Normalize values
map.each { |key,value|
# Escape
value.gsub!('\\', '\\\\')
value.gsub!('\'', '\\\\\'')
if key == 'pages'
if value =~ /^(\d+)\s*--\s*(\d+)$/
value = $1 + ', ' + $2
elsif value =~ /^(\d+)\s*-\s*(\d+)$/
value = $1 + ', ' + $2
end
elsif value =~ /^(\d+)$/
elsif key == 'author'
# Normalize author names
value = quote(value.split(/ and /).map { |x|
x =~ /(.+), (.+)/ ? $2 + ' ' + $1 : x
}.join(' and '))
else
value = quote(value)
end
map[key] = value
}
lines = []
lines << "entry!('#{id}',"
# Call more succinct functions rather than just using key-value pairs
addItem = lambda { |func,fields|
lines << " #{func}(" + fields.map { |f| (map[f] || f.upcase) }.join(', ') + "),"
}
# Special case arXiv
usedMacro = false
if map['journal'] =~ /arXiv:(\d+\.\d+)/
map['arxivId'] = quote($1)
addItem.call('arxiv', ['year', 'arxivId'])
map.delete('journal')
map.delete('year')
map.delete('arxivId')
usedMacro = true
end
# Special case popular conferences and journals
if not usedMacro
$venues.each { |name,func|
if map['booktitle'] =~ /#{name}/
addItem.call(func, ['year'])
map.delete('booktitle')
map.delete('year')
usedMacro = true
break
elsif map['journal'] =~ /#{name}/
p name
addItem.call(func, ['year', 'volume'])
map.delete('journal')
map.delete('year')
map.delete('volume')
usedMacro = true
break
end
}
end
# Conferences and journals
if not usedMacro
if type == 'inproceedings'
addItem.call(type, ['booktitle', 'year'])
map.delete('booktitle')
map.delete('year')
elsif type == 'article'
addItem.call(type, ['journal', 'year', 'volume'])
map.delete('journal')
map.delete('year')
map.delete('volume')
end
end
map.each { |key,value|
lines << ' ' + key + '(' + value + '),'
}
unprocessed.each { |line|
lines << ' #' + line
}
lines << "nil)"
output << lines.join("\n")
# Reset
type = nil
id = nil
unprocessed = []
map = {}
}
bibtexLines.each { |line|
line = line.sub(/^\s*/, '').sub(/\s*$/, '')
next if line == ''
if line =~ /^@(\w+)\s*\{\s*([^,]+)/
type, id = $1, $2
elsif line =~ /^(\w+)\s*=\s*\{(.+)\}[^}]*/ # title = {...}
map[$1] = $2
elsif line =~ /^(\w+)\s*=\s*"(.+)"[^"]*/ # title = "..."
map[$1] = $2
elsif line =~ /^(\w+)\s*=\s*(\d+)/ # volume = 19
map[$1] = $2
elsif line =~ /^\}$/
flush.call
elsif line =~ /^%/
# Comment
else
p line
unprocessed << line
end
}
output
end
bibtexLines = []
while line = gets
bibtexLines << line
end
rubyLines = []
bibtexToRuby(bibtexLines).each { |ruby|
rubyLines << ''
rubyLines << ruby
}
outPath = 'data/' + username + '.rb'
puts "### Writing the following to #{outPath} ###"
out = open(outPath, 'a+')
rubyLines.each { |line| puts line; out.puts line }
out.close