INSPEC/BibTeX.pm untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 252 CC 23 SCAR 52.2
CC
23
Cov
0%
Inspec_to_BibTeX
342.0
authors_inspec_to_bibtex
20.0
swap_jr
12.0
CRAP 185.0
1 package INSPEC::BibTeX;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
5
6 $VERSION = '0.01';
7
8 require 5.000;
9
10 use Exporter;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw();
14 @EXPORT_OK = qw(
15
16 );
17
18
19 #
20 # Inspec BibTex.pm Routines to translate inspec citations
21 # to bibtex format.
22 #
23 sub inspec_su
24 {
25 # recursively cleans out /sup / and /sup / markings
26 # returned from INSPEC
27 #
28 # 0 text to clean
29 # 1 sub or sup
30 # 2
31 #
32 my($string,$su,$rep) = @_;
33
34 my ($start_i,$end_i,$text,$pre,$post);
35 my $start = "/".$su." ";
36 my $end = "/";
37 my $count = 0;
38 my $new_start = "{\$".$rep."{";
39 my $new_end = "}\$}";
40 while ($string =~ /\/$su /g) { $count++ };
41 for(my $i=1;$i<=$count;$i++)
42 {
43 $start_i = index($string,$start);
44 $pre = substr($string,0,$start_i);
45
46 $start_i += length($start);
47 $end_i = index($string,$end,$start_i);
48
49 $text = substr($string,$start_i,$end_i-$start_i);
50 $post = substr($string,$end_i+1);
51
52 #print "pre=[".$pre."]\n";
53 #print "text=[".$text."]\n";
54 #print "post=[".$post."]\n";
55 $string = $pre.$new_start.$text.$new_end.$post;
56 }
57 return $string;
58 }
59
60 sub INSPEC_to_TeX
61 {
62 #
63 # converts inspec markups to tex markups for bibtex purposes cant do nested thingies yet!!!
64 #
65 my($ret) = @_;
66 $ret =~ s/\*/\{\$\\times\$\}/g;
67 $ret =~ s/\"/\{\"\}/g;
68 $ret =~ s/%/\{\\%\}/g;
69 #
70 # greek letters lowercase
71 #
72 $ret =~ s/ (alpha) / \{\$\\\1\$\} /g;
73 $ret =~ s/ (beta) / \{\$\\\1\$\} /g;
74 $ret =~ s/ (gamma) / \{\$\\\1\$\} /g;
75 $ret =~ s/ (delta) / \{\$\\\1\$\} /g;
76 $ret =~ s/ (epsilon) / \{\$\\\1\$\} /g;
77 $ret =~ s/ (varepsilon) / \{\$\\\1\$\} /g;
78 $ret =~ s/ (zeta) / \{\$\\\1\$\} /g;
79 $ret =~ s/ (eta) / \{\$\\\1\$\} /g;
80 $ret =~ s/ (theta) / \{\$\\\1\$\} /g;
81 $ret =~ s/ (vartheta) / \{\$\\\1\$\} /g;
82 $ret =~ s/ (iota) / \{\$\\\1\$\} /g;
83 $ret =~ s/ (kappa) / \{\$\\\1\$\} /g;
84 $ret =~ s/ (lambda) / \{\$\\\1\$\} /g;
85 $ret =~ s/ (mu) / \{\$\\\1\$\} /g;
86 $ret =~ s/ (nu) / \{\$\\\1\$\} /g;
87 $ret =~ s/ (xi) / \{\$\\\1\$\} /g;
88 $ret =~ s/ (pi) / \{\$\\\1\$\} /g;
89 $ret =~ s/ (varpi) / \{\$\\\1\$\} /g;
90 $ret =~ s/ (rho) / \{\$\\\1\$\} /g;
91 $ret =~ s/ (varrho) / \{\$\\\1\$\} /g;
92 $ret =~ s/ (sigma) / \{\$\\\1\$\} /g;
93 $ret =~ s/ (varsigma) / \{\$\\\1\$\} /g;
94 $ret =~ s/ (tau) / \{\$\\\1\$\} /g;
95 $ret =~ s/ (upsilon) / \{\$\\\1\$\} /g;
96 $ret =~ s/ (phi) / \{\$\\\1\$\} /g;
97 $ret =~ s/ (varphi) / \{\$\\\1\$\} /g;
98 $ret =~ s/ (chi) / \{\$\\\1\$\} /g;
99 $ret =~ s/ (psi) / \{\$\\\1\$\} /g;
100 $ret =~ s/ (omega) / \{\$\\\1\$\} /g;
101 $ret =~ s/ (AA) / \{\$\\\1\$\} /g;
102 $ret =~ s/ (aa) / \{\$\\\1\$\} /g;
103 $ret =~ s/ h\(cross\) / \{\$\\hbar\$\} /g;
104 $ret =~ s/approximately=/\{\$\\approx\$\}/g;
105 $ret =~ s/ square root /\{\$\\surd\$\}/g;
106 $ret =~ s/\+or-/\{\$\\pm\$\}/g;
107 $ret =~ s/-or\+/\{\$\\mp\$\}/g;
108 $ret =~ s/>or=/\{\$\\geq\$\}/g;
109 $ret =~ s/<or=/\{\$\\leq\$\}/g;
110 $ret =~ s/not=/\{\$\\neq\$\}/g;
111 $ret =~ s/=/\{=\}/g;
112
113
114 $ret =~ s/ (Delta) / \{\$\\\1\$\} /g;
115 $ret =~ s/ (Gamma) / \{\$\\\1\$\} /g;
116 $ret =~ s/ (Theta) / \{\$\\\1\$\} /g;
117 $ret =~ s/ (Lambda) / \{\$\\\1\$\} /g;
118 $ret =~ s/ (Xi) / \{\$\\\1\$\} /g;
119 $ret =~ s/ (Pi) / \{\$\\\1\$\} /g;
120 $ret =~ s/ (Sigma) / \{\$\\\1\$\} /g;
121 $ret =~ s/ (Upsilon) / \{\$\\\1\$\} /g;
122 $ret =~ s/ (Phi) / \{\$\\\1\$\} /g;
123 $ret =~ s/ (Psi) / \{\$\\\1\$\} /g;
124 $ret =~ s/ (Omega) / \{\$\\\1\$\} /g;
125
126
127 $ret =~ s/ degrees C/ \{\$^\{\\circ\}\$\}C/g;
128 $ret =~ s/(.)\/sup to \//\{\$\\vec\{\1\}\$\}/g;
129 $ret = inspec_su($ret,"sub","_");
130 $ret = inspec_su($ret,"sup","^");
131
132 return $ret;
133 }
134
135 sub swap_jr
136 {
137 my($aut) = @_;
138 # swap Jr.
139 #
140 # handle
141 # Carl Ventrice, Jr. do nothing
142 # Ventrice, C.A., Jr. swap into Ventrice, Jr., C.A.
143 #
144 if( ($aut =~ tr/,//) > 1 && index($aut,"Jr.") != -1 )
145 {
146 ## has 2 ,'s and a Jr. so must do a swap
147 my @subwords = quotewords(",",0,$aut);
148 if( $#subwords == 2)
149 {
150 $aut = $subwords[0].",".$subwords[2].",".$subwords[1];
151 }
152 else
153 {
154 print "ERROR Handling $aut\nI thought that it had a Jr. in it but I think I'm wrong!\n";
155 return $aut;
156 }
157 }
158 return $aut;
159 }
160
161
162
163 sub name_bibtex_to_inspec
164 {
165 #
166 # convert a single author name from bibtex to inspec format
167 #
168 my ($ret) = @_;
169 $ret = swap_jr($ret);
170 # remove spaces in periods
171 $ret =~ s/\. /\./g;
172 # replace 2 or more spaces with just one space
173 #$_[0] =~ s/ {2,}/ /g;
174 # replace ". ," WITH ".,"
175 #$_[0] =~ s/. ,/.,/g;
176 # remove trailing spaces
177 $ret =~ s/^\s+//;
178 $ret =~ s/\s+$//;
179 return $ret;
180 }
181
182 sub name_inspec_to_bibtex
183 {
184 #
185 # convert a single author name from inspec to bibtex format
186 #
187 my ($ret) = @_;
188 $ret = swap_jr($ret);
189 # add spaces in periods
190 $ret =~ s/\./\. /g;
191 # replace 2 or more spaces with just one space
192 $ret =~ s/ {2,}/ /g;
193 # replace ". ," WITH ".,"
194 $ret =~ s/. ,/.,/g;
195 # remove trailing spaces
196 $ret =~ s/^\s+//;
197 $ret =~ s/\s+$//;
198 return $ret;
199 }
200
201 sub authors_inspec_to_bibtex
202 {
203 # convert a INPSEC authors string containing single or
204 # multiple authors ro bibtex format
205 # second argument of retunr in first authors last name FALN
206 #
207 #
208 # must parse the authors a little
209 #
210 # example author names
211 #<b>Author(s)</b>: Hartmut Gau; Herminghaus, S.<br>
212 #<b>Author(s)</b>: Makse, H.A.; Johnson, D.L.; Schwartz, L.M.<br>
213 #<b>Author(s)</b>: Zandvliet, H.J.W.<br>
214 # must get first authors last name and put spaces in after periods
215 #
216 use Text::ParseWords;
217 my ($authors) = @_;
218 my $FALN = "";
219 if(index($authors,";") != -1 )
220 {
221 # multiple author names split on ";"
222 # escape quotes
223 $authors=~s/\'/\\\'/g;
224 $authors=~s/\`/\\\`/g;
225 $authors=~s/\"/\\\"/g;
226 my @words = parse_line(";",1,$authors);
227 my $i=0;
228 $authors = "";
229 # rebuild authors string
230 foreach my $aut (@words)
231 {
232 $aut=~s/\\\'/\'/g;
233 $aut=~s/\\\`/\`/g;
234 $aut=~s/\\\"/\"/g;
235 $aut = name_inspec_to_bibtex($aut);
236 if($i==0)
237 {
238 # first authors last name
239 $FALN = $aut;
240 $i++;
241 $authors=$aut;
242 }
243 else
244 {
245 $authors.=" and ".$aut;
246 }
247 }
248 }
249 else
250 {
251 # single author
252 $authors = name_inspec_to_bibtex($authors);
253 $FALN = $authors;
254 }
255 #
256 # get first authors last name FALN and clean it up
257 #
258 # name examples
259 # Vincent LaBella
260 # V. P. LaBella
261 # LaBella, V. P.
262 # Carl Ventrice, Jr.
263 # Ventrice, Jr., C. A.
264 # delete the Jr. part
265 $FALN =~ s/, Jr.//g;
266 if(index($FALN,",") != -1 )
267 {
268 # has a comma
269 $FALN = substr($FALN,0,index($FALN,","));
270 }
271 else
272 {
273 # no comma get last word
274 my @temp_words = quotewords(" ",0, $FALN);
275 $FALN = $temp_words[-1];
276 }
277 #
278 # get first authors last name, everything up to first comma and clean it up for key
279 #
280 $FALN =~ s/\'//g;
281 $FALN =~ s/~//g;
282 $FALN =~ s/-//g;
283 $FALN =~ s/`//g; #`
284 $FALN =~ s/ //g;
285 $FALN = lc($FALN);
286 return ($authors, $FALN);
287 }
288
289 sub Inspec_to_BibTeX{
290 #
291 # convert the inspec citation hash to BibTeX citation hash
292 #
293 # must take in a hash which is the inspec information and
294 # returns a hash of bibtex information
295 #
296 # makes default key the way I like it
297 # First authors last name:CODEN:volume:page
298 # you can change the key to your liking
299 #
300 my ($ie) = @_;
301 my %ret;
302 my ($FALN,$key,$authors);
303 foreach $key (keys %$ie)
304 {
305 if(uc($key) eq uc("Volume"))
306 {
307 $ret{"volume"}=$ie->{$key};
308 }
309 elsif(uc($key) eq uc("Start Page"))
310 {
311 $ret{"pages"}=$ie->{$key};
312 }
313 elsif(uc($key) eq uc("Author affiliation"))
314 {
315 $ret{"affiliation"}=$ie->{$key};
316 }
317 elsif(uc($key) eq uc("Title"))
318 {
319 $ret{"title"}=INSPEC_to_TeX($ie->{$key});
320 }
321 elsif(uc($key) eq uc("Author(s)"))
322 {
323 ($authors,$FALN) = authors_inspec_to_bibtex($ie->{$key});
324 $ret{"author"}=$authors;
325 }
326 elsif(uc($key) eq uc("CODEN"))
327 {
328 $ret{"coden"}=$ie->{$key};
329 }
330 elsif(uc($key) eq uc("Issue"))
331 {
332 $ret{"number"}=$ie->{$key};
333 }
334 elsif(uc($key) eq uc("Publication year"))
335 {
336 $ret{"year"}=$ie->{$key};
337 }
338 elsif(uc($key) eq uc("ISSN"))
339 {
340 $ret{"issn"}=$ie->{$key};
341 }
342 elsif(uc($key) eq uc("INSPEC Abstract number"))
343 {
344 $ret{"inspec"}=$ie->{$key};
345 }
346 elsif(uc($key) eq uc("Accession number"))
347 {
348 $ret{"accession"}=$ie->{$key};
349 }
350 elsif(uc($key) eq uc("Number of references"))
351 {
352 $ret{"num_ref"}=$ie->{$key};
353 }
354 elsif(uc($key) eq uc("Publisher"))
355 {
356 $ret{"publisher"}=$ie->{$key};
357 }
358 elsif( index(uc($key),uc("journal title")) != -1)
359 {
360 $ret{"journal"}=$ie->{$key};
361 $ret{"journal_name"}=$ie->{$key};
362 }
363 elsif(uc($key) eq uc("Publication date"))
364 {
365 $ret{"pub_date"}=$ie->{$key};
366 }
367 elsif(uc($key) eq uc("Inclusive page numbers"))
368 {
369 $ret{"page_numbers"}=$ie->{$key};
370 }
371 elsif(uc($key) eq uc("Abstract"))
372 {
373 $ret{"abstract"}=INSPEC_to_TeX($ie->{$key});
374 }
375 }
376 $ret{"type"}="article";
377 $ret{"key"}=$FALN.":".$ret{"coden"}.":".$ret{"volume"}.":".$ret{"pages"};
378
379 return %ret;
380 }
381
382
383
384
385 1;
386 __END__