Debian Codegraph @

texmf-dist/doc/latex/unamth-template/Latex/Classes/urlbst

raw
1 #! /usr/bin/env perl
2 #
3 # Usage: ./urlbst.pl [--eprint] [--doi]
4 # [--nohyperlinks] [--inlinelinks] [--hypertex] [--hyperref]
5 # [input-file [output-file]]
6 # If either input-file or output-file is omitted, they are replaced by
7 # stdin or stdout respectively.
8 #
9 # See http://purl.org/nxg/dist/urlbst for documentation
10 #
11 # $Id: urlbst.in,v 1.8 2007/03/26 17:21:56 norman Exp $
12
13 $version = '0.6';
14 ($progname = $0) =~ s/.*\///;
15 $mymarker = "% $progname";
16 $mymarkerend = "% ...$progname to here";
17 $myurl = 'http://purl.org/nxg/dist/urlbst';
18
19
20 $infile = '-';
21 $outfile = '-';
22
23 $addeprints = 0; # if true (nonzero) we add support for eprints
24 $eprintprefix = 'arXiv:'; # make these settable with --eprint? syntax?
25 $eprinturl = 'http://arxiv.org/abs/';
26
27 $adddoiresolver = '0';
28 $doiprefix = 'doi:';
29 $doiurl = 'http://dx.doi.org/';
30
31 $makehref = 0;
32 $availablestring = "Available from: ";
33 $inlinelinks = 0;
34
35 $Usage = "$progname [--eprint] [--doi]\n [--nohyperlinks] [--inlinelinks] [--hypertex] [--hyperref]\n [--help] [input-file [output-file]]";
36
37 while ($#ARGV >= 0) {
38 if ($ARGV[0] eq '--eprint') {
39 $addeprints = 1;
40 } elsif ($ARGV[0] eq '--doi') {
41 $adddoiresolver = 1;
42 } elsif ($ARGV[0] eq '--nohyperlinks') {
43 $makehref = 0;
44 } elsif ($ARGV[0] eq '--hypertex') {
45 $makehref = 1;
46 } elsif ($ARGV[0] eq '--hyperref') {
47 $makehref = 2;
48 } elsif ($ARGV[0] eq '--inlinelinks') {
49 $inlinelinks = 1;
50 } elsif ($ARGV[0] eq '--help') {
51 print <<EOD;
52 urlbst version $version
53 Usage: $Usage
54 --eprint: include support for `eprint' fields
55 --doi: include support for `doi' field
56 --nohyperlinks do not include active links anywhere
57 --inlinelinks add hyperlinks to entry titles
58 --hypertex: include HyperTeX-style hyperlink support
59 --hyperref: include {hyperref}-style hyperlink support
60 (generally better)
61 --help: print this help
62 Input and output files may be given as `-' (default) to indicate stdin/out
63 EOD
64 exit(0);
65 } elsif ($ARGV[0] =~ /^-/) {
66 die "Unrecognised option $ARGV[0]: Usage: $Usage\n";
67 } elsif ($infile eq '-') {
68 $infile = $ARGV[0];
69 } elsif ($outfile eq '-') {
70 $outfile = $ARGV[0];
71 } else {
72 die "Usage: $Usage\n";
73 }
74 shift(@ARGV);
75 }
76
77 if ($inlinelinks && $makehref == 0) {
78 print <<'EOD';
79 Warning: --inlinelinks and --nohyperlinks were both specified (possibly
80 implicitly). That combination makes no sense, so I'll ignore
81 --nohyperlinks and use --hyperref instead
82 EOD
83 $makehref = 2;
84 }
85
86 $exitstatus = 0; # success status
87
88 open (IN, "<$infile") || die "Can't open $infile to read";
89 open (OUT, ">$outfile") || die "Can't open $outfile to write";
90
91 # We have to make certain assumptions about the source files, in order
92 # to patch them at the correct places. Specifically, we assume that
93 #
94 # - there's a function init.state.consts
95 #
96 # - ...and an output.nonnull which does the actual outputting, which
97 # has the `usual' interface.
98 #
99 # - we can replace
100 # fin.entry
101 # by
102 # new.block
103 # output.url % the function which formats and displays any URL
104 # fin.entry
105 #
106 # - there is a function which handles the `article' entry type (this
107 # will always be true)
108 #
109 # - there is a function output.bibitem which is called at the
110 # beginning of each entry type
111 # - ...and one called fin.entry which is called at the end
112 #
113 # If the functions format.date, format.title or new.block are not defined (the
114 # former is not in apalike, for example, and the last is not in the
115 # AMS styles), then replacements are included in the output.
116 #
117 # All these assumptions are true of the standard files and, since most
118 # style files derive from them more or less directly, are true of most (?)
119 # other style files, too.
120 #
121 # There's some rather ugly Perl down here. The parsing for
122 # brace-matching could probably do with being rewritten in places, to
123 # make it less ugly, and more robust.
124
125 print OUT "%%% Modification of BibTeX style file ", ($infile eq '-' ? '<stdin>' : $infile), "\n";
126 print OUT "%%% ... by $progname, version $version (marked with \"$mymarker\")\n%%% See <$myurl>\n";
127 print OUT "%%% Added webpage entry type, and url and lastchecked fields.\n";
128 print OUT "%%% Added eprint support.\n" if ($addeprints);
129 print OUT "%%% Added DOI support.\n" if ($adddoiresolver);
130 print OUT "%%% Added HyperTeX support.\n" if ($makehref == 1);
131 print OUT "%%% Added hyperref support.\n" if ($makehref == 2);
132 print OUT "%%% Original headers follow...\n\n";
133
134 $found{initconsts} = 0;
135 $found{outputnonnull} = 0;
136 $found{article} = 0;
137 $found{outputbibitem} = 0;
138 $found{finentry} = 0;
139 $found{formatdate} = 0;
140 $found{formattitle} = 0;
141 $found{newblock} = 0;
142
143 while (<IN>) {
144 /^ *%/ && do {
145 # Pass commented lines unchanged
146 print OUT;
147 next;
148 };
149
150 /^ *ENTRY/ && do {
151 # Work through the list of entry types, finding what ones are there.
152 # If we find a URL entry there already, object, since these edits
153 # will mess things up.
154 $line = $_;
155 until ($line =~ /\{\s*(\w*)/) {
156 $line .= <IN>;
157 }
158 $bracematchtotal = 0; # reset
159 bracematcher($line);
160 $line =~ /\{\s*(\w*)/;
161 $found{'entry'.$1} = 1;
162 print OUT $line;
163 $line = <IN>;
164 until (bracematcher($line) == 0) {
165 # XXX deal with multiple entries on one line
166 ($line =~ /^\s*(\w*)/) && ($found{'entry'.$1} = 1);
167 print OUT $line;
168 $line = <IN>;
169 }
170 if (defined($found{entryurl})) {
171 print STDERR "$progname: style file $infile already has URL entry!\n";
172 # print out the rest of the file, and give up
173 print OUT $line;
174 while (<IN>) {
175 print OUT;
176 }
177 $exitstatus = 1;
178 last;
179 } else {
180 print OUT " eprint $mymarker\n doi $mymarker\n url $mymarker\n lastchecked $mymarker\n";
181 }
182 print OUT $line;
183 next;
184 };
185
186 /^ *FUNCTION *\{init\.state\.consts\}/ && do {
187 # In the init.state.consts function, add an extra set of
188 # constants at the beginning. Also use this as the marker for
189 # the place to add the init strings function.
190 print OUT <<EOD;
191 STRINGS { urlintro eprinturl eprintprefix doiprefix doiurl openinlinelink closeinlinelink } $mymarker...
192 INTEGERS { hrefform inlinelinks makeinlinelink addeprints adddoiresolver }
193 % Following constants may be adjusted by hand, if desired
194 FUNCTION {init.urlbst.variables}
195 {
196 "$availablestring" 'urlintro := % prefix before URL
197 "$eprinturl" 'eprinturl := % prefix to make URL from eprint ref
198 "$eprintprefix" 'eprintprefix := % text prefix printed before eprint ref
199 "$doiurl" 'doiurl := % prefix to make URL from DOI
200 "$doiprefix" 'doiprefix := % text prefix printed before DOI ref
201 #$addeprints 'addeprints := % 0=no eprints; 1=include eprints
202 #$adddoiresolver 'adddoiresolver := % 0=no DOI resolver; 1=include it
203 #$makehref 'hrefform := % 0=no crossrefs; 1=hypertex xrefs; 2=hyperref refs
204 #$inlinelinks 'inlinelinks := % 0=URLs explicit; 1=URLs attached to titles
205 % the following are internal state variables, not config constants
206 #0 'makeinlinelink := % state variable managed by setup.inlinelink
207 "" 'openinlinelink := % ditto
208 "" 'closeinlinelink := % ditto
209 }
210 INTEGERS {
211 bracket.state
212 outside.brackets
213 open.brackets
214 within.brackets
215 close.brackets
216 }
217 $mymarkerend
218 EOD
219 $line = $_;
220 until ($line =~ /\{.*\}.*\{/s) {
221 $line .= <IN>;
222 }
223 $line =~ s/(\{.*?\}.*?\{)/$1 #0 'outside.brackets := $mymarker
224 #1 'open.brackets :=
225 #2 'within.brackets :=
226 #3 'close.brackets :=
227
228 /s;
229 print OUT $line;
230 $found{initconsts} = 1;
231 next;
232 };
233
234 /^ *EXECUTE *\{init\.state\.consts\}/ && do {
235 print OUT "EXECUTE {init.urlbst.variables}\n";
236 print OUT;
237 next;
238 };
239
240 /^ *FUNCTION *\{new.block\}/ && do {
241 $found{newblock} = 1;
242 };
243
244 /^ *FUNCTION *{output\.nonnull}/ && do {
245 print OUT "$mymarker\n";
246 print OUT "FUNCTION {output.nonnull.original}\n";
247 copy_block();
248 print_output_functions();
249 $found{outputnonnull} = 1;
250 next;
251 };
252
253 /FUNCTION *\{fin.entry\}/ && do {
254 # Rename fin.entry to fin.entry.original (wrapped below)
255 s/fin.entry/fin.entry.original/;
256 $found{finentry} = 1;
257 print OUT;
258 next;
259 };
260
261 /^ *FUNCTION *{format\.date}/ && do {
262 $found{formatdate} = 1;
263 print OUT;
264 next;
265 };
266
267 /^ *FUNCTION *{format\.title}/ && do {
268 # record that we found this
269 $found{formattitle} = 1;
270 print OUT;
271 next;
272 };
273
274 /^ *format\.b?title/ && do {
275 # interpolate a call to setup.inlinelink
276 print OUT " title empty\$ 'skip\$ 'setup\.inlinelink if\$ $mymarker\n";
277 print OUT;
278 next;
279 };
280
281 /^ *FUNCTION *\{article\}/ && do {
282
283 print_missing_functions();
284 print_webpage_def();
285
286 print OUT;
287 $found{article} = 1;
288 next;
289 };
290
291 /FUNCTION *\{output.bibitem\}/ && do {
292 # Rename output.bibitem to output.bibitem.original (wrapped below)
293 s/{output.bibitem\}/\{output.bibitem.original\}/;
294 $found{outputbibitem} = 1;
295 print OUT;
296 next;
297 };
298
299 print OUT;
300 };
301
302 if ($exitstatus == 0) {
303 # Skip this if we've already reported an error -- it'll only be confusing
304 foreach $k (keys %found) {
305 if ($found{$k} == 0) {
306 print STDERR "$progname: $infile: failed to find feature $k\n";
307 }
308 }
309 }
310
311 close (IN);
312 close (OUT);
313
314 exit $exitstatus;;
315
316
317
318
319
320
321 sub print_output_functions {
322 print OUT "$mymarker...\n";
323
324 print OUT <<'EOD';
325 % The following three functions are for handling inlinelink. They wrap
326 % a block of text which is potentially output with write$ by multiple
327 % other functions, so we don't know the content a priori.
328 % They communicate between each other using the variables makeinlinelink
329 % (which is true if a link should be made), and closeinlinelink (which holds
330 % the string which should close any current link. They can be called
331 % at any time, but start.inlinelink will be a no-op unless something has
332 % previously set makeinlinelink true, and the two ...end.inlinelink functions
333 % will only do their stuff if start.inlinelink has previously set
334 % closeinlinelink to be non-empty.
335 FUNCTION {setup.inlinelink}
336 { makeinlinelink
337 { hrefform #1 = % hypertex
338 { "\special {html:<a href=" quote$ * url * quote$ * "> }{" * 'openinlinelink :=
339 "\special {html:</a>}" 'closeinlinelink :=
340 }
341 { hrefform #2 = % hyperref
342 { "\href{" url * "}{" * 'openinlinelink :=
343 "}" 'closeinlinelink :=
344 }
345 'skip$
346 if$ % hrefform #2 =
347 }
348 if$ % hrefform #1 =
349 #0 'makeinlinelink :=
350 }
351 'skip$
352 if$ % makeinlinelink
353 }
354 FUNCTION {add.inlinelink}
355 { openinlinelink empty$
356 'skip$
357 { openinlinelink swap$ * closeinlinelink *
358 "" 'openinlinelink :=
359 }
360 if$
361 }
362 EOD
363
364 # new.block is defined elsewhere
365
366 print OUT <<'EOD';
367 FUNCTION {output.nonnull}
368 { % Save the thing we've been asked to output
369 's :=
370 % If the bracket-state is close.brackets, then add a close-bracket to
371 % what is currently at the top of the stack, and set bracket.state
372 % to outside.brackets
373 bracket.state close.brackets =
374 { "]" *
375 outside.brackets 'bracket.state :=
376 }
377 'skip$
378 if$
379 bracket.state outside.brackets =
380 { % We're outside all brackets -- this is the normal situation.
381 % Write out what's currently at the top of the stack, using the
382 % original output.nonnull function.
383 s
384 add.inlinelink
385 output.nonnull.original % invoke the original output.nonnull
386 }
387 { % Still in brackets. Add open-bracket or (continuation) comma, add the
388 % new text (in s) to the top of the stack, and move to the close-brackets
389 % state, ready for next time (unless inbrackets resets it). If we come
390 % into this branch, then output.state is carefully undisturbed.
391 bracket.state open.brackets =
392 { " [" * }
393 { ", " * } % bracket.state will be within.brackets
394 if$
395 s *
396 close.brackets 'bracket.state :=
397 }
398 if$
399 }
400
401 % Call this function just before adding something which should be presented in
402 % brackets. bracket.state is handled specially within output.nonnull.
403 FUNCTION {inbrackets}
404 { bracket.state close.brackets =
405 { within.brackets 'bracket.state := } % reset the state: not open nor closed
406 { open.brackets 'bracket.state := }
407 if$
408 }
409
410 FUNCTION {format.lastchecked}
411 { lastchecked empty$
412 { "" }
413 { inbrackets "cited " lastchecked * }
414 if$
415 }
416 EOD
417 print OUT "$mymarkerend\n";
418 }
419
420 sub print_webpage_def {
421 print OUT "$mymarker...\n";
422
423 # Some of the functions below call new.block, so we need a dummy
424 # version, in the case where the style being edited doesn't supply
425 # that function.
426 if (! $found{newblock}) {
427 print OUT "FUNCTION {new.block} % dummy new.block function\n{\n % empty\n}\n\n";
428 $found{newblock} = 1;
429 }
430
431 print OUT <<'EOD';
432 % Functions for making hypertext links.
433 % In all cases, the stack has (link-text href-url)
434 %
435 % make 'null' specials
436 FUNCTION {make.href.null}
437 {
438 pop$
439 }
440 % make hypertex specials
441 FUNCTION {make.href.hypertex}
442 {
443 "\special {html:<a href=" quote$ *
444 swap$ * quote$ * "> }" * swap$ *
445 "\special {html:</a>}" *
446 }
447 % make hyperref specials
448 FUNCTION {make.href.hyperref}
449 {
450 "\href {" swap$ * "} {\path{" * swap$ * "}}" *
451 }
452 FUNCTION {make.href}
453 { hrefform #2 =
454 'make.href.hyperref % hrefform = 2
455 { hrefform #1 =
456 'make.href.hypertex % hrefform = 1
457 'make.href.null % hrefform = 0 (or anything else)
458 if$
459 }
460 if$
461 }
462
463 % If inlinelinks is true, then format.url should be a no-op, since it's
464 % (a) redundant, and (b) could end up as a link-within-a-link.
465 FUNCTION {format.url}
466 { inlinelinks #1 = url empty$ or
467 { "" }
468 { hrefform #1 =
469 { % special case -- add HyperTeX specials
470 urlintro "\url{" url * "}" * url make.href.hypertex * }
471 { urlintro "\url{" * url * "}" * }
472 if$
473 }
474 if$
475 }
476
477 FUNCTION {format.eprint}
478 { eprint empty$
479 { "" }
480 { eprintprefix eprint * eprinturl eprint * make.href }
481 if$
482 }
483
484 FUNCTION {format.doi}
485 { doi empty$
486 { "" }
487 { doiprefix doi * doiurl doi * make.href }
488 if$
489 }
490
491 % Output a URL. We can't use the more normal idiom (something like
492 % `format.url output'), because the `inbrackets' within
493 % format.lastchecked applies to everything between calls to `output',
494 % so that `format.url format.lastchecked * output' ends up with both
495 % the URL and the lastchecked in brackets.
496 FUNCTION {output.url}
497 { url empty$
498 'skip$
499 { new.block
500 format.url output
501 format.lastchecked output
502 }
503 if$
504 }
505
506 FUNCTION {output.web.refs}
507 {
508 new.block
509 output.url
510 addeprints eprint empty$ not and
511 { format.eprint output.nonnull }
512 'skip$
513 if$
514 adddoiresolver doi empty$ not and
515 { format.doi output.nonnull }
516 'skip$
517 if$
518 }
519
520 % Wrapper for output.bibitem.original.
521 % If the URL field is not empty, set makeinlinelink to be true,
522 % so that an inline link will be started at the next opportunity
523 FUNCTION {output.bibitem}
524 { outside.brackets 'bracket.state :=
525 output.bibitem.original
526 inlinelinks url empty$ not and
527 { #1 'makeinlinelink := }
528 { #0 'makeinlinelink := }
529 if$
530 }
531
532 % Wrapper for fin.entry.original
533 FUNCTION {fin.entry}
534 { output.web.refs % urlbst
535 makeinlinelink % ooops, it appears we didn't have a title for inlinelink
536 { setup.inlinelink % add some artificial link text here, as a fallback
537 "[link]" output.nonnull }
538 'skip$
539 if$
540 bracket.state close.brackets = % urlbst
541 { "]" * }
542 'skip$
543 if$
544 fin.entry.original
545 }
546
547 % Webpage entry type.
548 % Title and url fields required;
549 % author, note, year, month, and lastchecked fields optional
550 % See references
551 % ISO 690-2 http://www.nlc-bnc.ca/iso/tc46sc9/standard/690-2e.htm
552 % http://www.classroom.net/classroom/CitingNetResources.html
553 % http://neal.ctstateu.edu/history/cite.html
554 % http://www.cas.usf.edu/english/walker/mla.html
555 % for citation formats for web pages.
556 FUNCTION {webpage}
557 { output.bibitem
558 author empty$
559 { editor empty$
560 'skip$ % author and editor both optional
561 { format.editors output.nonnull }
562 if$
563 }
564 { editor empty$
565 { format.authors output.nonnull }
566 { "can't use both author and editor fields in " cite$ * warning$ }
567 if$
568 }
569 if$
570 new.block
571 title empty$ 'skip$ 'setup.inlinelink if$
572 format.title "title" output.check
573 inbrackets "online" output
574 new.block
575 year empty$
576 'skip$
577 { format.date "year" output.check }
578 if$
579 % We don't need to output the URL details ('lastchecked' and 'url'),
580 % because fin.entry does that for us, using output.web.refs. The only
581 % reason we would want to put them here is if we were to decide that
582 % they should go in front of the rather miscellaneous information in 'note'.
583 new.block
584 note output
585 fin.entry
586 }
587 EOD
588
589 print OUT "$mymarkerend\n\n\n";
590 }
591
592
593 sub print_missing_functions {
594 # We've got to the bit of the file which handles the entry
595 # types, so write out the webpage entry handler. This uses
596 # the format.date function, which which many but not all
597 # bst files have (for example, apalike doesn't). So
598 # check that we either have found this function already, or
599 # add it.
600 if (! $found{formatdate}) {
601 if ($found{entrymonth}) {
602 print OUT <<'EOD';
603 FUNCTION {format.date}
604 { year empty$
605 { month empty$
606 { "" }
607 { "there's a month but no year in " cite$ * warning$
608 month
609 }
610 if$
611 }
612 { month empty$
613 'year
614 { month " " * year * }
615 if$
616 }
617 if$
618 }
619 EOD
620 } else {
621 print OUT <<'EOD';
622 FUNCTION {format.date}
623 { year empty$
624 'skip$
625 { %write$
626 "(" year * ")" *
627 }
628 if$
629 }
630 EOD
631 }
632 $found{formatdate} = 1;
633 }
634
635 # If the style file didn't supply a format.title function, then supply
636 # one here (the {webpage} function requires it).
637 if (! $found{formattitle}) {
638 print OUT <<'EOD';
639 FUNCTION {format.title}
640 { title empty$
641 { "" }
642 { title "t" change.case$ }
643 if$
644 }
645 EOD
646 $found{formattitle} = 1;
647 }
648 }
649
650 # Utility function: Keep track of open and close braces in the string argument.
651 # Keep state in $bracematchtotal, return the current value.
652 sub bracematcher {
653 my $s = shift;
654 $s =~ s/[^\{\}]//g;
655 #print "s=$s\n";
656 foreach my $c (split (//, $s)) {
657 $bracematchtotal += ($c eq '{' ? 1 : -1);
658 }
659 return $bracematchtotal;
660 }
661
662 # Utility function: use bracematcher to copy the complete block which starts
663 # on or after the current line.
664 sub copy_block {
665 $bracematchtotal = 0;
666 # copy any leading lines which don't have braces (presumably comments)
667 while (defined ($line = <IN>) && ($line !~ /{/)) {
668 print OUT $line;
669 }
670 while (defined ($line) && bracematcher($line) > 0) {
671 print OUT $line;
672 $line = <IN>;
673 }
674 print OUT "$line\n"; # print out terminating \} (assumed
675 # alone on the line)
676 }