summaryrefslogtreecommitdiff
path: root/macros/plain/contrib/zztex/tex/zzbiblio.tex
blob: cc834e4109e7e833fb5261fd1b67bdb89fafc19c (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Module:    ZzTeX Bibliography Facility
%
% Synopsis:  This module provides a new bibliography facility to replace
%            zzbibtex. It is more flexible so that it can handle numeric,
%            key, and author/year citations.
%
% Author:    Paul C. Anagnostopoulos
% Created:   1 December 2016
%
% Copyright 1989--2020 by Paul C. Anagnostopoulos
% under The MIT License (opensource.org/licenses/MIT)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%                       Initialization
%                       --------------


\def \zbiblioinit {%
  \if \definedp{\bibliodesign}%
    \begingroup
      \bibliodesign
      \globaldefs = 1
      \citestyle = \citestyle
    \endgroup
  \fi}

%                       Bibliography Cross-Reference
%                       ------------ ---------------


% When tags are loaded, define the name \=zB:tag to contain the
% bibliographic info in this format:
%   folio\zmark key\zmark {author}{year}{full-author}

\def \xrefbib #1#2#3#4{%                {folio}{key}{{author}{year}{full-author}}{tag}
  \ifnum \xrefmode=\xrefloadtagmode
    \if \definedp{\=zB:#4}\zduptag{#4}\fi
    \withname\gdef{\=zB:#4}{#1\zmark#2\zmark#3}%
  \fi}

% This macro loads the cite cross-reference entries and invokes the
% specified macro for each one.

\def \mapxrefcites #1{%                                         {\macro}
  {\let \zmapcitemacro = #1\relax
  \zxloadcomp{\xrefloadcitemode}}}

\def \xrefcite #1#2#3#4{%                                       {page}{}{}{tag}
  \ifnum \xrefmode=\xrefloadcitemode
    \zmapcitemacro{#1}{#4}%
  \fi}

%                       Biblio Block
%                       ------ -----


\defineblock{\biblio}{\endbiblio}{\false}{}

%~block biblio
% \citestyle = integer                  % Citation style.
% \setflag \indexcites = boolean        % If true, index each cite (not implemented).
% \setflag \citeself = boolean          % If true, \bibitem cites itself (not implemented).
%~end

\definecount{\citestyle}{-1}

\chardef \citebynumber     = 0
\chardef \citebykey        = 1
\chardef \citebyauthoryear = 2


\def \biblio {%
  \blockcantbein{\biblio}{\biblio}%
  \blockmustbein{\biblio}{\list}%
  \beginblockscope{biblio}%
  \global\increment \bibliodepth
  \setflag \citeself = \false                   %~default soft
  \processdesign{\biblio}{}%
  \global\increment \biblionumber}

\def \endbiblio {%
  \endgraf
  \fakepar                                      % In case biblio is empty.
  \global\decrement \bibliodepth
  \endblockscope{biblio}}

%                       Bibliography Items
%                       ------------ -----


\definecount{\zbibitemnumber}{0}
\definetoks{\zbibauthor}
\definetoks{\zbibyear}
\definetoks{\zbibfullauthor}
\definetoks{\bibitemtag}                        % Tag of current \bibitem.


%~ The |\bibitem| command is used in a |\biblio| block to start
%~ each entry in the bibliography. It takes two arguments; the
%~ format of the first argument depends on the citation style.
%~ * Cited by number: |\bibitem{}{*tag*}|
%~ * Cited by key: |\bibitem{*key*}{*tag*}|
%~ * Cited by author/year: |\bibitem{*author*;*year*;*full-author*}{*tag*}|
%~ *.
%~ In the author/year format, the second semicolon and *full-author* are
%~ optional.

\def \bibitem #1#2{%                            {[info]}{tag}
  \blockmustbein{\bibitem}{\biblio}%
  \zbibitemnumber = \itemnumber
  \increment \zbibitemnumber
  \ifcase \citestyle
    \edef \zbibkey {\the\zbibitemnumber}%
    \zbibauthor = {}%
    \zbibyear = {}%
    \zbibfullauthor = {}%
  \or
    \def \zbibkey {#1}%
    \zbibauthor = {}%
    \zbibyear = {}%
    \zbibfullauthor = {}%
  \or
    \def \zbibkey {}%
    \zparseauthoryear #1;;;\zmark
  \else
    \error{invalidcitestyle}{The \string\citestyle parameter is invalid: \the\citestyle}%
  \fi
  \bibitemtag = {#2}%
  \item{\zbibkey}%
  \if \PDFhyperlinks \PDFmark{T:#2}\fi
  \if \notp{\emptyargp{#2}}%
    \edef \znext {%
      \noexpand\xref{bib}{\noexpand\folio}
                         {\zbibkey}
                         {{\the\zbibauthor}{\the\zbibyear}{\the\zbibfullauthor}}
                         {#2}}%
    \znext
  \fi
%%%  \if \indexcites
%%%    \edef \znext {\noexpand\xbiblio1{\the\zbibauthor}}%
%%%    \znext
%%%  \fi
  \ignorespaces}

\def \zparseauthoryear #1;#2;#3;#4\zmark{%
  \if \orp{\emptyargp{#1}}{\emptyargp{#2}}%
    \error{nocitetags}{The author and/or year are missing}%
  \fi
  \zbibauthor = {#1}%
  \zbibyear = {#2}%
  \zbibfullauthor = {#3}}

%                       Bibliography Citation Page List
%                       ------------ -------- ---- ----


\setflag \zcitexrefsloaded = \false


%~ If this macro is specified as the |\endpartext| of a bibliography
%~ |\list|, then each entry in the bibliography will include a list
%~ of the pages citing that entry. The argument determines whether
%~ each page is made into a hyperlinked PDF button.
%~
%~ In order for the citation information to be available, the
%~ |\xrefcites| document parameter must be set to 1.

\def \citepagelist #1{%                                 {button?}
  \if \notp{\zcitexrefsloaded}%
    \mapxrefcites{\zbibcitelist}%
    \global\setflag \zcitexrefsloaded = \true
  \fi
  \setflag \zbibfirstpage = \true
  \def \zbibprevpage {}%
  \withname\maplistlf {\=citetag:\the\bibitemtag}
                      {\zbibcitepage{##1}{#1}}}

\def \zbibcitelist #1#2{%                               {page}{tag}
  \if \withname\undefinedp {\=citetag:#2}%
    \withname\setlist {\=citetag:#2}={}%
  \fi
  \withname\appendlf {\=citetag:#2}{#1}}

\def \zbibcitepage #1#2{%                               {page}{button?}
  \stringeql{\zsamepage}{#1}{\zbibprevpage}%
  \if \notp{\zsamepage}%
    \if \notp{\emptydefp{\zbibprevpage}},\space \fi
    \if #2%
      \pagebutton{#1}{#1}%
    \else
      #1%
    \fi
    \def \zbibprevpage {#1}%
  \fi}

%                       Low-Level Citation Commands
%                       --------- -------- --------


%~ This command produces the key of the bibliography entry specified
%~ by the *tag*. The command makes sense only for the numbered or
%~ keyed citation styles.

\def \citekey #1{%                                      {tag} %^citation
  \if \eqlp{\citestyle}{\citebyauthoryear}%
    \warning{badcitekey}{The \noexpand\citekey command doesn't make sense
                         when citing by author/year}%
  \fi
  \zcitetaginfo{#1}%
  \zcitetext{#1}{\zcitekey}%
  \zxrefcite{\true}{#1}}
%%%  \if \indexcites \zciteindex{\zcitekey}\fi}

%~ This command produces the author of the bibliography entry specified
%~ by the *tag*. The command makes sense only for the author/year
%~ citation style.

\def \citeauthor #1{%                                      {tag} %^citation
  \if \neqlp{\citestyle}{\citebyauthoryear}%
    \warning{badciteauthor}{The \noexpand\citeauthor command only makes sense
                            when citing by author/year}%
  \fi
  \zcitetaginfo{#1}%
  \zcitetext{#1}{\zciteauthor}%
  \zxrefcite{\true}{#1}}

%~ This command produces the year of the bibliography entry specified
%~ by the *tag*. The command makes sense only for the author/year
%~ citation style.

\def \citeyear #1{%                                      {tag} %^citation
  \if \neqlp{\citestyle}{\citebyauthoryear}%
    \warning{badciteyear}{The \noexpand\citeyear command only makes sense
                          when citing by author/year}%
  \fi
  \zcitetaginfo{#1}%
  \zcitetext{#1}{\zciteyear}%
  \zxrefcite{\true}{#1}}

%~ This command produces the full author of the bibliography entry specified
%~ by the *tag*. The command makes sense only for the author/year
%~ citation style.

\def \citefullauthor #1{%                                      {tag} %^citation
  \if \neqlp{\citestyle}{\citebyauthoryear}%
    \warning{badcitefullauthor}{The \noexpand\citefullauthor command only makes sense
                                when citing by author/year}%
  \fi
  \zcitetaginfo{#1}%
  \zcitetext{#1}{\zcitefullauthor}%
  \zxrefcite{\true}{#1}}

\def \zcitetaginfo #1{%                                 {tag}
  \zcitenoinfo
  \if \definedp{\=zB:#1}%
    \expandafterthrice\zcitetaginfoa \name{\=zB:#1}%
  \else
    \writelog{ZzTeX citation: undefined tag `#1'.}%
    \zxwriteinfo{\false}{undef-tag}{#1}{}%
    \global\increment \zuntagrefs
  \fi}

\def \zcitetaginfoa #1\zmark#2\zmark#3#4#5{% folio\zmark key\zmark
                                           % {author}{year}{full-author}
  \if \eqlp{\citestyle}{\citebyauthoryear}%
    \def \zciteauthor     {#3}%
    \def \zciteyear       {#4}%
    \def \zcitefullauthor {#5}%
  \else
    \def \zcitekey        {#2}%
  \fi}

\def \zcitenoinfo {%
  \def \zcitekey        {???}%
  \def \zciteauthor     {???author???}%
  \def \zciteyear       {???}%
  \def \zcitefullauthor {???full-author???}}

\def \zcitetext #1#2{%                                  {tag}{text}
  \if \DVIWindoinuse \colorspecial{ifview color push rgb .5 .22 .24}\fi
  \if \PDFhyperlinks
    \tagbutton{#1}{#2}%
  \else
    #2%
  \fi
  \if \DVIWindoinuse \colorspecial{ifview color pop}\fi}

\def \zxrefcite #1#2{%                                  {defined?}{tag}
  \ifcase \xrefcites
    \relax
  \or
    \xref{cite}{\folio}{}{}{#2}%
  \else
    \customcitexref{#1}{#2}%                            {boolean}{tag}
  \fi}

%%%\def \zciteindex #1{%                           {\zcitemacro}
%%%  \expandafter\xcite\expandafter1\expandafter{#1}}

%                       High-Level Citation Commands
%                       ---------- -------- --------


%~ This high-level citation command produces the key or author/year
%~ of the bibliography entry specified by the *tag*. The text
%~ is formatted in the style appropriate for a noun in a sentence.
%~ There are two formats for the command:
%~ * |\citenoun{*tag*}| accepts a tag and produces the key or author/year.
%~ * |\citenoun{*tag*;*info*}| accepts a tag and additional 
%~ information such as a page number or section reference.
%~ *.
%~ The format of the text produced is controlled by the |\citenounformat|,
%~ |\citenouninfoformat|, and |\citepunct| macros in the design file.

\def \citenoun #1{%                             {tag[;info]} %^citation
  \expandafter\zsetcitepunc \citepunct
  \zcitetagsinfo #1;;\zmark
  \def \zcitenounsep {}%
  \expandafter\zcitenoun \zcitetags,\zmark}

\def \zcitenoun #1,#2\zmark {%                  tag,other-tags\zmark
  \zcitenounsep
  \edef \zcitenounsep {\zcitesep}%
  \if \eqlp{\citestyle}{\citebyauthoryear}%
    \if \emptydefp{\zciteinfo}%
      \zciteonetag{\citenounformat}#1!!\zmark
    \else
      \citenouninfoformat{\citeauthor{#1}}{\citeyear{#1}}{\zciteinfo}%
    \fi
  \else
    \if \emptydefp{\zciteinfo}%
      \citenounformat{\citekey{#1}}%
    \else
      \citenouninfoformat{\citekey{#1}}{\zciteinfo}%
    \fi
  \fi
  \if \notp{\emptyargp{#2}}\zcitenoun #2\zmark \fi}

%~ This high-level citation command produces the key or author/year
%~ of the bibliography entry specified by the *tag*. The text
%~ is formatted in the style appropriate for a parenthetical
%~ citation list. There are two formats for the command:
%~ * |\citeparen{*tag-list*}| accepts one or more tags separated
%~ by commas and produces a parenthetical list of keys or author/years.
%~ * |\citeparen{*tag*;*info*}| accepts a tag and additional 
%~ information such as a page number or section reference.
%~ *.
%~ The format of the text produced is controlled by the |\citeparenformat|,
%~ |\citepareninfoformat|, and |\citepunct| macros in the design file.

\def \citeparen #1{%                            {tags[;info]} %^citation
  \expandafter\zsetcitepunc \citepunct
  \zcitetagsinfo #1;;\zmark
  \expandafter\zciteparen \zcitetags,\zmark
  \zcitecfence}

\def \zciteparen #1,#2\zmark {%                 tag,other-tags\zmark
  \zciteofence
  \edef \zciteofence {\zcitesep}%
  \if \eqlp{\citestyle}{\citebyauthoryear}%
    \if \emptydefp{\zciteinfo}%
      \zciteonetag{\citeparenformat}#1!!\zmark
    \else
      \citepareninfoformat{\citeauthor{#1}}{\citeyear{#1}}{\zciteinfo}%
    \fi
  \else
    \if \emptydefp{\zciteinfo}%
      \citeparenformat{\citekey{#1}}%
    \else
      \citepareninfoformat{\citekey{#1}}{\zciteinfo}%
    \fi
  \fi
  \if \notp{\emptyargp{#2}}\zciteparen #2\zmark \fi}

%~ This high-level citation command produces the key or author/year
%~ of the bibliography entry specified by the *tag*. The text
%~ is formatted in the style appropriate for a citation that is part
%~ of a surrounding parenthetical sentence.
%~ There are two formats for the command:
%~ * |\citeplain{*tag*}| accepts a tag and produces the key or author/year.
%~ * |\citeplain{*tag*;*info*}| accepts a tag and additional 
%~ information such as a page number or section reference.
%~ *.
%~ The format of the text produced is controlled by the |\citeplainformat|
%~ and |\citeplaininfoformat| macros in the design file.

\def \citeplain #1{%                            {tag[;info]} %^citation
  \zcitetagsinfo #1;;\zmark
  \expandafter\zciteplain \zcitetags,\zmark}

\def \zciteplain #1,#2\zmark {%                 tag,other-tags\zmark
  \if \eqlp{\citestyle}{\citebyauthoryear}%
    \if \emptydefp{\zciteinfo}%
      \zciteonetag{\citeparenformat}#1!!\zmark
    \else
      \citepareninfoformat{\citeauthor{#1}}{\citeyear{#1}}{\zciteinfo}%
    \fi
  \else
    \if \emptydefp{\zciteinfo}%
      \citeparenformat{\citekey{#1}}%
    \else
      \citepareninfoformat{\citekey{#1}}{\zciteinfo}%
    \fi
  \fi
  \if \notp{\emptyargp{#2}}\zciteplain #2\zmark \fi}

\def \zsetcitepunc #1#2#3{%
  \def \zciteofence {#1}%
  \def \zcitecfence {#2}%
  \def \zcitesep    {#3}}

\def \zcitetagsinfo #1;#2;#3\zmark {%           {tags[;info]}
  \if \emptyargp{#1}%
    \warning{nocitetags}{No bibliography entry tags were specified}%
  \fi
  \def \zcitetags {#1}%
  \def \zciteinfo {#2}}

\def \zciteonetag #1#2!#3!#4\zmark {%
  \if \emptyargp{#2}%
    \citeyear{#3}%
  \else
    #1{\citeauthor{#2}}{\citeyear{#2}}%
  \fi}