summaryrefslogtreecommitdiff
path: root/macros/luatex/optex/base/usebib.opm
blob: e278774c97a61c8a4e6d5a21ede4a50e723aa3d2 (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
%% This is part of OpTeX project, see http://petr.olsak.net/optex

\_codedecl \MakeReference {Reading bib databases <2020-03-13>} % loaded on demand by \usebib

   \_doc -----------------------------
   Loading the `librarian.tex` macro package. See `texdoc librarian`
   for more information about it.

   We want to ignore `\errmessage` and we want not to create `\jobname.lbr`
   file.
   \_cod -----------------------------

\_def\errmessage#1{}
\_def\newwrite#1{\_csname lb@restoreat\_endcsname \_endinput}
\_def\_tmpb{\_catcode`\_=12 \_input librarian \_catcode`\_=11 }\_tmpb
\_let\errmessage=\_errmessage
\_let\newwrite=\_newwrite

\_private \BibFile \ReadList \SortList \SortingOrder \NameCount \AbbreviateFirstname
   \CreateField \RetrieveFieldInFor \RetrieveFieldIn ;

   \_doc -----------------------------
   The `\usebib` command.
   \_cod -----------------------------

\_def\_usebib/#1 (#2) #3 {%
  \_ifx\_citelist\_empty
     \_opwarning{No cited items. \_noexpand\usebib ignored}%
  \_else
     \_bgroup \_par
        \_emergencystretch=.3\_hsize
        \_ifx\_bibpart\_undefined \_def\_bibpart{none}\_fi
        \_def\_optexbibstyle{#2}%
        \_setctable\_optexcatcodes
        \_ea \_skiptoendinput \_input languages.opm
        \_input bib-#2.opm
        \_the \_bibtexhook
        \_ifcsname _mt:bib.and:\_cs{_lan:\_the\_language}\_endcsname \_else
           \_opwarning{\_string\usebib: No phrases for language
                       "\_cs{_lan:\_the\_language}" (using "en")}%
           \_language=0 \_chardef\_documentlanguage=0
        \_fi
        \_let\_citeI=\_relax \_xdef\_citelist{\_citelist\_citelistB}%
        \_global\_let\_addcitelist=\_writeXcite
        \_def\_tmp##1[*]##2\_relax{\_def\_tmp{##2}}\_expandafter\_tmp\_citelist[*]\_relax
        \_ifx\_tmp\_empty\_else % there was \nocite[*] used. 
           \_setbox0=\_vbox{\_hsize=\_maxdimen \_def\_citelist{}\_adef@{\_readbibentry}%
           \_input #3.bib
           \_expandafter}\_expandafter\_def\_expandafter\_citelist\_expandafter{\_citelist}%
        \_fi
        \_def\_citeI[##1]{\_csname lb@cite\_endcsname{##1}{\_bibpart}{}{}}\_citelist
        \_BibFile{#3}%
        \_if s#1\_SortList{\_bibpart}\_fi
        \_ReadList{\_bibpart}%
        \_restorectable
     \_egroup
  \_fi
}
\_long\_def\_skiptoendinput#1\_endinput{}
\_def\_readbibentry#1#{\_readbibentryA}
\_def\_readbibentryA#1{\_readbibentryB#1,,\_relax!.}
\_def\_readbibentryB#1#2,#3\_relax!.{\_addto\_citelist{\_citeI[#1#2]}}

   \_doc -----------------------------
   Corrections in librarian macros.
   \_cod -----------------------------

\_tmpnum=\_catcode`\@  \_catcode`\@=11
\_def\lb@checkmissingentries#1,{% we needn't \errmessage here, only \opmacwarning
  \_def\lb@temp{#1}%
  \_unless\_ifx\lb@temp\lb@eoe
    \lb@ifcs{#1}{fields}%
            {}%
            {\_opwarning{\_string\usebib: entry [#1] isn't found in .bib}}%
    \_ea\lb@checkmissingentries
  \_fi
}
\_def\lb@readentry#1#2#3,{% space before key have to be ingnored
  \_def\lb@temp{#2#3}%      we need case sensitive keys
  \_def\lb@next{\_ea\lb@gotoat\lb@gobbletoeoe}%
  \lb@ifcs\lb@temp{requested}%
          {\_let\lb@entrykey\lb@temp
           \lb@ifcs\lb@entrykey{fields}{}%
                {\lb@defcs\lb@entrykey{fields}{}%
                 \_lowercase{\lb@addfield{entrytype}{#1}}%
                 \_let\lb@next\lb@analyzeentry}}{}%
  \lb@next
}
\_let\lb@compareA=\lb@compare
\_let\lb@preparesortA=\lb@preparesort
\_def\lb@compare#1\lb@eoe#2\lb@eoe{% SpecialSort:
  \_ifx\lb@sorttype\lb@namestring
     \_ifx\_sortfield\_undefined \lb@compareA#1\lb@eoe#2\lb@eoe
     \_else 
        \_ea\_RetrieveFieldInFor\_ea{\_sortfield}\lb@entrykey\lb@temp
        \_ifx\lb@temp\_empty \_toks1={#1\lb@eoe}\_else \_toks1=\_ea{\lb@temp\lb@eoe}\_fi
        \_ea\_RetrieveFieldInFor\_ea{\_sortfield}\lb@currententry\lb@temp
        \_ifx\lb@temp\_empty \_toks2={#2\lb@eoe}\_else \_toks2=\_ea{\lb@temp\lb@eoe}\_fi
        \_edef\lb@temp{\_noexpand\lb@compareA\_space\_the\_toks1 \_space\_the\_toks2}\lb@temp
     \_fi
  \_else \lb@compareA#1\lb@eoe#2\lb@eoe \_fi
}
\_def\lb@preparesort#1#2\lb@eoe{%
  \_if#1-%
    \_def\lb@sorttype{#2}%
  \_else
    \_def\lb@sorttype{#1#2}%
  \_fi
  \lb@preparesortA#1#2\lb@eoe
}
\_def\_SpecialSort#1{\_def\_sortfield{#1}}
\_def\WriteImmediateInfo#1{}  % the existence of .lbr file bocks new reading of .bib
\_catcode`\@=\_tmpnum

   \_doc -----------------------------
   Main action per every entry.
   \_cod -----------------------------

\_def\MakeReference{\_par \_bibskip
  \_advance\_bibnum by1
  \_isdefined{_bim:\_the\_bibnum}\_iftrue
     \_edef\_tmpb{\_csname _bim:\_the\_bibnum\_endcsname}%
     \_bibmark=\_ea{\_tmpb}%
  \_else \_bibmark={}\_fi
  \_edef\_tmpb{\EntryKey}%
  \_noindent \_dest[cite:\_the\_bibnum]\_printlabel\EntryKey 
  \_printbib 
  {%  
     \_RetrieveFieldIn{entrytype}\_entrytype
     \_csname _print:BEGIN\_endcsname
     \_isdefined{_print:\_entrytype}\_iftrue 
        \_csname _print:\_entrytype\_endcsname
     \_else 
        \_ifx\_entrytype\_empty \_else
           \_opwarning{Entrytype @\_entrytype\_space from [\EntryKey] undefined}%
           \_csname _print:misc\_endcsname
     \_fi\_fi
     \_csname _print:END\_endcsname
     \_ifx\_wref\_wrefrelax\_else 
         \_immediate\_wref\_Xbib{{\EntryKey}{\_the\_bibnum}{\_the\_bibmark}}\_fi
  }\_par
}

   \_doc -----------------------------
   The \`\_bprinta`, \`\_bprintb`, \`\_bprintc`, \`\_bprintv` 
   commands used in the style files:
   \_cod -----------------------------

\_def\_bprinta {\_bprintb*}
\_def\_bprintb #1[#2#3]{%
   \_def\_bibfieldname{#2#3}%
   \_if!#2\_relax 
      \_def\_bibfieldname{#3}%
      \_RetrieveFieldIn{#3}\_bibfield
      \_ifx\_bibfield\_empty\_else
         \_RetrieveFieldIn{#3number}\_namecount
         \_def\_bibfield{\_csname _Read#3\_ea\_endcsname \_csname _pp:#3\_endcsname}%
      \_fi
   \_else
      \_RetrieveFieldIn{#2#3}\_bibfield
   \_fi
   \_if^#1^%
      \_ifx\_bibfield\_empty \_ea\_ea\_ea \_doemptyfield
      \_else \_ea\_ea\_ea \_dofullfield \_fi
   \_else \_ea \_bprintaA
   \_fi
}
\_def\_dofullfield#1#2{\_def\_dofield##1{#1}\_ea\_dofield\_ea{\_bibfield}}
\_def\_doemptyfield#1#2{\_def\_dofield##1{#2}\_ea\_dofield\_ea{\_bibfield}}
\_let\_Readauthor=\ReadAuthor  \_let\_Readeditor=\ReadEditor
\_def\_bprintaA #1#2{\_ifx\_bibfield\_empty #2\_else\_bprintaB #1**\_eee\_fi}
\_def\_bprintaB #1*#2*#3\_eee{\_if^#3^#1\_else\_ea\_bprintaC\_ea{\_bibfield}{#1}{#2}\_fi}
\_def\_bprintaC #1#2#3{#2#1#3}
\_def\_bprintc#1#2{\_bprintcA#1#2**\_relax}
\_def\_bprintcA#1#2*#3*#4\_relax{\_ifx#1\_empty \_else \_if^#4^#2\_else#2#1#3\_fi\_fi}
\_def\_bprintv [#1]#2#3{\_def\_tmpa{#2}\_def\_tmpb{#3}\_bprintvA #1,,}
\_def\_bprintvA #1,{%
   \_if^#1^\_tmpb\_else
      \_RetrieveFieldIn{#1}\_tmp
      \_ifx \_tmp\_empty 
      \_else \_tmpa \_def\_tmpb{}\_def\_tmpa{}%
      \_fi
   \_ea \_bprintvA
   \_fi
} 
\_sdef{_pp:author}{\_letNames\_authorname}
\_sdef{_pp:editor}{\_letNames\_editorname}
\_def\_letNames{\_let\_Firstname=\Firstname \_let\_Lastname=\Lastname
   \_let\_Von=\Von \_let\_Junior=\Junior
}

   \_doc -----------------------------
   Various macros + multilinguas. Note that \`\_nobibwarnlist` is used in
   \`\_bibwarning` and it is set by \^`\nobibwarning` macro.
   \_cod -----------------------------

\_def\_bibwarning{%
   \_ea\_isinlist \_ea\_nobibwarnlist\_ea{\_ea,\EntryKey,}\_iffalse
       \_opwarning{Missing field "\_bibfieldname" in [\EntryKey]}\_fi}

\_endcode

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The file `usebib.opm` implements the command
\~`\usebib/<sorttype> (<style>) <bibfiles>`
where `<sorttype>` is one letter `c` (references ordered by citation order in
the text) or `s` (references ordered by key in the style file), 
`<style>` is the part of the name `bib-<style>.opm` of the style file
and `<bibfiles>` are one or more `.bib` file names without suffix separated by
comma without space. Example:

\begtt
\usebib/s (simple) mybase,yourbase
\endtt

This command reads the `<bibfiles>` directly and creates the list of
bibliographics references (only those declared by by \~`\cite[]` or
\~`\nocite[]` in the text). The formatting of such references is defined in the style
file.

The principle \"first entry wins" is used. Suppose `\usebib/s (simple) local,global`.
If an entry with the same label is declared in `local.bib` and in
`global.bib` too then the first wins. So, you can set an exceptions in your
`local.bib` file for your document.

The `bib-<style>.opm` declares entry types (like `@BOOK`, `@ARTICLE`) and declares
their mandatory and optionals fields (like `author`, `title`). When
mandatory field is missing in an entry in the `.bib` file then warning is printed on the
terminal about it. You can suppress such warnings by command
\^`\nobibwarning`~`[<bib-labels>]`, where <bib-labels> is comma separated list
of labels (without spaces) where missing mandatory fields will be no warned.

Old `.bib` files may use obscure notation for accents like `{\"o}`.
Recommendation: convert such old files to Unicode encoding. If you are
unable to do this then you can set \^`\bibtexhook={`\^`\oldaccents}`.

\secc Notes for bib style writers

The `.bib` files include records in the format:

\begtt
@<entry-type>{<label>,
   <field-name> = "<field-data>",
   <field-name> = "<field-data>",
   ...etc
}
\endtt
%
see the file `demo/op-biblist.bib` for a real example. The <entry-types> and
<field-names> are case insensitive.

Ancient Bib\TeX/ has read such files and has generated files appropriate for
reading by \LaTeX. It has worked with a set of <entry-types>, see the www
page \url{http://en.wikipedia.org/wiki/BibTeX}. The set of entry types
listed on this www page is de facto the Bib\TeX/ standard. The \OpTeX/ bib
style writer must \"declare" all such entry types and more non-standard
entry types can be declared too, if there is a good reason for doing it. The
word \"declare" used in previous sentence means that bib style writer must
define the printing rules for each <entry-type>.
The printing rules for <entry-type> include: which fields will be printed, in
what oder, by what format they will printed on (italic, caps, etc.),
which fields are mandatory, which are optional and which are ingnored in
`.bib` records.

The style writer can be inspired by two styles alredy done: `bib-simple.opm`
and `bib-iso690.opm`. The second one is documented in detail in
section~\ref[isobib].

The printing rules for each <entry-type> must be declared by
`\_sdef{_print:<entry-type>}` in `bib-<style>.opm` file.
The `<entry-type>` have to be lowercase here. \OpTeX/ supports following
macros for more comfortable setting of printing rules:

\begitems
* \^`\_bprinta` `[<field-name>]  {<if defined>} {<if not defined>}`. The part <if defined>
    is executed if <field-name> is declared in .bib file for the entry which is
    currently processed. Else the part <if not defined> is processed.
    The part <if defined> can include the `*` parameter which is replaced
    by the value of the <field-name>.
*   The part <if not defined> can include
    the \^`\_bibwarning` command if the <field-name> is mandatory.
* \^`\_bprintb` `[<field-name>]  {<if defined>} {<if not defined>}`. The same as
    `\_bprinta`, but the `##1` parameter is used instead `*`. Differences: `##1`
    parameter can be used more than once and can be enclosed in nested
    braces. The `*` parameter can be used at most once and cannot be enclosed
    in braces. Warning: if the `\_bprintb` commands are nested (`\_bprintb` in
    `\_bprintb`), then you need to write `####1` parameter for internal `\_bprintb`.
    But if `\_bprinta` commands are nested then the parameter is not duplicated.
* \^`\_bprintc` `\macro {<if non-empty>}`. The <if non-empty> part is executed if
    `\macro` is non-empty. The `*` parameter can be used, it is replaced by
    the `\macro`.
* \^`\_bprintv` `[<field1>,<field2>,...] {<if defined>} {<if not defined>}`.
    The part <if defined> is executed if <field1> or <filed2> or ...
    is defined, else the second part <if not defined> is executed.
    There is one filed name or the list field names separated by commas.  
    The parts cannot include any parameter.
\enditems

There are two special field-names: `!author` and `!editor`. The processed list of
authors or editors are printed here instead of raw data, see the commands
\~`\_authorname` and \~`\_editorname` below.

The bib style writer can define `_print:BEGIN` and/or `_print:END`.
They are executed at the begin or
end of each `<entry-type>`. The formatting does not solve the numbering and
paragraph indentation of the entry. This is processed by \^`\_printbib` macro
used in \OpTeX/ (and may be redefined by the author or document designer).

The \^`\bibmark={something}` can be declared, for instance in the `_print:END` macro.
Such bibmark is saved to the `.ref` file and used in next \TeX/ run as
`\cite` marks when `\nonumcitations` is set.

Moreover, the bib style writer must declare the format of
printing of special fields `author` and `editor`. These fields include a
list of names, each name is precessed individually in a loop. The
\^`\_authorname` or \^`\_editorname` is called for each name in the list.
The bib style writer must define
the \`\_authorname` and \`\_editorname` commands
in order to declare the format of printing each individual name.
The following control sequences can be used in these macros:

\begitems
* `\_NameCount`: the number of currently processed author in the list
* `\_namecont`: the total number of the authors in the list
* `\_Lastname`, `\_Firstname`, `\_Von`, `\_Junior`: the parts of the name.
\enditems

The whole style file is read in the group during `\usebib` command is executed
before typesetting the reference list. Each definition or setting is local
here.

The auto-generated phrases (dependent on current language) can be used in
bib style files by `\_mtext{bib.<identifier>}`, where <ident> is an identifier of
the phrase and the phrase itself is defined by
`\_sdef{_mt:bib.<identifier>:<language>}{<phrase>}`. See section~\ref[langphrases]
for more detail. Phrases for <identifiers>:
and, etal, edition, citedate, volume, number, prepages, postpages, editor,
editors, available, availablealso, bachthesis, masthesis, phdthesis
are defined already, see the end of section~\ref[langphrases].

If you are using non-standard field-names in .bib database and bib style,
you have to declare them by `\_CreateField {<fieldname>}`.

You can declare `\_SortingOrder` in the manner documented by librarian package.

User or author of the bib style can create the hidden field which has a
precedence while sorting names. Example:

\begtt
\CreateField {sortedby}
\SpecialSort {sortedby}
\endtt
Suppose that the .bib file includes:
\begtt
...
author   = "Jan Chadima",
sortedby = "Hzzadima Jan",
...
\endtt
Now, this author is sorted between H and I, because the Ch digraph in this
name has to be sorted by this rule.

If you need (for example) to place the autocitations before other citations,
then you can mark your entries in `.bib` file by `sortedby = "@"`, because this
character is sorted before `A`.

\_endinput

History:
--------

Apr. 2014:  released in OPmac
Jan. 2016:  "first entry wins" implemented
Apr. 2016:  \readentry -> \readbibentry, \usebib->\MakeReference
Feb. 2018:  \input librarian does not create \jobname.lbr

% Source:

% opmac-bib
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Petr Olsak, version Feb. 2018 (beta)

% This is module for OPmac macros, see http://petr.olsak.net/opmac.html
% The command:
%    \usebib/<sorttype> (<style>) (bibfiles>
% for direct reading .bib files by OPmac macros (without bibTeX) is implemented here.
% The implementation depends on librarian.tex package by Paul Isambert.
% Use:
%    \input opmac  \input opmac-bib
% or simply:
%    \input opmac-bib
% before using the command \usebib.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end