summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/biblatex-ext/biblatex-ext-oa.sty
blob: 9795ae2f7880abc6c3f2183a0569b0bfae446627 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
\ProvidesPackage{biblatex-ext-oa}
  [2019/01/04 v0.6a mark open access in biblatex bibliographies (MW)]

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

\protected\def\extblx@oa@warning@noline#1{%
  \PackageWarningNoLine{biblatex-ext-oa}{#1}}

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

\@ifpackageloaded{biblatex}
  {}
  {\extblx@oa@error
     {biblatex not loaded!\MessageBreak
      This package must be loaded after biblatex.\MessageBreak
      This is a fatal error, the package will not\MessageBreak be loaded}
     {Load biblatex with \string\usepackage{biblatex} before
      you load 'biblatex-ext-oa'.\MessageBreak
      Please see the documentation of biblatex-ext for more information}%
   \endinput}

% xpatch has xapptobibmacro,
% but I did not want to buy into all of expl3 just for this one macro
% ... at least for the moment
% note that this is not nearly as sophisticated as xpatch and does not
% deal with optional arguments
\newcommand*{\extblx@oa@apptobibmcro}[1]{%
  \expandafter\apptocmd
    \expandafter{\csname abx@macro@\detokenize{#1}\endcsname}}

\newtoggle{extblx@oa@opt@forcedoiapi}
\DeclareBibliographyOption[string]{openaccess}[true]{%
  \ifstrequal{#1}{forcedoiapi}
    {\@firstoftwo}
    {\ifstrequal{#1}{forceonlydoiapi}
       {\@firstoftwo}
       {\@secondoftwo}}
    {\toggletrue{extblx@oa@opt@forcedoiapi}}
    {}%
  \extblx@oa@blxopt@openaccess{#1}}
\DeclareTypeOption[string]{openaccess}[true]{%
  \ifstrequal{#1}{forcedoiapi}
    {\toggletrue{extblx@oa@opt@forcedoiapi}%
     \csletcs{extblx@oa@url@from}{extblx@oa@url@from@forcedoiapi}}
    {}%
  \ifstrequal{#1}{forcedoiapi}
    {\toggletrue{extblx@oa@opt@forceonlydoiapi}%
     \csletcs{extblx@oa@url@from}{extblx@oa@url@from@forceonlydoiapi}}
    {}%
  \iftoggle{extblx@oa@opt@forcedoiapi}
    {}
    {\extblx@oa@blxopt@openaccess{#1}}}
\DeclareEntryOption[string]{openaccess}[true]{%
  \iftoggle{extblx@oa@opt@forcedoiapi}
    {}
    {\extblx@oa@blxopt@openaccess{#1}}}

\def\extblx@oa@blxopt@openaccess#1{%
  \ifcsundef{extblx@oa@url@from@#1}
    {\ifcsundef{extblx@oa@warn@field@\abx@field@entrykey @#1}
       {\extblx@oa@warning@noline{%
          Field '#1' not set up for\MessageBreak
          open access detection.\MessageBreak
          Ignoring the option\MessageBreak
          'openaccess=#1'\MessageBreak
          for entry '\abx@field@entrykey'}%
        \global\cslet{extblx@oa@warn@field@\abx@field@entrykey @#1}\@empty}
       {}}
    {\csletcs{extblx@oa@url@from}{extblx@oa@url@from@#1}}%
  \ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@onlydoiapi}
    {\@firstoftwo}
    {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@doiapi}
       {\@firstoftwo}
       {\@secondoftwo}}
    {\ifextblx@oa@opt@doiapi
     \else
       \extblx@oa@error
         {'openaccess=#1' requires 'doiapi=true'\MessageBreak
          and LuaLaTeX}
         {If you want to use 'openaccess=#1', you must load the
          'biblatex-ext-oa'\MessageBreak
          package with the option 'doiapi=true'.
          That option also requires LuaLaTeX.\MessageBreak
          Please see the documentation of biblatex-ext for more information}
     \fi}
    {}
}

% define special detection settings
% auto, all (=:true), none (=:false),
% doiapi, forcedoiapi (=doiapi, but overrides more specific options)
% onlydoiapi, forceonlydoiapi
\csdef{extblx@oa@url@from@auto}{auto}
\csdef{extblx@oa@url@from@all}{all}
\csletcs{extblx@oa@url@from@true}{extblx@oa@url@from@all}
\csdef{extblx@oa@url@from@none}{none}
\csletcs{extblx@oa@url@from@false}{extblx@oa@url@from@none}
\csdef{extblx@oa@url@from@doiapi}{doiapi}
\csdef{extblx@oa@url@from@onlydoiapi}{onlydoiapi}
\csletcs{extblx@oa@url@from@forcedoiapi}{extblx@oa@url@from@doiapi}
\csletcs{extblx@oa@url@from@forceonlydoiapi}{extblx@oa@url@from@onlydoiapi}

% {<field_1,...,field_n>}
\newrobustcmd*{\DeclareOpenAccessUrlFieldPriority}{%
  \let\extblx@oa@url@autofields\@empty
  \forcsvlist{\listadd\extblx@oa@url@autofields}}

% {<macroname>}{<field>}{<url spec>}{<openness>}
\def\extblx@oa@url@definefieldurl#1#2#3#4{%
  \csdef{extblx@oa@url@from@#2}{#2}%
  \ifstrequal{#4}{always}
    {\csdef{extblx@oa@url@getfromfield@#1}{%
       \iffieldannotation[#2]{closedaccess}
         {}
         {\def\extblx@oa@url{#3}}}}
    {\csdef{extblx@oa@url@getfromfield@#1}{%
       \iffieldannotation[#2]{closedaccess}
          {\@secondoftwo}
          {\iffieldannotation[#2]{openaccess}
             {\@firstoftwo}
             {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@#2}
                {\@firstoftwo}
                {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@all}
                  {\@firstoftwo}
                  {\@secondoftwo}}}}
         {\def\extblx@oa@url{#3}}
         {}}}}

% [<openness>]{<field>}{<url spec>}
\newrobustcmd*{\DeclareOpenAccessFieldUrl}[3][conditional]{%
  \extblx@oa@url@definefieldurl{#2}{#2}{#3}{#1}}
% [<openness>]{<eprint type>}{<url spec>}
\newrobustcmd*{\DeclareOpenAccessEprintUrl}[3][conditional]{%
  \extblx@oa@url@definefieldurl{eprint@#2}{eprint}{#3}{#1}}

\newrobustcmd*{\DeclareOpenAccessEprintAlias}[2]{%
  \csdef{extblx@oa@url@getfromfield@eprint@#1}{%
    \csuse{extblx@oa@url@getfromfield@eprint@#2}}}

\def\extblx@oa@url@undefinefieldurl#1#2{
  \csundef{extblx@oa@url@from@#2}
  \csundef{extblx@oa@url@getfromfield@#1}}

\newrobustcmd*{\UndeclareOpenAccessFieldUrl}[1]{%
  \extblx@oa@url@undefinefieldurl{#1}{#1}}
\newrobustcmd*{\UndeclareOpenAccessEprintUrl}[1]{%
  \extblx@oa@url@undefinefieldurl{eprint@#1}{eprint}}


\def\extblx@oa@url@processfield@@generic#1{%
  \csuse{extblx@oa@url@getfromfield@#1}}

\def\extblx@oa@url@processfield@eprint#1{%
  \iffieldundef{eprinttype}
    {\csuse{extblx@oa@url@getfromfield@#1}}
    {\csuse{extblx@oa@url@getfromfield@eprint@\thefield{eprinttype}}}}

\def\extblx@oa@url@processfield#1{%
  \iffieldundef{#1}
    {}
    {\ifcsundef{extblx@oa@url@processfield@#1}
       {\extblx@oa@url@processfield@@generic{#1}}
       {\csuse{extblx@oa@url@processfield@#1}{#1}}}}

\newcommand*{\extblx@oa@url@getoaurl}{%
  \ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@onlydoiapi}
    {\iffieldundef{doi}
       {}
       {\extblx@oa@api@getoaurl{\thefield{doi}}}}
    {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@doiapi}
       {\iffieldundef{doi}
         {\forlistloop{\extblx@oa@url@processfield}{\extblx@oa@url@autofields}}
         {\extblx@oa@api@getoaurl{\thefield{doi}}}}
       {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@none}
          {}
          {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@all}
             {\@firstoftwo}
             {\ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@auto}
                {\@firstoftwo}
                {\@secondoftwo}}
           {\forlistloop{\extblx@oa@url@processfield}{\extblx@oa@url@autofields}}
           {\extblx@oa@url@processfield{\csuse{extblx@oa@url@from}}}}}}}


\newrobustcmd*{\LoadTikZOASymbol}[1][plos]{%
  \@ifpackageloaded{biblatex-ext-oa-tikzsymbols}
    {\extblx@oa@warning{%
       TikZ open access symbols already loaded.\MessageBreak
       Redefining the symbol with\MessageBreak\string\DefineTikZOASymbol}%
     \DefineTikZOASymbol[#1]}
    {\RequirePackage[symbol=#1]{biblatex-ext-oa-tikzsymbols}}%
  \gdef\LoadTikZOASymbol{%
    \extblx@oa@warning{%
      TikZ open access symbols can not be\MessageBreak
      loaded more than once.\MessageBreak
      I'll now behave like \string\DefineTikZOASymbol\MessageBreak}%
    \DefineTikZOASymbol}}
\@onlypreamble\LoadTikZOASymbol

\def\extblx@oa@opt@tikzsymbol@val@false{}
\def\extblx@oa@opt@tikzsymbol@val@true{\LoadTikZOASymbol[plos]}

\def\extblx@oa@opt@tikzsymbol@process#1{%
  \begingroup
  \edef\extblx@oa@tempa{\endgroup
    \noexpand\extblx@oa@opt@tikzsymbol@process@i{#1}}
  \extblx@oa@tempa}

\def\extblx@oa@opt@tikzsymbol@process@i#1{%
  \ifcsundef{extblx@oa@opt@tikzsymbol@val@#1}
    {\LoadTikZOASymbol[#1]}
    {\csuse{extblx@oa@opt@tikzsymbol@val@#1}}}

\SetupKeyvalOptions{
  family=extblx@oa@opt,
  prefix=extblx@oa@opt@,
}

\DeclareBoolOption[true]{enable}
\DeclareBoolOption[false]{doiapi}
\DeclareStringOption[false]{tikzsymbol}[true]


\ProcessKeyvalOptions*

\extblx@oa@opt@tikzsymbol@process{\csuse{extblx@oa@opt@tikzsymbol}}

\ifbool{extblx@oa@opt@doiapi}
  {\RequirePackage{biblatex-ext-oa-doiapi}}
  {}

\def\extbl@oa@bbl@fielddef@bestoaurl#1{%
  \blx@bbl@fielddef{bestoaurl}{#1}}

\AtDataInput{%
  \undef\extblx@oa@url
  \extblx@oa@url@getoaurl
  \ifundef\extblx@oa@url
    {}
    {\expandafter\extbl@oa@bbl@fielddef@bestoaurl\expandafter{\extblx@oa@url}}}



% based on egreg's (https://tex.stackexchange.com/users/4427/egreg)
% answer to https://tex.stackexchange.com/a/123451/35864
% originally CC BY-SA 3.0, but dual-licensed under LPPL
% see https://tex.meta.stackexchange.com/a/3333/35864
\newcommand{\extblx@oa@marginmark}{\strut\vadjust{\extblx@oa@marginmark@do}}
\newcommand{\extblx@oa@marginmark@do}{%
  \vbox to 0pt{%
    \kern-3\dp\strutbox
    \strut
    \hfill
    \rlap{\kern1em
      \ifhyperref
        {\href{\thefield{bestoaurl}}}
        {\@firstofone}%
      {\oasymbol}}
    \vss
  }%
}


\ifbool{extblx@oa@opt@enable}
  {\csletcs{extblx@oa@url@from}{extblx@oa@url@from@auto}
   \DeclareOpenAccessFieldUrl{url}{\thefield{url}}
   \DeclareOpenAccessFieldUrl{doi}{https://doi.org/\thefield{doi}}

   \DeclareOpenAccessEprintUrl{jstor}{%
     http://www.jstor.org/stable/\thefield{eprint}}
   \DeclareOpenAccessEprintAlias{JSTOR}{jstor}
   \DeclareOpenAccessEprintUrl{hdl}{%
     http://hdl.handle.net/\thefield{eprint}}
   \DeclareOpenAccessEprintAlias{HDL}{hdl}
   \DeclareOpenAccessEprintUrl{pubmed}{%
     http://www.ncbi.nlm.nih.gov/pubmed/\thefield{eprint}}
   \DeclareOpenAccessEprintAlias{PubMed}{pubmed}
   \DeclareOpenAccessEprintUrl[always]{arxiv}{%
     https://arxiv.org/\abx@arxivpath/\thefield{eprint}}
   \DeclareOpenAccessEprintAlias{arXiv}{arxiv}

   \DeclareOpenAccessUrlFieldPriority{url,eprint,doi}

   \DeclareFieldFormat{bestoaurl}{\url{#1}}

   \newbibmacro*{oamark}{%
     \iffieldundef{bestoaurl}
       {}
       {\extblx@oa@marginmark}}

   % patch begentry bibmacro
   % safer than \renewbibmacro{begentry} if it contains something,
   % but uses the internal implementation of bibmacros
   \extblx@oa@apptobibmcro{begentry}
     {\usebibmacro{oamark}}
     {}
     {\extblx@oa@warning@noline{%
        Failed to patch 'begentry' bibmacro.\MessageBreak
        The open access logo may not be displayed at all}}

   \ifbool{extblx@oa@opt@doiapi}
     {\NewBibliographyString{openaccessat}
      \DefineBibliographyStrings{english}{
        openaccessat = {open access version at},
      }
      \newbibmacro{api:bestoaurl}{%
        \ifcsequal{extblx@oa@url@from}{extblx@oa@url@from@doiapi}
          {\iffieldundef{doi}
             {\@secondoftwo}
             {\iffieldundef{bestoaurl}
                {\@secondoftwo}
                {\OpenaccessURLisDOI{\thefield{doi}}
                   {\@secondoftwo}
                   {\@firstoftwo}}}}
          {\@secondoftwo}
          {\bibstring{openaccessat}%
           \setunit{\addspace}%
           \printfield{bestoaurl}%
           \newunit\newblock}
          {}}

     \extblx@oa@apptobibmcro{doi+eprint+url}
       {\newunit\newblock
        \usebibmacro{api:bestoaurl}}
       {}
       {\extblx@oa@warning@noline{%
          Failed to patch 'doi+eprint+url' bibmacro.\MessageBreak
          The best open access URL might not be shown}}}}
  {\csletcs{extblx@oa@url@from}{extblx@oa@url@from@none}}


\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
%