| 1 |
#! @PERL@ -w |
| 2 |
# |
| 3 |
# Usage: ./urlbst.pl [--eprint] [--doi] [--pubmed] |
| 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 https://purl.org/nxg/dist/urlbst for documentation |
| 10 |
# |
| 11 |
# Copyright @COPYRIGHTYEARS@, Norman Gray <https://nxg.me.uk> |
| 12 |
# |
| 13 |
# This program is distributed under the terms of the |
| 14 |
# GNU General Public Licence, v2.0. |
| 15 |
# The modifications to the input .bst files are asserted as Copyright @COPYRIGHTYEARS@, Norman Gray, |
| 16 |
# and distributed under the terms of the LaTeX Project Public Licence. |
| 17 |
# See the package README for further dicussion of licences. |
| 18 |
|
| 19 |
$version = '@PACKAGE_VERSION@'; |
| 20 |
$releasedate = '@RELEASEDATE@'; |
| 21 |
($progname = $0) =~ s/.*\///; |
| 22 |
$mymarker = "% $progname"; |
| 23 |
$mymarkerend = "% ...$progname to here"; |
| 24 |
$homepageurl = 'https://purl.org/nxg/dist/urlbst'; |
| 25 |
$repourl = '@REPOURL@'; |
| 26 |
|
| 27 |
$infile = '-'; |
| 28 |
$outfile = '-'; |
| 29 |
|
| 30 |
# eprint support |
| 31 |
$settings{addeprints} = @WITHEPRINTS@; |
| 32 |
$docstring{addeprints} = '0=no eprints; 1=include eprints'; |
| 33 |
$literals{eprintprefix} = 'arXiv:'; # make these settable with --eprint? syntax? |
| 34 |
$docstring{eprintprefix} = "text prefix printed before eprint ref"; |
| 35 |
$literals{eprinturl} = '@EPRINTURL@'; |
| 36 |
$docstring{eprinturl} = 'prefix to make URL from eprint ref'; |
| 37 |
|
| 38 |
# DOI resolver |
| 39 |
$settings{adddoi} = @WITHDOIRESOLVER@; |
| 40 |
$docstring{adddoi} = '0=no DOI resolver; 1=include it'; |
| 41 |
$literals{doiprefix} = 'doi:'; |
| 42 |
$docstring{doiprefix} = 'printed text to introduce DOI'; |
| 43 |
$literals{doiurl} = '@DOIURL@'; |
| 44 |
$docstring{doiurl} = 'prefix to make URL from DOI'; |
| 45 |
$settings{doiform} = 0; |
| 46 |
$docstring{doiform} = '0=with href; 1=with \\doi{}'; |
| 47 |
|
| 48 |
# PUBMED resolver |
| 49 |
$settings{addpubmed} = @WITHPUBMEDRESOLVER@; |
| 50 |
$docstring{addpubmed} = '0=no PUBMED resolver; 1=include it'; |
| 51 |
$literals{pubmedprefix} = 'PMID:'; |
| 52 |
$docstring{pubmedprefix} = 'text prefix printed before PUBMED ref'; |
| 53 |
$literals{pubmedurl} = '@PUBMEDURL@'; |
| 54 |
$docstring{pubmedurl} = 'prefix to make URL from PUBMED'; |
| 55 |
|
| 56 |
# how do we make hypterlinks |
| 57 |
$settings{hrefform} = @WITHHREF@; |
| 58 |
$docstring{hrefform} = '0=no crossrefs; 1=hypertex hrefs; 2=hyperref hrefs'; |
| 59 |
$literals{urlintro} = "URL: "; |
| 60 |
$docstring{urlintro} = 'text prefix before URL'; |
| 61 |
$settings{inlinelinks} = 0; |
| 62 |
$docstring{inlinelinks} = '0=URLs explicit; 1=URLs attached to titles'; |
| 63 |
|
| 64 |
# other text |
| 65 |
$literals{onlinestring} = "online"; |
| 66 |
$docstring{onlinestring} = 'label that a resource is online'; |
| 67 |
$literals{citedstring} = "cited "; |
| 68 |
$docstring{citedstring} = 'label in "lastchecked" remark'; |
| 69 |
$literals{linktextstring} = "[link]"; |
| 70 |
$docstring{linktextstring} = 'anonymous link text'; |
| 71 |
|
| 72 |
$automatic_output_filename = 0; |
| 73 |
|
| 74 |
$Usage = "$progname [--literal key=value]\n [--setting key=value]\n [--help] [input-file [output-file]]"; |
| 75 |
|
| 76 |
# Magic environment variable: if this is set, then we're being called from a Platypus wrapper |
| 77 |
# See http://www.sveinbjorn.org/platypus |
| 78 |
if ($ENV{"CALLED_FROM_PLATYPUS"}) { |
| 79 |
$automatic_output_filename = 1; |
| 80 |
} |
| 81 |
|
| 82 |
while ($#ARGV >= 0) { |
| 83 |
if ($ARGV[0] eq '--eprint') { |
| 84 |
$settings{addeprints} = 1; |
| 85 |
} elsif ($ARGV[0] eq '--noeprint') { |
| 86 |
$settings{addeprints} = 0; |
| 87 |
} elsif ($ARGV[0] eq '--doi') { |
| 88 |
$settings{adddoi} = 1; |
| 89 |
} elsif ($ARGV[0] eq '--nodoi') { |
| 90 |
$settings{adddoi} = 0; |
| 91 |
} elsif ($ARGV[0] eq '--pubmed') { |
| 92 |
$settings{addpubmed} = 1; |
| 93 |
} elsif ($ARGV[0] eq '--nopubmed') { |
| 94 |
$settings{addpubmed} = 0; |
| 95 |
} elsif ($ARGV[0] eq '--nohyperlinks') { |
| 96 |
$settings{hrefform} = 0; |
| 97 |
} elsif ($ARGV[0] eq '--hyperlinks') { |
| 98 |
$settings{hrefform} = 1; # redundant with --hypertex, for consistency |
| 99 |
} elsif ($ARGV[0] eq '--hypertex') { |
| 100 |
$settings{hrefform} = 1; |
| 101 |
} elsif ($ARGV[0] eq '--hyperref') { |
| 102 |
$settings{hrefform} = 2; |
| 103 |
} elsif ($ARGV[0] eq '--inlinelinks') { |
| 104 |
$settings{inlinelinks} = 1; |
| 105 |
} elsif ($ARGV[0] eq '--noinlinelinks') { |
| 106 |
$settings{inlinelinks} = 0; |
| 107 |
} elsif ($ARGV[0] eq '--automatic-output') { |
| 108 |
$automatic_output_filename = 1; |
| 109 |
} elsif ($ARGV[0] eq '--literal') { |
| 110 |
shift; |
| 111 |
my $showstrings = 0; |
| 112 |
if ((($k, $v) = ($ARGV[0] =~ /^(\w+)=(.*)/)) && exists $literals{$k}) { |
| 113 |
$literals{$k} = $v; |
| 114 |
} elsif ($ARGV[0] eq 'help') { |
| 115 |
$showstrings = 1; |
| 116 |
} else { |
| 117 |
print "No literal found in $ARGV[0]; ignoring that\n"; |
| 118 |
$showstrings = 1; |
| 119 |
} |
| 120 |
if ($showstrings) { |
| 121 |
print "Possible literal strings (and defaults) are...\n"; |
| 122 |
while (($k, $v) = each (%literals)) { |
| 123 |
print " $k \t$docstring{$k} [$v]\n"; |
| 124 |
} |
| 125 |
exit(0); |
| 126 |
} |
| 127 |
} elsif ($ARGV[0] eq '--setting') { |
| 128 |
shift; |
| 129 |
my $showstrings = 0; |
| 130 |
if ((($k, $v) = ($ARGV[0] =~ /^(\w+)=(.*)/)) && exists $settings{$k}) { |
| 131 |
$settings{$k} = $v; |
| 132 |
} elsif ($ARGV[0] eq 'help') { |
| 133 |
$showstrings = 1; |
| 134 |
} else { |
| 135 |
print "No setting found in $ARGV[0]; ignoring that\n"; |
| 136 |
$showstrings = 1; |
| 137 |
} |
| 138 |
if ($showstrings) { |
| 139 |
print "Possible settings (and defaults) are...\n"; |
| 140 |
while (($k, $v) = each (%settings)) { |
| 141 |
print " $k \t$docstring{$k} [$v]\n"; |
| 142 |
} |
| 143 |
exit(0); |
| 144 |
} |
| 145 |
} elsif ($ARGV[0] eq '--help') { |
| 146 |
print <<EOD; |
| 147 |
urlbst version $version, $releasedate |
| 148 |
Usage: $Usage |
| 149 |
|
| 150 |
Options: |
| 151 |
--literal key=value set one of the literal strings |
| 152 |
(see `--literal help' for possibilities) |
| 153 |
--setting key=value (as --literal) |
| 154 |
--help print this help |
| 155 |
|
| 156 |
Convenience options (can also be controlled using --setting): |
| 157 |
--[no]eprint include support for `eprint' fields |
| 158 |
--[no]doi include support for `doi' field |
| 159 |
--[no]pubmed include support for `pubmed' field |
| 160 |
--nohyperlinks do not include active links anywhere |
| 161 |
--[no]inlinelinks add hyperlinks to entry titles |
| 162 |
--hypertex include HyperTeX-style hyperlink support |
| 163 |
--hyperref include {hyperref}-style hyperlink support |
| 164 |
(generally better) |
| 165 |
|
| 166 |
Input and output files may be given as `-' (default) to indicate stdin/out. |
| 167 |
|
| 168 |
Homepage: $homepageurl |
| 169 |
Repository: $repourl |
| 170 |
|
| 171 |
EOD |
| 172 |
printf(" Defaults:\n"); |
| 173 |
printf(" eprint refs %s\n", ($settings{addeprints} ? "included" : "not included")); |
| 174 |
printf(" DOIs %s\n", ($settings{adddoi} ? "included" : "not included")); |
| 175 |
printf(" PUBMED refs %s\n", ($settings{addpubmed} ? "included" : "not included")); |
| 176 |
printf(" hyperlinks %s\n", ($settings{hrefform}==0 ? "none" : $settings{hrefform}==1 ? "hypertex" : "hyperref")); |
| 177 |
printf(" inline links %s\n", ($settings{inlinelinks} ? "included" : "not included")); |
| 178 |
exit(0); |
| 179 |
} elsif ($ARGV[0] =~ /^-/) { |
| 180 |
die "Unrecognised option $ARGV[0]: Usage: $Usage\n"; |
| 181 |
} elsif ($infile eq '-') { |
| 182 |
$infile = $ARGV[0]; |
| 183 |
} elsif ($outfile eq '-') { |
| 184 |
$outfile = $ARGV[0]; |
| 185 |
} else { |
| 186 |
die "Usage: $Usage\n"; |
| 187 |
} |
| 188 |
shift(@ARGV); |
| 189 |
} |
| 190 |
|
| 191 |
if ($settings{inlinelinks} && $settings{hrefform} == 0) { |
| 192 |
print <<'EOD'; |
| 193 |
Warning: --inlinelinks and --nohyperlinks were both specified (possibly |
| 194 |
implicitly). That combination makes no sense, so I'll ignore |
| 195 |
--nohyperlinks and use --hyperref instead |
| 196 |
EOD |
| 197 |
$settings{hrefform} = 2; |
| 198 |
} |
| 199 |
|
| 200 |
if ($automatic_output_filename) { |
| 201 |
if ($infile eq '-') { |
| 202 |
print "ERROR: No input filename given with --automatic-output\n"; |
| 203 |
} |
| 204 |
$outfile = $infile; |
| 205 |
@outparts = split /\./, $outfile; |
| 206 |
$ext = pop(@outparts); |
| 207 |
$outfile=join('.', @outparts); |
| 208 |
if ($outfile eq '') { |
| 209 |
$outfile = $ext . '-url'; |
| 210 |
} else { |
| 211 |
$outfile = $outfile . '-url.' . $ext; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
$exitstatus = 0; # success status |
| 216 |
|
| 217 |
open (IN, "<$infile") || die "Can't open $infile to read"; |
| 218 |
open (OUT, ">$outfile") || die "Can't open $outfile to write"; |
| 219 |
|
| 220 |
# We have to make certain assumptions about the source files, in order |
| 221 |
# to patch them at the correct places. Specifically, we assume that |
| 222 |
# |
| 223 |
# - there's a function init.state.consts |
| 224 |
# |
| 225 |
# - ...and an output.nonnull which does the actual outputting, which |
| 226 |
# has the `usual' interface. |
| 227 |
# |
| 228 |
# - we can replace |
| 229 |
# fin.entry |
| 230 |
# by |
| 231 |
# new.block |
| 232 |
# output.url % the function which formats and displays any URL |
| 233 |
# fin.entry |
| 234 |
# |
| 235 |
# - there is a function which handles the `article' entry type (this |
| 236 |
# will always be true) |
| 237 |
# |
| 238 |
# - there is a function output.bibitem which is called at the |
| 239 |
# beginning of each entry type |
| 240 |
# - ...and one called fin.entry which is called at the end |
| 241 |
# |
| 242 |
# If the functions format.date, format.title or new.block are not defined (the |
| 243 |
# former is not in apalike, for example, and the last is not in the |
| 244 |
# AMS styles), then replacements are included in the output. |
| 245 |
# |
| 246 |
# All these assumptions are true of the standard files and, since most |
| 247 |
# style files derive from them more or less directly, are true of most (?) |
| 248 |
# other style files, too. |
| 249 |
# |
| 250 |
# There's some rather ugly Perl down here. The parsing for |
| 251 |
# brace-matching could probably do with being rewritten in places, to |
| 252 |
# make it less ugly, and more robust. |
| 253 |
|
| 254 |
print OUT "%%% Modification of BibTeX style file ", ($infile eq '-' ? '<stdin>' : $infile), "\n"; |
| 255 |
print OUT "%%% ... by $progname, version $version (marked with \"$mymarker\")\n%%% See <$homepageurl> and repository <$repourl>\n"; |
| 256 |
print OUT "%%% Modifications Copyright @COPYRIGHTYEARS@, Norman Gray,\n"; |
| 257 |
print OUT "%%% and distributed under the terms of the LPPL; see README for discussion.\n"; |
| 258 |
print OUT "%%%\n"; |
| 259 |
print OUT "%%% Added webpage entry type, and url and lastchecked fields.\n"; |
| 260 |
print OUT "%%% Added eprint support.\n" if ($settings{addeprints}); |
| 261 |
print OUT "%%% Added DOI support.\n" if ($settings{adddoi}); |
| 262 |
print OUT "%%% Added PUBMED support.\n" if ($settings{addpubmed}); |
| 263 |
print OUT "%%% Added HyperTeX support.\n" if ($settings{hrefform} == 1); |
| 264 |
print OUT "%%% Added hyperref support.\n" if ($settings{hrefform} == 2); |
| 265 |
print OUT "%%% Original headers follow...\n\n"; |
| 266 |
|
| 267 |
$found{initconsts} = 0; |
| 268 |
$found{outputnonnull} = 0; |
| 269 |
$found{article} = 0; |
| 270 |
$found{outputbibitem} = 0; |
| 271 |
$found{finentry} = 0; |
| 272 |
$found{formatdate} = 0; |
| 273 |
$found{formattitle} = 0; |
| 274 |
$found{newblock} = 0; |
| 275 |
# The following are initialised negative, which Perl treats as true, |
| 276 |
# so the simple test 'if ($found{formateprint}) ...' will be true. |
| 277 |
$found{formateprint} = -1; |
| 278 |
$found{formatdoi} = -1; |
| 279 |
$found{formatpubmed} = -1; |
| 280 |
|
| 281 |
while (<IN>) { |
| 282 |
/^ *%/ && do { |
| 283 |
# Pass commented lines unchanged |
| 284 |
print OUT; |
| 285 |
next; |
| 286 |
}; |
| 287 |
|
| 288 |
/^ *ENTRY/ && do { |
| 289 |
# Work through the list of entry types, finding what ones are there. |
| 290 |
# If we find a URL entry there already, object, since these edits |
| 291 |
# will mess things up. |
| 292 |
$line = $_; |
| 293 |
until ($line =~ /\{\s*(\w*)/) { |
| 294 |
$line .= <IN>; |
| 295 |
} |
| 296 |
$bracematchtotal = 0; # reset |
| 297 |
bracematcher($line); |
| 298 |
$line =~ /\{\s*(\w*)/; |
| 299 |
$found{'entry'.$1} = 1; |
| 300 |
print OUT $line; |
| 301 |
$line = <IN>; |
| 302 |
until (bracematcher($line) == 0) { |
| 303 |
# XXX deal with multiple entries on one line |
| 304 |
($line =~ /^\s*(\w*)/) && ($found{'entry'.$1} = 1); |
| 305 |
print OUT $line; |
| 306 |
$line = <IN>; |
| 307 |
} |
| 308 |
if (defined($found{entryurl})) { |
| 309 |
print STDERR "$progname: style file $infile already has URL entry!\n"; |
| 310 |
# print out the rest of the file, and give up |
| 311 |
print OUT $line; |
| 312 |
while (<IN>) { |
| 313 |
print OUT; |
| 314 |
} |
| 315 |
$exitstatus = 1; |
| 316 |
last; |
| 317 |
} else { |
| 318 |
print OUT " eprint $mymarker\n doi $mymarker\n pubmed $mymarker\n url $mymarker\n lastchecked $mymarker\n"; |
| 319 |
} |
| 320 |
print OUT $line; |
| 321 |
next; |
| 322 |
}; |
| 323 |
|
| 324 |
/^ *FUNCTION *\{init\.state\.consts\}/ && do { |
| 325 |
# In the init.state.consts function, add an extra set of |
| 326 |
# constants at the beginning. Also use this as the marker for |
| 327 |
# the place to add the init strings function. |
| 328 |
print OUT <<EOD; |
| 329 |
$mymarker... |
| 330 |
% urlbst constants and state variables |
| 331 |
STRINGS { urlintro |
| 332 |
eprinturl eprintprefix doiprefix doiurl pubmedprefix pubmedurl |
| 333 |
citedstring onlinestring linktextstring |
| 334 |
openinlinelink closeinlinelink } |
| 335 |
INTEGERS { hrefform doiform inlinelinks makeinlinelink |
| 336 |
addeprints adddoi addpubmed } |
| 337 |
FUNCTION {init.urlbst.variables} |
| 338 |
{ |
| 339 |
% The following constants may be adjusted by hand, if desired |
| 340 |
|
| 341 |
% The first set allow you to enable or disable certain functionality. |
| 342 |
EOD |
| 343 |
|
| 344 |
while (($k, $v) = each(%settings)) { |
| 345 |
print OUT " #$v '$k :=\t% $docstring{$k}\n"; |
| 346 |
} |
| 347 |
|
| 348 |
print OUT "\n % String constants, which you _might_ want to tweak.\n"; |
| 349 |
while (($k, $v) = each(%literals)) { |
| 350 |
print OUT " \"$v\" '$k :=\t% $docstring{$k}\n"; |
| 351 |
} |
| 352 |
|
| 353 |
print OUT <<EOD; |
| 354 |
|
| 355 |
% The following are internal state variables, not configuration constants, |
| 356 |
% so they shouldn't be fiddled with. |
| 357 |
#0 'makeinlinelink := % state variable managed by possibly.setup.inlinelink |
| 358 |
"" 'openinlinelink := % ditto |
| 359 |
"" 'closeinlinelink := % ditto |
| 360 |
} |
| 361 |
INTEGERS { |
| 362 |
bracket.state |
| 363 |
outside.brackets |
| 364 |
open.brackets |
| 365 |
within.brackets |
| 366 |
close.brackets |
| 367 |
} |
| 368 |
$mymarkerend |
| 369 |
EOD |
| 370 |
$line = $_; |
| 371 |
until ($line =~ /\{.*\}.*\{/s) { |
| 372 |
$line .= <IN>; |
| 373 |
} |
| 374 |
$line =~ s/(\{.*?\}.*?\{)/$1 #0 'outside.brackets := $mymarker... |
| 375 |
#1 'open.brackets := |
| 376 |
#2 'within.brackets := |
| 377 |
#3 'close.brackets := $mymarkerend |
| 378 |
|
| 379 |
/s; |
| 380 |
print OUT $line; |
| 381 |
$found{initconsts} = 1; |
| 382 |
next; |
| 383 |
}; |
| 384 |
|
| 385 |
/^ *EXECUTE *\{init\.state\.consts\}/ && do { |
| 386 |
print OUT "EXECUTE {init.urlbst.variables} $mymarker\n"; |
| 387 |
print OUT; |
| 388 |
next; |
| 389 |
}; |
| 390 |
|
| 391 |
/^ *FUNCTION *\{new.block\}/ && do { |
| 392 |
$found{newblock} = 1; |
| 393 |
}; |
| 394 |
|
| 395 |
/^ *FUNCTION *\{format.doi\}/ && do { |
| 396 |
#print STDERR "$progname: style file $infile already supports DOIs; urlbst format.doi disabled\n(see generated .bst style: you may need to make edits near \$settings{adddoi})\n"; |
| 397 |
$found{formatdoi} = 1; |
| 398 |
$settings{adddoi} = 0; |
| 399 |
}; |
| 400 |
|
| 401 |
/^ *FUNCTION *\{format.eprint\}/ && do { |
| 402 |
#print STDERR "$progname: style file $infile already supports eprints; urlbst format.eprint disabled\n(see generated .bst style: you may need to make edits near \$settings{addeprints})\n"; |
| 403 |
$found{formateprint} = 1; |
| 404 |
$settings{addeprints} = 0; |
| 405 |
}; |
| 406 |
|
| 407 |
/^ *FUNCTION *\{format.pubmed\}/ && do { |
| 408 |
#print STDERR "$progname: style file $infile already supports Pubmed; urlbst format.pubmed disabled\n(see generated .bst style: you may need to make edits near \$settings{addpubmed})\n"; |
| 409 |
$found{formatpubmed} = 1; |
| 410 |
$settings{addpubmed} = 0; |
| 411 |
}; |
| 412 |
|
| 413 |
/^ *FUNCTION *{output\.nonnull}/ && do { |
| 414 |
print OUT "$mymarker\n"; |
| 415 |
print OUT "FUNCTION {output.nonnull.original}\n"; |
| 416 |
copy_block(); |
| 417 |
print_output_functions(); |
| 418 |
$found{outputnonnull} = 1; |
| 419 |
next; |
| 420 |
}; |
| 421 |
|
| 422 |
/FUNCTION *\{fin.entry\}/ && do { |
| 423 |
# Rename fin.entry to fin.entry.original (wrapped below) |
| 424 |
s/fin.entry/fin.entry.original/; |
| 425 |
s/$/ $mymarker (renamed from fin.entry, so it can be wrapped below)/; |
| 426 |
$found{finentry} = 1; |
| 427 |
print OUT; |
| 428 |
next; |
| 429 |
}; |
| 430 |
|
| 431 |
/^ *FUNCTION *{format\.date}/ && do { |
| 432 |
$found{formatdate} = 1; |
| 433 |
print OUT; |
| 434 |
next; |
| 435 |
}; |
| 436 |
|
| 437 |
/^ *FUNCTION *{format\.title}/ && do { |
| 438 |
# record that we found this |
| 439 |
$found{formattitle} = 1; |
| 440 |
print OUT; |
| 441 |
next; |
| 442 |
}; |
| 443 |
|
| 444 |
/^ *format\.b?title/ && do { |
| 445 |
# interpolate a call to possibly.setup.inlinelink |
| 446 |
print OUT " title empty\$ 'skip\$ 'possibly.setup\.inlinelink if\$ $mymarker\n"; |
| 447 |
print OUT; |
| 448 |
next; |
| 449 |
}; |
| 450 |
|
| 451 |
/^ *format\.vol\.num\.pages/ && do { |
| 452 |
# interpolate a call to possibly.setup.inlinelink |
| 453 |
s/^( *)/$1possibly.setup.inlinelink /; |
| 454 |
s/$/$mymarker/; |
| 455 |
print OUT; |
| 456 |
next; |
| 457 |
}; |
| 458 |
|
| 459 |
/^ *FUNCTION *\{article\}/ && do { |
| 460 |
|
| 461 |
print_missing_functions(); |
| 462 |
print_webpage_def(); |
| 463 |
|
| 464 |
print OUT; |
| 465 |
$found{article} = 1; |
| 466 |
next; |
| 467 |
}; |
| 468 |
|
| 469 |
/FUNCTION *\{output.bibitem\}/ && do { |
| 470 |
# Rename output.bibitem to output.bibitem.original (wrapped below) |
| 471 |
s/{output.bibitem\}/\{output.bibitem.original\}/; |
| 472 |
s/$/ $mymarker (renamed from output.bibitem, so it can be wrapped below)/; |
| 473 |
$found{outputbibitem} = 1; |
| 474 |
print OUT; |
| 475 |
next; |
| 476 |
}; |
| 477 |
|
| 478 |
print OUT; |
| 479 |
}; |
| 480 |
|
| 481 |
if ($exitstatus == 0) { |
| 482 |
# Skip this if we've already reported an error -- it'll only be confusing |
| 483 |
foreach $k (keys %found) { |
| 484 |
if ($found{$k} == 0) { |
| 485 |
print STDERR "$progname: $infile: failed to find feature $k\n"; |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
close (IN); |
| 491 |
close (OUT); |
| 492 |
|
| 493 |
exit $exitstatus;; |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
sub print_output_functions { |
| 501 |
print OUT "$mymarker...\n"; |
| 502 |
|
| 503 |
print OUT <<'EOD'; |
| 504 |
% Minimal DOI parsing. |
| 505 |
% Given a DOI on the stack, check whether it starts with 'doiurl' or not. |
| 506 |
% In either case, leave on the stack first a DOI with, and then a DOI without, the URL prefix. |
| 507 |
FUNCTION {parse.doi} |
| 508 |
{ |
| 509 |
#1 doiurl text.length$ substring$ |
| 510 |
doiurl = |
| 511 |
{ doi |
| 512 |
doi doiurl text.length$ #1 + #999 substring$ } |
| 513 |
{ doiurl doi * |
| 514 |
doi } |
| 515 |
if$ |
| 516 |
} |
| 517 |
% The following three functions are for handling inlinelink. They wrap |
| 518 |
% a block of text which is potentially output with write$ by multiple |
| 519 |
% other functions, so we don't know the content a priori. |
| 520 |
% They communicate between each other using the variables makeinlinelink |
| 521 |
% (which is true if a link should be made), and closeinlinelink (which holds |
| 522 |
% the string which should close any current link. They can be called |
| 523 |
% at any time, but start.inlinelink will be a no-op unless something has |
| 524 |
% previously set makeinlinelink true, and the two ...end.inlinelink functions |
| 525 |
% will only do their stuff if start.inlinelink has previously set |
| 526 |
% closeinlinelink to be non-empty. |
| 527 |
% (thanks to 'ijvm' for suggested code here) |
| 528 |
FUNCTION {uand} |
| 529 |
{ 'skip$ { pop$ #0 } if$ } % 'and' (which isn't defined at this point in the file) |
| 530 |
FUNCTION {possibly.setup.inlinelink} |
| 531 |
{ makeinlinelink hrefform #0 > uand |
| 532 |
{ doi empty$ adddoi uand |
| 533 |
{ pubmed empty$ addpubmed uand |
| 534 |
{ eprint empty$ addeprints uand |
| 535 |
{ url empty$ |
| 536 |
{ "" } |
| 537 |
{ url } |
| 538 |
if$ } |
| 539 |
{ eprinturl eprint * } |
| 540 |
if$ } |
| 541 |
{ pubmedurl pubmed * } |
| 542 |
if$ } |
| 543 |
% { doiurl doi * } |
| 544 |
{ doi empty$ |
| 545 |
{ "XXX" } |
| 546 |
{ doi parse.doi pop$ } |
| 547 |
if$ |
| 548 |
} |
| 549 |
if$ |
| 550 |
% an appropriately-formatted URL is now on the stack |
| 551 |
hrefform #1 = % hypertex |
| 552 |
{ "\special {html:<a href=" quote$ * swap$ * quote$ * "> }{" * 'openinlinelink := |
| 553 |
"\special {html:</a>}" 'closeinlinelink := } |
| 554 |
{ "\href {" swap$ * "} {" * 'openinlinelink := % hrefform=#2 -- hyperref |
| 555 |
% the space between "} {" matters: a URL of just the right length can cause "\% newline em" |
| 556 |
"}" 'closeinlinelink := } |
| 557 |
if$ |
| 558 |
#0 'makeinlinelink := |
| 559 |
} |
| 560 |
'skip$ |
| 561 |
if$ % makeinlinelink |
| 562 |
} |
| 563 |
FUNCTION {add.inlinelink} |
| 564 |
{ openinlinelink empty$ |
| 565 |
'skip$ |
| 566 |
{ openinlinelink swap$ * closeinlinelink * |
| 567 |
"" 'openinlinelink := |
| 568 |
} |
| 569 |
if$ |
| 570 |
} |
| 571 |
EOD |
| 572 |
|
| 573 |
# new.block is defined elsewhere |
| 574 |
|
| 575 |
print OUT <<'EOD'; |
| 576 |
FUNCTION {output.nonnull} |
| 577 |
{ % Save the thing we've been asked to output |
| 578 |
's := |
| 579 |
% If the bracket-state is close.brackets, then add a close-bracket to |
| 580 |
% what is currently at the top of the stack, and set bracket.state |
| 581 |
% to outside.brackets |
| 582 |
bracket.state close.brackets = |
| 583 |
{ "]" * |
| 584 |
outside.brackets 'bracket.state := |
| 585 |
} |
| 586 |
'skip$ |
| 587 |
if$ |
| 588 |
bracket.state outside.brackets = |
| 589 |
{ % We're outside all brackets -- this is the normal situation. |
| 590 |
% Write out what's currently at the top of the stack, using the |
| 591 |
% original output.nonnull function. |
| 592 |
s |
| 593 |
add.inlinelink |
| 594 |
output.nonnull.original % invoke the original output.nonnull |
| 595 |
} |
| 596 |
{ % Still in brackets. Add open-bracket or (continuation) comma, add the |
| 597 |
% new text (in s) to the top of the stack, and move to the close-brackets |
| 598 |
% state, ready for next time (unless inbrackets resets it). If we come |
| 599 |
% into this branch, then output.state is carefully undisturbed. |
| 600 |
bracket.state open.brackets = |
| 601 |
{ " [" * } |
| 602 |
{ ", " * } % bracket.state will be within.brackets |
| 603 |
if$ |
| 604 |
s * |
| 605 |
close.brackets 'bracket.state := |
| 606 |
} |
| 607 |
if$ |
| 608 |
} |
| 609 |
|
| 610 |
% Call this function just before adding something which should be presented in |
| 611 |
% brackets. bracket.state is handled specially within output.nonnull. |
| 612 |
FUNCTION {inbrackets} |
| 613 |
{ bracket.state close.brackets = |
| 614 |
{ within.brackets 'bracket.state := } % reset the state: not open nor closed |
| 615 |
{ open.brackets 'bracket.state := } |
| 616 |
if$ |
| 617 |
} |
| 618 |
|
| 619 |
FUNCTION {format.lastchecked} |
| 620 |
{ lastchecked empty$ |
| 621 |
{ "" } |
| 622 |
{ inbrackets citedstring lastchecked * } |
| 623 |
if$ |
| 624 |
} |
| 625 |
EOD |
| 626 |
print OUT "$mymarkerend\n"; |
| 627 |
} |
| 628 |
|
| 629 |
sub print_webpage_def { |
| 630 |
print OUT "$mymarker...\n"; |
| 631 |
|
| 632 |
# Some of the functions below call new.block, so we need a dummy |
| 633 |
# version, in the case where the style being edited doesn't supply |
| 634 |
# that function. |
| 635 |
if (! $found{newblock}) { |
| 636 |
print OUT "FUNCTION {new.block} % dummy new.block function\n{\n % empty\n}\n\n"; |
| 637 |
$found{newblock} = 1; |
| 638 |
} |
| 639 |
|
| 640 |
print OUT <<'EOD'; |
| 641 |
% Functions for making hypertext links. |
| 642 |
% In all cases, the stack has (link-text href-url) |
| 643 |
% |
| 644 |
% make 'null' specials |
| 645 |
FUNCTION {make.href.null} |
| 646 |
{ |
| 647 |
pop$ |
| 648 |
} |
| 649 |
% make hypertex specials |
| 650 |
FUNCTION {make.href.hypertex} |
| 651 |
{ |
| 652 |
"\special {html:<a href=" quote$ * |
| 653 |
swap$ * quote$ * "> }" * swap$ * |
| 654 |
"\special {html:</a>}" * |
| 655 |
} |
| 656 |
% make hyperref specials |
| 657 |
FUNCTION {make.href.hyperref} |
| 658 |
{ |
| 659 |
"\href {" swap$ * "} {\path{" * swap$ * "}}" * |
| 660 |
} |
| 661 |
FUNCTION {make.href} |
| 662 |
{ hrefform #2 = |
| 663 |
'make.href.hyperref % hrefform = 2 |
| 664 |
{ hrefform #1 = |
| 665 |
'make.href.hypertex % hrefform = 1 |
| 666 |
'make.href.null % hrefform = 0 (or anything else) |
| 667 |
if$ |
| 668 |
} |
| 669 |
if$ |
| 670 |
} |
| 671 |
|
| 672 |
% If inlinelinks is true, then format.url should be a no-op, since it's |
| 673 |
% (a) redundant, and (b) could end up as a link-within-a-link. |
| 674 |
FUNCTION {format.url} |
| 675 |
{ inlinelinks #1 = url empty$ or |
| 676 |
{ "" } |
| 677 |
{ hrefform #1 = |
| 678 |
{ % special case -- add HyperTeX specials |
| 679 |
urlintro "\url{" url * "}" * url make.href.hypertex * } |
| 680 |
{ urlintro "\url{" * url * "}" * } |
| 681 |
if$ |
| 682 |
} |
| 683 |
if$ |
| 684 |
} |
| 685 |
EOD |
| 686 |
|
| 687 |
$formateprintfunction = <<'EOD'; |
| 688 |
FUNCTION {format.eprint} |
| 689 |
{ eprint empty$ |
| 690 |
{ "" } |
| 691 |
{ eprintprefix eprint * eprinturl eprint * make.href } |
| 692 |
if$ |
| 693 |
} |
| 694 |
EOD |
| 695 |
output_replacement_function($found{formateprint}, |
| 696 |
'format.eprint', |
| 697 |
'addeprints', |
| 698 |
$formateprintfunction); |
| 699 |
|
| 700 |
$formatdoifunction = <<'EOD'; |
| 701 |
FUNCTION {format.doi} |
| 702 |
{ doi empty$ |
| 703 |
{ "" } |
| 704 |
{ doi parse.doi % leaves "https://doi.org/DOI" DOI on the stack |
| 705 |
's := 't := |
| 706 |
doiform #1 = |
| 707 |
{ "\doi{" s * "}" * } |
| 708 |
{ doiprefix s * t make.href } |
| 709 |
if$ |
| 710 |
} |
| 711 |
if$ |
| 712 |
} |
| 713 |
EOD |
| 714 |
output_replacement_function($found{formatdoi}, |
| 715 |
'format.doi', |
| 716 |
'adddoi', |
| 717 |
$formatdoifunction); |
| 718 |
|
| 719 |
$formatpubmedfunction = <<'EOD'; |
| 720 |
FUNCTION {format.pubmed} |
| 721 |
{ pubmed empty$ |
| 722 |
{ "" } |
| 723 |
{ pubmedprefix pubmed * pubmedurl pubmed * make.href } |
| 724 |
if$ |
| 725 |
} |
| 726 |
EOD |
| 727 |
output_replacement_function($found{formatpubmed}, |
| 728 |
'format.pubmed', |
| 729 |
'addpubmed', |
| 730 |
$formatpubmedfunction); |
| 731 |
|
| 732 |
print OUT <<'EOD'; |
| 733 |
% Output a URL. We can't use the more normal idiom (something like |
| 734 |
% `format.url output'), because the `inbrackets' within |
| 735 |
% format.lastchecked applies to everything between calls to `output', |
| 736 |
% so that `format.url format.lastchecked * output' ends up with both |
| 737 |
% the URL and the lastchecked in brackets. |
| 738 |
FUNCTION {output.url} |
| 739 |
{ url empty$ |
| 740 |
'skip$ |
| 741 |
{ new.block |
| 742 |
format.url output |
| 743 |
format.lastchecked output |
| 744 |
} |
| 745 |
if$ |
| 746 |
} |
| 747 |
|
| 748 |
FUNCTION {output.web.refs} |
| 749 |
{ |
| 750 |
new.block |
| 751 |
inlinelinks |
| 752 |
'skip$ % links were inline -- don't repeat them |
| 753 |
{ % If the generated DOI will be the same as the URL, |
| 754 |
% then don't print the URL (thanks to Joseph Wright |
| 755 |
% for (the original version of) this code, |
| 756 |
% at http://tex.stackexchange.com/questions/5660) |
| 757 |
adddoi |
| 758 |
doi empty$ { "X" } { doi parse.doi pop$ } if$ % DOI URL to be generated |
| 759 |
url empty$ { "Y" } { url } if$ % the URL, or "Y" if empty |
| 760 |
= % are the strings equal? |
| 761 |
and |
| 762 |
'skip$ |
| 763 |
{ output.url } |
| 764 |
if$ |
| 765 |
addeprints eprint empty$ not and |
| 766 |
{ format.eprint output.nonnull } |
| 767 |
'skip$ |
| 768 |
if$ |
| 769 |
adddoi doi empty$ not and |
| 770 |
{ format.doi output.nonnull } |
| 771 |
'skip$ |
| 772 |
if$ |
| 773 |
addpubmed pubmed empty$ not and |
| 774 |
{ format.pubmed output.nonnull } |
| 775 |
'skip$ |
| 776 |
if$ |
| 777 |
} |
| 778 |
if$ |
| 779 |
} |
| 780 |
|
| 781 |
% Wrapper for output.bibitem.original. |
| 782 |
% If the URL field is not empty, set makeinlinelink to be true, |
| 783 |
% so that an inline link will be started at the next opportunity |
| 784 |
FUNCTION {output.bibitem} |
| 785 |
{ outside.brackets 'bracket.state := |
| 786 |
output.bibitem.original |
| 787 |
inlinelinks url empty$ not doi empty$ not or pubmed empty$ not or eprint empty$ not or and |
| 788 |
{ #1 'makeinlinelink := } |
| 789 |
{ #0 'makeinlinelink := } |
| 790 |
if$ |
| 791 |
} |
| 792 |
|
| 793 |
% Wrapper for fin.entry.original |
| 794 |
FUNCTION {fin.entry} |
| 795 |
{ output.web.refs % urlbst |
| 796 |
makeinlinelink % ooops, it appears we didn't have a title for inlinelink |
| 797 |
{ possibly.setup.inlinelink % add some artificial link text here, as a fallback |
| 798 |
linktextstring output.nonnull } |
| 799 |
'skip$ |
| 800 |
if$ |
| 801 |
bracket.state close.brackets = % urlbst |
| 802 |
{ "]" * } |
| 803 |
'skip$ |
| 804 |
if$ |
| 805 |
fin.entry.original |
| 806 |
} |
| 807 |
|
| 808 |
% Webpage entry type. |
| 809 |
% Title and url fields required; |
| 810 |
% author, note, year, month, and lastchecked fields optional |
| 811 |
% See references |
| 812 |
% ISO 690-2 http://www.nlc-bnc.ca/iso/tc46sc9/standard/690-2e.htm |
| 813 |
% http://www.classroom.net/classroom/CitingNetResources.html |
| 814 |
% http://neal.ctstateu.edu/history/cite.html |
| 815 |
% http://www.cas.usf.edu/english/walker/mla.html |
| 816 |
% for citation formats for web pages. |
| 817 |
FUNCTION {webpage} |
| 818 |
{ output.bibitem |
| 819 |
author empty$ |
| 820 |
{ editor empty$ |
| 821 |
'skip$ % author and editor both optional |
| 822 |
{ format.editors output.nonnull } |
| 823 |
if$ |
| 824 |
} |
| 825 |
{ editor empty$ |
| 826 |
{ format.authors output.nonnull } |
| 827 |
{ "can't use both author and editor fields in " cite$ * warning$ } |
| 828 |
if$ |
| 829 |
} |
| 830 |
if$ |
| 831 |
new.block |
| 832 |
title empty$ 'skip$ 'possibly.setup.inlinelink if$ |
| 833 |
format.title "title" output.check |
| 834 |
inbrackets onlinestring output |
| 835 |
new.block |
| 836 |
year empty$ |
| 837 |
'skip$ |
| 838 |
{ format.date "year" output.check } |
| 839 |
if$ |
| 840 |
% We don't need to output the URL details ('lastchecked' and 'url'), |
| 841 |
% because fin.entry does that for us, using output.web.refs. The only |
| 842 |
% reason we would want to put them here is if we were to decide that |
| 843 |
% they should go in front of the rather miscellaneous information in 'note'. |
| 844 |
new.block |
| 845 |
note output |
| 846 |
fin.entry |
| 847 |
} |
| 848 |
EOD |
| 849 |
|
| 850 |
print OUT "$mymarkerend\n\n\n"; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
sub output_replacement_function { |
| 855 |
my $emit_function = $_[0]; |
| 856 |
my $function_name = $_[1]; |
| 857 |
my $disabling_variable = $_[2]; |
| 858 |
my $function_definition_string = $_[3]; |
| 859 |
|
| 860 |
if ($emit_function > 0) { |
| 861 |
print OUT <<"EOD"; |
| 862 |
%%% The style file $infile already supports $function_name, |
| 863 |
%%% but it might not do so in the same way as urlbst expects. |
| 864 |
%%% I've therefore left $infile 's function unchanged, |
| 865 |
%%% and disabled urlbst's version; proceed with some caution. |
| 866 |
EOD |
| 867 |
|
| 868 |
print STDERR "$progname: WARNING: style file $infile already includes a $function_name function;\nyou may need to disable the urlbst version by setting \$settings{$disabling_variable} to zero.\nYou might want to edit the output file (search for $function_name).\n"; |
| 869 |
|
| 870 |
($t = $function_definition_string) =~ s/\n/\n%%% /g; |
| 871 |
print OUT "%%% " . $t . "$mymarker\n"; |
| 872 |
} else { |
| 873 |
print OUT $function_definition_string; |
| 874 |
} |
| 875 |
print OUT "\n"; |
| 876 |
} |
| 877 |
|
| 878 |
sub print_missing_functions { |
| 879 |
# We've got to the bit of the file which handles the entry |
| 880 |
# types, so write out the webpage entry handler. This uses |
| 881 |
# the format.date function, which which many but not all |
| 882 |
# bst files have (for example, apalike doesn't). So |
| 883 |
# check that we either have found this function already, or |
| 884 |
# add it. |
| 885 |
if (! $found{formatdate}) { |
| 886 |
if ($found{entrymonth}) { |
| 887 |
print OUT <<'EOD'; |
| 888 |
FUNCTION {format.date} |
| 889 |
{ year empty$ |
| 890 |
{ month empty$ |
| 891 |
{ "" } |
| 892 |
{ "there's a month but no year in " cite$ * warning$ |
| 893 |
month |
| 894 |
} |
| 895 |
if$ |
| 896 |
} |
| 897 |
{ month empty$ |
| 898 |
'year |
| 899 |
{ month " " * year * } |
| 900 |
if$ |
| 901 |
} |
| 902 |
if$ |
| 903 |
} |
| 904 |
EOD |
| 905 |
} else { |
| 906 |
print OUT <<'EOD'; |
| 907 |
FUNCTION {format.date} |
| 908 |
{ year empty$ |
| 909 |
'skip$ |
| 910 |
{ %write$ |
| 911 |
"(" year * ")" * |
| 912 |
} |
| 913 |
if$ |
| 914 |
} |
| 915 |
EOD |
| 916 |
} |
| 917 |
$found{formatdate} = 1; |
| 918 |
} |
| 919 |
|
| 920 |
# If the style file didn't supply a format.title function, then supply |
| 921 |
# one here (the {webpage} function requires it). |
| 922 |
if (! $found{formattitle}) { |
| 923 |
print OUT <<'EOD'; |
| 924 |
FUNCTION {format.title} |
| 925 |
{ title empty$ |
| 926 |
{ "" } |
| 927 |
{ title "t" change.case$ } |
| 928 |
if$ |
| 929 |
} |
| 930 |
EOD |
| 931 |
$found{formattitle} = 1; |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
# Utility function: Keep track of open and close braces in the string argument. |
| 936 |
# Keep state in $bracematchtotal, return the current value. |
| 937 |
sub bracematcher { |
| 938 |
my $s = shift; |
| 939 |
$s =~ s/[^\{\}]//g; |
| 940 |
#print "s=$s\n"; |
| 941 |
foreach my $c (split (//, $s)) { |
| 942 |
$bracematchtotal += ($c eq '{' ? 1 : -1); |
| 943 |
} |
| 944 |
return $bracematchtotal; |
| 945 |
} |
| 946 |
|
| 947 |
# Utility function: use bracematcher to copy the complete block which starts |
| 948 |
# on or after the current line. |
| 949 |
sub copy_block { |
| 950 |
$bracematchtotal = 0; |
| 951 |
# copy any leading lines which don't have braces (presumably comments) |
| 952 |
while (defined ($line = <IN>) && ($line !~ /{/)) { |
| 953 |
print OUT $line; |
| 954 |
} |
| 955 |
while (defined ($line) && bracematcher($line) > 0) { |
| 956 |
print OUT $line; |
| 957 |
$line = <IN>; |
| 958 |
} |
| 959 |
print OUT "$line\n"; # print out terminating \} (assumed |
| 960 |
# alone on the line) |
| 961 |
} |