Debian Codegraph @

texmf-dist/source/latex/sblidx/sblidx.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (c) 2026 David Purton <[email protected]>
4 %
5 % This work may be distributed and/or modified under the conditions of
6 % the LaTeX Project Public License, either version 1.3c of this license
7 % or (at your option) any later version. The latest version of this
8 % license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3c or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 %<*driver>
14 \RequirePackage{pdfmanagement}
15 \documentclass[a4paper]{l3doc}
16 \usepackage[T1]{fontenc}
17 \usepackage{microtype}
18 \usepackage{mlmodern}
19 \usepackage[font=small, skip=6pt]{caption}
20 \usepackage{xcolor}
21 \usepackage{listings}
22 \usepackage{sblidx}
23
24 \AddToHook{env/macrocode/before}{%
25 \addvspace{\medskipamount}}
26
27 \AddToHook{env/macro/before}{%
28 \addvspace{\medskipamount}}
29
30 \begin{document}
31 \DocInput{\jobname.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 %
36 % \title{The \pkg{sblidx} Package}
37 % \author{David Purton\thanks{Email: \url{[email protected]}}}
38 % \date{2026-04-04 v1.0}
39 %
40 % \maketitle
41 %
42 % \begin{abstract}
43 % \pkg{sblidx} provides a \LaTeX\ package for creating indices in a style
44 % recommended by the Society of Biblical Literature as outlined in the
45 % \emph{Society of Biblical Literature Handbook of Style} and
46 % \emph{Preparing Indices}.\footnote{See \emph{The SBL Handbook of Style:
47 % For Biblical Studies and Related Disciplines}, 2nd ed.\@ (SBL Press,
48 % 2014); SBL Publications, \emph{Preparing Indices},
49 % \url{https://www.sbl-site.org/wp-content/uploads/2024/05/Indexing_SBL.pdf}.}
50 % This includes producing an index of ancient sources with the help of the
51 % \pkg{bibleref-sbl} package, an index of modern authors with the help of
52 % the \pkg{biblatex-sbl} package and an index of subjects. Page ranges are
53 % automatically compressed and when indexed entries appear in footnotes they
54 % are indicated with n.\ and nn.
55 % \end{abstract}
56 %
57 % \tableofcontents
58 %
59 % \section{Introduction}
60 %
61 % The Society of Biblical Literature has specific requirements for indexes.
62 % Typically there are separate indices for subjects, modern authors and
63 % ancient sources. And these different indices are laid out in a slightly
64 % different way. Additionally page ranges are compressed and index references
65 % in footnotes are indicated with n.\ and nn. This package attempts to handle
66 % these variations with as much automation as possible.
67 %
68 % A subject index is automatically enabled and indices of modern authors and
69 % ancient sources can optionally be added. Modern authors are indexed when
70 % they are cited using the \pkg{biblatex-sbl} package and books of the Bible
71 % are indexed using the \pkg{bibleref-sbl} package. The different format of
72 % these three indices as required by the SBL is handled automatically.
73 %
74 % \subsection{Usage}
75 %
76 % A simple example document is shown in Figure \ref{exampleusage} and the
77 % output is shown in Figure \ref{exampleoutput}.
78 %
79 % \textbf{Note:} You should load \pkg{sblidx} \emph{after} you load
80 % \pkg{biblatex}.
81 %
82 % \lstset{
83 % language={[LaTeX]TeX},
84 % morekeywords={\ibibleverse, \SBLPrintIndices, \autocite},
85 % basicstyle=\small\ttfamily,
86 % keywordstyle=\color{blue!70!black},
87 % frame=single,
88 % tabsize=2,
89 % }
90 %
91 % \begin{figure}[b!]
92 % \iffalse
93 %<*example>
94 % \fi
95 \begin{lstlisting}
96 \documentclass{article}
97 \begin{filecontents}{\jobname.bib}
98 @book{talbert:1992,
99 author = {Talbert, Charles H.},
100 title = {Reading John},
101 subtitle = {A Literary and Theological Commentary on the Fourth
102 Gospel and the Johannine Epistles},
103 location = {New York},
104 publisher = {Crossroad},
105 date = {1992}
106 }
107 \end{filecontents}
108 \usepackage[style=sbl]{biblatex}
109 \addbibresource{\jobname.bib}
110 \usepackage[subject, ancient sources, modern authors]{sblidx}
111 \begin{document}
112 A topic\index{topic}. A reference to \ibibleverse{John}(1:1)
113 \autocite{talbert:1992}. Another topic.\footnote{with an index
114 entry in the footnote.\index{another topic}.} And an example of a
115 cross reference in an index.\index{entry|see{topic}}
116 \SBLPrintIndices
117 \end{document}
118 \end{lstlisting}
119 % \iffalse
120 %</example>
121 % \fi
122 % \caption{Example \LaTeX\ source showing subject, ancient sources and
123 % modern authors indices.}
124 % \label{exampleusage}
125 % \end{figure}
126 %
127 % \begin{figure}
128 % \centering
129 % \framebox[0.5\linewidth]{%
130 % \parbox{\dimexpr 0.5\linewidth-2\fboxsep-2\fboxrule}{%
131 % \setlength{\parskip}{0pt}\small
132 % {\large\bfseries Ancient Sources Index\par}
133 % \medskip
134 % \textbf{New Testament}\par
135 % \quad John\par
136 % \qquad 1:1\hfill 1\par
137 % \bigskip
138 % {\large\bfseries Modern Authors Index\par}
139 % \medskip
140 % Talbert, Charles H.\hfill 1 n.~1\par
141 % \bigskip
142 % {\large\bfseries Subject Index\par}
143 % \medskip
144 % another topic, 1 n.~2\par
145 % \smallskip
146 % entry. \emph{See} topic\par
147 % \smallskip
148 % topic, 1
149 % }%
150 % }
151 % \caption{Example output showing subject, ancient sources and modern
152 % authors indices.}
153 % \label{exampleoutput}
154 % \end{figure}
155 %
156 % \subsection{Bug Reports and Feature Requests}
157 %
158 % Bug reports and feature requests can be made at the \pkg{sbltex} GitHub
159 % repository. See \url{https://github.com/dcpurton/sbltex}.
160 %
161 % \section{Package Options}
162 %
163 % \begin{function}{ancient sources}
164 % \begin{syntax}
165 % ancient sources = true \textbar\ false\hfill Default: false
166 % \end{syntax}
167 % Enable an index of ancient sources. Bible books (including
168 % deutero-canonical books) are indexed in SBL style use the
169 % \pkg{bibleref-sbl}, \pkg{bibleref-parse} and \pkg{bibleref} packages.
170 % \end{function}
171 %
172 % \begin{function}{ancient sources title}
173 % \begin{syntax}
174 % ancient sources title = \meta{title}\hfill Default: Ancient Sources Index
175 % \end{syntax}
176 % Set the title of the ancient sources index.
177 % \end{function}
178 %
179 % \begin{function}{modern authors}
180 % \begin{syntax}
181 % modern authors = true \textbar\ false\hfill Default: false
182 % \end{syntax}
183 % Enable an index of modern authors. Modern authors are indexed when cited
184 % using \pkg{biblatex-sbl}.
185 % \end{function}
186 %
187 % \begin{function}{modern authors title}
188 % \begin{syntax}
189 % modern authors title = \meta{title}\hfill Default: Modern Authors Index
190 % \end{syntax}
191 % Set the title of the modern authors index.
192 % \end{function}
193 %
194 % \begin{function}{subject}
195 % \begin{syntax}
196 % subject = true \textbar\ false\hfill Default: true
197 % \end{syntax}
198 % Enable an index of subjects. Subjects are indexed using |makeindex| and
199 % the standard \cs{index} macro.
200 % using \pkg{biblatex-sbl}.
201 % \end{function}
202 %
203 % \begin{function}{subject title}
204 % \begin{syntax}
205 % subject title = \meta{title}\hfill Default: Subject Index
206 % \end{syntax}
207 % Set the title of the subject index.
208 % \end{function}
209 %
210 % \section{Commands}
211 %
212 % \begin{function}{\SBLFootnoteIndex}
213 % \begin{syntax}
214 % \cs{SBLFootnotes} \oarg{name} \marg{entry}
215 % \end{syntax}
216 % \cs{index} is defined to this macro within footnotes so that note numbers
217 % are automatically included unless some other formatting is requested by
218 % the user. \meta{name} is the name of the raw index file and \meta{entry}
219 % should be formatted according to syntax required by |makeindex|. In
220 % general this macro should not be called directly as it automatically
221 % includes the current value of the |footnote| counter.
222 % \end{function}
223 %
224 % \begin{function}{\SBLPageWithNote}
225 % \begin{syntax}
226 % \cs{SBLPageWithNote} \marg{comma separated list of notes} \marg{page}
227 % \end{syntax}
228 % Output page with note number using n.\ or nn.\ depending on how many notes
229 % there are. When |\index| appears in a footnote, this command is called
230 % automatically using the |makeindex|
231 % \verb+\index{gnat|SBLPageWithNote{\thefootnote}}+ syntax.
232 % \end{function}
233 %
234 % \begin{function}{\SBLPrintIndices}
235 % \begin{syntax}
236 % \cs{SBLPrintIndices}
237 % \end{syntax}
238 % Print the indices. This command prints one or more indices depending on
239 % which are enabled with the |ancient sources|, |modern authors| and
240 % |subject| options.
241 % \end{function}
242 %
243 % \begin{function}{\SBLProcessIndexPages}
244 % \begin{syntax}
245 % \cs{SBLProcessIndexPages} \marg{list of pages}
246 % \end{syntax}
247 % Process a list of pages produced by |makeindex|, compressing page ranges
248 % and note ranges as needed according to SBL style. This function is called
249 % by the required custom index style file |sblidx.ist|.
250 % \end{function}
251 %
252 % \begin{function}{\SBLSetMaxRomanPage}
253 % \begin{syntax}
254 % \cs{SetMaxRomanPage} \marg{page}
255 % \end{syntax}
256 % Set the maximum Roman page number (as an integer, not in Roman format) in
257 % the document in case automatic detection fails. This should be set before
258 % the indices are printed.
259 % \end{function}
260 %
261 % \begin{function}{\see, \seealso}
262 % \begin{syntax}
263 % \cs{see} \Arg{index entry} \marg{page number} \\
264 % \cs{seealso} \Arg{index entry} \marg{page number}
265 % \end{syntax}
266 % \cs{see} and \cs{seealso} are used for index cross-references. These are
267 % slightly redefined from the default as SBL style requires capital letters
268 % for \emph{See} and \emph{See also}. They are called in the standard way
269 % for the \cs{index} command:
270 %
271 % \begin{center}
272 % \begin{tabular}{ll||l}
273 % Page 2: & \verb+\index{at}+ & at, 2 \\
274 % Page 2: & \verb+\index{at!bat|see{bat, at}}+ & \quad bat. \emph{See} bat, at
275 % \end{tabular}
276 % \end{center}
277 % \end{function}
278 %
279 % \section{Implementation}
280 %
281 % \setlength{\parindent}{0em}
282 %
283 % \subsection{Custom Index Style File}
284 %
285 % \begin{macrocode}
286 %<*indexstyle>
287 % \end{macrocode}
288 %
289 % Custom index style file which processes a list of indexed pages with the
290 % macro \cs{ProcessIndexPages} and removes the default comma delimiter before
291 % the first page number for an entry. SBL only uses a comma for the Subject
292 % Index.
293 %
294 % \begin{macrocode}
295 delim_0 "\\SBLProcessIndexPages{"
296 delim_1 "\\SBLProcessIndexPages{"
297 delim_2 "\\SBLProcessIndexPages{"
298 delim_t "}"
299 % \end{macrocode}
300 %
301 % \begin{macrocode}
302 %</indexstyle>
303 % \end{macrocode}
304 %
305 % \subsection{Main Package}
306 %
307 % \begin{macrocode}
308 %<*package>
309 %<@@=sblidx>
310 % \end{macrocode}
311 %
312 % \begin{macrocode}
313 \NeedsTeXFormat{LaTeX2e}
314 \ProvidesExplPackage{sblidx}{2026-04-04}{1.0}
315 {Society of Biblical Literature Indices (DCP)}
316 % \end{macrocode}
317 %
318 % Load \pkg{imakeidx} and \pkg{idxlayout} with appropriate options. Most
319 % layout is controlled with \pkg{idxlayout}, but \pkg{imakeidx} provides an
320 % interface for setting the index title and the convenience of running
321 % |makeindex| inline.
322 %
323 % \begin{macrocode}
324 \RequirePackage { imakeidx }
325 \RequirePackage { idxlayout }
326 \idxlayout
327 {
328 , columnsep = 0.5in
329 , font = small
330 , hangindent = 0.25in
331 , initsep = \bigskipamount
332 , itemlayout = relhang
333 , subindent = 0.25in
334 , subsubindent = 0.5in
335 , totoc
336 }
337 % \end{macrocode}
338 %
339 % \subsubsection{Define and Process Package Options}
340 %
341 % \begin{macrocode}
342 \keys_define:nn { sblidx }
343 {
344 , ancient~sources .bool_set:N = \l_@@_ancient_sources_bool
345 , ancient~sources~title .tl_set:N = \l_@@_ancient_sources_title_tl
346 , ancient~sources~title .initial:n = Ancient~Sources~Index
347 , modern~authors .bool_set:N = \l_@@_modern_authors_bool
348 , modern~authors~title .tl_set:N = \l_@@_modern_authors_title_tl
349 , modern~authors~title .initial:n = Modern~Authors~Index
350 , subject .bool_set:N = \l_@@_subject_bool
351 , subject .initial:n = true
352 , subject~title .tl_set:N = \l_@@_subject_title_tl
353 , subject~title .initial:n = Subject~Index
354 }
355 % \end{macrocode}
356 %
357 % \begin{macrocode}
358 \ProcessKeyOptions
359 % \end{macrocode}
360 %
361 % \subsubsection{Set Up Indices}
362 %
363 % The delimiter between the indexed entry and the first page is dependent on
364 % the index type, so it is set dynamically in \cs{SBLPrintIndices}.
365 %
366 % \begin{macrocode}
367 \tl_const:Nn \c_@@_delim_tl { , \c_space_tl }
368 \tl_new:N \l_@@_delim_first_tl
369 \tl_const:Nn \c_@@_delim_see_tl { . \c_space_tl }
370 % \end{macrocode}
371 %
372 % Set up code and call \cs{makeindex} for each required index.
373 %
374 % \begin{macrocode}
375 \bool_if:NT \l_@@_subject_bool
376 {
377 \makeindex
378 [
379 , title = \l_@@_subject_title_tl
380 , options = -s ~ sblidx.ist ~ -q
381 ]
382 }
383 % \end{macrocode}
384 %
385 % \begin{macrocode}
386 \bool_if:NT \l_@@_ancient_sources_bool
387 {
388 \RequirePackage { bibleref-parse }
389 \RequirePackage { bibleref-sbl }
390 \makeindex
391 [
392 name = \jobname-ancient-sources ,
393 title = \l_@@_ancient_sources_title_tl ,
394 options = -s ~ sblidx.ist ~ -q
395 ]
396 \cs_set_nopar:Npn \biblerefindex
397 {
398 \index [ \jobname-ancient-sources ]
399 }
400 }
401 % \end{macrocode}
402 %
403 % \begin{macrocode}
404 \bool_if:NT \l_@@_modern_authors_bool
405 {
406 \RequirePackage { biblatex }
407 \ExecuteBibliographyOptions { indexing = cite }
408 \makeindex
409 [
410 name = \jobname-modern-authors ,
411 title = \l_@@_modern_authors_title_tl ,
412 options = -s ~ sblidx.ist ~ -q
413 ]
414 \DeclareIndexNameFormat { default }
415 {
416 \usebibmacro { index:name }
417 { \index [ \jobname-modern-authors ] }
418 { \namepartfamily }
419 { \namepartgiven }
420 { \namepartprefix }
421 { \namepartsuffix }
422 }
423 }
424 % \end{macrocode}
425 %
426 % Set up \cs{footnote} so that \cs{index} includes the footnote number when
427 % indexing an entry in a footnote.
428 %
429 % \begin{macro}{\@@_set_up_footnotes:}Footnote set up code.
430 % \begin{macrocode}
431 \cs_new_protected:Nn \@@_set_up_footnotes:
432 {
433 \cs_set_eq:NN \@sblidx@index \index
434 \bool_if:NT \l_@@_ancient_sources_bool
435 {
436 \cs_set_nopar:Npn \bvidxpgformat
437 { SBLPageWithNote { \thefootnote } }
438 }
439 \cs_set_nopar:Npn \index { \SBLFootnoteIndex }
440 }
441 % \end{macrocode}
442 % \end{macro}
443 %
444 % For new footnote code, the set up can be injected with hooks. Otherwise the
445 % \cmd{footnote} command must be redefined. Note that in the latter case if
446 % you or some other package again redefines \cs{footnote} after loading
447 % \pkg{sblidx} automatically including note numbers with indexed entries in
448 % footnotes will not work.
449 %
450 % \begin{macrocode}
451 \IfDocumentMetadataTF
452 {
453 \hook_gput_code:nnn { fntext } { sblidx } { \@@_set_up_footnotes: }
454 }
455 {
456 \cs_set_eq:NN \@@_footnote: \footnote
457 \RenewDocumentCommand { \footnote } { o+m }
458 {
459 \group_begin:
460 \@@_set_up_footnotes:
461 \tl_if_novalue:nTF {#1}
462 { \@@_footnote: {#2} }
463 { \@@_footnote: [#1] {#2} }
464 \group_end:
465 }
466 }
467 % \end{macrocode}
468 %
469 % \begin{macro}{\see, \seealso}
470 % SBL want \emph{See} and \emph{See also} to begin a new sentence so
471 % redefine \cs{see} and \cs{seealso} to capitalise the first letter of
472 % \emph{See}.
473 %
474 % \begin{macrocode}
475 \cs_set_nopar:Npn \see #1 #2
476 {
477 \emph { \text_titlecase_first:n { \seename } }
478 \c_space_tl #1
479 }
480 % \end{macrocode}
481 %
482 % \begin{macrocode}
483 \cs_set_nopar:Npn \seealso #1 #2
484 {
485 \emph { \text_titlecase_first:n { \alsoname } }
486 \c_space_tl #1
487 }
488 % \end{macrocode}
489 % \end{macro}
490 %
491 % \begin{macro}{\SBLFootnoteIndex} \oarg{name} \marg{entry}
492 %
493 % \medskip
494 %
495 % \cs{index} is defined to this macro within footnotes so that note numbers
496 % are automatically included unless some other formatting is requested by
497 % the user. \meta{name} is the name of the raw index file and \meta{entry}
498 % should be formatted according to syntax required by |makeindex|.
499 %
500 % \begin{macrocode}
501 \NewDocumentCommand { \SBLFootnoteIndex } { om }
502 {
503 \tl_if_novalue:nTF {#1}
504 {
505 \str_if_in:nnTF {#2} { | }
506 { \@sblidx@index {#2} }
507 {
508 \@sblidx@index
509 { #2 | SBLPageWithNote { \thefootnote } }
510 }
511 }
512 {
513 \str_if_in:nnTF {#2} { | }
514 { \@sblidx@index [#1] {#2} }
515 {
516 \@sblidx@index [ #1 ]
517 { #2 | SBLPageWithNote { \thefootnote } }
518 }
519 }
520 }
521 % \end{macrocode}
522 % \end{macro}
523 %
524 % \begin{macro}{\SBLPageWithNote} \marg{comma separated list of notes}
525 % \marg{page}
526 %
527 % \medskip
528 %
529 % Output page with note number using n.\ or nn.\ depending on how many notes
530 % there are. When |\index| appears in a footnote, this command is called
531 % automatically using the |makeindex|
532 % \verb+\index{gnat|SBLPageWithNote{\thefootnote}}+ syntax. It allows for a
533 % comma separated list of notes because the command |\SBLProcessIndexPages|
534 % combines indexed entries in footnotes on the same page into one command
535 % with the format |\SBLPageWithNote{x,y,z}{page}|.
536 %
537 % \begin{macrocode}
538 \cs_new_protected:Npn \SBLPageWithNote #1 #2
539 {
540 #2
541 \c_space_tl
542 \int_compare:nNnTF { \clist_count:n {#1} } > { \c_one_int }
543 { nn. }
544 { n. }
545 \nobreakspace
546 \@@_compress_note_list:n {#1}
547 }
548 % \end{macrocode}
549 % \end{macro}
550 %
551 % \begin{macro}{\SBLPrintIndices}
552 % Print the indices. This command prints one or more indices depending on
553 % which are enabled with the |ancient sources|, |modern authors| and
554 % |subject| options.
555 %
556 % \begin{macrocode}
557 \cs_new_protected:Npn \SBLPrintIndices
558 {
559 \@@_set_up_hyperref:
560 \bool_if:NT \l_@@_ancient_sources_bool
561 {
562 \group_begin:
563 \idxlayout
564 {
565 subindent = 0pt ,
566 subsubindent = 0.25in ,
567 hangindent = 0.25in
568 }
569 \cs_set_nopar:Npn \@idxitem
570 { \indexspace \bfseries }
571 \cs_set_nopar:Npn \subitem
572 { \indexspace \normalfont }
573 \tl_set:Nn \l_@@_delim_first_tl { \quad \hfill }
574 \printindex [ \jobname-ancient-sources ]
575 \group_end:
576 }
577 \bool_if:NT \l_@@_modern_authors_bool
578 {
579 \group_begin:
580 \tl_set:Nn \l_@@_delim_first_tl { \quad \hfill }
581 \printindex [ \jobname-modern-authors ]
582 \group_end:
583 }
584 \bool_if:NT \l_@@_subject_bool
585 {
586 \group_begin:
587 \tl_set:Nn \l_@@_delim_first_tl { , \c_space_tl }
588 \printindex
589 \group_end:
590 }
591 }
592 % \end{macrocode}
593 % \end{macro}
594 %
595 % \begin{macro}{\SBLProcessIndexPages} \marg{list of pages}
596 %
597 % \medskip
598 %
599 % Process a list of pages produced by |makeindex|, compressing page ranges
600 % and note ranges as needed according to SBL style. This function is called
601 % by the required custom index style file |sblidx.ist|.
602 %
603 % \begin{macrocode}
604 \cs_new_protected:Npn \SBLProcessIndexPages #1
605 {
606 \@@_process_index_pages:n {#1}
607 }
608 % \end{macrocode}
609 % \end{macro}
610 %
611 % \begin{macro}{\SBLSetMaxRomanPage} \marg{page}
612 %
613 % \medskip
614 %
615 % Set the maximum Roman page number (as an integer, not in Roman format) in
616 % the document in case automatic detection fails. This should be set before
617 % the indices are printed.
618 %
619 % \begin{macrocode}
620 \cs_new_protected:Npn \SBLSetMaxRomanPage #1
621 {
622 \@@_set_max_roman_page:n {#1}
623 }
624 % \end{macrocode}
625 % \end{macro}
626 %
627 % \subsubsection{Page and Note Compression Code}
628 %
629 % The following code handles compression of page numbers and note numbers in
630 % indices according to SBL requirements.
631 %
632 % \begin{macro}{\@@_set_max_roman_page:n} \marg{number}
633 %
634 % \medskip
635 %
636 % Set the maximum Roman page number used in the front matter. This is needed
637 % for incrementing page numbers.
638 %
639 % \begin{macrocode}
640 \int_new:N \g_@@_max_roman_page_int
641 \int_gset:Nn \g_@@_max_roman_page_int { 1000 }
642 % \end{macrocode}
643 %
644 % \begin{macrocode}
645 \cs_new_protected:Nn \@@_set_max_roman_page:n
646 {
647 \int_gset:Nn \g_@@_max_roman_page_int {#1}
648 }
649 % \end{macrocode}
650 %
651 % Although not completely reliable, try to set this automatically when
652 % |\mainmatter| is called.
653 %
654 % \begin{macrocode}
655 \hook_gput_code:nnn { cmd / mainmatter / before } { sblidx }
656 {
657 \int_gset_eq:NN \g_@@_max_roman_page_int \c@page
658 \legacy_if:nT { @twoside }
659 {
660 \int_if_odd:nT { \c@page }
661 { \int_gincr:N \g_@@_max_roman_page_int }
662 }
663 }
664 % \end{macrocode}
665 % \end{macro}
666 %
667 % \begin{macro}{\@@_compress_range:n} \marg{number range}
668 %
669 % \medskip
670 %
671 % Compress a number range according to SBL style. Non-Arabic numbers and
672 % non-ranges are returned unaltered.
673 %
674 % \begin{macrocode}
675 \int_new:N \l_@@_range_start_int
676 \int_new:N \l_@@_range_end_int
677 \int_new:N \l_@@_range_length_int
678 \int_new:N \l_@@_range_end_position_int
679 \seq_new:N \l_@@_range_seq
680 \tl_new:N \l_@@_compressed_range_tl
681 \tl_new:N \l_@@_compressed_range_end_tl
682 \tl_new:N \l_@@_range_start_tl
683 \tl_new:N \l_@@_range_end_tl
684 % \end{macrocode}
685 %
686 % \begin{macrocode}
687 \regex_const:Nn \c_@@_range_regex { \A ( .*? ) -- ( .*? ) \Z }
688 \regex_const:Nn \c_@@_number_range_regex { \A ( \d+ ) -- ( \d+ ) \Z }
689 % \end{macrocode}
690 %
691 % \begin{macrocode}
692 \cs_new_protected:Nn \@@_compress_range:n
693 {
694 \@@_get_compressed_range_end:n {#1}
695 \tl_if_empty:NTF \l_@@_compressed_range_end_tl
696 {#1}
697 {
698 \tl_set:Ne \l_@@_compressed_range_tl
699 {
700 \l_@@_range_start_tl
701 --
702 \l_@@_compressed_range_end_tl
703 }
704 \tl_use:N \l_@@_compressed_range_tl
705 }
706 }
707 % \end{macrocode}
708 %
709 % |\@@_get_compressed_range_end:n| finds the compressed end value for an
710 % Arabic number range and stores it in |\l_@@_compressed_range_end_tl|.
711 %
712 % \begin{macrocode}
713 \cs_new_protected:Nn \@@_get_compressed_range_end:n
714 {
715 \tl_clear:N \l_@@_compressed_range_end_tl
716 % \end{macrocode}
717 %
718 % Only Arabic number ranges are compressed
719 %
720 % \begin{macrocode}
721 \seq_set_regex_extract_once:NNn
722 \l_@@_range_seq \c_@@_number_range_regex {#1}
723 \int_compare:nNnTF
724 { \seq_count:N \l_@@_range_seq } = { 3 }
725 {
726 \int_set:Nn \l_@@_range_start_int
727 { \seq_item:Nn \l_@@_range_seq { 2 } }
728 \int_set:Nn \l_@@_range_end_int
729 { \seq_item:Nn \l_@@_range_seq { 3 } }
730 % \end{macrocode}
731 %
732 % First number must be greater than 100.
733 %
734 % \begin{macrocode}
735 \int_compare:nNnT
736 { \l_@@_range_start_int } > { 100 }
737 {
738 % \end{macrocode}
739 %
740 % First number must not be divisible by 100.
741 %
742 % \begin{macrocode}
743 \int_compare:nNnT
744 {
745 \int_mod:nn
746 { \l_@@_range_start_int }
747 { 100 }
748 }
749 >
750 { \c_zero_int }
751 {
752 % \end{macrocode}
753 %
754 % Pages must be the same number of digits.
755 %
756 % \begin{macrocode}
757 \int_set:Nn \l_@@_range_length_int
758 {
759 \tl_count:e { \int_use:N \l_@@_range_start_int }
760 }
761 \int_compare:nNnT
762 { \l_@@_range_length_int }
763 =
764 {
765 \tl_count:e { \int_use:N \l_@@_range_end_int }
766 }
767 {
768 % \end{macrocode}
769 %
770 % Main compression code. Step through each digit of the start number in the
771 % range until the corresponding digit in the end number is different
772 % \emph{or} there are only two digits left. The remaining digits of the end
773 % number is the compressed value.
774 %
775 % \begin{macrocode}
776 \tl_set:No \l_@@_range_start_tl
777 { \int_use:N \l_@@_range_start_int }
778 \tl_set:No \l_@@_range_end_tl
779 { \int_use:N \l_@@_range_end_int }
780 \int_zero:N \l_@@_range_end_position_int
781 \tl_map_inline:Nn \l_@@_range_start_tl
782 {
783 \int_incr:N \l_@@_range_end_position_int
784 \int_compare:nNnT
785 { \l_@@_range_end_position_int + 1 }
786 =
787 { \l_@@_range_length_int }
788 {
789 \tl_set:Ne
790 \l_@@_compressed_range_end_tl
791 {
792 \tl_range:Nnn
793 \l_@@_range_end_tl
794 { \l_@@_range_end_position_int }
795 { \l_@@_range_length_int }
796 }
797 \tl_map_break:
798 }
799 \tl_if_eq:neF
800 {##1}
801 {
802 \tl_item:Nn
803 \l_@@_range_end_tl
804 { \l_@@_range_end_position_int }
805 }
806 {
807 \tl_set:Ne
808 \l_@@_compressed_range_end_tl
809 {
810 \tl_range:Nnn
811 \l_@@_range_end_tl
812 { \l_@@_range_end_position_int }
813 { \l_@@_range_length_int }
814 }
815 \tl_map_break:
816 }
817 }
818 % \end{macrocode}
819 %
820 % Remove leading zeroes from compressed range.
821 %
822 % \begin{macrocode}
823 \tl_regex_replace_once:Nnn
824 \l_@@_compressed_range_end_tl
825 { \A 0+ }
826 { }
827 }
828 }
829 }
830 }
831 {
832 % \end{macrocode}
833 %
834 % Check for non-Arabic range.
835 %
836 % \begin{macrocode}
837 \seq_set_regex_extract_once:NNn
838 \l_@@_range_seq \c_@@_range_regex {#1}
839 \seq_if_empty:NF \l_@@_range_seq
840 {
841 \tl_set:Ne \l_@@_range_start_tl
842 { \seq_item:Nn \l_@@_range_seq { 2 } }
843 \tl_set:Ne \l_@@_range_end_tl
844 { \seq_item:Nn \l_@@_range_seq { 3 } }
845 \tl_set_eq:NN
846 \l_@@_compressed_range_end_tl \l_@@_range_end_tl
847 }
848 }
849 }
850 % \end{macrocode}
851 % \end{macro}
852 %
853 % \begin{macro}{\@@_compress_note_range:n, \@@_compress_page_range:n,
854 % \@@_hyper_compress_page_rage:n} \marg{number range}
855 %
856 % \medskip
857 %
858 % Compress a note or page range according to SBL style. Non-Arabic numbers
859 % and non-ranges are returned unaltered. \pkg{hyperref} is supported for
860 % page numbers by |\@@_hyper_compress_page_range:n|.
861 %
862 % \begin{macrocode}
863 \cs_set:Nn \@@_compress_note_range:n
864 { \@@_compress_range:n {#1} }
865 \cs_set:Nn \@@_compress_page_range:n
866 { \@@_compress_range:n {#1} }
867 % \end{macrocode}
868 %
869 % \begin{macrocode}
870 \cs_new_protected:Nn \@@_hyper_compress_page_range:n
871 {
872 \@@_get_compressed_range_end:n {#1}
873 \tl_if_empty:NTF \l_@@_compressed_range_end_tl
874 {
875 \hyperlink { page. #1 } {#1}
876 }
877 {
878 \tl_set:Ne \l_@@_compressed_range_tl
879 {
880 \exp_not:N \hyperlink
881 { page. \l_@@_range_start_tl }
882 { \l_@@_range_start_tl }
883 --
884 \exp_not:N \hyperlink
885 { page. \l_@@_range_end_tl }
886 { \l_@@_compressed_range_end_tl }
887 }
888 \tl_use:N \l_@@_compressed_range_tl
889 }
890 }
891 % \end{macrocode}
892 % \end{macro}
893 %
894 % \begin{macro}{\@@_do_page:n, \@@_hyper_do_page:n} \marg{page}
895 %
896 % Create a hyperlink for \meta{page}. This does nothing if \pkg{hyperref} is
897 % not loaded.
898 %
899 % \begin{macrocode}
900 \cs_new:Nn \@@_do_number:n {#1}
901 \cs_new:Nn \@@_do_note:n {#1}
902 \cs_new:Nn \@@_do_page:n {#1}
903 % \end{macrocode}
904 %
905 % \begin{macrocode}
906 \cs_new:Nn \@@_hyper_do_page:n
907 {
908 \exp_not:N \hyperlink { page. #1 } {#1}
909 }
910 % \end{macrocode}
911 % \end{macro}
912 %
913 % \begin{macro}{\@@_compress_list:nN} \marg{sorted numbers}
914 % \meta{compressed list}
915 %
916 % \medskip
917 %
918 % Format a comma separated list of page or note numbers to use SBL style
919 % compressed ranges when there are three or more consecutive numbers and
920 % store the result in the \meta{compressed list} |clist| variable.
921 %
922 % \begin{macrocode}
923 \clist_new:N \l_@@_uncompressed_clist
924 \tl_new:N \l_@@_start_tl
925 \tl_new:N \l_@@_previous_tl
926 % \end{macrocode}
927 %
928 % \begin{macrocode}
929 \cs_new_protected:Nn \@@_compress_list:nN
930 {
931 \clist_set:Nn \l_@@_uncompressed_clist {#1}
932 \clist_clear:N #2
933 \clist_pop:NNT \l_@@_uncompressed_clist \l_@@_start_tl
934 {
935 \tl_set_eq:NN \l_@@_previous_tl \l_@@_start_tl
936 \clist_map_inline:Nn \l_@@_uncompressed_clist
937 {
938 \@@_page_compare:eNeTF
939 {##1}
940 =
941 { \@@_page_incr:V \l_@@_previous_tl }
942 {
943 \tl_set:Nn \l_@@_previous_tl {##1}
944 }
945 {
946 \@@_append_compressed_list:VVN
947 \l_@@_start_tl \l_@@_previous_tl #2
948 \tl_set:Nn \l_@@_start_tl {##1}
949 \tl_set:Nn \l_@@_previous_tl {##1}
950 }
951 }
952 \@@_append_compressed_list:VVN
953 \l_@@_start_tl \l_@@_previous_tl #2
954 }
955 }
956 % \end{macrocode}
957 %
958 % |\@@_append_compressed_list:nnN| and |\@@_append_compressed_list:VVN| are
959 % helper functions called by |\@@_compress_list:nN| to append the next
960 % number or number range to the current list when building a compressed
961 % list.
962 %
963 % \begin{macrocode}
964 \cs_new_protected:Nn \@@_append_compressed_list:nnN
965 {
966 \@@_page_compare:eNeTF {#2} > { \@@_page_incr:n {#1} }
967 {
968 \clist_put_right:Ne #3
969 { \@@_do_compress_range:n { #1 -- #2 } }
970 }
971 {
972 \clist_put_right:Nn #3
973 {
974 \@@_do_number:n {#1}
975 }
976 \tl_if_eq:nnF {#1} {#2}
977 {
978 \clist_put_right:Nn #3
979 {
980 \@@_do_number:n {#2}
981 }
982 }
983 }
984 }
985 % \end{macrocode}
986 %
987 % \begin{macrocode}
988 \cs_generate_variant:Nn \@@_append_compressed_list:nnN { VVN }
989 % \end{macrocode}
990 %
991 % |\@@_do_compress_range:n| needs to be set to either
992 % |\@@_compress_note_range:n| or |\@@_compress_page_range:n| before calling
993 % to ensure only page numbers are hyperlinked when \pkg{hyperref} is loaded.
994 %
995 % \begin{macrocode}
996 \cs_set:Nn \@@_do_compress_range:n
997 { \@@_compress_range:n {#1} }
998 % \end{macrocode}
999 % \end{macro}
1000 %
1001 % \begin{macro}{\@@_compress_note_list:n} \marg{sorted note numbers}
1002 %
1003 % \medskip
1004 %
1005 % Format a comma separated list of note numbers to use SBL style compressed
1006 % ranges when there are three or more consecutive numbers.
1007 %
1008 % \begin{macrocode}
1009 \clist_new:N \l_@@_compressed_clist
1010 % \end{macrocode}
1011 %
1012 % \begin{macrocode}
1013 \cs_new_protected:Nn \@@_compress_note_list:n
1014 {
1015 \cs_set:Nn \@@_do_compress_range:n
1016 { \@@_compress_note_range:n {##1} }
1017 \cs_set:Nn \@@_do_number:n
1018 { \@@_do_note:n {##1} }
1019 \@@_compress_list:nN {#1} \l_@@_compressed_clist
1020 \clist_use:Nnnn
1021 \l_@@_compressed_clist
1022 { ~ and ~ } { , ~ } { , ~ and ~ }
1023 }
1024 % \end{macrocode}
1025 % \end{macro}
1026 %
1027 % \begin{macro}{\@@_compress_page_list:nN, \@@_compress_page_list:VN}
1028 % \marg{sorted page numbers} \meta{list of compressed page}
1029 %
1030 % \medskip
1031 %
1032 % Format a comma separated list of page numbers to use SBL style compressed
1033 % ranges when there are three or more consecutive numbers.
1034 %
1035 % \begin{macrocode}
1036 \cs_new_protected:Nn \@@_compress_page_list:nN
1037 {
1038 \cs_set:Nn \@@_do_compress_range:n
1039 { \@@_compress_page_range:n {##1} }
1040 \cs_set:Nn \@@_do_number:n
1041 { \@@_do_page:n {##1} }
1042 \@@_compress_list:nN {#1} \l_@@_compressed_clist
1043 \tl_set:Ne
1044 #2
1045 {
1046 \clist_use:Nn \l_@@_compressed_clist { \c_@@_delim_tl }
1047 }
1048 }
1049 % \end{macrocode}
1050 %
1051 % \begin{macrocode}
1052 \cs_generate_variant:Nn \@@_compress_page_list:nN { VN }
1053 % \end{macrocode}
1054 % \end{macro}
1055 %
1056 % \begin{macro}{\@@_page_compare:nNnTF, \@@_page_compare:VNVT,
1057 % \@@_page_compare:eNeTF} \marg{first page number} \meta{relation}
1058 % \marg{second page number} \marg{true code} \marg{false code}
1059 %
1060 % \medskip
1061 %
1062 % Compare pages taking into account Roman and Arabic page numbering systems.
1063 % Note that this macro only takes into account a standard book with the
1064 % front matter numbered with Roman numbers and the main matter numbered with
1065 % Arabic numbers. The relations supported are |=|, |>| and |<|. |T| and
1066 % |TF| variants are available.
1067 %
1068 % \begin{macrocode}
1069 \int_new:N \l_@@_first_int
1070 \int_new:N \l_@@_second_int
1071 % \end{macrocode}
1072 %
1073 % \begin{macrocode}
1074 \prg_new_protected_conditional:Npnn \@@_page_compare:nNn #1 #2 #3
1075 { T, TF }
1076 {
1077 % \end{macrocode}
1078 %
1079 % Offset Arabic page numbers by |\g_@@_max_roman_page_int| so they are
1080 % always greater than any Roman page number.
1081 %
1082 % \begin{macrocode}
1083 \regex_if_match:nnTF { \A \d+ \Z } {#1}
1084 {
1085 \int_set:Nn \l_@@_first_int
1086 { #1 + \g_@@_max_roman_page_int }
1087 }
1088 {
1089 \int_set:Nn \l_@@_first_int
1090 { \exp_args:Ne \int_from_roman:n {#1} }
1091 }
1092 \regex_if_match:nnTF { \A \d+ \Z } {#3}
1093 {
1094 \int_set:Nn \l_@@_second_int
1095 { #3 + \g_@@_max_roman_page_int }
1096 }
1097 {
1098 \int_set:Nn \l_@@_second_int
1099 { \exp_args:Ne \int_from_roman:n {#3} }
1100 }
1101 % \end{macrocode}
1102 %
1103 % Now we just need a simple integer comparison.
1104 %
1105 % \begin{macrocode}
1106 \if_int_compare:w \l_@@_first_int #2 \l_@@_second_int
1107 \prg_return_true:
1108 \else:
1109 \prg_return_false:
1110 \fi:
1111 }
1112 % \end{macrocode}
1113 %
1114 % \begin{macrocode}
1115 \cs_generate_variant:Nn \@@_page_compare:nNnT { VNVT }
1116 \cs_generate_variant:Nn \@@_page_compare:nNnTF { eNeTF }
1117 % \end{macrocode}
1118 % \end{macro}
1119 %
1120 % \begin{macro}{\@@_page_incr:n} \marg{page number}
1121 %
1122 % \medskip
1123 %
1124 % Increment a page number taking into account whether it is Roman or Arabic,
1125 % leaving the result on the input stream. Trying to increment something
1126 % other than a Roman or positive Arabic number results in an empty token
1127 % list. This function is fully expandable, but as a result the test for an
1128 % Arabic page number checks if the first character is a digit and otherwise
1129 % assumes the input is roman.
1130 %
1131 % \begin{macrocode}
1132 \cs_new:Nn \@@_page_incr:n
1133 {
1134 \exp_args:Ne \@@_page_incr:nn { \str_head:n {#1} } {#1}
1135 }
1136 % \end{macrocode}
1137 %
1138 % |\@@_page_incr:nn| is a helper function that allows |\@@_page_incr:n| to be
1139 % fully expandable.
1140 %
1141 % \begin{macrocode}
1142 \cs_new:Nn \@@_page_incr:nn
1143 {
1144 \str_case:nnTF {#1}
1145 {
1146 { 0 } { } { 1 } { } { 2 } { } { 3 } { } { 4 } { }
1147 { 5 } { } { 6 } { } { 7 } { } { 8 } { } { 9 } { }
1148 }
1149 {
1150 \int_eval:n { #2 + 1 }
1151 }
1152 {
1153 \int_compare:nNnTF
1154 { \int_from_roman:n {#2} } = { \g_@@_max_roman_page_int }
1155 { 1 }
1156 { \int_to_roman:n { \int_from_roman:n {#2} + 1 } }
1157 }
1158 }
1159 % \end{macrocode}
1160 %
1161 % \begin{macrocode}
1162 \cs_generate_variant:Nn \@@_page_incr:n { V }
1163 % \end{macrocode}
1164 % \end{macro}
1165 %
1166 % \subsubsection{Index Processing Code}
1167 %
1168 % The following code processes page numbers and notes for an index entry and
1169 % produces an index entry in SBL style.
1170 %
1171 % \begin{macro}{\@@_set_up_hyperref:}
1172 %
1173 % The \pkg{hyperref} package changes the format of the index, so needs
1174 % special handling at a number of points.
1175 %
1176 % \begin{macrocode}
1177 \bool_new:N \l_@@_hyperref_bool
1178 \regex_new:N \l_@@_page_regex
1179 \regex_new:N \l_@@_page_encap_regex
1180 \regex_new:N \l_@@_see_also_regex
1181 \regex_new:N \l_@@_page_with_note_regex
1182 % \end{macrocode}
1183 %
1184 % \begin{macrocode}
1185 \cs_new_protected:Nn \@@_set_up_hyperref:
1186 {
1187 \cs_if_exist:NTF \hyperxindexformat
1188 {
1189 \bool_set_true:N \l_@@_hyperref_bool
1190 \cs_set:Nn \@@_compress_page_range:n
1191 { \@@_hyper_compress_page_range:n {##1} }
1192 \cs_set:Nn \@@_do_page:n
1193 { \@@_hyper_do_page:n {##1} }
1194 \regex_set:Nn \l_@@_page_regex
1195 { \A \c{ hyperpage } \cB\{ ( .*? ) \cE\} \Z }
1196 \regex_set:Nn \l_@@_page_encap_regex
1197 {
1198 \A \c{ hyperxindexformat } \cB\{ ( \c{ .* } .* ) \cE\}
1199 \cB\{ ( .*? ) \cE\} \Z
1200 }
1201 \regex_set:Nn \l_@@_see_also_regex
1202 {
1203 \A \c{ hyperxindexformat }
1204 \cB\{ ( [ \c{ see } \c{ seealso } ] .* ) \cE\}
1205 \cB\{ .*? \cE\} \Z
1206 }
1207 \regex_set:Nn \l_@@_page_with_note_regex
1208 {
1209 \A \c{ hyperxindexformat }
1210 \cB\{ \c{ SBLPageWithNote } \cB\{ ( .*? ) \cE\} \cE\}
1211 \cB\{ ( .*? ) \cE\} \Z
1212 }
1213 }
1214 {
1215 \cs_set:Nn \@@_compress_page_range:n
1216 { \@@_compress_range:n {##1} }
1217 \cs_set:Nn \@@_do_page:n {##1}
1218 \regex_set:Nn \l_@@_page_encap_regex
1219 { \A ( \c{ .* } .* ) \cB\{ ( .*? ) \cE\} \Z }
1220 \regex_set:Nn \l_@@_see_also_regex
1221 { \A ( [ \c{ see } \c{ seealso } ] .* ) \cB\{ .*? \cE\} \Z }
1222 \regex_set:Nn \l_@@_page_with_note_regex
1223 {
1224 \A \c{ SBLPageWithNote }
1225 \cB\{ ( .*? ) \cE\} \cB\{ ( .*? ) \cE\} \Z
1226 }
1227 }
1228 }
1229 % \end{macrocode}
1230 % \end{macro}
1231 %
1232 % \begin{macro}{\l_@@_page_data_seq}
1233 %
1234 % \medskip
1235 %
1236 % Create a data structure for storing and manipulating page data. These can
1237 % then added to the |\l_@@_page_data_seq| sequence. All terms should always be
1238 % specified and their values should be braced. E.g.,
1239 % |page={1},note={},encap={}|.
1240 %
1241 % \begin{macrocode}
1242 \seq_new:N \l_@@_page_data_seq
1243 \tl_new:N \l_@@_page_tl
1244 \tl_new:N \l_@@_note_tl
1245 \cs_new_nopar:Nn \@@_encap: { \exp_not:n {} }
1246 % \end{macrocode}
1247 %
1248 % \begin{macrocode}
1249 \keys_define:nn { sblidx / pagedata }
1250 {
1251 , page .tl_set:N = \l_@@_page_tl
1252 , note .tl_set:N = \l_@@_note_tl
1253 , encap .code:n = \cs_set_nopar:Nn \@@_encap:
1254 { \exp_not:n {#1} }
1255 }
1256 % \end{macrocode}
1257 % \end{macro}
1258 %
1259 % \begin{macro}{\@@_sort_page_data:N} \meta{page data sequence}
1260 %
1261 % \medskip
1262 %
1263 % Sort the specified page data sequence by page number. Encapsulated page
1264 % numbers sort ahead of plain page numbers which sort ahead of page numbers
1265 % with notes.
1266 %
1267 % \begin{macrocode}
1268 \bool_new:N \l_@@_sort_returned_bool
1269 \cs_set_nopar:Nn \@@_empty_encap: { \exp_not:n {} }
1270 % \end{macrocode}
1271 %
1272 % \begin{macrocode}
1273 \cs_new_protected:Nn \@@_sort_page_data:N
1274 {
1275 \seq_sort:Nn #1
1276 {
1277 \bool_set_false:N \l_@@_sort_returned_bool
1278 \keys_set:nn { sblidx / pagedata } {##1}
1279 \tl_set_eq:NN \l_tmpa_tl \l_@@_page_tl
1280 \tl_set_eq:NN \l_tmpb_tl \l_@@_note_tl
1281 \cs_set_eq:NN \@@_tmpa: \@@_encap:
1282 \keys_set:nn { sblidx / pagedata } {##2}
1283 % \end{macrocode}
1284 %
1285 % Page is primary sort key.
1286 %
1287 % \begin{macrocode}
1288 \@@_page_compare:VNVT \l_tmpa_tl > \l_@@_page_tl
1289 {
1290 \bool_set_true:N \l_@@_sort_returned_bool
1291 \sort_return_swapped:
1292 }
1293 \@@_page_compare:VNVT \l_tmpa_tl < \l_@@_page_tl
1294 {
1295 \bool_set_true:N \l_@@_sort_returned_bool
1296 \sort_return_same:
1297 }
1298 \tl_if_eq:NNT \l_tmpa_tl \l_@@_page_tl
1299 {
1300 % \end{macrocode}
1301 %
1302 % If the page is the same, then calculate a sorting weight for each item.
1303 %
1304 % \begin{macrocode}
1305 \@@_page_data_weight:NNN
1306 \l_tmpb_tl \@@_tmpa: \l_tmpa_int
1307 \@@_page_data_weight:NNN
1308 \l_@@_note_tl \@@_encap: \l_tmpb_int
1309 \int_compare:nNnT { \l_tmpa_int } < { \l_tmpb_int }
1310 {
1311 \bool_set_true:N \l_@@_sort_returned_bool
1312 \sort_return_same:
1313 }
1314 \int_compare:nNnT { \l_tmpa_int } > { \l_tmpb_int }
1315 {
1316 \bool_set_true:N \l_@@_sort_returned_bool
1317 \sort_return_swapped:
1318 }
1319 % \end{macrocode}
1320 %
1321 % If the sorting weight indicates both items have a note then sort by note.
1322 %
1323 % \begin{macrocode}
1324 \int_compare:nNnT { \l_tmpa_int } = { \l_tmpb_int }
1325 {
1326 \bool_set_true:N \l_@@_sort_returned_bool
1327 \int_compare:nNnTF { \l_tmpa_int } = { 2 }
1328 {
1329 \tl_if_empty:NTF \l_@@_note_tl
1330 { \int_zero:N \l_tmpa_int }
1331 { \int_set:Nn \l_tmpa_int { \l_@@_note_tl } }
1332 \tl_if_empty:NTF \l_tmpb_tl
1333 { \int_zero:N \l_tmpb_int }
1334 { \int_set:Nn \l_tmpb_int { \l_tmpb_tl } }
1335 \int_compare:nNnTF { \l_tmpa_int } < { \l_tmpb_int }
1336 { \sort_return_swapped: }
1337 { \sort_return_same: }
1338 }
1339 { \sort_return_same: }
1340 }
1341 }
1342 % \end{macrocode}
1343 %
1344 % Make sure we return for a cases where |\l_@@_page_tl| is a roman numeral
1345 % range.
1346 %
1347 % \begin{macrocode}
1348 \bool_if:NF \l_@@_sort_returned_bool
1349 { \sort_return_same: }
1350 }
1351 }
1352 % \end{macrocode}
1353 % \end{macro}
1354 %
1355 % \begin{macro}{\@@_page_data_weight:NNN} \meta{note} \meta{encap}
1356 % \meta{weight}
1357 %
1358 % \medskip
1359 %
1360 % Return a sorting weight depending on the presence and value of \meta{note}
1361 % and \meta{encap}. Set the \meta{weight} integer variable to |0| if
1362 % \meta{note} is an empty token list and \meta{encap} is |\@@_empty_encap:|,
1363 % |1| if there is a \meta{note} and \meta{encap} is |\@@_empty_encap:| and
1364 % |2| if \meta{note} is an empty token list and \meta{encap} is not
1365 % |\@@_empty_encap:|.
1366 %
1367 % \begin{macrocode}
1368 \cs_new_protected:Nn \@@_page_data_weight:NNN
1369 {
1370 \cs_if_eq:NNTF #2 \@@_empty_encap:
1371 {
1372 \tl_if_empty:NTF #1
1373 {
1374 \int_set:Nn #3 { \c_one_int }
1375 }
1376 {
1377 \int_set:Nn #3 { 2 }
1378 }
1379 }
1380 { \int_zero:N #3 }
1381 }
1382 % \end{macrocode}
1383 % \end{macro}
1384 %
1385 % \begin{macro}{\@@_page_data_to_tl:N} \meta{page data sequence}
1386 % \meta{token list}
1387 %
1388 % \medskip
1389 %
1390 % Convert a page data sequence to a token list applying compression and
1391 % encapsulation. The page data sequence must already be sorted and
1392 % consolidated.
1393 %
1394 % \begin{macrocode}
1395 \bool_new:N \l_@@_item_added_bool
1396 \tl_new:N \l_@@_compressed_page_tl
1397 % \end{macrocode}
1398 %
1399 % \begin{macrocode}
1400 \cs_new_protected:Nn \@@_page_data_to_tl:N
1401 {
1402 \tl_clear:N \l_tmpa_tl
1403 \seq_map_inline:Nn #1
1404 {
1405 \bool_set_false:N \l_@@_item_added_bool
1406 \keys_set:nn { sblidx / pagedata } {##1}
1407 \@@_compress_page_list:VN
1408 \l_@@_page_tl
1409 \l_@@_compressed_page_tl
1410 \cs_if_eq:NNF \@@_encap: \@@_empty_encap:
1411 {
1412 % \end{macrocode}
1413 %
1414 % Test if the encap is |\see| or |\seealso| and if it is insert a period
1415 % instead of a comma.
1416 %
1417 % \begin{macrocode}
1418 \tl_set:Ne \l_tmpb_tl { \@@_encap: }
1419 \tl_if_in:NnTF \l_tmpb_tl { \see }
1420 {
1421 \tl_put_right:NV \l_tmpa_tl \c_@@_delim_see_tl
1422 }
1423 {
1424 \tl_if_in:NnTF \l_tmpb_tl { \seealso }
1425 {
1426 \tl_put_right:NV \l_tmpa_tl \c_@@_delim_see_tl
1427 }
1428 {
1429 \tl_if_empty:NTF \l_tmpa_tl
1430 { \tl_set:NV \l_tmpa_tl \l_@@_delim_first_tl }
1431 { \tl_put_right:NV \l_tmpa_tl \c_@@_delim_tl }
1432 }
1433 }
1434 \tl_put_right:Ne \l_tmpa_tl
1435 {
1436 \@@_encap: { \l_@@_compressed_page_tl }
1437 }
1438 \bool_set_true:N \l_@@_item_added_bool
1439 }
1440 \bool_if:NF \l_@@_item_added_bool
1441 {
1442 \tl_if_empty:NF \l_@@_note_tl
1443 {
1444 \tl_if_empty:NTF \l_tmpa_tl
1445 { \tl_set:NV \l_tmpa_tl \l_@@_delim_first_tl }
1446 { \tl_put_right:NV \l_tmpa_tl \c_@@_delim_tl }
1447 \tl_put_right:Ne \l_tmpa_tl
1448 {
1449 \SBLPageWithNote
1450 { \l_@@_note_tl }
1451 { \l_@@_compressed_page_tl }
1452 }
1453 \bool_set_true:N \l_@@_item_added_bool
1454 }
1455 }
1456 \bool_if:NF \l_@@_item_added_bool
1457 {
1458 \tl_if_empty:NTF \l_tmpa_tl
1459 { \tl_set:NV \l_tmpa_tl \l_@@_delim_first_tl }
1460 { \tl_put_right:NV \l_tmpa_tl \c_@@_delim_tl }
1461 \tl_put_right:Ne \l_tmpa_tl
1462 { \l_@@_compressed_page_tl }
1463 }
1464 }
1465 % \end{macrocode}
1466 %
1467 % Put formatted index pages on to the input stream.
1468 %
1469 % \begin{macrocode}
1470 \tl_use:N \l_tmpa_tl
1471 }
1472 % \end{macrocode}
1473 % \end{macro}
1474 %
1475 % \begin{macro}{\g_@@_status_code_int}
1476 %
1477 % The functions below set a status code so that the result of the most
1478 % recent function can be tested.
1479 %
1480 % \begin{macrocode}
1481 \bool_new:N \l_@@_stepping_page_bool
1482 \int_new:N \g_@@_status_code_int
1483 \tl_new:N \l_@@_end_tl
1484 % \end{macrocode}
1485 % \end{macro}
1486 %
1487 % \begin{macro}{\@@_range_to_clist:nN, \@@_range_to_clist:eN}
1488 % \marg{range} \meta{list}
1489 %
1490 % \medskip
1491 %
1492 % Convert a range of the form \meta{integer}|--|\meta{integer} to a comma
1493 % separated list of integers, leaving the result in \meta{list} |clist|
1494 % variable. If the form is not found the input is added unaltered to
1495 % \meta{list}. A status code of |0| indicates that a range was found and
1496 % successfully added to \meta{list}.
1497 %
1498 % \begin{macrocode}
1499 \cs_new_protected:Nn \@@_range_to_clist:nN
1500 {
1501 \seq_set_regex_extract_once:NNn
1502 \l_tmpb_seq \c_@@_range_regex {#1}
1503 \seq_if_empty:NTF \l_tmpb_seq
1504 {
1505 \clist_set:Nn #2 {#1}
1506 \int_gset:Nn \g_@@_status_code_int { \c_one_int }
1507 }
1508 {
1509 \clist_clear:N #2
1510 \bool_set_true:N \l_@@_stepping_page_bool
1511 \tl_set:Ne \l_@@_page_tl { \seq_item:Nn \l_tmpb_seq { 2 } }
1512 \tl_set:Ne \l_@@_end_tl { \seq_item:Nn \l_tmpb_seq { 3 } }
1513 \bool_do_while:nn { \l_@@_stepping_page_bool }
1514 {
1515 \clist_put_right:NV #2 \l_@@_page_tl
1516 \tl_set:Ne \l_@@_page_tl
1517 { \@@_page_incr:V \l_@@_page_tl }
1518 \@@_page_compare:VNVT \l_@@_page_tl = \l_@@_end_tl
1519 {
1520 \clist_put_right:NV #2 \l_@@_page_tl
1521 \bool_set_false:N \l_@@_stepping_page_bool
1522 }
1523 }
1524 \int_gzero:N \g_@@_status_code_int
1525 }
1526 }
1527 % \end{macrocode}
1528 %
1529 % \begin{macrocode}
1530 \cs_generate_variant:Nn \@@_range_to_clist:nN { eN }
1531 % \end{macrocode}
1532 % \end{macro}
1533 %
1534 % \begin{macro}{\@@_check_page:n} \marg{index item}
1535 %
1536 % \medskip
1537 %
1538 % Assume an \meta{index item} is a plain page or range and append it the
1539 % page to the \meta{page data} sequence. A status code of |0| indicates a
1540 % page or range was successfully appended to \meta{page data}.
1541 %
1542 % \begin{macrocode}
1543 \cs_new_protected:Nn \@@_check_page:n
1544 {
1545 \tl_clear:N \l_tmpa_tl
1546 \bool_if:NTF \l_@@_hyperref_bool
1547 {
1548 \seq_set_regex_extract_once:NNn
1549 \l_tmpa_seq \l_@@_page_regex {#1}
1550 \seq_if_empty:NF \l_tmpa_seq
1551 {
1552 \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l_tmpa_seq { 2 } }
1553 }
1554 }
1555 {
1556 \tl_set:Nn \l_tmpa_tl {#1}
1557 }
1558 \tl_if_empty:NTF \l_tmpa_tl
1559 {
1560 \int_gset:Nn \g_@@_status_code_int { \c_one_int }
1561 }
1562 {
1563 \clist_set:NV \l_tmpa_clist \l_tmpa_tl
1564 \clist_map_inline:Nn \l_tmpa_clist
1565 {
1566 \@@_range_to_clist:nN {##1} \l_tmpb_clist
1567 \clist_map_inline:Nn \l_tmpb_clist
1568 {
1569 \seq_put_right:Ne \l_@@_page_data_seq
1570 {
1571 page = {####1} ,
1572 note = { } ,
1573 encap = { }
1574 }
1575 }
1576 }
1577 \int_gzero:N \g_@@_status_code_int
1578 }
1579 }
1580 % \end{macrocode}
1581 % \end{macro}
1582 %
1583 % \begin{macro}{\@@_check_page_encap:n} \marg{index item}
1584 %
1585 % \medskip
1586 %
1587 % Check an \meta{index item} if it is encapsulated. If it is then append the
1588 % page and encap function to the \meta{page data} sequence. A status code of
1589 % |0| indicates an encapsulated page was found and successfully appended to
1590 % \meta{page data}.
1591 %
1592 % \begin{macrocode}
1593 \cs_new_protected:Nn \@@_check_page_encap:n
1594 {
1595 \seq_set_regex_extract_once:NNn
1596 \l_tmpa_seq \l_@@_page_encap_regex {#1}
1597 \seq_if_empty:NTF \l_tmpa_seq
1598 {
1599 \int_gset:Nn \g_@@_status_code_int { \c_one_int }
1600 }
1601 {
1602 \@@_range_to_clist:eN
1603 { \seq_item:Nn \l_tmpa_seq { 3 } }
1604 \l_tmpa_clist
1605 \clist_map_inline:Nn \l_tmpa_clist
1606 {
1607 \seq_put_right:Ne \l_@@_page_data_seq
1608 {
1609 page = {##1} ,
1610 note = { } ,
1611 encap = { \seq_item:Nn \l_tmpa_seq { 2 } }
1612 }
1613 }
1614 \int_gzero:N \g_@@_status_code_int
1615 }
1616 }
1617 % \end{macrocode}
1618 % \end{macro}
1619 %
1620 % \begin{macro}{\@@_check_see_also:n} \marg{index item}
1621 %
1622 % \medskip
1623 %
1624 % Check an \meta{index item} if it use |\see| or |\seealso|. If it does then
1625 % append to the \meta{page data} sequence with a large page number to ensure
1626 % correct sorting. A status code of |0| indicates |\see| or |\seealso| was
1627 % found and successfully appended to \meta{page data}.
1628 %
1629 % \begin{macrocode}
1630 \int_new:N \l_@@_see_also_int
1631 \int_set:Nn \l_@@_see_also_int { 100000 }
1632 % \end{macrocode}
1633 %
1634 % \begin{macrocode}
1635 \cs_new_protected:Nn \@@_check_see_also:n
1636 {
1637 \seq_set_regex_extract_once:NNn
1638 \l_tmpa_seq \l_@@_see_also_regex {#1}
1639 \seq_if_empty:NTF \l_tmpa_seq
1640 {
1641 \int_gset:Nn \g_@@_status_code_int { \c_one_int }
1642 }
1643 {
1644 \int_incr:N \l_@@_see_also_int
1645 \seq_put_right:Ne \l_@@_page_data_seq
1646 {
1647 page = { \int_use:N \l_@@_see_also_int } ,
1648 note = { } ,
1649 encap = { \seq_item:Nn \l_tmpa_seq { 2 } }
1650 }
1651 \int_gzero:N \g_@@_status_code_int
1652 }
1653 }
1654 % \end{macrocode}
1655 % \end{macro}
1656 %
1657 % \begin{macro}{\@@_check_page_with_note:n} \marg{index item}
1658 %
1659 % \medskip
1660 %
1661 % Check an \meta{index item} if it is in the form
1662 % \cs{SBLPageWithNote}\marg{note}\marg{page}. If it is then append the page
1663 % and note to the \meta{page data} sequence. A status code of |0| indicates
1664 % a page with a note was found and successfully appended to \meta{page
1665 % data}.
1666 %
1667 % \begin{macrocode}
1668 \cs_new_protected:Nn \@@_check_page_with_note:n
1669 {
1670 \seq_set_regex_extract_once:NNn
1671 \l_tmpa_seq \l_@@_page_with_note_regex {#1}
1672 \seq_if_empty:NTF \l_tmpa_seq
1673 {
1674 \int_gset:Nn \g_@@_status_code_int { \c_one_int }
1675 }
1676 {
1677 \seq_put_right:Ne \l_@@_page_data_seq
1678 {
1679 page = { \seq_item:Nn \l_tmpa_seq { 3 } } ,
1680 note = { \seq_item:Nn \l_tmpa_seq { 2 } } ,
1681 encap = { }
1682 }
1683 \int_gzero:N \g_@@_status_code_int
1684 }
1685 }
1686 % \end{macrocode}
1687 % \end{macro}
1688 %
1689 % \begin{macro}{\@@_consolidate_page_data:N} \meta{page data}
1690 %
1691 % \medskip
1692 %
1693 % Loop through a \meta{page data} sequence combining consecutive pages into
1694 % single terms, notes into single pages and removing unneeded pages.
1695 %
1696 % \begin{macrocode}
1697 \bool_new:N \l_@@_put_page_data_bool
1698 \clist_new:N \l_@@_notes_clist
1699 \clist_new:N \l_@@_pages_clist
1700 % \end{macrocode}
1701 %
1702 % \begin{macrocode}
1703 \cs_new_protected:Nn \@@_consolidate_page_data:N
1704 {
1705 \keys_set:ne { sblidx / pagedata }
1706 {
1707 \seq_item:Nn #1 { \c_one_int }
1708 }
1709 \clist_set:NV \l_@@_pages_clist \l_@@_page_tl
1710 \clist_set:NV \l_@@_notes_clist \l_@@_note_tl
1711 \cs_set_eq:NN \@@_encap_previous: \@@_encap:
1712 \seq_clear:N \l_tmpa_seq
1713 \int_step_inline:nnn { 2 } { \seq_count:N #1 }
1714 {
1715 \keys_set:ne { sblidx / pagedata }
1716 { \seq_item:Nn #1 {##1} }
1717 \tl_if_eq:eeTF
1718 { \l_@@_page_tl }
1719 { \clist_item:Nn \l_@@_pages_clist { -1 } }
1720 {
1721 \clist_if_empty:NF \l_@@_notes_clist
1722 {
1723 \clist_put_right:NV
1724 \l_@@_notes_clist \l_@@_note_tl
1725 }
1726 }
1727 {
1728 \bool_set_false:N \l_@@_put_page_data_bool
1729 % \end{macrocode}
1730 %
1731 % If the |encap| changes then add current data to page data sequence.
1732 %
1733 % \begin{macrocode}
1734 \cs_if_eq:NNF \@@_encap: \@@_encap_previous:
1735 { \bool_set_true:N \l_@@_put_page_data_bool }
1736 % \end{macrocode}
1737 %
1738 % If there are notes in the list then add current data to page data sequence.
1739 %
1740 % \begin{macrocode}
1741 \clist_if_empty:NF \l_@@_notes_clist
1742 { \bool_set_true:N \l_@@_put_page_data_bool }
1743 % \end{macrocode}
1744 %
1745 % If the current item has a note then add current data to page data sequence.
1746 %
1747 % \begin{macrocode}
1748 \tl_if_empty:NF \l_@@_note_tl
1749 { \bool_set_true:N \l_@@_put_page_data_bool }
1750 % \end{macrocode}
1751 %
1752 % Either add data to the page data sequence or append the current page to a
1753 % page list.
1754 %
1755 % \begin{macrocode}
1756 \bool_if:NTF \l_@@_put_page_data_bool
1757 {
1758 \seq_put_right:Ne \l_tmpa_seq
1759 {
1760 page = { \clist_use:N \l_@@_pages_clist } ,
1761 note = { \clist_use:N \l_@@_notes_clist } ,
1762 encap = { \@@_encap_previous: }
1763 }
1764 \clist_set:NV \l_@@_pages_clist \l_@@_page_tl
1765 \clist_set:NV \l_@@_notes_clist \l_@@_note_tl
1766 \cs_set_eq:NN \@@_encap_previous: \@@_encap:
1767 }
1768 {
1769 \clist_put_right:NV
1770 \l_@@_pages_clist \l_@@_page_tl
1771 }
1772 }
1773 }
1774 \seq_put_right:Ne \l_tmpa_seq
1775 {
1776 page = { \clist_use:N \l_@@_pages_clist } ,
1777 note = { \clist_use:N \l_@@_notes_clist } ,
1778 encap = { \@@_encap_previous: }
1779 }
1780 \seq_set_eq:NN #1 \l_tmpa_seq
1781 }
1782 % \end{macrocode}
1783 % \end{macro}
1784 %
1785 % \begin{macro}{\@@_process_index_pages:n} \marg{index pages}
1786 %
1787 % \medskip
1788 %
1789 % Process a list of pages produced by |makeindex|, compressing page ranges
1790 % and note ranges as needed according to SBL style.
1791 %
1792 % \begin{macrocode}
1793 \cs_new_protected:Nn \@@_process_index_pages:n
1794 {
1795 \seq_clear:N \l_@@_page_data_seq
1796 \int_set:Nn \l_@@_see_also_int { 100000 }
1797 \clist_map_inline:nn { #1 }
1798 {
1799 \@@_check_page_with_note:n {##1}
1800 \int_compare:nNnF { \g_@@_status_code_int } = { \c_zero_int }
1801 { \@@_check_see_also:n {##1} }
1802 \int_compare:nNnF { \g_@@_status_code_int } = { \c_zero_int }
1803 { \@@_check_page_encap:n {##1} }
1804 \int_compare:nNnF { \g_@@_status_code_int } = { \c_zero_int }
1805 { \@@_check_page:n {##1} }
1806 }
1807 \@@_sort_page_data:N \l_@@_page_data_seq
1808 \@@_consolidate_page_data:N \l_@@_page_data_seq
1809 \@@_page_data_to_tl:N \l_@@_page_data_seq
1810 }
1811 % \end{macrocode}
1812 % \end{macro}
1813 %
1814 % \begin{macrocode}
1815 \endinput
1816 % \end{macrocode}
1817 %
1818 % \begin{macrocode}
1819 %</package>
1820 % \end{macrocode}