-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbib2xml.pl
More file actions
262 lines (224 loc) · 8.39 KB
/
bib2xml.pl
File metadata and controls
262 lines (224 loc) · 8.39 KB
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
#!/usr/bin/env perl
# Convert BibTeX files to Publist's XML format (https://github.com/eitanf/publist)
#
# This script tries to parse BibTeX file(s) (provided as command line
# parameters) and produce one file in XML format (on standard output)
# that conforms to Publist's XML input format.
# This script will complain about anything it doesn't understand, but will
# continue to output unparsed lines in HTML comment format, to be edited
# in-place
# Output is left at the same order as the input.
# Note - if your BibTeX depends on additional files (e.g., macros), make sure
# to include them first in your input order. This script will try to deal
# with @Strings and will also try to convert months to the proper numeric value.
#
# Limitations:
#
# - This script cannot tell the difference between conferences and
# workshops or journal and periodical (Publist makes this distinction). If you
# want to make that distinction yourself, go over the output file and change
# the type as appropriate.
# - You may want to edit notes, area, and subarea manually.
# BibTeX Doesn't know posters and talks - these again have to be added manually.
# - There's an attempt to translate URLs correctly, but it's not too general.
# - A bug in Text::BibTex might result in a "parse_ok" error message. In that
# case, some entries, the closing "</pubDB>" may not appear in the output
# - There seems to be a bug in Text::BibTex that causes it to fail on the
# second entry of a .bib file. If this script complains that it cannot
# convert the second bibtex entry, try inserting a dummy entry in your .bib
# file, right after the first one.
#
# Requires package Text::BibTeX
# (http://search.cpan.org/~gward/Text-BibTeX-0.34/BibTeX.pm)
#
# Copyright 2005--2025 by Eitan Frachtenberg (publist@frachtenber.org) -- BETA status --
# This program may be freely distributed under the terms of the GNU Public License
###############################################################################
use Text::BibTeX qw(:macrosubs);
## Globals:
%Months = ( 'jan' => 1, 'jan.' => 1, 'january' => 1,
'feb' => 2, 'feb.' => 2, 'february' => 2,
'mar' => 3, 'mar.' => 3, 'march' => 3,
'apr' => 4, 'apr.' => 4, 'april' => 4,
'may' => 5, 'may.' => 5, 'may' => 5,
'jun' => 6, 'jun.' => 6, 'june' => 6,
'jul' => 7, 'jul.' => 7, 'july' => 7,
'aug' => 8, 'aug.' => 8, 'august' => 8,
'sep' => 9, 'sep.' => 9, 'september' => 9,
'oct' => 10, 'oct.' => 10, 'october' => 10,
'nov' => 11, 'nov.' => 11, 'november' => 11,
'dec' => 12, 'dec.' => 12, 'december' => 12
);
%Types = ( 'inproceedings' => 'conference',
'article' => 'journal',
'inbook' => 'chapter',
'book' => 'book',
'techreport' => 'report',
'phdthesis' => 'thesis',
'mastersthesis' => 'thesis'
);
$tab = 0;
$spacing = 2; # Indentation level of XML
#################################################################
############################# Routines ##########################
#################################################################
# clean: remove quotes, braces, and runs of spaces from a string
# Tries to handle some special HTML characters.
# Also tries to handle URLs nicely, and to remove "Proceedings of.."
sub clean {
@ret = ();
foreach (@_) {
s/\\\&/\&/g; # Ampersands
s/\\w*{(.*?)}/$1/g; # BibTeX braces and formatting
s/[ ]+/ /g; # Runs of spaces
s/Proceedings of the //gi; # Proceedings
s/Proceedings of //gi;
s/Proc\. //gi;
s/\\\"(.)/\&$1uml\;/g; # Translate umlaut
s/\\\'(.)/\&$1acute\;/g; # Translate acute
s/\\\`(.)/\&$1grave\;/g; # Translate grave
s/\</\</g; # <
s/\>/\>/g; # >
s/(?<!\/)\~/ /g; # ~, but not after /
s/\\url\{(.*?)\}/[[a href="$1"]]$1\[[\/a]]/g; # URLs
if (/href=/) {
if (!/http:\/\// && !/ftp:\/\//) {
s/href=\"/href="http:\/\//g;
}
}
s/[\{\}]//g; # Just braces
push @ret, $_;
}
return join (' ', @ret);
}
#################################################################
# open_entry: start a publication, tab to the right
sub open_entry {
my $entry = shift;
print ' 'x$tab . "<publication>\n";
$tab += $spacing;
print ' 'x$tab . "<key>" . $entry->key . "</key>\n";
}
#################################################################
# close_entry: end a publication, tab to the left
sub close_entry {
print ' 'x$tab . "<area></area>\n";
print ' 'x$tab . "<subarea></subarea>\n";
print ' 'x$tab . "<invited>false</invited>\n";
print ' 'x$tab . "<islocal>true</islocal>\n";
$tab -= $spacing;
print ' 'x$tab . "</publication>\n\n";
}
#################################################################
# get_names: format and list of names to an output string
# Receives entry and field name
sub get_names {
my $entry = shift;
my @names = $entry->names(shift);
my $first = 1;
my $ret = "";
foreach $name (@names) {
if ($first) { $first = 0; } else { $ret .= " and "; }
my $v = &clean ($name->part('von'));
$ret .= $v . " " if ($v ne "");
$ret .= &clean ($name->part('last'));
my $j = &clean ($name->part('jr'));
$ret .= (" " . $j) if ($j ne "");
my $f = &clean ($name->part('first'));
$ret .= (", " . $f) if ($f ne "");
}
return $ret;
}
#################################################################
# print_fields: outputs all of bibtex's fields in XML format,
# except it translates key, journal, note, and type.
# Has special cases for thesis, author, and editor
sub print_fields {
my $entry = shift;
my @fields = $entry->fieldlist();
my $ftype, $fval;
foreach $field (@fields) {
$ftype = $field;
$fval = &clean($entry->get($field));
if ($field eq 'key') {
$ftype = 'bibtex-key';
}
elsif ($field eq 'type') {
$ftype = 'bibtex-type';
}
elsif ($field eq 'journal') {
$ftype = 'booktitle';
}
elsif ($field eq 'author') {
$fval = &get_names ($entry, 'author');
}
elsif ($field eq 'editor') {
$fval = &get_names ($entry, 'editor');
}
print ' 'x$tab . "<$ftype>$fval</$ftype>\n";
}
# Handle theses:
if ($entry->type eq "phdthesis") {
print ' 'x$tab . "<booktitle>";
print "Ph.D. dissertation, " . $entry->get('school');
print "</booktitle>\n";
}
elsif ($entry->type eq "mastersthesis") {
print ' 'x$tab . "<booktitle>";
print "Master's thesis, " . $entry->get('school');
print "</booktitle>\n";
}
}
#################################################################
# print_type: print the type of the entry in XML format
# Assumes type has already been validated to be one of Publist's
# supported types.
sub print_type {
my $entry = shift;
print ' 'x$tab . "<type>";
print $Types{lc $entry->type};
print "</type>\n";
}
#################################################################
# output_entry: print out all entry details
sub output_entry {
my $entry = shift;
open_entry ($entry);
print_type ($entry);
print_fields ($entry);
close_entry ($entry);
}
#################################################################
# output_unknown: dump the bibtext entry to the output as comment
# It will also clean the entry and convert '--' to '-'
sub output_unknown {
my $entry = shift;
print "<!-- I cannot parse the following BibTeX entry:\n";
my $str = &clean ($entry->print_s());
$str =~ s/--/-/g;
print ($str);
print "-->\n\n";
}
#################################################################
sub toxml {
my $entry = shift;
if ($entry->type eq "string") {
return; # Ignore strings
} elsif (($entry->type eq "inproceedings")
|| ($entry->type eq "article")
|| ($entry->type eq "inbook")
|| ($entry->type eq "book")
|| ($entry->type eq "techreport")
|| ($entry->type eq "phdthesis")
|| ($entry->type eq "mastersthesis")) {
output_entry ($entry);
} else {
output_unknown ($entry);
}
}
########################################## MAIN ###############################
&add_macro_text ($macro, $value)
while (($macro, $value) = each %Months);
print "<pubDB>\n\n";
&Text::BibTeX::bibloop (sub {&toxml(shift);}, \@ARGV);
print "\n\n</pubDB>\n";