summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/biblatex-contrib/biblatex-ext/biblatex-ext-oa-doiapi.sty
blob: 662599201d5662b061e39f76d6b1c2d43c4352cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
\ProvidesPackage{biblatex-ext-oa-doiapi}
  [2021/04/22 v0.12b Obtain open access information from DOI via
   unpaywall.org and doi.org (MW)]

\RequirePackage{etoolbox}

\protected\def\extblx@oa@api@error#1#2{%
  \PackageError{biblatex-ext-oa-doiapi}{#1}{#2.}}

\protected\def\extblx@oa@api@warning@noline{%
  \PackageWarningNoLine{biblatex-ext-oa-doiapi}}

\protected\def\extblx@oa@api@info@noline#1{%
  \PackageInfo{biblatex-ext-oa-doiapi}{#1\@gobble}}

\let\extblx@oa@api@warning\extblx@oa@api@warning@noline
\AtEndOfPackage{%
  \protected\def\extblx@oa@api@warning{%
    \PackageWarning{biblatex-ext-oa-doiapi}}}

% LuaLaTeX only!
\ifundef\luatexversion
  {\extblx@oa@api@error
     {Not running under LuaLaTeX!\MessageBreak
      This package needs LuaLaTeX.\MessageBreak
      I will stop right here}
     {This package can only be run with LuaLaTeX.\MessageBreak
      Please see the documentation of biblatex-ext for more information}
   \endinput}
  {}

% from Manuel Pégourié-Gonnar's luacode (https://ctan.org/pkg/luacode)
% licensed under LPPL 1.3c
\def\extblx@oa@api@luastring#1{"\luaescapestring{#1}"}
\def\extblx@oa@api@luastringN#1{%
  \extblx@oa@api@luastring{\unexpanded{#1}}}

% no local here, since that would restrict the assignment to
% this \directlua only, that would be too local
\directlua{blxextdoiapi = require("blxextdoiapi")}

% The oai file should contain a (global!) Lua table with the relevant
% OA data
\IfFileExists{\jobname.oai}
  {\extblx@oa@api@info@noline{%
     Loading OA database from\MessageBreak
     \jobname.oai ...}%
   \directlua{dofile(\extblx@oa@api@luastring{\jobname.oai})}}
  {\extblx@oa@api@info@noline{%
     No OA database file present at\MessageBreak
     \jobname.oai}}

\newcommand*{\SetDOIAPIMail}[1]{%
  \cslet{extblx@oa@api@mailset}\@empty
  \directlua{
    blxextdoiapi.mail = \extblx@oa@api@luastring{#1}}}

\newcommand*{\SetDOIAPICacheExpiration}[1]{%
  \directlua{
    blxextdoiapi.cache_expire = \extblx@oa@api@luastring{#1}}}

\newcommand*{\IsOpenaccess}[1]{%
  \directlua{
    blxextdoiapi.texify_conditional(
      blxextdoiapi.is_openaccess(\extblx@oa@api@luastring{#1}))}}

\newcommand*{\GetOpenaccessURLWrapped}[1]{%
  \directlua{
    blxextdoiapi.get_openaccess_url_tex(\extblx@oa@api@luastring{#1})}}

\newcommand*{\OpenaccessURLisDOI}[1]{%
  \directlua{
    blxextdoiapi.texify_conditional(
      blxextdoiapi.openaccess_url_is_doi(\extblx@oa@api@luastring{#1}))}}

\newcommand*{\extblx@oa@api@getoaurl}[1]{%
  \directlua{
    blxextdoiapi.assign_openaccess_url_to
      (\extblx@oa@api@luastringN{\extblx@oa@url},
       \extblx@oa@api@luastring{#1})
  }%
}

\AtEndPreamble{%
  \ifcsundef{extblx@oa@api@mailset}
    {\extblx@oa@api@warning@noline{%
       No mail address declared.\MessageBreak
       To access the Unpaywall.org api\MessageBreak
       an email address is required.\MessageBreak
       It should be given in the preamble\MessageBreak
       with '\string\SetDOIAPIMail'.\MessageBreak
       The API request will fail with a\MessageBreak
       fatal error if no email is provided}}
    {}}

\AtEndDocument{%
  \directlua{%
    table.tofile(\extblx@oa@api@luastring{\jobname.oai}, oadb, "oadb")}}

\endinput
%
% This file is part of the biblatex-ext bundle.
% biblatex-ext is released under the LaTeX Project Public License v1.3c
% or later.
%
% A complete list of files included in that package can be found in
% README.md or - failing that - in ext-standard.bbx.
%
% Official releases of this package are on CTAN
%   https://www.ctan.org/pkg/biblatex-ext
% development takes place on GitHub
%   https://github.com/moewew/biblatex-ext
%