| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
=pod |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
ltx2crossrefxml.pl - create XML files for submitting to crossref.org |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
ltx2crossrefxml [B<--debug>] [B<-c> I<config_file>] [B<-o> I<output_file>] [B<-input-is-xml>] |
| 12 |
I<latex_file1> I<latex_file2> ... |
| 13 |
|
| 14 |
=head1 OPTIONS |
| 15 |
|
| 16 |
=over 4 |
| 17 |
|
| 18 |
=item B<-c> I<config_file> |
| 19 |
|
| 20 |
Configuration file. If this file is absent, defaults are used. |
| 21 |
See below for its format. |
| 22 |
|
| 23 |
=item B<-o> I<output_file> |
| 24 |
|
| 25 |
Output file. If this option is not used, the XML is output to stdout. |
| 26 |
|
| 27 |
=item B<-rpi-is-xml> |
| 28 |
|
| 29 |
Do not transform author and title input strings, assume they are valid XML. |
| 30 |
|
| 31 |
=item B<--debug> |
| 32 |
|
| 33 |
Output some progress reports. |
| 34 |
|
| 35 |
=back |
| 36 |
|
| 37 |
The usual C<--help> and C<--version> options are also supported. Options |
| 38 |
can begin with either C<-> or C<-->, and ordered arbitrarily. |
| 39 |
|
| 40 |
=head1 DESCRIPTION |
| 41 |
|
| 42 |
For each given I<latex_file>, this script reads C<.rpi> and (if they |
| 43 |
exist) C<.bbl> and C<.aux> files and outputs corresponding XML that |
| 44 |
can be uploaded to Crossref (L<https://crossref.org>). Any extension |
| 45 |
of I<latex_file> is ignored, and I<latex_file> itself is not read (and |
| 46 |
need not even exist). |
| 47 |
|
| 48 |
Each C<.rpi> file specifies the metadata for a single article to be |
| 49 |
uploaded to Crossref (a C<journal_article> element in their schema); an |
| 50 |
example is below. These files are output by the C<resphilosophica> |
| 51 |
package (L<https://ctan.org/pkg/resphilosophica>), C<aomart> package |
| 52 |
(L<https://ctan.org/pkg/aomart>), the TUGboat publication procedure |
| 53 |
(L<https://tug.org/TUGboat/repository.html>) and other packages. They |
| 54 |
can also be created by hand or by whatever other method you implement. |
| 55 |
|
| 56 |
Any C<.bbl>, C<.aux>, and C<.bib> files are used for the citation |
| 57 |
information in the output XML. See the L<CITATIONS> section below. |
| 58 |
|
| 59 |
Unless C<--rpi-is-xml> is specified, for all text (authors, title, |
| 60 |
citations), standard TeX control sequences are replaced with plain text |
| 61 |
or UTF-8 or eliminated, as appropriate. The C<LaTeX::ToUnicode::convert> |
| 62 |
routine is used for this (L<https://ctan.org/pkg/bibtexperllibs>). |
| 63 |
Tricky TeX control sequences will almost surely not be handled |
| 64 |
correctly. |
| 65 |
|
| 66 |
If C<--rpi-is-xml> is given, the author and title strings from the rpi |
| 67 |
files are output as-is, assuming they are valid XML; no checking is |
| 68 |
done. |
| 69 |
|
| 70 |
Citation text from C<.bbl> files is always converted from LaTeX to plain |
| 71 |
text. |
| 72 |
|
| 73 |
This script just writes an XML file. It's up to you to do the |
| 74 |
uploading to Crossref; for example, you can use their Java tool |
| 75 |
C<crossref-upload-tool.jar> |
| 76 |
(L<https://www.crossref.org/education/member-setup/direct-deposit-xml/https-post>). |
| 77 |
|
| 78 |
For the definition of the Crossref schema currently output by this |
| 79 |
script, see |
| 80 |
L<https://data.crossref.org/reports/help/schema_doc/5.4.0/index.html> |
| 81 |
with additional links and information at |
| 82 |
L<https://www.crossref.org/documentation/schema-library/metadata-deposit-schema-5-4-0/>. |
| 83 |
|
| 84 |
=head1 CONFIGURATION FILE FORMAT |
| 85 |
|
| 86 |
The configuration file is read as Perl code. Thus, comment lines |
| 87 |
starting with C<#> and blank lines are ignored. The other lines are |
| 88 |
typically assignments in the form (spaces are optional): |
| 89 |
|
| 90 |
$variable = value ; |
| 91 |
|
| 92 |
Usually the value is a C<"string"> enclosed in ASCII double-quote or |
| 93 |
single-quote characters, per Perl syntax. The idea is to specify the |
| 94 |
user-specific and journal-specific values needed for the Crossref |
| 95 |
upload. The variables which are used are these: |
| 96 |
|
| 97 |
$depositorName = "Depositor Name"; |
| 98 |
$depositorEmail = '[email protected]'; |
| 99 |
$registrant = 'Registrant'; # required, organization name |
| 100 |
$fullTitle = "FULL TITLE"; # required, journal name |
| 101 |
$issn = "1234-5678"; # required, ISSN |
| 102 |
$abbrevTitle = "ABBR. TTL."; # optional, abbreviated journal name |
| 103 |
$coden = "CODEN"; # optional |
| 104 |
|
| 105 |
For a given run, all C<.rpi> data read is assumed to belong to the |
| 106 |
journal that is specified in the configuration file. More precisely, the |
| 107 |
configuration data is written as a C<journal_metadata> element, with |
| 108 |
given C<full_title>, C<issn>, etc., and then each C<.rpi> is written as |
| 109 |
C<journal_issue> plus C<journal_article> elements. |
| 110 |
|
| 111 |
The configuration file can also define a Perl function |
| 112 |
C<LaTeX_ToUnicode_convert_hook>. If it is defined, it is called at the |
| 113 |
beginning of the procedure that converts LaTeX text to Unicode, which is |
| 114 |
done with the L<LaTeX::ToUnicode> module, from the C<bibtexperllibs> |
| 115 |
package (L<https://ctan.org/pkg/bibtexperllibs>). The function must |
| 116 |
accept one string (the LaTeX text), and return one string (presumably |
| 117 |
the transformed string). The standard conversions are then applied to |
| 118 |
the returned string, so the configured function need only handle special |
| 119 |
cases, such as control sequences particular to the journal at hand. |
| 120 |
(See TUGboat's C<ltx2crossrefxml-tugboat.cfg> for an example.) |
| 121 |
|
| 122 |
The configuration file can also define a hash C<BibentryToCrossref>) that |
| 123 |
maps Crossref entry types to BibTeX entry types used in the |
| 124 |
bibliography processing (see L<CITATIONS>), for example |
| 125 |
|
| 126 |
%BibentryToCrossref = ('WEBPAGE' => 'other', |
| 127 |
'MISC' => 'other'); |
| 128 |
|
| 129 |
The keys in this hash must be in the upper case, while the entries |
| 130 |
must be in the lower case. |
| 131 |
|
| 132 |
=head1 RPI FILE FORMAT |
| 133 |
|
| 134 |
Here's the (relevant part of the) C<.rpi> file corresponding to the |
| 135 |
C<rpsample.tex> example in the C<resphilosophica> package |
| 136 |
(L<https://ctan.org/pkg/resphilosophica>): |
| 137 |
|
| 138 |
%authors=Boris Veytsman\and A. U. Th{\o }r\and C. O. R\"espondent |
| 139 |
%title=A Sample Paper:\\ \emph {A Template} |
| 140 |
%year=2012 |
| 141 |
%volume=90 |
| 142 |
%issue=1--2 |
| 143 |
%startpage=1 |
| 144 |
%endpage=1 |
| 145 |
%doi=10.11612/resphil.A31245 |
| 146 |
%paperUrl=http://borisv.lk.net/paper12 |
| 147 |
%publicationType=full_text |
| 148 |
|
| 149 |
Other lines, some not beginning with %, are ignored (and not shown). |
| 150 |
For more details on processing, see the code. |
| 151 |
|
| 152 |
The C<%paperUrl> value is what will be associated with the given C<%doi> |
| 153 |
(output as the C<resource> element). Crossref strongly recommends that |
| 154 |
the url be for a so-called landing page, and not directly for a pdf |
| 155 |
(L<https://www.crossref.org/education/member-setup/creating-a-landing-page/>). |
| 156 |
Special case: if the url is not specified, |
| 157 |
and the journal is I<S<Res Philosophica>>, |
| 158 |
a special-purpose search url using L<pdcnet.org> is returned. |
| 159 |
Any other journal must always specify this. |
| 160 |
|
| 161 |
The C<%authors> field is split at C<\and> (ignoring whitespace before |
| 162 |
and after), and output as the C<contributors> element, using |
| 163 |
C<sequence="first"> for the first listed, C<sequence="additional"> for |
| 164 |
the remainder. The authors are parsed using C<BibTeX::Parser::Author> |
| 165 |
(L<https://ctan.org/pkg/bibtexperllibs>). |
| 166 |
|
| 167 |
If the C<%publicationType> is not specified, it defaults to |
| 168 |
C<full_text>, since that has historically been the case; C<full_text> |
| 169 |
can also be given explicitly. The other values allowed by the Crossref |
| 170 |
schema are C<abstract_only> and C<bibliographic_record>. Finally, if the |
| 171 |
value is C<omit>, the C<publication_type> attribute is omitted entirely |
| 172 |
from the given C<journal_article> element. |
| 173 |
|
| 174 |
Each C<.rpi> must contain information for only one article, but multiple |
| 175 |
files can be read in a single run. It would not be difficult to support |
| 176 |
multiple articles in a single C<.rpi> file, but it makes debugging and |
| 177 |
error correction easier to keep the input to one article per file. |
| 178 |
|
| 179 |
=head2 MORE ABOUT AUTHOR NAMES |
| 180 |
|
| 181 |
The three formats for names recognized are (not coincidentally) the same |
| 182 |
as BibTeX: |
| 183 |
|
| 184 |
First von Last |
| 185 |
von Last, First |
| 186 |
von Last, Jr., First |
| 187 |
|
| 188 |
The forms can be freely intermixed within a single C<%authors> line, |
| 189 |
separated with C<\and> (including the backslash). Commas as name |
| 190 |
separators are not supported, unlike BibTeX. |
| 191 |
|
| 192 |
In short, you may almost always use the first form; you shouldn't if |
| 193 |
either there's a Jr part, or the Last part has multiple tokens but |
| 194 |
there's no von part. See the C<btxdoc> (``BibTeXing'' by Oren Patashnik) |
| 195 |
document for details. The authors are parsed using |
| 196 |
C<BibTeX::Parser::Author> (L<https://ctan.org/pkg/bibtexperllibs>). |
| 197 |
|
| 198 |
In the C<%authors> line of a C<.rpi> file, some secondary directives are |
| 199 |
recognized, indicated by C<|> characters. Easiest to explain with an |
| 200 |
example: |
| 201 |
|
| 202 |
%authors=|organization|\LaTeX\ Project Team \and Alex Brown|orcid=123 |
| 203 |
|
| 204 |
Thus: 1) if C<|organization|> is specified, the author name will be output |
| 205 |
as an C<organization> contributor, instead of the usual C<person_name>, |
| 206 |
as the Crossref schema requires. |
| 207 |
|
| 208 |
2) If C<|orcid=I<value>|> is specified, the I<value> is output as an |
| 209 |
C<ORCID> element for that C<person_name>. |
| 210 |
|
| 211 |
These two directives, C<|organization>| and C<|orcid|> are mutually |
| 212 |
exclusive, because that's how the Crossref schema defines them. The C<=> |
| 213 |
sign after C<orcid> is required, while all spaces after the C<orcid> |
| 214 |
keyword are ignored. Other than that, the ORCID value is output |
| 215 |
literally. (E.g., the ORCID value of C<123> above is clearly invalid, |
| 216 |
but it would be output anyway, with no warning.) |
| 217 |
|
| 218 |
Extra C<|> characters, at the beginning or end of the entire C<%authors> |
| 219 |
string, or doubled in the middle, are accepted and ignored. Whitespace |
| 220 |
is ignored around all C<|> characters. |
| 221 |
|
| 222 |
=head1 CITATIONS |
| 223 |
|
| 224 |
Each C<.bbl> file corresponding to an input C<.rpi> file is read and |
| 225 |
used to output a C<citation_list> element for that C<journal_article> in |
| 226 |
the output XML. If no C<.bbl> file exists for a given C<.rpi>, |
| 227 |
no C<citation_list> is output for that article. |
| 228 |
|
| 229 |
The C<.bbl> files are processed to create the C<unstructured_citation> |
| 230 |
references defined by Crossref, that is, the contents of the citation |
| 231 |
(each paragraph in the C<.bbl>) as a single flat string without markup |
| 232 |
of any kind, including font changes. |
| 233 |
|
| 234 |
Bibliography text is unconditionally converted from TeX to XML, via the |
| 235 |
method described above. It is not unusual for the conversion to be |
| 236 |
incomplete or incorrect. It is up to you to check for this; e.g., if any |
| 237 |
backslashes or pairs of dollar signs remain in the output, it is most |
| 238 |
likely an error. |
| 239 |
|
| 240 |
Furthermore, it is assumed that the C<.bbl> file contains a sequence of |
| 241 |
references, each starting with C<\bibitem{I<KEY>}> (which itself must be |
| 242 |
at the beginning of a line, preceded only by whitespace), and the whole |
| 243 |
bibliography ending with C<\end{thebibliography}> (similarly at the |
| 244 |
beginning of a line). A C<.bbl> file not following this format will not |
| 245 |
produce useful results. The C<.bbl> file can be created by hand, or with |
| 246 |
BibTeX, or any other method, as long as it has this format. |
| 247 |
|
| 248 |
The C<key> attribute for the C<citation> element is taken as the I<KEY> |
| 249 |
argument to the C<\bibitem> command. The sequential number of the |
| 250 |
citation (1, 2, ...). The argument to C<\bibitem> can be |
| 251 |
empty (C<\bibitem{}>, and the sequence number will be used on its own. |
| 252 |
Although TeX will not handle empty C<\bibitem> keys, it can be |
| 253 |
convenient when creating a C<.bbl> purely for Crossref. |
| 254 |
|
| 255 |
The C<.rpi> file is also checked for the bibliography information, in |
| 256 |
this same format. |
| 257 |
|
| 258 |
Crossref's structured citations are added as follows: |
| 259 |
|
| 260 |
=over 4 |
| 261 |
|
| 262 |
=item 1. If an C<.aux> file is present, it is checked for any C<\bibdata> |
| 263 |
commands. The C<bib> files in these commands are read, and the |
| 264 |
information there is used to generate XML entries. The script uses |
| 265 |
C<kpsewhich> to look for the bib files, so the usual BibTeX |
| 266 |
conventions for the search paths are followed. |
| 267 |
|
| 268 |
=item 2. For any citation the corresponding entry in the C<bib> file is |
| 269 |
processed. |
| 270 |
|
| 271 |
=item 3. The Crossref entry type is determined according to the algorithm |
| 272 |
describe below (L<CITATION ENTRY TYPES>). |
| 273 |
|
| 274 |
=item 4. The entry fields are used to populate structured citation. |
| 275 |
|
| 276 |
=back |
| 277 |
|
| 278 |
=head2 CITATION ENTRY TYPES |
| 279 |
|
| 280 |
The current Crossref schema |
| 281 |
L<https://data.crossref.org/reports/help/schema_doc/5.4.0/schema_5_4_0.html> |
| 282 |
defines C<type> attribute for a citation. Unfortunately the list of |
| 283 |
possible types does not fully coincide with the list of BibTeX entry |
| 284 |
types. Therefore the script uses the following algorithm to determine |
| 285 |
the Crossref entry type for a citation: |
| 286 |
|
| 287 |
=over 4 |
| 288 |
|
| 289 |
=item 1. If the entry has the field C<crossrefentrytype>, it is used. |
| 290 |
|
| 291 |
=item 2. Otherwise if BibTeX entry type appears in the hash |
| 292 |
C<BibentryToCrossref>) in the configuration file (L<CONFIGURATION FILE |
| 293 |
FORMAT>), its value is used. |
| 294 |
|
| 295 |
=item 3. Otherwise the default mapping is used. The script knows many |
| 296 |
BibTeX entry types, and should do a good job in most cases. |
| 297 |
|
| 298 |
=back |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
=head1 EXAMPLES |
| 303 |
|
| 304 |
ltx2crossrefxml.pl ../paper1/paper1.tex ../paper2/paper2.tex \ |
| 305 |
-o result.xml |
| 306 |
|
| 307 |
ltx2crossrefxml.pl -c myconfig.cfg paper.tex -o paper.xml |
| 308 |
|
| 309 |
=head1 AUTHOR |
| 310 |
|
| 311 |
Boris Veytsman L<https://github.com/borisveytsman/crossrefware> |
| 312 |
|
| 313 |
=head1 COPYRIGHT AND LICENSE |
| 314 |
|
| 315 |
Copyright (C) 2012-2025 Boris Veytsman |
| 316 |
|
| 317 |
This is free software. You may redistribute copies of it under the |
| 318 |
terms of the GNU General Public License (any version) |
| 319 |
L<https://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the |
| 320 |
extent permitted by law. |
| 321 |
|
| 322 |
=cut |
| 323 |
|
| 324 |
use strict; |
| 325 |
use warnings; |
| 326 |
|
| 327 |
use Cwd; |
| 328 |
use File::Basename; |
| 329 |
use File::Spec; |
| 330 |
|
| 331 |
BEGIN { |
| 332 |
# find files relative to our installed location within TeX Live |
| 333 |
chomp(my $TLMaster = `kpsewhich -var-value=TEXMFROOT`); # TL root |
| 334 |
if (length($TLMaster)) { |
| 335 |
unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; |
| 336 |
} |
| 337 |
# find development bibtexperllibs in sibling checkout to this script, |
| 338 |
# even if $0 is a symlink. All irrelevant when using from an installation. |
| 339 |
my $real0 = Cwd::abs_path($0); |
| 340 |
my $scriptdir = File::Basename::dirname($real0); |
| 341 |
my $dev_btxperllibs = Cwd::abs_path("$scriptdir/../bibtexperllibs"); |
| 342 |
# we need the lib/ subdirectories inside ... |
| 343 |
unshift (@INC, glob ("$dev_btxperllibs/*/lib")) if -d $dev_btxperllibs; |
| 344 |
} |
| 345 |
|
| 346 |
use POSIX qw(strftime); |
| 347 |
|
| 348 |
our $BibTeX_Parser_options = {}; # can be set by config file |
| 349 |
use BibTeX::Parser::Author; |
| 350 |
use BibTeX::Parser; |
| 351 |
use LaTeX::ToUnicode; |
| 352 |
use IO::File; |
| 353 |
|
| 354 |
my $USAGE = <<END; |
| 355 |
Usage: $0 [--debug] [-c CONFIG] [-o OUTPUT] [--rpi-is-xml] LTXFILE... |
| 356 |
|
| 357 |
Convert .rpi and (if any are present) .bbl and .aux files corresponding |
| 358 |
to each LTXFILE to xml, for submitting to crossref.org. The LTXFILE is |
| 359 |
not read, and need not even exist; any extension given is replaced by |
| 360 |
.rpi, .bbl, .aux. |
| 361 |
|
| 362 |
The .rpi files are plain text, with values on lines beginning with %, as |
| 363 |
output by (for example) the resphilosophica LaTeX package. The .bbl |
| 364 |
files (used to generate <unstructured_citation> elements) are as output |
| 365 |
by BibTeX. The .aux files (used to generate structured citation |
| 366 |
elements) are as written by LaTeX. All may also be created by other |
| 367 |
methods. The documentation for this script has examples. |
| 368 |
|
| 369 |
The xml is written to standard output by default; the -o (--output) |
| 370 |
option overrides this. |
| 371 |
|
| 372 |
If the -c (--config) option is given, the given file is read before any |
| 373 |
processing is done. This is used to define journal-specific defaults. |
| 374 |
|
| 375 |
The usual --help and --version options are also supported. |
| 376 |
--debug outputs some progress reports to stderr. |
| 377 |
|
| 378 |
For an example of using this script and associatd code, see the TUGboat |
| 379 |
processing at |
| 380 |
https://github.com/TeXUsersGroup/tugboat/tree/trunk/capsules/crossref. |
| 381 |
|
| 382 |
This script depends on https://github.com/borisveytsman/bibtexperllibs. |
| 383 |
|
| 384 |
For somewhat more documentation, see the man page or crossrefware.pdf. |
| 385 |
|
| 386 |
Development sources, bug tracker: https://github.com/borisveytsman/crossrefware |
| 387 |
Releases: https://ctan.org/pkg/crossrefware |
| 388 |
END |
| 389 |
|
| 390 |
my $VERSION = <<END; |
| 391 |
ltx2crossrefxml (crossrefware) 2025-07-09 |
| 392 |
This is free software: you are free to change and redistribute it, under |
| 393 |
the terms of the GNU General Public License |
| 394 |
http://www.gnu.org/licenses/gpl.html (any version). |
| 395 |
There is NO WARRANTY, to the extent permitted by law. |
| 396 |
|
| 397 |
Written by Boris Veytsman with contributions by Karl Berry. |
| 398 |
END |
| 399 |
use Getopt::Long; |
| 400 |
my %opts; |
| 401 |
|
| 402 |
GetOptions( |
| 403 |
"config|c=s" => \($opts{c}), |
| 404 |
"debug!" => \($opts{debug}), |
| 405 |
"output|o=s" => \($opts{o}), |
| 406 |
"rpi-is-xml!"=> \($opts{xi}), |
| 407 |
"version|V" => \($opts{V}), |
| 408 |
"help|?" => \($opts{h})); |
| 409 |
|
| 410 |
if ($opts{h}) { print "$USAGE\n$VERSION"; exit 0; } |
| 411 |
if ($opts{V}) { print $VERSION; exit 0; } |
| 412 |
|
| 413 |
use utf8; |
| 414 |
binmode(STDOUT, ":utf8"); |
| 415 |
|
| 416 |
|
| 417 |
################################################################ |
| 418 |
# Defaults and parameters |
| 419 |
################################################################ |
| 420 |
|
| 421 |
*OUT=*STDOUT; |
| 422 |
|
| 423 |
if (defined($opts{o})) { |
| 424 |
open (OUT, ">$opts{o}") or die "open($opts{o}) for writing failed: $!\n"; |
| 425 |
binmode(OUT, ":utf8") |
| 426 |
} |
| 427 |
|
| 428 |
our $ERROR_COUNT = 0; |
| 429 |
our $depositorName = 'DEPOSITOR_NAME'; |
| 430 |
our $depositorEmail = 'DEPOSITOR_EMAIL'; |
| 431 |
our $registrant = 'REGISTRANT'; |
| 432 |
our $fullTitle = "FULL TITLE"; |
| 433 |
our $abbrevTitle = "ABBR. TTL."; |
| 434 |
our $issn = "0000-0000"; |
| 435 |
our $coden = "CODEN"; |
| 436 |
our $timestamp = strftime("%Y%m%d%H%M%S", gmtime); |
| 437 |
# use timestamp in batchid, since the value is supposed to be unique |
| 438 |
# for every submission to crossref by a given publisher. |
| 439 |
# https://data.crossref.org/reports/help/schema_doc/5.4.0/schema_5_4_0.html#doi_batch_id |
| 440 |
our $batchId="ltx2crossref-$timestamp-$$"; |
| 441 |
|
| 442 |
# Default mappings of BibTeX entries to Crossref types |
| 443 |
our %BibentryToCrossrefDefault = ( |
| 444 |
# Default types |
| 445 |
'ARTICLE' => 'journal_article', |
| 446 |
'BOOK' => 'book', |
| 447 |
'BOOKLET' => 'book', |
| 448 |
'CONFERENCE' => 'conference_paper', |
| 449 |
'INBOOK' => 'book_chapter', |
| 450 |
'INCOLLECTION' => 'book_chapter', |
| 451 |
'INPROCEEDINGS' => 'conference_paper', |
| 452 |
'MANUAL' => 'software', |
| 453 |
'MASTERSTHESIS' => 'dissertation', |
| 454 |
'MISC' => 'other', |
| 455 |
'PHDTHESIS' => 'dissertation', |
| 456 |
'PROCEEDINGS' => 'conference_proceedings', |
| 457 |
'TECHREPORT' => 'report', |
| 458 |
'UNPUBLISHED' => 'other', |
| 459 |
# From TUGboat |
| 460 |
'CTAN' => 'software', |
| 461 |
'ONLINE' => 'web_resource', |
| 462 |
'SOFTWARE' => 'software', |
| 463 |
'WEBPAGE' => 'web_resource', |
| 464 |
# From ACM |
| 465 |
'UNDERREVIEW' => 'other', |
| 466 |
'PRESENTATION' => 'poster', |
| 467 |
'GAME' => 'software', |
| 468 |
'VIDEO' => 'web_resource', |
| 469 |
'ARTIFACTSOFTWARE' => 'software', |
| 470 |
'ARTIFACTDATASET' => 'dataset', |
| 471 |
'DATASET' => 'dataset', |
| 472 |
'PRERINT' => 'preprint', |
| 473 |
# Some other popular types |
| 474 |
'PATENT' => 'patent', |
| 475 |
'STANDARD' => 'standard' |
| 476 |
); |
| 477 |
|
| 478 |
our %BibentryToCrossref; # empty hash, might be overriden by config |
| 479 |
|
| 480 |
if ($opts{c}) { |
| 481 |
if (-r $opts{c}) { |
| 482 |
# if config arg is absolute, fine; if not, prepend "./" as slightly |
| 483 |
# less troublesome than putting "." in the @INC path. |
| 484 |
my $rel = (File::Spec->file_name_is_absolute($opts{c}) ? "" : "./"); |
| 485 |
require "$rel$opts{c}"; |
| 486 |
} else { |
| 487 |
die "Cannot read config file $opts{c}. Goodbye."; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
PrintHead(); |
| 494 |
|
| 495 |
# |
| 496 |
# The hash %papers. Keys year->vol->issue->number |
| 497 |
# |
| 498 |
my %papers; |
| 499 |
|
| 500 |
# Read the papers. |
| 501 |
foreach my $file (@ARGV) { |
| 502 |
&debug("reading paper: $file\n"); |
| 503 |
AddPaper($file); |
| 504 |
} |
| 505 |
|
| 506 |
# Write the papers. |
| 507 |
foreach my $year (keys %papers) { |
| 508 |
foreach my $volume (keys %{$papers{$year}}) { |
| 509 |
foreach my $issue (keys %{$papers{$year}->{$volume}}) { |
| 510 |
PrintIssueHead($year, $volume, $issue); |
| 511 |
my $paperList = $papers{$year}->{$volume}->{$issue}; |
| 512 |
&debug("writing papers for " |
| 513 |
. "year=$year, volume=$volume, issue=$issue\n"); |
| 514 |
# Nice to keep the output xml in stable order, so sort |
| 515 |
# by starting page. Doesn't matter if it's not perfect. |
| 516 |
foreach my $paper (sort { $a->{startpage} <=> $b->{startpage} } |
| 517 |
@{$paperList}) { |
| 518 |
PrintPaper($paper); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
PrintTail(); |
| 525 |
exit($ERROR_COUNT); |
| 526 |
|
| 527 |
|
| 528 |
##################################################### |
| 529 |
# Printing the head and the tail |
| 530 |
##################################################### |
| 531 |
sub PrintHead { |
| 532 |
# do not output the <coden> or <abbrev_title> if the journal doesn't |
| 533 |
# have them. |
| 534 |
my $indent = " "; |
| 535 |
my $coden_out = $coden ne "CODEN" ? "\n$indent<coden>$coden</coden>" : ""; |
| 536 |
my $abbrev_title_out = $abbrevTitle ne "ABBR. TTL." |
| 537 |
? "\n$indent<abbrev_title>$abbrevTitle</abbrev_title>" |
| 538 |
: ""; |
| 539 |
|
| 540 |
# Crossref schema info: |
| 541 |
# https://www.crossref.org/documentation/schema-library/schema-versions/ |
| 542 |
print OUT <<END; |
| 543 |
<doi_batch |
| 544 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 545 |
version="5.4.0" |
| 546 |
xmlns="http://www.crossref.org/schema/5.4.0" |
| 547 |
xsi:schemaLocation="http://www.crossref.org/schema/5.4.0 |
| 548 |
https://www.crossref.org/schemas/crossref5.4.0.xsd"> |
| 549 |
<head> |
| 550 |
<doi_batch_id>$batchId</doi_batch_id> |
| 551 |
<timestamp>$timestamp</timestamp> |
| 552 |
<depositor> |
| 553 |
<depositor_name>$depositorName</depositor_name> |
| 554 |
<email_address>$depositorEmail</email_address> |
| 555 |
</depositor> |
| 556 |
<registrant>$registrant</registrant> |
| 557 |
</head> |
| 558 |
<body><journal> |
| 559 |
<journal_metadata language="en"> |
| 560 |
<full_title>$fullTitle</full_title>$abbrev_title_out |
| 561 |
<issn>$issn</issn>$coden_out |
| 562 |
</journal_metadata> |
| 563 |
END |
| 564 |
} |
| 565 |
|
| 566 |
sub PrintTail { |
| 567 |
print OUT <<END; |
| 568 |
</journal></body> |
| 569 |
</doi_batch> |
| 570 |
END |
| 571 |
|
| 572 |
return; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
####################################################### |
| 577 |
# Adding one paper from $file.rpi and .bbl to global %papers. |
| 578 |
####################################################### |
| 579 |
sub AddPaper { |
| 580 |
my $file = shift; |
| 581 |
my ($name,$path,$suffix) = fileparse($file, '\.[^\.]*$'); |
| 582 |
my $rpifile = File::Spec->catfile($path, "$name.rpi"); |
| 583 |
open (RPI, $rpifile) |
| 584 |
or die "$0: open($rpifile) failed: $! (did you process $file?)\n"; |
| 585 |
my %data; |
| 586 |
&debug("reading rpi file: $rpifile\n"); |
| 587 |
while (<RPI>) { |
| 588 |
chomp; |
| 589 |
if (/^%([^=]*)\s*=\s*(.*)\s*$/) { |
| 590 |
if (exists $data{$1}) { |
| 591 |
warn "$rpifile:$.: already saw data{$1}=$data{$1};" |
| 592 |
. " an .rpi file should have data for only one article," |
| 593 |
. " but overwriting with `$2' anyway.\n"; |
| 594 |
} |
| 595 |
$data{$1} = $2; |
| 596 |
} |
| 597 |
} |
| 598 |
close RPI; |
| 599 |
|
| 600 |
# also look for bibliographies in FILE.bbl. |
| 601 |
my @bibliography; |
| 602 |
foreach my $bblfile ($rpifile, File::Spec->catfile($path, "$name.bbl")) { |
| 603 |
push (@bibliography, AddBibliography($bblfile)); |
| 604 |
} |
| 605 |
$data{'bibliography'} = \@bibliography; |
| 606 |
|
| 607 |
my @refs; |
| 608 |
foreach my $citation_hash (@bibliography) { |
| 609 |
@refs = (@refs, keys (%$citation_hash)); |
| 610 |
} |
| 611 |
|
| 612 |
$data{'bibtexbib'} = |
| 613 |
AddBibtexBib(File::Spec->catfile($path, "$name.aux"), |
| 614 |
\@refs); |
| 615 |
|
| 616 |
|
| 617 |
# Die if the fields we use unconditionally are empty. Not all of |
| 618 |
# them are required by the schema, but we can wait to generalize. |
| 619 |
foreach my $field (qw(title year volume issue startpage endpage doi |
| 620 |
paperUrl)) { |
| 621 |
if (! $data{$field}) { |
| 622 |
die ("$0: field must not be empty: $field\n " |
| 623 |
. &debug_hash_as_string("whole hash", %data)); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
#warn &debug_hash_as_string("new issue $data{volume}:$data{issue}", %data); |
| 628 |
push @{$papers{$data{year}}->{$data{volume}}->{$data{issue}}}, \%data; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
############################################################## |
| 633 |
# Read a list of references from BIBFILE and adding it to the |
| 634 |
# bibliography. Each item is assumed to start with |
| 635 |
# \bibitem{KEY} and the whole bib to end with \end{thebibliography}. |
| 636 |
# |
| 637 |
# We return a list of hashes, each hash with a single key, the citation |
| 638 |
# key with an integer (starting at 1), and its value a flat string of |
| 639 |
# the entry. |
| 640 |
# |
| 641 |
# No conversion of the text is done here. |
| 642 |
############################################################## |
| 643 |
sub AddBibliography { |
| 644 |
my $bibfile = shift; |
| 645 |
open (BIB, $bibfile) or return; |
| 646 |
|
| 647 |
my $insidebibliography = 0; |
| 648 |
my $currpaper = ""; # the current bib entry |
| 649 |
my $bibno = 0; |
| 650 |
my @result; |
| 651 |
my $key; |
| 652 |
while (<BIB>) { |
| 653 |
chomp; |
| 654 |
next if /^\s*%/; # TeX comment line |
| 655 |
s/[ \t]%.*//; # remove TeX comment |
| 656 |
# |
| 657 |
# allow empty \bibitem key for the sake of handwritten bbls. |
| 658 |
# Similarly, might be more stuff on the line when handwritten. |
| 659 |
# Ignore a TeX %comment following. |
| 660 |
if (s/^\s*\\bibitem(?:\[.*?\])?+\s*\{(.*?)\}\s*(%.*$)?//) { |
| 661 |
my $newkey = $1; |
| 662 |
if ($insidebibliography) { |
| 663 |
if ($currpaper) { |
| 664 |
# Append the current sequence number for this citation, |
| 665 |
# since that's what Crossref recommends (sort of). |
| 666 |
# For prettiness, if the key is otherwise empty, |
| 667 |
# don't include a dash beforehand. |
| 668 |
$bibno++; |
| 669 |
$key .= ($key ? "-" : "") . $bibno; |
| 670 |
# |
| 671 |
my %paperhash; |
| 672 |
$paperhash{$key} = $currpaper; |
| 673 |
push @result, \%paperhash; |
| 674 |
} |
| 675 |
} |
| 676 |
# The citation key (required by schema) starts as the bibitem key. |
| 677 |
$key = $newkey; |
| 678 |
|
| 679 |
$currpaper = $_; |
| 680 |
$insidebibliography = 1; |
| 681 |
next; |
| 682 |
} |
| 683 |
if (/^\s*\\end\{thebibliography\}/) { |
| 684 |
if ($currpaper) { |
| 685 |
$bibno++; |
| 686 |
$key .= ($key ? "-" : "") . $bibno; |
| 687 |
# |
| 688 |
my %paperhash; |
| 689 |
$paperhash{$key} = $currpaper; |
| 690 |
push @result, \%paperhash; |
| 691 |
} |
| 692 |
$currpaper = ""; |
| 693 |
$insidebibliography = 0; |
| 694 |
next; |
| 695 |
} |
| 696 |
if ($insidebibliography) { |
| 697 |
$currpaper .= " $_"; |
| 698 |
} |
| 699 |
} |
| 700 |
close BIB or warn "close($bibfile) failed: $!"; |
| 701 |
|
| 702 |
# We look in the .rpi files too, which will generally have none. |
| 703 |
if (@result == 0 && $bibfile =~ /\.bbl$/) { |
| 704 |
warn "$0: *** no \\bibitems found in: $bibfile; check if ok\n"; |
| 705 |
} elsif ($insidebibliography) { |
| 706 |
warn "$0: *** no \\end{thebibliography} found in: $bibfile\n"; |
| 707 |
warn "$0: so the last bib entry is missing!\n"; |
| 708 |
$ERROR_COUNT++; |
| 709 |
} |
| 710 |
return @result; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
############################################################## |
| 715 |
# Read \bibdata commands from AUX file, if exists. |
| 716 |
# No error if it doesn't exist. |
| 717 |
# |
| 718 |
# Extract bib entries corresponding to the list of keys and convert |
| 719 |
# them to XML structured citations accoding to Crossref |
| 720 |
# scheme at |
| 721 |
# https://data.crossref.org/reports/help/schema_doc/5.4.0/schema_5_4_0.html#citation |
| 722 |
# |
| 723 |
# Return a hash reference, with each element's key being the citation |
| 724 |
# key plus an integer, the same keys as in AddBibliography from the .bbl |
| 725 |
# file. Each value is a hash with two elements: entrytype being the |
| 726 |
# type of the entry, and citation being the structured citation items for |
| 727 |
# that entry. |
| 728 |
############################################################## |
| 729 |
sub AddBibtexBib { |
| 730 |
my ($auxfile,$refs) = @_; |
| 731 |
my %result; |
| 732 |
&debug("checking for aux file: $auxfile\n"); |
| 733 |
if (! -r $auxfile) { |
| 734 |
return \%result; |
| 735 |
} |
| 736 |
|
| 737 |
# A hash translating from the real citekeys to crossref numbered ones |
| 738 |
my %crkeys; |
| 739 |
foreach my $crkey (@$refs) { |
| 740 |
my $key = $crkey; |
| 741 |
$key =~ s/-\d*$//; |
| 742 |
$crkeys{$key}=$crkey; |
| 743 |
} |
| 744 |
|
| 745 |
# Reading the .aux file |
| 746 |
|
| 747 |
my %bibfiles; |
| 748 |
open (AUX, $auxfile) or return(\%result); |
| 749 |
&debug(" parsing aux file: $auxfile\n"); |
| 750 |
while (<AUX>) { |
| 751 |
if (/\\bibdata\{([^}]*)\}/) { |
| 752 |
my $bibdata=$1; |
| 753 |
foreach my $bibfile (split /,/, $bibdata) { |
| 754 |
$bibfiles{$bibfile} = 1; |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
close AUX; |
| 760 |
|
| 761 |
foreach my $bibfile (keys %bibfiles) { |
| 762 |
$bibfile .= ".bib" |
| 763 |
if $bibfile !~ /\.bib$/; # might end in .bib already |
| 764 |
my $bibfilename = `kpsewhich $bibfile`; # --debug=-1 |
| 765 |
chomp $bibfilename; |
| 766 |
my $fh = IO::File->new($bibfilename); |
| 767 |
if (! defined $fh) { |
| 768 |
warn "$0: could not open bib file: $bibfile\n"; |
| 769 |
warn "$0: kpsewhich found: $bibfilename\n"; |
| 770 |
warn "$0: cwd: ", `pwd`; |
| 771 |
next; |
| 772 |
} |
| 773 |
&debug(" parsing bib file: $bibfilename\n"); |
| 774 |
# $BibTeX_Parser_options can be set by the config file. |
| 775 |
my $parser = BibTeX::Parser->new($fh, $BibTeX_Parser_options); |
| 776 |
while (my $entry = $parser->next) { |
| 777 |
my $key=$entry->key; |
| 778 |
if (exists $crkeys{$key}) { |
| 779 |
my $crkey = $crkeys{$key}; |
| 780 |
$result{$crkey} = ConvertBibentryToCr ($entry); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
} |
| 785 |
|
| 786 |
return \%result; |
| 787 |
} |
| 788 |
|
| 789 |
############################################################## |
| 790 |
# Get Crossref entry type for a given entry. |
| 791 |
# Returnr a flat string |
| 792 |
############################################################## |
| 793 |
sub CrEntrytype { |
| 794 |
my $entry=shift; |
| 795 |
if ($entry->{'crossrefentrytype'}) { |
| 796 |
&debug(" Found explicit entry type, $entry->{'crossrefentrytype'}\n"); |
| 797 |
return($entry->{'crossrefentrytype'}); |
| 798 |
} |
| 799 |
|
| 800 |
my $type = $entry->type; |
| 801 |
&debug("Bibtex type $type\n"); |
| 802 |
|
| 803 |
if (exists $BibentryToCrossref{$type}) { |
| 804 |
&debug(" Found a custom mapping $BibentryToCrossref{$type}\n"); |
| 805 |
return($BibentryToCrossref{$type}); |
| 806 |
} |
| 807 |
|
| 808 |
if (exists $BibentryToCrossrefDefault{$type}) { |
| 809 |
&debug(" Found a default mapping $BibentryToCrossrefDefault{$type}\n"); |
| 810 |
return($BibentryToCrossrefDefault{$type}); |
| 811 |
} |
| 812 |
|
| 813 |
&debug(" Cannot find a mapping, returning 'other'\n"); |
| 814 |
return('other'); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
############################################################## |
| 819 |
# Convert a BibTeX entry to Crossref structured citation |
| 820 |
# according to |
| 821 |
# https://data.crossref.org/reports/help/schema_doc/5.4.0/schema_5_4_0.html#citation |
| 822 |
# |
| 823 |
# Return a hash with two elements: entrytype and citation |
| 824 |
############################################################## |
| 825 |
sub ConvertBibentryToCr { |
| 826 |
my $entry = shift; |
| 827 |
&debug_hash_as_string("Processing citation entry", $entry); |
| 828 |
my %result = (); |
| 829 |
|
| 830 |
$result{'entrytype'} = CrEntrytype($entry); |
| 831 |
$result{'citation'} = ""; |
| 832 |
|
| 833 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'journal', 'journal_title'); |
| 834 |
|
| 835 |
my $issn = $entry->{"issn"}; |
| 836 |
if ($issn && $issn !~ /\d{4}-?\d{3}[\dX]/) { |
| 837 |
# that's the regexp crossref matches. |
| 838 |
warn "$0: goodbye, invalid issn value: $issn\n"; |
| 839 |
die "$0: ", &debug_hash_as_string("in entry", $entry); |
| 840 |
} |
| 841 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'issn'); |
| 842 |
|
| 843 |
# Somehow crossref wants only the first author. Why? |
| 844 |
my @authors = $entry->author; |
| 845 |
if (! scalar(@authors)) { |
| 846 |
@authors = $entry->editor; |
| 847 |
} |
| 848 |
if (scalar(@authors)) { |
| 849 |
my $author = shift @authors; |
| 850 |
$result{'citation'} .= "<author>"; |
| 851 |
$result{'citation'} .= SanitizeTextEntities($author->to_string()); |
| 852 |
$result{'citation'} .= "</author>\n"; |
| 853 |
} |
| 854 |
|
| 855 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'volume'); |
| 856 |
|
| 857 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'issue'); |
| 858 |
|
| 859 |
# We need only the first page |
| 860 |
if ($entry->field('pages')) { |
| 861 |
my $page = $entry->field('pages'); |
| 862 |
$page =~ s/-.*//; |
| 863 |
$result{'citation'} .= "<first_page>$page</first_page>\n"; |
| 864 |
} |
| 865 |
|
| 866 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'eprint', 'elocation_id'); |
| 867 |
|
| 868 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'year', 'cYear'); |
| 869 |
|
| 870 |
my $doi = $entry->{"doi"}; |
| 871 |
if ($doi) { |
| 872 |
# Crossref accepts only a bare doi, so remove any leading |
| 873 |
# https://doi.org/, etc. Complain now if it doesn't match |
| 874 |
# the regexp the Crossref schema specifies. |
| 875 |
$doi =~ s,^(https?://)?(dx\.)?doi\.org/,,; |
| 876 |
if ($doi !~ m!10\.[0-9]{4,9}/.{1,200}!) { # per crossref schema |
| 877 |
warn "$0: invalid doi for crossref: $entry->{doi} (-> $doi)\n"; |
| 878 |
warn "$0: (in title: $entry->{title})\n"; |
| 879 |
} |
| 880 |
if ($doi ne $entry->{"doi"}) { |
| 881 |
# If we made any changes to the value, update field value so we |
| 882 |
# can call the usual routine. |
| 883 |
$entry->{"doi_orig"} = $entry->{"doi"}; |
| 884 |
$entry->{"doi"} = $doi; |
| 885 |
} |
| 886 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'doi'); |
| 887 |
} |
| 888 |
|
| 889 |
my $isbn = $entry->{"isbn"}; |
| 890 |
if ($isbn) { |
| 891 |
# requirements at |
| 892 |
# data.crossref.org/reports/help/schema_doc/5.4.0/schema_5_4_0.html#http___www.crossref.org_schema_5.4.0_isbn |
| 893 |
my $len = length($isbn); |
| 894 |
if ($len == 9 && $isbn =~ /^\d+$/) { |
| 895 |
# 9-digit standard book number from the 1960s; prepend 0 to |
| 896 |
# make it an ISBN (according to wikipedia.org/ISBN). |
| 897 |
$isbn = "0" . $isbn; |
| 898 |
$len++; |
| 899 |
# have to update the hash since that's the value that gets |
| 900 |
# used below. |
| 901 |
$entry->{isbn} = $isbn; |
| 902 |
} |
| 903 |
if ($len < 10 || $len > 17) { |
| 904 |
warn "$0: goodbye, invalid isbn length: $len ($isbn)\n"; |
| 905 |
die "$0: ", &debug_hash_as_string("in entry", $entry); |
| 906 |
} elsif ($isbn !~ /(97(8|9)-)?\d[\d \-]+[\dX]/) { |
| 907 |
warn "$0: goodbye, invalid isbn value: $isbn\n"; |
| 908 |
die "$0: ", &debug_hash_as_string("in entry", $entry); |
| 909 |
} else { |
| 910 |
# value apparently ok, use it. |
| 911 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'isbn'); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'series', 'series_title'); |
| 916 |
|
| 917 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'booktitle', 'volume_title'); |
| 918 |
|
| 919 |
$result{'citation'} .= ConvertBibFieldToCfield($entry, 'title', 'article_title'); |
| 920 |
|
| 921 |
chomp $result{'citation'}; # Delete the last \n |
| 922 |
|
| 923 |
return \%result; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
############################################################## |
| 928 |
# Convert a BibTeX field ENTRY->{BFIELD} into Crossref field |
| 929 |
# CFIELD. If CFIELD is empty, use the same name for |
| 930 |
# CFIELD and BFIELD. |
| 931 |
# |
| 932 |
# Return a string: <cfield>sanitized-bfield-value</cfield>, |
| 933 |
# or the empty string if the bfield member is empty. |
| 934 |
############################################################## |
| 935 |
sub ConvertBibFieldToCfield { |
| 936 |
my ($entry, $bfield, $cfield) = @_; |
| 937 |
if (!length($cfield)) { |
| 938 |
$cfield = $bfield; |
| 939 |
} |
| 940 |
if ($entry->field($bfield)) { |
| 941 |
return( "<$cfield>" . |
| 942 |
SanitizeTextEntities($entry->field($bfield)) . |
| 943 |
"</$cfield>\n"); |
| 944 |
} else { |
| 945 |
return(""); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
################################################################# |
| 953 |
# Printing information about one issue |
| 954 |
################################################################# |
| 955 |
sub PrintIssueHead { |
| 956 |
my ($year, $volume, $issue) = @_; |
| 957 |
print OUT <<END; |
| 958 |
<journal_issue> |
| 959 |
<publication_date media_type="print"> |
| 960 |
<year>$year</year> |
| 961 |
</publication_date> |
| 962 |
<journal_volume><volume>$volume</volume></journal_volume> |
| 963 |
<issue>$issue</issue> |
| 964 |
</journal_issue> |
| 965 |
END |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
############################################################### |
| 970 |
# Printing information about one paper |
| 971 |
############################################################### |
| 972 |
sub PrintPaper { |
| 973 |
my $paper = shift; |
| 974 |
#warn (&debug_hash_as_string ("doing paper", $paper)); |
| 975 |
my $title = SanitizeText($paper->{title}); |
| 976 |
my $url = GetURL($paper); |
| 977 |
my $publication_type = GetPublicationType($paper->{publicationType}); |
| 978 |
|
| 979 |
&TitleCheck($title); |
| 980 |
print OUT <<END; |
| 981 |
<journal_article$publication_type> |
| 982 |
<titles> |
| 983 |
<title>$title</title> |
| 984 |
</titles> |
| 985 |
<contributors> |
| 986 |
END |
| 987 |
my @authors = split /\s*\\and\s*/, $paper->{authors}; |
| 988 |
my $seq = 'first'; |
| 989 |
foreach my $author (@authors) { |
| 990 |
PrintAuthor($author, $seq); |
| 991 |
$seq = 'additional'; |
| 992 |
} |
| 993 |
|
| 994 |
print OUT <<END; |
| 995 |
</contributors> |
| 996 |
<publication_date media_type="print"> |
| 997 |
<year>$paper->{year}</year> |
| 998 |
</publication_date> |
| 999 |
<pages> |
| 1000 |
<first_page>$paper->{startpage}</first_page> |
| 1001 |
<last_page>$paper->{endpage}</last_page> |
| 1002 |
</pages> |
| 1003 |
<doi_data> |
| 1004 |
<doi>$paper->{doi}</doi> |
| 1005 |
<timestamp>$timestamp</timestamp> |
| 1006 |
<resource>$url</resource> |
| 1007 |
</doi_data> |
| 1008 |
END |
| 1009 |
|
| 1010 |
if (scalar(@{$paper->{bibliography}})) { |
| 1011 |
PrintCitationList($paper->{bibliography}, $paper->{bibtexbib}); |
| 1012 |
} |
| 1013 |
|
| 1014 |
print OUT <<END; |
| 1015 |
</journal_article> |
| 1016 |
END |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
############################################################### |
| 1021 |
# Crossref <title> strings can contain a few so-called "face" HTML |
| 1022 |
# commands. Complain if they have anything anything else. |
| 1023 |
# schema doc: https://data.crossref.org/reports/help/schema_doc/5.4.0/schema_5_4_0.html#title |
| 1024 |
# face doc: https://www.crossref.org/documentation/schema-library/markup-guide-metadata-segments/face-markup/ |
| 1025 |
# |
| 1026 |
# We don't technically validate the string, e.g., mismatched tags will |
| 1027 |
# go unnoticed here. The real validator at Crossref should catch everything. |
| 1028 |
############################################################### |
| 1029 |
sub TitleCheck { |
| 1030 |
my $title = shift; |
| 1031 |
my $orig_title = $title; |
| 1032 |
|
| 1033 |
foreach my $tag (qw(b em i ovl scp strong sub sup tt u)) { |
| 1034 |
$title =~ s,<\s*/?$tag\s*>,,g; # eradicate <tag> and </tag> |
| 1035 |
} |
| 1036 |
|
| 1037 |
# <font> can (maybe?) take lots of extra attributes: |
| 1038 |
$title =~ s,<\s*/?font.*?>,,g; |
| 1039 |
|
| 1040 |
# MathML is too complex; just wipe it all out. If there are |
| 1041 |
# problems, the real validator at Crossref will complain. |
| 1042 |
$title =~ s,<\s*mml:math.*/mml:math\s*>,,g; |
| 1043 |
|
| 1044 |
# No tags should remain. |
| 1045 |
if ($title =~ /</) { |
| 1046 |
die "$0: invalid tags remaining in: $title (original: $orig_title)\n"; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
################################################################ |
| 1052 |
# Printing one author in arg ORIG_AUTHOR, in sequence SEQ. |
| 1053 |
################################################################ |
| 1054 |
sub PrintAuthor { |
| 1055 |
my ($orig_author,$seq) = @_; |
| 1056 |
|
| 1057 |
# recognize extra directives, either |organization| |
| 1058 |
# or |orcid=<value>|. |
| 1059 |
my $organization = 0; |
| 1060 |
my $orcid = 0; |
| 1061 |
my $author = ""; |
| 1062 |
my @name_parts = split (/\|/, $orig_author); |
| 1063 |
foreach my $np (@name_parts) { |
| 1064 |
$np =~ s/^\s*(.*)\s*$/$1/s; # remove leading and trailing whitespace |
| 1065 |
if ($np eq "organization") { |
| 1066 |
$organization = 1; |
| 1067 |
} elsif ($np =~ /^orcid/) { |
| 1068 |
($orcid = $np) =~ s/^orcid\s*=//; |
| 1069 |
$orcid =~ s/\s//g; # remove all whitespace from value |
| 1070 |
if (! $orcid) { |
| 1071 |
warn "$0: ignoring empty orcid specified in: $orig_author\n"; |
| 1072 |
} |
| 1073 |
} elsif (! $np) { |
| 1074 |
# silently ignore empty part, as in || |
| 1075 |
} else { |
| 1076 |
if ($author) { |
| 1077 |
die ("$0: already saw author name `$author', should not" |
| 1078 |
. " have second: $np\n"); |
| 1079 |
} |
| 1080 |
$author = $np; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
if ($organization && $orcid) { |
| 1085 |
die ("$0: orcid and organization cannot both be present in:" |
| 1086 |
. " $orig_author\n"); |
| 1087 |
} |
| 1088 |
|
| 1089 |
# for both author types, organization and person, we have to output |
| 1090 |
# the sequence number and the contributor role, which we assume to |
| 1091 |
# be author. |
| 1092 |
my $author_elts = qq!sequence="$seq" contributor_role="author"!; |
| 1093 |
# for organizations, nothing to do but output it. |
| 1094 |
if ($organization) { |
| 1095 |
my $line = SanitizeText($author); |
| 1096 |
print OUT <<END; |
| 1097 |
<organization $author_elts>$line</organization> |
| 1098 |
END |
| 1099 |
return; |
| 1100 |
} |
| 1101 |
|
| 1102 |
# what's left is the common case of a person, not an organization. |
| 1103 |
print OUT <<END; |
| 1104 |
<person_name $author_elts> |
| 1105 |
END |
| 1106 |
|
| 1107 |
# must split the person's name. |
| 1108 |
my $person=new BibTeX::Parser::Author ($author); |
| 1109 |
#warn (debug_list_as_string ($author, $person)); |
| 1110 |
|
| 1111 |
if ($person->first) { |
| 1112 |
my $line = $person->first; |
| 1113 |
$line = SanitizeText($line); |
| 1114 |
print OUT <<END; |
| 1115 |
<given_name>$line</given_name> |
| 1116 |
END |
| 1117 |
} |
| 1118 |
|
| 1119 |
if ($person->last) { |
| 1120 |
my $line = SanitizeText($person->last); |
| 1121 |
if ($person->von) { |
| 1122 |
$line = SanitizeText($person->von)." $line"; |
| 1123 |
} |
| 1124 |
print OUT <<END; |
| 1125 |
<surname>$line</surname> |
| 1126 |
END |
| 1127 |
} |
| 1128 |
|
| 1129 |
if ($person->jr) { |
| 1130 |
my $line = SanitizeText($person->jr); |
| 1131 |
print OUT <<END; |
| 1132 |
<suffix>$line</suffix> |
| 1133 |
END |
| 1134 |
} |
| 1135 |
|
| 1136 |
if ($orcid) { |
| 1137 |
print OUT <<END; |
| 1138 |
<ORCID>https://orcid.org/$orcid</ORCID> |
| 1139 |
END |
| 1140 |
} |
| 1141 |
|
| 1142 |
print OUT <<END; |
| 1143 |
</person_name> |
| 1144 |
END |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
############################################################# |
| 1149 |
# Print citations in order from BIBLIOGRAPHY, a list reference, and |
| 1150 |
# BIBTEXBIB, a hash reference. Each element in BIBLIOGRAPHY is a |
| 1151 |
# one-element hash, with the key being the citation key and the value |
| 1152 |
# the (original) bbl text. We sanitize (de-texify) the text. |
| 1153 |
# |
| 1154 |
# Each element in BIBTEXBIB has key the citation key (from the same |
| 1155 |
# set) and value the structured citation string. |
| 1156 |
# |
| 1157 |
############################################################# |
| 1158 |
sub PrintCitationList { |
| 1159 |
my ($bibliography, $bibtexbib) = @_; |
| 1160 |
|
| 1161 |
print OUT " <citation_list>\n"; |
| 1162 |
foreach my $citation_hash (@$bibliography) { |
| 1163 |
foreach my $citekey (keys (%{$citation_hash})) { # only one key |
| 1164 |
my $citation_text = $citation_hash->{$citekey}; |
| 1165 |
$citation_text = SanitizeTextEntities($citation_text); |
| 1166 |
|
| 1167 |
&debug(" printing citation $citekey: $citation_text\n"); |
| 1168 |
my $structured_citation_hash = $bibtexbib->{$citekey} || ""; |
| 1169 |
if ($structured_citation_hash) { |
| 1170 |
my $entrytype = $structured_citation_hash -> {'entrytype'}; |
| 1171 |
my $structured_citation = $structured_citation_hash -> {'citation'}; |
| 1172 |
$structured_citation = "\n" . $structured_citation; |
| 1173 |
$structured_citation =~ s/^(.)/ $1/mg; |
| 1174 |
&debug (" with structured citation: $structured_citation\n"); |
| 1175 |
|
| 1176 |
print OUT <<END; |
| 1177 |
<citation key="$citekey" type="$entrytype">$structured_citation |
| 1178 |
<unstructured_citation>$citation_text</unstructured_citation> |
| 1179 |
</citation> |
| 1180 |
END |
| 1181 |
} else { |
| 1182 |
print OUT <<END; |
| 1183 |
<citation key="$citekey"> |
| 1184 |
<unstructured_citation>$citation_text</unstructured_citation> |
| 1185 |
</citation> |
| 1186 |
END |
| 1187 |
} |
| 1188 |
|
| 1189 |
} |
| 1190 |
} |
| 1191 |
print OUT " </citation_list>\n"; |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
############################################################## |
| 1196 |
# Return publication_type attribute for <journal_article>, given $PUBTYPE. |
| 1197 |
# https://data.crossref.org/reports/help/schema_doc/5.4.0/NO_NAMESPACE.html#publication_type.atts_publication_type |
| 1198 |
# |
| 1199 |
# If not specified in input, return " publication_type=full_text" since |
| 1200 |
# it was hardwired that way before. If set to "omit", return empty |
| 1201 |
# string. Else return " publication_type=$PUBTYPE>, if the value |
| 1202 |
# is valid. If not, die. (Leading space is so result can be directly used.) |
| 1203 |
############################################################## |
| 1204 |
sub GetPublicationType { |
| 1205 |
my $pubtype = shift; |
| 1206 |
my $ret; |
| 1207 |
|
| 1208 |
if (! $pubtype) { |
| 1209 |
$ret = "full_text"; |
| 1210 |
} elsif ($pubtype eq "omit") { |
| 1211 |
$ret = ""; |
| 1212 |
} elsif ($pubtype =~ /^(abstract_only|full_text|bibliographic_record)$/) { |
| 1213 |
$ret = $pubtype; |
| 1214 |
} else { |
| 1215 |
die "$0: invalid publication_type: $pubtype\n"; |
| 1216 |
} |
| 1217 |
|
| 1218 |
$ret = " publication_type=\"$ret\"" if $ret; |
| 1219 |
return $ret; |
| 1220 |
} |
| 1221 |
|
| 1222 |
############################################################## |
| 1223 |
# Calculating URL. Res Philosophica gets special treatment. |
| 1224 |
############################################################## |
| 1225 |
sub GetURL { |
| 1226 |
my $paper = shift; |
| 1227 |
|
| 1228 |
my $result; |
| 1229 |
if ($paper->{paperUrl}) { |
| 1230 |
$result = $paper->{paperUrl} |
| 1231 |
|
| 1232 |
} elsif ($paper->{doi} =~ m,^10\.11612/resphil,) { |
| 1233 |
my $doi = $paper->{doi}; |
| 1234 |
$result = 'http://www.pdcnet.org/oom/service?url_ver=Z39.88-2004&rft_val_fmt=&rft.imuse_synonym=resphilosophica&rft.DOI='.$doi.'&svc_id=info:www.pdcnet.org/collection'; |
| 1235 |
|
| 1236 |
} else { |
| 1237 |
die ("$0: paperUrl field is required\n " |
| 1238 |
. &debug_hash_as_string("whole hash", $paper)); |
| 1239 |
} |
| 1240 |
|
| 1241 |
$result =~ s/&/&/g; # amp(ersand) |
| 1242 |
return $result; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
############################################################### |
| 1247 |
# Simplistic TeX-to-html |
| 1248 |
# (no-op for rpi text if --input-is-xml was given). |
| 1249 |
############################################################### |
| 1250 |
sub SanitizeText { |
| 1251 |
my $string = shift; |
| 1252 |
return $string if $opts{xi}; # do nothing if --rpi-is-xml |
| 1253 |
return SanitizeTextEntities($string); |
| 1254 |
} |
| 1255 |
|
| 1256 |
# Conversion of accented control sequences to characters, etc. |
| 1257 |
# This uses &#uuuu; entities instead of literal UTF-8; Crossref |
| 1258 |
# recommends it, and it's easier for postprocessing. |
| 1259 |
# |
| 1260 |
sub SanitizeTextEntities { |
| 1261 |
my $string = shift; |
| 1262 |
return SanitizeTextNoEntities($string, entities => 1, @_); |
| 1263 |
} |
| 1264 |
|
| 1265 |
# Generic sanitize text. |
| 1266 |
sub SanitizeTextNoEntities { |
| 1267 |
my $string = shift; |
| 1268 |
|
| 1269 |
# pass user hook subroutine if defined. |
| 1270 |
my @hook = (defined(&{"LaTeX_ToUnicode_convert_hook"})) |
| 1271 |
? ("hook" => \&LaTeX_ToUnicode_convert_hook) |
| 1272 |
: (); |
| 1273 |
|
| 1274 |
$string = LaTeX::ToUnicode::convert($string, @hook, @_); |
| 1275 |
|
| 1276 |
return $string; |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
############################################################## |
| 1281 |
# debug_hash_as_string($LABEL, HASH) |
| 1282 |
# |
| 1283 |
# Return LABEL followed by HASH elements, followed by a newline, as a |
| 1284 |
# single string. If HASH is a reference, it is followed (but no |
| 1285 |
# recursive dereferencing). |
| 1286 |
############################################################### |
| 1287 |
sub debug_hash_as_string { |
| 1288 |
my ($label) = shift; |
| 1289 |
my (%hash) = (ref $_[0] && $_[0] =~ /.*HASH.*/) ? %{$_[0]} : @_; |
| 1290 |
|
| 1291 |
my $str = "$label: {"; |
| 1292 |
my @items = (); |
| 1293 |
for my $key (sort keys %hash) { |
| 1294 |
my $val = $hash{$key}; |
| 1295 |
$val = ".undef" if ! defined $val; |
| 1296 |
$key =~ s/\n/\\n/g; |
| 1297 |
$val =~ s/\n/\\n/g; |
| 1298 |
push (@items, "$key:$val"); |
| 1299 |
} |
| 1300 |
$str .= join (",", @items); |
| 1301 |
$str .= "}"; |
| 1302 |
|
| 1303 |
return "$str\n"; |
| 1304 |
} |
| 1305 |
|
| 1306 |
############################################################## |
| 1307 |
# debug_list_as_string($LABEL, LIST) |
| 1308 |
# |
| 1309 |
# Same but for lists. |
| 1310 |
############################################################## |
| 1311 |
sub debug_list_as_string { |
| 1312 |
my ($label) = shift; |
| 1313 |
my (@list) = (ref $_[0] && $_[0] =~ /.*ARRAY.*/) ? @{$_[0]} : @_; |
| 1314 |
|
| 1315 |
my $str = "$label [" . join (",", @list) . "]"; |
| 1316 |
return "$str\n"; |
| 1317 |
} |
| 1318 |
|
| 1319 |
sub debug { $opts{"debug"} && warn "@_"; } |