-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbibtex-batch.rb
326 lines (277 loc) · 10.5 KB
/
bibtex-batch.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# encoding: UTF-8
$:.push(File.dirname($0))
require 'utility-functions'
require 'bibtex'
require 'citeproc'
require 'find'
require 'rss/maker'
require 'sanitize'
# batch processes entire bibliography file and generates ref:bibliography in wiki, used for refnotes database
log "Started bibtex-batch on #{Time.now}"
# trigger generating different categories of pages - takes a long time
authoropt = true
journalopt = true
keywordopt = true
#######################################################################
try { dt = app('BibDesk') }
try { dt.document.save }
def sort_pubs(pubs)
return pubs.sort {|x,y| x.to_s.scan(/[0-9]+/)[0].to_i <=> y.to_s.scan(/[0-9]+/)[0].to_i}
end
def make_rss_feed
fname = Wiki_path + "/rss-temp"
rss_entries = Marshal::load(File.read(fname))
version = "2.0" # ["0.9", "1.0", "2.0"]
urls = Array.new
content = RSS::Maker.make(version) do |m|
m.channel.title = Wiki_title
m.channel.link = Internet_path
m.channel.description = Wiki_desc
m.items.do_sort = true # sort items by date
rss_entries.each do |entry|
next if urls.index(entry[:link]) # avoid duplicate content (should not be possible, but just in case)
urls << entry[:link]
i = m.items.new_item
i.title = entry[:title]
puts "--#{i.title}"
i.link = entry[:link]
i.date = entry[:date]
i.description = Sanitize.clean( entry[:description], Sanitize::Config::RELAXED )
end
end
# make sure all links point to the online version, not the localhost
feedcontent = content.to_s.gsub(Internet_path, Server_path)
File.write(Wiki_path + "/feed.xml", feedcontent)
end
timetot = Time.now
timetmp = Time.now
if defined?(Make_RSS) && Make_RSS
puts "Making RSS feed"
make_rss_feed
puts "RSS feed complete (will be updated next time you sync with server) (#{Time.now - timetmp} s.)"
end
timetmp = Time.now
puts "Parsing BibTeX"
b = BibTeX.parse(File.read(Bibliography))
b.parse_names
puts "Initial parse complete (#{Time.now - timetmp} s.)"
timetmp = Time.now
out1 = ''
out2 = ''
out3 = ''
out4 = ''
authors = Hash.new
json = Hash.new
keywords = Hash.new
journals = Hash.new
counter = Hash.new
counter[:hasref] = 0
counter[:noref] = 0
counter[:notes] = 0
counter[:clippings] = 0
counter[:images] = 0
counter[:pdf] = 0
puts "Starting secondary parse"
b.each do |item|
next unless try { item.title } # fragment, doesn't need its own listing
ax = []
if (item.respond_to? :author) && (item.author != nil)
item.author.each do |a|
next if item.author == nil #thanks Bodong
authors[nice_name(a)] = Array.new unless authors[nice_name(a)]
ax << a.last.gsub(/[\{\}]/,"")
authors[nice_name(a)] << item.key
end
end
if item.respond_to? :keywords
item.keywords.split(";").each do |a|
a.strip!
keywords[a] = Array.new unless keywords[a]
keywords[a] << item.key
end
end
if item.respond_to? :journal
j = item[:journal].to_s
journals[j] = Array.new unless journals[j]
journals[j] << item.key
end
cit = CiteProc.process item.to_citeproc, :style => :apa
cit.gsub!(/Retrieved from(.+?)$/, '')
item[:cit] = cit
year = (defined? item.year) ? item.year.to_s : "n.d."
if year == "n.d." and cit.match(/\((....)\)/)
year = $1
end
json[item.key.to_s] = [namify(ax), year, cit, item.title, item[:"oa-url"].to_s]
hasfiles = Array.new
hasfiles[2] = '' # ensure that array is filled even if some fields are empty, for alignment
if File.exists?("#{Wiki_path}/data/pages/ref/#{item.key}.txt")
counter[:hasref] += 1
if File.exists?("#{Wiki_path}/data/pages/clip/#{item.key}.txt") || File.exists?("#{Wiki_path}/data/pages/kindle/#{item.key}.txt")
counter[:clippings] += 1
hasfiles[1] = "C"
end
if File.exists?("#{Wiki_path}/data/pages/skimg/#{item.key}.txt")
counter[:images] += 1
hasfiles[2] = "I"
end
if item.respond_to? :"oa-url"
pdfurl = "[[#{item[:"oa-url"]}|{{wiki:filetype_pdf.png}}]]"
counter[:pdf] += 1
else
pdfurl = ''
end
if File.exists?("#{Wiki_path}/data/pages/notes/#{item.key}.txt")
counter[:notes] += 1
hasfiles[0] = "N"
end
txt = "| [#:ref:#{item.key}|#{item.key}] #{pdfurl}| #{hasfiles.join(" | ")} |#{cit}|\n"
if hasfiles[0] == "N"
out1 << txt
elsif hasfiles[1] == "C"
out2 << txt
end
end
# mark as read if notes exist
# if File.exists?("#{Wiki_path}/data/pages/clip/#{item.key}.txt") || File.exists?("#{Wiki_path}/data/pages/kindle/#{item.key}.txt")
# dt.document.search({:for =>item.key.to_s})[0].fields["Read"].value.set("1")
# end
end
puts "Finished secondary parse, generating main bibliography (#{Time.now - timetmp} s.)"
timetmp = Time.now
out = "h1. Bibliography\n\nDownload [[http://dl.dropbox.com/u/1341682/Bibliography.bib|entire BibTeX file]].
Also see bibliography by [[abib:start|author]] or by [[kbib:start|keyword]].\n\nPublications that have their
own pages are listed on top, and hyperlinked. Most of these also have clippings and many have key ideas.\n\n
Statistics: Totally **#{counter[:hasref] + counter[:noref]}** publications, and **#{counter[:hasref]}**
publications have their own wikipages. Of these, **#{counter[:images]}** with notes (key ideas) **(N)**,
**#{counter[:clippings]}** with highlights (imported from Kindle or Skim) **(C)**, and **#{counter[:images]}**
with images (imported from Skim) **(I)**. **#{counter[:pdf]}** publications are OA, and available for immediate download.\n\n"
#dt.document.save
File.open("#{Wiki_path}/lib/plugins/dokuresearchr/json.tmp","w"){|f| f << JSON.fast_generate(json)}
out << out1 << out2 << out3
File.open("#{Wiki_path}/data/pages/bib/bibliography.txt", 'w') {|f| f << out}
###############################################
# generate individual files for each author
if authoropt
puts "Generating individual files for each author"
authorlisted = Array.new
authors.each do |axx, pubs|
out =''
out1 = ''
out2 =''
author = axx.strip
# only generates individual author pages for authors with full names. this is because I want to deduplicate author names
# when you import bibtex, you get many different spellings etc.
next if (author.strip[-1] == "." || author[-2] == " " || author[-2] == author[-2].upcase || author[1] == '.')
out = "h2. #{author}'s publications\n\n"
sort_pubs(pubs).each do |i|
item = b[i]
if File.exists?("#{Wiki_path}/data/pages/ref/#{item.key}.txt")
out1 << "| [@#{item.key}] | #{item[:cit]}|\n"
else
out2 << "| [@#{item.key}] | #{item[:cit]}|\n"
end
end
out << out1 << out2
authorname = clean_pagename(author)
authorlisted << [authorname,author,pubs.size]
File.open("#{Wiki_path}/data/pages/abib/#{authorname}.txt", 'w') {|f| f << out}
end
File.open("#{Wiki_path}/data/pages/abib/start.txt","w") do |f|
f << "h1.List of authors with publications\n\nList of authors with publications. Only includes authors with three or more publications, with full names.\n\n"
authorlisted.sort {|x,y| y[2].to_i <=> x[2].to_i}.each do |ax|
apage = ''
if File.exists?("#{Wiki_path}/data/pages/a/#{ax[0]}.txt")
apage = "[#:a:#{ax[0]}|author page]"
end
f << "| [##{ax[0]}|#{ax[1]}] | #{apage} |#{ax[2]}|\n"
end
end
end
###############################################
# generate individual files for each keyword
puts "Finished (#{Time.now - timetmp} s.)"
if keywordopt
timetmp = Time.now
puts "Generating individual files for each keyword"
keywordslisted = Array.new
keywords.each do |keyword, pubs|
out =''
out1 = ''
out2 =''
out = "h2. Publications with keyword \"#{keyword}\"\n\n"
sort_pubs(pubs).each do |i|
item = b[i]
if File.exists?("#{Wiki_path}/data/pages/ref/#{item.key}.txt")
out1 << "| [@#{item.key}] | #{item[:cit]}|\n"
else
out2 << "| [@#{item.key}] | #{item[:cit]}|\n"
end
end
out << out1 << out2
kwname = keyword.gsub(/[\,\.\/ ]/,"_").downcase
keywordslisted << [kwname,keyword,pubs.size]
File.open("#{Wiki_path}/data/pages/kbib/#{kwname}.txt", 'w') {|f| f << out}
end
File.open("#{Wiki_path}/data/pages/kbib/start.txt","w") do |f|
f << "h1. List of publication keywords\n\n"
keywordslisted.sort {|x,y| y[2].to_i <=> x[2].to_i}.each do |ax|
f << "|[##{ax[0]}|#{ax[1]}]|#{ax[2]}|\n"
end
end
puts "Finished (#{Time.now - timetmp} s.)"
end
###############################################
# generate individual files for each journal with more than five cits.
if journalopt
timetmp = Time.now
puts "Generating individual files for each journal"
authorlisted = Array.new
journals.each do |axx, pubs|
out =''
out1 = ''
out2 =''
author = axx.strip
next unless pubs.size > 5
# only generates individual author pages for authors with full names. this is because I want to deduplicate author names
# when you import bibtex, you get many different spellings etc.
out = "h2. Publications in #{author}\n\n"
sort_pubs(pubs).each do |i|
item = b[i]
if File.exists?("#{Wiki_path}/data/pages/ref/#{item.key}.txt")
out1 << "| [@#{item.key}] | #{item[:cit]}|\n"
else
out2 << "| [@#{item.key}] | #{item[:cit]}|\n"
end
end
out << out1 << out2
authorname = clean_pagename(author)
authorlisted << [authorname,author,pubs.size]
File.open("#{Wiki_path}/data/pages/jbib/#{authorname}.txt", 'w') {|f| f << out}
end
end
File.open("#{Wiki_path}/data/pages/jbib/start.txt","w") do |f|
f << "h1.List of journals with publications\n\nList of journals with publications. Only includes journals with five or more publications.\n\n"
authorlisted.sort {|x,y| y[2].to_i <=> x[2].to_i}.each do |ax|
apage = ''
if File.exists?("#{Wiki_path}/data/pages/a/#{ax[0]}.txt")
apage = "[#:a:#{ax[0]}|author page]"
end
f << "| [##{ax[0]}|#{ax[1]}] | #{apage} |#{ax[2]}|\n"
end
puts "Finished (#{Time.now - timetmp} s.)"
end
###############################################
# generate file with imported citations missing key ideas
# pages = Array.new
# out = "h1.Needs key ideas\n\nList of publications with clippings, which do not have key ideas.\n\n"
#
# Find.find("#{Wiki_path}/data/pages/ref") do |path|
# next unless File.file?(path)
# fn = File.basename(path)
# if (File.exists?("#{Wiki_path}/data/pages/kindle/#{fn}") || File.exists?("#{Wiki_path}/data/pages/clip/#{fn}")) && !File.exists?("#{Wiki_path}/data/pages/notes/#{fn}")
# out << " * [@#{fn[0..-5]}]\n"
# end
# end
# File.open("#{Wiki_path}/data/pages/bib/needs_key_ideas.txt","w") {|f| f << out}
puts "All tasks finished. Total time #{Time.now - timetot} s."