-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitalife_bst.rb
678 lines (621 loc) · 13.8 KB
/
mitalife_bst.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# coding: utf-8
#
# mitalife_bst.rb - RubyBTeX style file for Artificial Life Journal
#
# Copyright (c) 2014 Kazuto Tominaga
#
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
require_relative 'rubybsup.rb'
$entrytypes = [ 'article', 'book', 'booklet', 'conference',
'inbook', 'incollection', 'inproceedings', 'manual', 'mastersthesis',
'misc', 'phdthesis', 'proceedings', 'techreport', 'unpublished' ]
$styles['article'] = "(:[:author_or_journal:]:) (:([:year:]):).\n" +
"(:\\newblock [:c_title*:].\n:)" +
"(:\\newblock [:articleoptions*:].\n:)" +
"(:\\newblock [:note*:].\n:)"
$styles['book'] = "(:[:author_editor:]:) (:([:year:]):).\n" +
"\\newblock (:\\emph{[:c_title:]}:)(:[:bookoptions*:]:).\n" +
"\\newblock (:[:address*:]: :)(:[:publisher:]:).\n" +
"(:\\newblock [:note*:].\n:)"
$styles['booklet'] = "(:[:author_or_title:]:)(: ([:year*:]):).\n" +
"(:\\newblock \\emph{[:title_ifauthor*:]}.\n:)" +
"(:\\newblock [:bookletoptions*:].\n:)" +
"(:\\newblock [:note*:].\n:)"
$styles['inbook'] = "(:[:author_editor:]:) (:([:year:]):).\n" +
"\\newblock (:\\emph{[:c_title:]}:)(:[:vnseries_ifvn*:]:)" +
"(: ([:chapter_pp_pages:]):)(:. \\emph{[:series_ifnovn*:]}:).\n" +
"\\newblock (:[:address*:]: :)(:[:publisher:]:).\n" +
"(:\\newblock [:note*:].\n:)"
$styles['incollection'] = "(:[:author:]:) (:([:year:]):).\n" +
"\\newblock (:[:c_title:]:).\n" +
"\\newblock In (:[:editor*:], :)(:\\emph{[:booktitle:]}:)(:[:bookoptions*:]:)" +
"(: ([:chapter_pp_pages*:]):).\n" +
"\\newblock (:[:address*:]: :)(:[:publisher:]:).\n"
$styles['inproceedings'] = "(:[:author:]:) (:([:year:]):).\n" +
"\\newblock (:[:c_title:]:).\n" +
"\\newblock In (:[:editor*:], :)(:\\emph{[:booktitle:]}:)(:[:bookoptions*:]:)" +
"(: ([:volume_pp_pages*:]):).\n" +
"(:\\newblock [:organization_address_publisher*:].\n:)" +
"(:\\newblock [:note*:].\n:)"
$styles['manual'] = "(:[:author_or_title:]:)(: ([:year*:]):).\n" +
"(:\\newblock \\emph{[:title_ifauthor*:]}.\n:)" +
"(:\\newblock [:manualoptions*:].\n:)" +
"(:\\newblock [:note*:].\n:)"
$styles['mastersthesis'] = "(:[:author:]:) (:([:year:]):).\n" +
"\\newblock (:[:c_title:]:).\n" +
"\\newblock (:[:mastersthesistype:]:), (:[:address*:]: :)(:[:school:]:).\n" +
"(:\\newblock [:note*:].\n:)"
$styles['misc'] = "(:[:author_or_title*:]:)(: ([:year*:]):)(:[:period_ifaty*:]:)\n" +
"(:\\newblock \\emph{[:title_ifauthor*:]}.\n:)" +
"(:\\newblock [:howpublished*:].\n:)" +
"(:\\newblock [:note*:].\n:)"
$styles['phdthesis'] = "(:[:author:]:) (:([:year:]):).\n" +
"\\newblock (:\\emph{[:c_title:]}:).\n" +
"\\newblock (:[:phdthesistype:]:), (:[:address*:]: :)(:[:school:]:).\n" +
"(:\\newblock [:note*:].:)"
$styles['proceedings'] = "(:[:editor_or_title:]:) (:([:year:]):).\n" +
"(:\\newblock \\emph{[:title_ifeditor*:]}.\n:)" +
"(:\\newblock [:proceedingsoptions:].\n:)" +
"(:\\newblock [:organization_address_publisher*:].\n:)" +
"(:\\newblock [:note*:].\n:)"
$styles['techreport'] = "(:[:author:]:) (:([:year:]):).\n" +
"\\newblock (:\\emph{[:c_title:]}:) ((:[:techreporttype:]:)(: [:number*:]:)).\n" +
"(:\\newblock [:institution:].\n:)"
$styles['unpublished'] = "(:[:author:]:)(: ([:year*:]):).\n" +
"\\newblock (:[:c_title:]:).\n" +
"\\newblock (:[:note:]:).\n"
# XXX: cross reference entries
$styles['inbook_xref'] = "(:[:author_xref:]:) (:([:year_xref:]):).\n" +
"\\newblock (:[:title:]:)(: ([:chapter_pages:]):).\n" +
"\\newblock In (:[:xrefcite:]:).\n"
$author_namestyle = '(:[:vv||~:]~:)(:[:ll||~:]:)(: [:jj||~:]:)(:, [:f|.| :]:)'
$author_andspecs = [ ', \& ', ', \& ' ]
$editor_namestyle = '(:[:f|.| :] :)(:[:vv||~:]~:)(:[:ll||~:]:)(: [:jj||~:]:)'
$editor_andspecs = [ ', \& ', ', \& ' ]
$sort_namestyle = '(:[:vv||~:]~:)(:[:ll||~:]:)(: [:ff||~:]:)'
$sort_andspecs = [ ' ', ' ' ]
# Samples
# $author_namestyle = '(:[:ff|| :] :)(:[:vv||~:]~:)(:[:ll||~:]:)(:, [:jj||~:]:)'
# $author_andspecs = [ ' and ', ', and ' ]
class Formatter
include NameFormatter
include MonthFormatter
include MiscFormatter
def f_author
a = $db[$cite]['author']
if a.nil?
return nil
else
names, numauthor = format_names(a, $author_namestyle, $author_andspecs)
return names
end
end
def f_author_xref
s = f_author
if s
return s
end
xrefcite = $db[$cite]['crossref']
if $db[xrefcite]['author']
names, num = format_names($db[xrefcite]['author'], $author_namestyle, $author_andspecs)
return names
else
$log.puts "Required field missing: no author for #{$cite} including the cited entry #{xrefcite}"
return nil
end
end
def f_editor
e = $db[$cite]['editor']
if e
names, numeditor = format_names(e, $editor_namestyle, $editor_andspecs)
if numeditor == 1
return names + ' (Ed.)'
else
return names + ' (Eds.)'
end
else
return nil
end
end
def f_author_editor
a = $db[$cite]['author']
e = $db[$cite]['editor']
if a and e
$log.puts "Redundant fields: author and editor for #{$cite}; taking author"
end
if a
names, num = format_names(a, $author_namestyle, $author_andspecs)
return names
elsif e
names, num = format_names(e, $editor_namestyle, $editor_andspecs)
if num == 1
return names + ' (Ed.)'
else
return names + ' (Eds.)'
end
else
return nil
end
end
def f_author_or_title
a = $db[$cite]['author']
t = $db[$cite]['title']
if a
names, num = format_names(a, $author_namestyle, $author_andspecs)
return names
elsif t
return "\\emph{#{t}}"
else
return nil
end
end
def f_author_or_journal
a = $db[$cite]['author']
j = $db[$cite]['journal']
if a
names, num = format_names(a, $author_namestyle, $author_andspecs)
return names
elsif j
return "\\emph{#{j}}"
else
return nil
end
end
def f_editor_or_title
e = $db[$cite]['editor']
t = $db[$cite]['title']
if e
names, num = format_names(e, $editor_namestyle, $editor_andspecs)
if num == 1
return names + ' (Ed.)'
else
return names + ' (Eds.)'
end
elsif t
return "\\emph{#{t}}"
else
return nil
end
end
def f_title
$db[$cite]['title']
end
def f_c_title
t = $db[$cite]['title']
if t
capitalize_toplevel(t)
else
nil
end
end
def f_booktitle
capitalize_toplevel($db[$cite]['booktitle'])
end
def f_year
$db[$cite]['year']
end
def f_year_xref
s = f_year
if s
return s
end
xrefcite = $db[$cite]['crossref']
return $db[xrefcite]['year']
end
def f_journal
$db[$cite]['journal']
end
def f_volume
$db[$cite]['volume']
end
def f_number
$db[$cite]['number']
end
def f_pages
$db[$cite]['pages']
end
def f_volume_number_pages
v = $db[$cite]['volume']
n = $db[$cite]['number']
p = $db[$cite]['pages']
s = ''
if v
s += "\\emph{#{v}}"
end
if n
s += "(#{n})"
end
if p
if s == ''
s = p
else
s += ", #{p}"
end
end
if s == ''
nil
else
s
end
end
def f_pp_pages
pages = $db[$cite]['pages']
if pages.nil?
return nil
elsif /[-+]/ =~ pages
return 'pp.~' + pages
else
return 'p.~' + pages
end
end
def f_chapter_pp_pages
c = $db[$cite]['chapter']
p = $db[$cite]['pages']
t = $db[$cite]['type']
s = ''
if c
if t
s = "#{lowercase_toplevel(t)} #{c}"
else
s = "Chap.~#{c}"
end
end
if p
if /[-+]/ =~ p
if c
pp = p
else
pp = 'pp.~' + p
end
else
pp = 'p.~' + p
end
end
if p
if c
s += ", " + pp
else
s = pp
end
end
if s == ''
return nil
else
return s
end
end
def f_volume_pp_pages
v = $db[$cite]['volume']
p = $db[$cite]['pages']
s = ''
if v
s = "Vol.~#{v}"
end
if p
if /[-+]/ =~ p
pp = 'pp.~' + p
else
pp = 'p.~' + p
end
end
if p
if v
s += ", " + pp
else
s = pp
end
end
if s == ''
nil
else
s
end
end
def f_chapter_pages
c = $db[$cite]['chapter']
p = $db[$cite]['pages']
if p
if /[-+]/ =~ p
pp = 'pp.~' + p
else
pp = 'p.~' + p
end
end
if c and p
return 'chapter ' + c + ', ' + pp
elsif c
return 'chapter ' + c
elsif p
return pp
else
return nil
end
end
def f_address
$db[$cite]['address']
end
def f_publisher
$db[$cite]['publisher']
end
def f_howpublished
$db[$cite]['howpublished']
end
def f_institution
$db[$cite]['institution']
end
def f_school
$db[$cite]['school']
end
def f_bookoptions
n = $db[$cite]['number']
s = $db[$cite]['series']
e = $db[$cite]['edition']
t = ''
if s
t = ", \\emph{#{s}}"
end
if n
if t == ''
t = n
else
t += ", \\emph{#{n}}"
end
end
if e
if t == ''
t = " (#{e} ed.)"
else
t += " (#{e} ed.)"
end
end
# $stderr.puts "debug: f_bookoptions returning #{t}"
if t != ''
"#{t}"
else
nil
end
end
def f_proceedingsoptions
v = $db[$cite]['volume']
n = $db[$cite]['number']
s = $db[$cite]['series']
t = nil
if v and n
$log.puts "Warning: volume and number exist for #{$cite}; taking volume"
t = "volume #{v}"
elsif v
t = "volume #{v}"
elsif n
t = "number #{n}"
else
t = nil
end
if s
if t
if v
t += " of \\emph{#{s}}"
else
t += " in \\emph{#{s}}"
end
else
t = "\\emph{#{s}}"
end
end
s
end
def f_articleoptions
a = $db[$cite]['author']
j = $db[$cite]['journal']
v = $db[$cite]['volume']
n = $db[$cite]['number']
p = $db[$cite]['pages']
vnp = f_volume_number_pages
s = ''
if a
s += "\\emph{#{j}}"
if vnp
s += ", " + vnp
end
else
# no author; the entire journal is specified.
# This case, the journal appears at the top;
# we do not need to place it here.
if vnp
s += vnp
end
end
if s == ''
nil
else
s
end
end
def f_period_ifaty
if $db[$cite]['author'] or $db[$cite]['title'] or $db[$cite]['year']
'.'
else
nil
end
end
def f_vnseries_ifvn
if $db[$cite]['volume'] or $db[$cite]['number']
f_bookoptions
else
nil
end
end
def f_series_ifnovn
if $db[$cite]['volume'] or $db[$cite]['number']
nil
else
$db[$cite]['series']
end
end
def f_title_ifauthor
return $db[$cite]['title'] if $db[$cite]['author']
end
def f_journal_ifauthor
return $db[$cite]['journal'] if $db[$cite]['author']
end
def f_title_ifeditor
return $db[$cite]['title'] if $db[$cite]['editor']
end
def f_bookletoptions
a = $db[$cite]['address']
h = $db[$cite]['howpublished']
s = ''
if a
s += "#{a}: "
end
if h
s += "#{h}"
end
if s != ''
return s
else
return nil
end
end
def f_manualoptions
s = ''
o = $db[$cite]['organization']
a = $db[$cite]['address']
e = $db[$cite]['edition']
if e
s += "#{capitalize_toplevel(e)} edition."
if a or o
s += " "
end
end
if a and o
s += "#{a}: #{o}."
elsif a
s += "#{a}."
elsif o
s += "#{o}."
end
if s == ''
nil
else
s
end
end
def f_organization_address_publisher
o = $db[$cite]['organization']
a = $db[$cite]['address']
p = $db[$cite]['publisher']
s = ''
if o
s += o
end
if a
if o
s += ", #{a}"
else
s = a
end
end
if p
if a
s += ": " + p
elsif o
s += ", " + p
else
s = p
end
end
if s == ''
return nil
else
return s
end
end
def f_phdthesistype
if $db[$cite]['type']
return $db[$cite]['type']
else
return 'PhD thesis'
end
end
def f_mastersthesistype
if $db[$cite]['type']
return $db[$cite]['type']
else
return 'Masters thesis'
end
end
def f_techreporttype
if $db[$cite]['type']
return $db[$cite]['type']
else
return 'Technical report'
end
end
def f_note
$db[$cite]['note']
end
def f_xrefcite
xref = $db[$cite]['crossref']
if xref
return "\\cite{#{xref}}"
else
$stderr.puts "Cross reference is missing: from #{$cite}"
return nil
end
end
#
# interface
#
def widestlabel
# $stderr.puts "debug: widestlabel: $cites.size is #{$cites.size}"
if $cites.size <= 0
$log.puts "Only #{$cites.size} citations found"
"9"
else
(10**(Math.log10($cites.size).to_i+1)-1).to_s # e.g., 21 -> 99
end
end
def sort
$cites.sort! do |a, b|
if $db[a].nil? and $db[b].nil?
0
elsif $db[a].nil?
-1
elsif $db[b].nil?
1
else
aauthor = $db[a]['author']
bauthor = $db[b]['author']
aeditor = $db[a]['editor']
beditor = $db[b]['editor']
akey = $db[a]['key']
bkey = $db[b]['key']
# XXX: cross references ignored
if aauthor
namesa, numa = format_names(aauthor, $sort_namestyle, $sort_andspecs)
elsif aeditor
namesa, numa = format_names(aeditor, $sort_namestyle, $sort_andspecs)
elsif akey
namesa = akey
else
namesa = 'zzz' # XXX: cannot figure out; put it to the end
end
if bauthor
namesb, numb = format_names(bauthor, $sort_namestyle, $sort_andspecs)
elsif beditor
namesb, numb = format_names(beditor, $sort_namestyle, $sort_andspecs)
elsif bkey
namesb = bkey
else
namesb = 'zzz' # XXX: cannot figure out; put it to the end
end
namesa = namesa.gsub(/~/, ' ').gsub(/[^\w. -]/){|c| ''}.downcase
namesb = namesb.gsub(/~/, ' ').gsub(/[^\w. -]/){|c| ''}.downcase
# $stderr.puts "debug: sort #{namesa} <=> #{namesb}"
namesa <=> namesb
end
end
end
end
# EOF