summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/bibleref-mouth/bibleref-mouth.dtx
blob: 1be6e0f92b7fe1eeba454c297c93d73075dc3a01 (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
% \iffalse meta-comment
% 
% Copyright 2012 Chad Parry
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% 
% \fi
% 
% \iffalse
% 
%<code>
%<code>\NeedsTeXFormat{LaTeX2e}
%<code>\ProvidesPackage{bibleref-mouth}[2012/02/26 v1.0 Expandable Bible reference style]
%<code>
%<code>\RequirePackage{fmtcount}
%<code>\RequirePackage{hyperref}[2008/12/26]
%<code>
% 
%<*driver>
\documentclass{ltxdoc}
\usepackage{bibleref-mouth}
\usepackage{afterpage}
\usepackage{longtable}
\usepackage{MnSymbol}
\usepackage[protrusion]{microtype}
\usepackage{bigfoot}
\usepackage{hyperref}[2008/12/26]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\frenchspacing
\sloppy
\begin{document}
\DocInput{bibleref-mouth.dtx}
\end{document}
%</driver>
% \fi
% 
% \CheckSum{950}
% 
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
% 
% \changes{v1.0}{2012/02/26}{Initial version}
% 
% \GetFileInfo{bibleref-mouth.sty}
% 
% \title{
%     The \textsf{bibleref-mouth} package%
%     \thanks{This document corresponds to \textsf{bibleref-mouth}~\fileversion, dated~\filedate.}
% }
% \author{Chad Parry \\ \href{mailto:spam@chad.parry.org}{\nolinkurl{spam@chad.parry.org}}}
% 
% \maketitle
% 
% \begin{abstract}
%     The \textsf{bibleref-mouth} package is a \LaTeXe{} library that allows Bible references to be formatted in a consistent way.
%     It is similar to the \textsf{bibleref} package, except that the formatting macros are all purely expandable.
%     That is, they are all implemented in \TeX's mouth.
%     This means that they can be used in any expandable context, such as an argument to the \cs{url} command.
%     To write a reference, the document should use the command \verb|\bibleref|\marg{reference}.
% \end{abstract}
% 
% \StopEventually{
%     \PrintIndex
% }
% 
% \let\origverbatim\verbatim
% \let\origendverbatim\endverbatim
% \renewenvironment{verbatim}{%
%     \origverbatim%
%     \microtypesetup{protrusion=false}%
% }{%
%     \origendverbatim%
% }
% \newcommand*{\callback}{\meta{\textbackslash{}callback}}
% \newcommand*{\cursor}{$\downfootline$}
% 
% \section{Introduction}
% 
% Two excellent packages already exist for formatting Bible references:
% \href{http://ctan.org/pkg/bibleref}{\textsf{bibleref}} by Nicola Talbot and
% \href{http://ctan.org/pkg/bibleref-parse}{\textsf{bibleref-parse}} by Sebastian Kuhnert.
% This package draws heavily from the ideas in both of those.
% Those packages should meet any reasonable requirement for formatting a Bible reference, except for one:
% they do not work in an expandable context.
% This limitation occasionally turns into a problem, for example in these situations:
% \begin{itemize}
%     \item A Bible reference gets written within an \cs{edef} command
%     \item A Bible reference is part of a section heading
%     \item A Bible reference is used within a hypertext reference
% \end{itemize}
% The last situation may sound contrived, but it is reasonable that an informal document might wish to link to an online Bible, such as at \href{http://www.biblegateway.com/}{Bible Gateway}.
% This package supports such a linked reference.
% For example, a hypertext reference like the following can be produced by the command below:
% \setbiblestyle{biblegateway.com}%
% \bibleref{Gen 1:27}.
% 
% \begin{verbatim}
%\setbiblestyle{biblegateway.com}
%\bibleref{Gen 1:27}
% \end{verbatim}
% 
% \section{Usage}
% 
% \label{bibleref}
% \DescribeMacro{\bibleref}
% For a reasonable default experience, it is sufficient to know only one command.
% The document should create a Bible reference by calling \verb|\bibleref|\marg{reference}.
% The entire reference should be passed as a single argument, even if it contains a range or list of verses.
% 
% Books can be spelled out or abbreviated.
% A list of supported abbreviations is given in Table~\ref{books}.
% Numbered books can be specified with Arabic or Roman numerals, with an optional space separating the number from the book.
% Abbreviations may have an optional trailing period.
% For example, it is equally acceptable to say
% \verb|IISamuel|,
% \verb|IISam.|,
% \verb|IISam|,
% \verb|2Samuel|,
% \verb|2Sam.|,
% \verb|2Sam|,
% \verb|II Samuel|,
% \verb|II Sam.|,
% \verb|II Sam|,
% \verb|2 Samuel|,
% \verb|2 Sam.|, or
% \verb|2 Sam|.
% Regardless of the format used in the \cs{bibleref} command, the reference will be formatted according to the active reference style.
% New custom books and abbreviations can be added, according to the pattern shown in section \ref{booknames}.
% 
% \makeatletter
% \brm@foreachbook@abbrv{\providebiblebook{abbrvname}}{.}{\brm@number@arabicspace}{\brm@number@arabicspace}
% \brm@foreachbook@altabbrv{\providebiblebook{altabbrvname}}{.}{\ }{\brm@number@arabicspace}{\brm@number@arabicspace}
% \newcommand{\bookrow}[2]{#2 & \brm@bookname{altabbrvname}{#1} & \brm@bookname{abbrvname}{#1}\\}
% \afterpage{
% \clearpage
% \begin{center}
% \begin{longtable}{p{0.34\textwidth}p{0.33\textwidth}p{0.33\textwidth}}
% \caption{Supported Abbreviations}\label{books}\\
% \bfseries Book & \bfseries Long Abbreviation & \bfseries Short Abbreviation\\
% \endfirsthead
% \caption{Supported Abbreviations (continued)}\\
% \bfseries Book & \bfseries Long Abbreviation & \bfseries Short Abbreviation\\
% \endhead
% \brm@foreachbook@full{\bookrow}{}{}{}{}{}{}{\brm@number@arabicspace}{\brm@number@arabicspace}
% \end{longtable}
% \end{center}
% \newpage
% }
% \makeatother
% 
% References must contain a book name.
% They may optionally contain a chapter, and if they contain a chapter then they may optionally contain a verse.
% References may be created for a range from one passage to another.
% References may also be created for a list of passages.
% A space should separate the book name from the chapter number.
% (Spaces will actually be ignored by the parser, so it is also allowed to write the book and the chapter with no separation between them).
% The chapter and verse should be separated by a \texttt{:} character, and optionally spaces.
% The begin and end passages of a range should be separated by a \texttt{-} character, and optionally spaces.
% Two passages that are part of a list should be separated by a \texttt{;} character, and optionally spaces.
% If the passages in a range or a list are from the same book, then the book does not need to be specified for the second passage.
% Likewise, if the passages in a range or a list have the same book and chapter, then the chapter does not need to be specified for the second passage.
% However, if two passages are in a list and they have the same book and chapter, then they should be separated by a \texttt{,} character instead of a \texttt{;} character.
% This helps resolve the ambiguity around whether the number following the delimiter represents a chapter or a verse.
% 
% The following examples show most of the different reference types that are supported by the parser.
% \begin{itemize}
%     \item Single book: \verb|\bibleref{Genesis}|
%     \item Single chapter: \verb|\bibleref{Exodus 32}|
%     \item Single verse: \verb|\bibleref{Amos 3:7}|
%     \item Multiple books: \verb|\bibleref{Matt; Mark; Luke; John}|
%     \item Multiple chapters: \verb|\bibleref{Psalms 2; 16; 32; 69; 95; 110}|
%     \item Multiple verses within a chapter: \verb|\bibleref{Matthew 5:13,48}|
%     \item Multiple verses across chapters: \verb|\bibleref{John 7:17; 14:15; 17:3}|
%     \item Multiple different references: \verb|\bibleref{Isa. 9:6; Ezek. 37; Rev.}|
%     \item Range of books: \verb|\bibleref{Genesis - Deuteronomy}|
%     \item Range of chapters: \verb|\bibleref{Mal. 3-4}|
%     \item Range of verses: \verb|\bibleref{Proverbs 3:5-6}|
%     \item Range from a book to a chapter: \verb|\bibleref{1 Chronicles - 2 Chronicles 9}|
%     \item Range in a list within a chapter: \verb|\bibleref{James 1:5-6,17}|
%     \item Range in a list across chapters: \verb|\bibleref{Ezra 7-10; Neh 8}|
% \end{itemize}
% 
% \DescribeMacro{\setbiblestyle}
% The style of the rendered reference can be changed by calling \verb|\setbiblestyle|\marg{stylename}.
% The supplied styles, with an example reference, are shown in Table~\ref{styles}.
% Custom styles can also be added using the command \cs{providebiblestyle}, as shown in \ref{defaultstyles}.
% 
% \newcommand{\stylerow}[1]{#1 & \setbiblestyle{#1}\bibleref{2 Tim. 3:16-17}\\}
% \begin{table}[tbh]
% \caption{Supported Styles}\label{styles}
% \begin{center}
% \begin{tabular}{lp{0.5\textwidth}}
% \bfseries Style & \bfseries Example\\
% \stylerow{fullname}
% \stylerow{jerusalem}
% \stylerow{anglosaxon}
% \stylerow{JEH}
% \stylerow{NTG}
% \stylerow{MLA}
% \stylerow{chicago}
% \stylerow{text}
% \stylerow{biblegateway.com}
% \end{tabular}
% \end{center}
% \end{table}
% 
% \section{Implementation}
% 
% All the internal macros have a name beginning with \verb|brm@|, which stands for \textsf{bibleref-mouth}, so as to avoid polluting the global namespace.
% 
% It would have been nice if an expandable Bible reference could be created using the existing \textsf{bibleref} package, perhaps by adding \cs{protect} commands.
% Unfortunately, the reality of \TeX{} is more complicated than that.
% The entire package needed to be reimplemented using a functional programming paradigm.
% 
% There are some techniques that need to be used over and over to perform complicated computations in \TeX's mouth.
% All non-expandable primitives must be avoided.
% That means that assignments, (e.g.\ \cs{def}), cannot be used.
% Instead, recursion and currying and the command pattern are relied on extensively.
% In order to read this code, it doesn't hurt to be familiar with a language like \href{http://www.haskell.org/haskellwiki/Haskell}{Haskell}.
% 
% \subsection{Entry Points}
% 
% \begin{macro}{\setbiblestyle}
% The user can set which reference style is in effect with \verb|\setbiblestyle|\marg{stylename}.
% The available styles are defined in Table~\ref{styles}.
% Custom styles can also be added using the command \hyperlink{providebiblestyle}{\cs{providebiblestyle}}.
% This command just stores the style for later use by \cs{bibleref}.
% 
% Note that this command is itself \emph{not} expandable, so it must be called outside of any expandable context.
% If you need to switch between different styles within an expandable context, you should use the \hyperlink{brm@bibleref}{\cs{brm@bibleref}} command.
%    \begin{macrocode}
\newcommand*{\setbiblestyle}[1]{%
    \def\brm@currentstyle{#1}%
}
%    \end{macrocode}
% \end{macro}
% 
% The default style for documents that don't override it is the one that outputs the full name of the books.
%    \begin{macrocode}
\setbiblestyle{fullname}
%    \end{macrocode}
% 
% \begin{macro}{\bibleref}
% This is the main entry point to the library.
% The user can specify any Bible reference, including lists and ranges, as the argument to \verb|\bibleref|\marg{reference}.
% Detailed usage instructions are given in \autoref{bibleref}.
% This command just retrieves the current style and delegates to \cs{brm@bibleref}.
%    \begin{macrocode}
\newcommand*{\bibleref}[1]{%
    \brm@bibleref{\brm@currentstyle}{#1}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@bibleref}
% \hypertarget{brm@bibleref}{}
% The \verb|\brm@bibleref|\marg{stylename}\marg{reference} command checks that the style exists, and then outputs a consistently formatted reference.
% It gets a list of ranges from \cs{brm@parseall}.
% Then it starts reading the list by creating a cons handler from the partially applied function \cs{brm@formatall}.
% The first argument passed to \cs{brm@formatall} is a style, which is itself a function with the signature \callback\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
% No nil handler needs to be provided, because an empty list would be an error, and would not need to be formatted anyway.
%    \begin{macrocode}
\newcommand*{\brm@bibleref}[2]{%
    \brm@ifnameexists{\brm@style{#1}}{%
        \brm@readlist{\brm@parseall{#2}}{%
            \brm@formatall{\csname\brm@style{#1}\endcsname}%
        }{%
        }%
    }{%
        \brm@error{Unrecognized style: #1}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@formatall}
% The formatting of a Bible reference starts with \verb|\brm@formatall|\marg{style}\marg{range}\marg{list}.
% This command gets called from \cs{brm@readlist}, so the last two arguments are the head and tail of a list.
% This command unpacks a range into a begin passage and an end passage, which are passed to the helper \cs{brm@applystyle}.
%    \begin{macrocode}
\newcommand*{\brm@formatall}[3]{%
    \brm@unpackrange{#2}{\brm@applystyle{#1}{#3}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@applystyle}
% The command \verb|\brm@applystyle|\marg{style}\marg{list}\marg{beginpassage}\-\marg{endpassage} rearranges the arguments into the order that is recognized by the style function.
% It forwards the begin passage and end passage, and it also allows continued extraction from the list by passing the partially applied function \cs{brm@nextrange}.
% The style can finish applying \cs{brm@nextrange} by providing a style delegate and a nil handler.
% The style delegate that gets provided to format subsequent ranges need not be the same style command that is being applied to format the first range.
%    \begin{macrocode}
\newcommand*{\brm@applystyle}[4]{%
    #1{#3}{#4}{\brm@nextrange{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@nextrange}
% The command \verb|brm@nextrange|\marg{list}\marg{style}\marg{nilhandler} allows styles to read the next range off of a list of references.
% Since the list will already have been given by \cs{brm@applystyle}, the style only needs to provide the style delegate and nil handler.
%    \begin{macrocode}
\newcommand*{\brm@nextrange}[3]{%
    \brm@readlist{#1}{%
        \brm@formatall{#2}%
    }{%
        #3%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Data Structures}
% 
% A list can be represented as a recursive data structure, as in other functional languages.
% But data structures themselves are difficult to construct in \TeX.
% Some hints can be found in the \href{http://ctan.org/pkg/lazylist}{\textsf{lazylist}} package.
% A data structure can be represented as a macro that accepts a callback.
% When the callback is invoked, all the data structure's fields are passed to it.
% The simplest form of callback is one that returns one field and ignores all the others.
% If that sounded complicated, then the following sample code might help or it might make the confusion worse.
% 
% \begin{macro}{\brm@packpassage}
% One important data structure is the passage.
% It is created by \verb|\brm@packpassage|\marg{book}\marg{chapter}\marg{verse}.
% You may notice that the command signature advertises 4 parameters, but the user only calls it with 3 arguments.
% That's because the fourth argument is a callback that will be supplied later, when it's time to extract the data.
% For now, all this command does with its arguments is passes them to the callback.
%    \begin{macrocode}
\newcommand*{\brm@packpassage}[4]{%
    #4{#1}{#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\thebook}
% The first such callback is \verb|\brm@choosebook|\marg{book}\marg{chapter}\marg{verse}.
% It gets passed the three fields from the passage.
% It returns the book, and ignores the chapter and verse.
% There is also a public command, \verb|\thebook|\marg{passage}, which wraps the operation in a command that has a more familiar syntax.
% These commands work in concert to support use cases like the following:
% \begin{verbatim}
%% Store a passage in \mypassage.
%\def\mypassage{\brm@packpassage{Jeremiah}{16}{16}}
%% Later, extract the book from \mypassage into \mybook.
%\def\mybook{\thebook{\mypassage}}
% \end{verbatim}
%    \begin{macrocode}
\newcommand*{\thebook}[1]{%
    #1{\brm@choosebook}%
}
\newcommand*{\brm@choosebook}[3]{%
    #1%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\thechapter}
% The \verb|\thechapter|\marg{passage} command is a similar command for extracting a chapter.
% The callback is \cs{brm@choosechapter}.
% It ignores the first and third fields, and returns the second field, which is the chapter.
%    \begin{macrocode}
\newcommand*{\thechapter}[1]{%
    #1{\brm@choosechapter}%
}
\newcommand*{\brm@choosechapter}[3]{%
    #2%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\theverse}
% Likewise, \verb|\theverse|\marg{passage} extracts a verse.
% The callback \cs{brm@chooseverse} returns the third field and ignores the others.
%    \begin{macrocode}
\newcommand*{\theverse}[1]{%
    #1{\brm@chooseverse}%
}
\newcommand*{\brm@chooseverse}[3]{%
    #3%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@packrange}
% Another data structure is the range.
% It has a begin passage and and end passage.
% A range is created by calling \verb|\brm@packrange|\-\marg{beginpassage}\-\marg{endpassage}.
%    \begin{macrocode}
\newcommand*{\brm@packrange}[3]{%
    #3{#1}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@unpackrange}
% The corresponding command to extract the fields is \verb|\brm@unpackrange|\marg{range}\marg{callback}.
% This doesn't follow exactly the same pattern as the passage helpers.
% Instead of having separate commands to extract the two fields, there is a single function that extracts both fields at the same time.
% Clients should pass in a callback with the signature \callback\-\marg{beginpassage}\-\marg{endpassage}.
%    \begin{macrocode}
\newcommand*{\brm@unpackrange}[2]{%
    #1{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@packseparators}
% Another data structure is for separators.
% This structure stores the delimiter characters that will be output between books and chapters, and between chapters and verses, in Bible references.
%    \begin{macrocode}
\newcommand*{\brm@packseparators}[3]{%
    #3{#1}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\bookchapterseparator}
% To extract the first field of the separators structure, use \verb|\bookchapterseparator|\marg{separators}.
% Instead of defining a custom callback, it was sufficient to use the existing \cs{@firstoftwo} command, which already does exactly what we want.
% It returns the first field and ignores the second field.
%    \begin{macrocode}
\newcommand*{\bookchapterseparator}[1]{%
    #1{\@firstoftwo}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\chapterverseseparator}
% To extract the second field of the separators structure, use \verb|\chapterverseseparator|\marg{separators}.
%    \begin{macrocode}
\newcommand*{\chapterverseseparator}[1]{%
    #1{\@secondoftwo}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@nillist}
% Now we arrive at the package's only recursive data structure, which is for storing lists.
% The definition mirrors lists in the lamba calculus.
% This mechanism was suggested by the \textsf{lazylist} package.
% Each list node is a data structure that is either a nil or a node.
% A nil means an empty list.
% It is represented as a function that ignores its first argument and expands its second argument.
%    \begin{macrocode}
\newcommand*{\brm@nillist}{%
    \@secondoftwo%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@conslist}
% Every non-empty list node contains fields for the list head and the list tail.
% The list head is the current first item in the list.
% The list tail is its own list, and it contains all the items except the head.
% If the head is the only element in the list, then the tail is nil.
% A list node is represented as a function that calls its first argument and ignores its second argument.
% The first argument must be a callback with the signature \callback\marg{head}\marg{tail}.
%    \begin{macrocode}
\newcommand*{\brm@conslist}[2]{%
    \brm@conslist@choose{#1}{#2}%
}
\newcommand*{\brm@conslist@choose}[4]{%
    #3{#1}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@readlist}
% Reading a list is done with the command \verb|\brm@readlist|\marg{list}\marg{conshandler}\marg{nilhandler}.
% If the list is empty, then the \meta{nilhandler} will be called without any arguments.
% If the list is non-empty, then the \meta{conshandler} will be called.
% It should have the signature \callback\marg{head}\marg{tail}.
%    \begin{macrocode}
\newcommand*{\brm@readlist}[3]{%
    #1{#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@recordrange}
% A list of references is kept using the list structure.
% Each element is one of the range structures.
% The list is constructed with \verb|\brm@recordrange|\-\marg{beginbook}\marg{beginchapter}\marg{beginverse}\-\marg{endbook}\marg{endchapter}\marg{endverse}\-\marg{tail}.
% The tail passed to this command should be a list that contains all subsequent ranges, or nil if there are no more ranges.
%    \begin{macrocode}
\newcommand*{\brm@recordrange}[7]{%
    \brm@conslist{%
        \brm@packrange{%
        	\brm@packpassage{#1}{#2}{#3}%
        }{%
        	\brm@packpassage{#4}{#5}{#6}%
        }%
    }{%
        #7%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@recordpassage}
% A single passage can be added to the list as a range that has a regular begin passage and empty fields for the end passage.
% It is done with the command \verb|\brm@recordpassage|\marg{book}\marg{chapter}\marg{verse}\marg{tail}.
%    \begin{macrocode}
\newcommand*{\brm@recordpassage}[4]{%
    \brm@recordrange{#1}{#2}{#3}{}{}{}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Parsing}
% 
% \subsubsection{Reference Parsing}
% 
% \begin{macro}{\brm@parseall}
% Parsing starts with the command \verb|\brm@parseall|\marg{remainingtext}.
% It checks whether the reference begins with a valid book.
% It finds the longest string at the beginning of the input that is a valid book, consuming both letters and numbers that are part of the book name.
% If it finds a match, then parsing continues with the \cs{brm@checkchapter} command.
% 
% Throughout this section, \texttt{1 Cor 15:20-22,40-42;16} will be used as a sample reference.
% The reference will be shown with a vertical bar, or cursor, at the point where the parsing has advanced.
% When this command is called, the cursor will be at: \texttt{\cursor1 Cor 15:20-1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@parseall}[1]{%
    \brm@parsebook{#1}{%
        \brm@checkchapter%
    }{%
        \brm@error{Unrecognized book: #1}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkchapter}
% After the book has been parsed, \verb|\brm@checkchapter|\marg{book}\marg{remainingtext} is called.
% 
% A valid reference may contain only a book name, in which case there is no remaining text.
% 
% The remaining text could also contain a chapter, which this command will check for.
% The last alternative is that there is a delimiter separating this reference from more references.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor \cursor15:20-1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkchapter}[2]{%
    \brm@ifempty{#2}{%
        \brm@recordpassage{#1}{}{}{\brm@nillist}%
    }{%
        \brm@ifsamehead{#2}{-}{%
            \brm@parserange{#1}{}{}{\brm@tailstr{#2}}%
        }{%
            \brm@ifsamehead{#2}{;}{%
                \brm@recordpassage{#1}{}{}{%
                    \brm@parseall{\brm@tailstr{#2}}%
                }%
            }{%
                \brm@parsechapter{#1}{#2}%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsechapter}
% After it has been determined that the next characters contain a chapter number, \verb|\brm@parsechapter|\marg{book}\marg{remainingtext} will consume it.
% The \cs{brm@parsenumber} helper will pass the number and the remaining text to the \cs{brm@checkchapterdelim} callback.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor \cursor15:20-1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@parsechapter}[2]{%
    \brm@parsenumber{#2}{\brm@checkchapterdelim{#1}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkchapterdelim}
% After a chapter has been parsed, \verb|\brm@checkchapterdelim|\-\marg{book}\marg{chapter}\-\marg{remainingtext} is called.
% If there are any characters remaining in the input, the next one should be a delimiter.
% There are several delimiters that would be valid at this point.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15\cursor:20-1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkchapterdelim}[3]{%
    \brm@ifempty{#3}{%
        \brm@recordpassage{#1}{#2}{}{\brm@nillist}%
    }{%
        \brm@ifsamehead{#3}{:}{%
            \brm@parseverse{#1}{#2}{\brm@tailstr{#3}}%
        }{%
            \brm@ifsamehead{#3}{-}{%
                \brm@parserange{#1}{#2}{}{\brm@tailstr{#3}}%
            }{%
                \brm@ifsamehead{#3}{;}{%
                    \brm@recordpassage{#1}{#2}{}{%
                        \brm@parsechapterlist{#1}{\brm@tailstr{#3}}%
                    }%
                }{%
                    \brm@error{Unrecognized chapter trailer: #3}%
                }%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parseverse}
% After a \texttt{:} delimiter, the next characters must be a verse number.
% The verse is parsed by \verb|\brm@parseverse|\-\marg{book}\marg{chapter}\-\marg{remainingtext}
% The \cs{brm@parsenumber} helper will pass the number and the remaining text to the \cs{brm@checkversetrailer} callback.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:\cursor20-1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@parseverse}[3]{%
    \brm@parsenumber{#3}{\brm@checkversetrailer{#1}{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkversetrailer}
% After the verse has been parsed, there may be some remaining input.
% It is passed to \verb|\brm@checkversetrailer|\-\marg{book}\marg{chapter}\marg{verse}\-\marg{remainingtext}.
% 
% If there are no more characters, then parsing of the passage is done.
% If there are more characters, then the next one should be a delimiter.
% There are several delimiters that would be valid at this point.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20\cursor-1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkversetrailer}[4]{%
    \brm@ifempty{#4}{%
        \brm@recordpassage{#1}{#2}{#3}{\brm@nillist}%
    }{%
        \brm@ifsamehead{#4}{-}{%
            \brm@parserange{#1}{#2}{#3}{\brm@tailstr{#4}}%
        }{%
            \brm@ifsamehead{#4}{;}{%
                \brm@recordpassage{#1}{#2}{#3}{%
                    \brm@parsechapterlist{#1}{\brm@tailstr{#4}}%
                }%
            }{%
                \brm@ifsamehead{#4}{,}{%
                    \brm@recordpassage{#1}{#2}{#3}{%
                        \brm@parseverse{#1}{#2}{\brm@tailstr{#4}}%
                    }%
                }{%
                    \brm@error{Unrecognized verse trailer: #4}%
                }%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parserange}
% After a \texttt{-} delimiter, the end passage of a range is parsed by \verb|\brm@parserange|\-\marg{beginbook}\marg{beginchapter}\marg{beginverse}\-\marg{remainingtext}.
% The next characters could represent a book, chapter, or verse.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20-\cursor1 Cor 15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@parserange}[4]{%
    \brm@parsebook{#4}{%
        \brm@checkrangechapter{#1}{#2}{#3}%
    }{%
        \brm@ifempty{#2}{%
            \brm@error{Unrecognized range book: #4}%
        }{%
            \brm@parsenumber{#4}{%
                \brm@checkrangenumbertrailer{#1}{#2}{#3}{#1}%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkrangechapter}
% While the end passage of a range is being parsed, after a book has been found, the command \verb|\brm@checkrangechapter|\-\marg{beginbook}\marg{beginchapter}\marg{beginverse}\-\marg{endbook}\-\marg{remainingtext} looks for a subsequent chapter.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20-1 Cor \cursor15:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkrangechapter}[5]{%
    \brm@ifempty{#5}{%
        \brm@recordrange{#1}{#2}{#3}{#4}{}{}{\brm@nillist}%
    }{%
        \brm@ifsamehead{#5}{;}{%
            \brm@recordrange{#1}{#2}{#3}{#4}{}{}{%
                \brm@parseall{\brm@tailstr{#5}}%
            }%
        }{%
            \brm@parsenumber{#5}{%
                \brm@checkrangechapterdelim{#1}{#2}{#3}{#4}%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkrangechapterdelim}
% While the end passage of a range is being parsed, after a book and chapter have been found, the command \verb|\brm@checkrangechapterdelim|\-\marg{beginbook}\marg{beginchapter}\marg{beginverse}\-\marg{endbook}\marg{endchapter}\-\marg{remainingtext} is called.
% If there is any remaining text, it is expected to be a delimiter.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20-1 Cor 15\cursor:22,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkrangechapterdelim}[6]{%
    \brm@ifempty{#6}{%
        \brm@recordrange{#1}{#2}{#3}{#4}{#5}{}{\brm@nillist}%
    }{%
        \brm@ifsamehead{#6}{:}{%
            \brm@parsenumber{\brm@tailstr{#6}}{%
                \brm@checkrangeversetrailer{#1}{#2}{#3}{#4}{#5}%
            }%
        }{%
            \brm@error{Unrecognized range chapter trailer: #6}%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkrangenumbertrailer}
% If a range starts with a number, then it can be ambiguous whether that number represents a chapter or verse.
% In that case, \verb|\brm@checkrangenumbertrailer|\-\marg{beginbook}\marg{beginchapter}\marg{beginverse}\-\marg{endbook}\marg{endnumber}\-\marg{remainingtext} is called.
% The ambiguity can be resolved by checking whether there is a delimiter remaining, and which delimiter it is.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20-1 Cor 15:22,40-42\cursor;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkrangenumbertrailer}[6]{%
    \brm@ifempty{#6}{%
        \brm@ifempty{#3}{%
            \brm@recordrange{#1}{#2}{}{#4}{#5}{}{\brm@nillist}%
        }{%
            \brm@recordrange{#1}{#2}{#3}{#4}{#2}{#5}{\brm@nillist}%
        }%
    }{%
        \brm@ifsamehead{#6}{:}{%
            \brm@parsenumber{\brm@tailstr{#6}}{%
                \brm@checkrangeversetrailer{#1}{#2}{#3}{#4}{#5}%
            }%
        }{%
            \brm@ifempty{#3}{%
                \brm@ifsamehead{#6}{;}{%
                    \brm@recordrange{#1}{#2}{}{#4}{#5}{}{%
                        \brm@parsechapterlist{#4}{\brm@tailstr{#6}}%
                    }%
                }{%
                    \brm@error{Unrecognized range number trailer: #6}%
                }%
            }{%
                \brm@ifsamehead{#6}{;}{%
                    \brm@recordrange{#1}{#2}{#3}{#4}{#2}{#5}{%
                        \brm@parsechapterlist{#4}{\brm@tailstr{#6}}%
                    }%
                }{%
                    \brm@ifsamehead{#6}{,}{%
                        \brm@recordrange{#1}{#2}{#3}{#4}{#2}{#5}{%
                            \brm@parseverse{#4}{#2}{\brm@tailstr{#6}}%
                        }%
                    }{%
                        \brm@error{Unrecognized range number trailer: #6}%
                    }%
                }%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@checkrangeversetrailer}
% After the begin and end passage of a range have both been parsed, \verb|\brm@checkrangeversetrailer|\-\marg{beginbook}\marg{beginchapter}\marg{beginverse}\-\marg{endbook}\marg{endchapter}\marg{endverse}\-\marg{remainingtext} is called.
% There may be remaining text for another reference.
% The next character is expected to be a delimiter.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20-1 Cor 15:22\cursor,40-42;16}.
%    \begin{macrocode}
\newcommand*{\brm@checkrangeversetrailer}[7]{%
    \brm@ifempty{#7}{%
        \brm@recordrange{#1}{#2}{#3}{#4}{#5}{#6}{\brm@nillist}%
    }{%
        \brm@ifsamehead{#7}{;}{%
            \brm@recordrange{#1}{#2}{#3}{#4}{#5}{#6}{%
                \brm@parsechapterlist{#4}{\brm@tailstr{#7}}%
            }%
        }{%
            \brm@ifsamehead{#7}{,}{%
                \brm@recordrange{#1}{#2}{#3}{#4}{#5}{#6}{%
                    \brm@parseverse{#4}{#5}{\brm@tailstr{#7}}%
                }%
            }{%
                \brm@error{Unrecognized verse trailer: #7}%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsechapterlist}
% If the previous range ended with a \texttt{;} delimiter, then the next characters are known to be either a book or a chapter.
% The command \verb|\brm@parsechapterlist|\marg{book}\marg{remainingtext} will handle either case.
% 
% When this command is called, the cursor will be at: \texttt{1 Cor 15:20-1 Cor 15:22,40-42;\cursor16}.
%    \begin{macrocode}
\newcommand*{\brm@parsechapterlist}[2]{%
    \brm@parsebook{#2}{%
        \brm@checkchapter%
    }{%
        \brm@parsechapter{#1}{#2}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{Book Name Parsing}
% 
% \begin{macro}{\brm@parsebook}
% The \verb|\brm@parsebook|\marg{text}\marg{successhandler}\marg{failurehandler} command parses a book name.
% A book is known to exist if the definition \cs{brm@bookalias@\meta{book}} exists.
% This command will find the longest string of the source that is a valid book name.
% In other words, it will check to see whether the whole string is a valid book name, and then it will try to match the whole string minus the last character, and so on recursively.
% Eventually it will either find a valid book or it will exhaust every possibility and fail.
% If it finds a valid book then it will call the success handler, which is expected to have the signature \callback\-\marg{book}\-\marg{remainingtext}.
% If it doesn't find a valid book it will call the failure handler with no arguments.
% 
% This command depends on \cs{brm@headstr}, which consumes all spaces in its input.
% That means that spaces in the text will be ignored.
% For example, if the text contains \texttt{Song of Songs}, then it will only match a book alias \texttt{SongofSongs}.
% 
% The real work is done by the \cs{brm@parsebook@accumulate} helper, so this method just delegates to it.
% It makes sure the text is fully-expanded before beginning.
% It passes an empty string as the accumulated text, because at the start of the process nothing has accumulated yet.
%    \begin{macrocode}
\newcommand*{\brm@parsebook}[3]{%
    \expandafter\brm@parsebook@accumulate\expandafter%
        {\romannumeral-`0#1}{}{#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsebook@accumulate}
% The recursive workhorse for parsing books is \verb|\brm@parsebook@accumulate|\-\marg{remainingtext}\marg{accumulatedtext}\-\marg{successhandler}\marg{unwindhandler}.
% The unwind handler is called with no arguments if the parsing fails.
% 
% This command uses \cs{brm@headstr} to pull the first token off of the remaining text string.
% It then adds that character to the end of the accumulated text.
% The new string is a potential book alias that has not been tested yet.
% So in that way, character by character, it is able to try every possible book name.
% 
% A complication is that it encounters each possible book name starting with the shortest, but it actually wants to give precedence to the longest book names.
% So when it finds a possible book name, it doesn't actually test it yet to see whether it is valid.
% Instead it simply makes a recursive call to try the next longer possible book name.
% It passes in an unwind handler, which will only be called if all the longer book names fail.
% It is only within the unwind handler that tests whether the string is valid.
% If the string is valid, then the success handler is called, and parsing stops.
% If the string is invalid, then the caller's unwind handler will be invoked.
% In that way it will continue to unwind all the recursive calls, testing each successively shorter string at each level.
% 
% This code uses the \cs{romannumeral} trick to make sure that the text strings are always fully expanded, because otherwise the \cs{brm@tailstr} command would have to be re-evaluated many times on the same input.
% With the full expansions in place, this algorithm completes in linear time.
% Without them, it would be polynomial time.
%    \begin{macrocode}
\newcommand*{\brm@parsebook@accumulate}[4]{%
    \brm@ifempty{#1}{%
        #4%
    }{%
        \expandafter\brm@parsebook@expand\expandafter%
            {\romannumeral-`0\brm@headstr{#1}}%
            {#2}%
            {\brm@tailstr{#1}}%
            {#3}%
            {%
                \brm@ifnameexists{\brm@bookalias{#2\brm@headstr{#1}}}{%
                    \expandafter\brm@parsebook@success\expandafter%
                        {\romannumeral-`0\brm@tailstr{#1}}%
                        {\csname\brm@bookalias%
                            {#2\brm@headstr{#1}}\endcsname}%
                        {#3}%
                }{%
                    #4%
                }%
            }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsebook@expand}
% The \TeX{} syntax sometimes makes simple tasks difficult.
% In this case, it is difficult to fully expand two arguments to a command.
% The workaround is to expand one argument, pass everything to a helper, and then expand the other argument in the helper.
% The \verb|\brm@parsebook@expand|\-\marg{lastaccumulatedcharacter}\marg{accumulatedtext}\-\marg{remainingtext}\-\marg{successhandler}\marg{unwindhandler} command exists to expand the remaining text argument.
%    \begin{macrocode}
\newcommand*{\brm@parsebook@expand}[5]{%
    \expandafter\brm@parsebook@accumulate\expandafter%
        {\romannumeral-`0#3}{#2#1}{#4}{#5}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsebook@success}
% When a successful match with a book name has been made, the success handler is called.
% The remaining text needs to be expanded fully and then passed to the handler as the second argument.
% But it is difficult to fully expand any argument other than the first.
% As a workaround, the remaining text is fully expanded in \cs{brm@parsebook@accumulate}, which passes it to this helper as the first argument.
% Then \verb|\brm@parsebook@success|\-\marg{remainingtext}\-\marg{book}\-\marg{successhandler} rearranges the arguments and calls the success handler.
%    \begin{macrocode}
\newcommand*{\brm@parsebook@success}[3]{%
    #3{#2}{#1}%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{Number Parsing}
% 
% \begin{macro}{\brm@parsenumber}
% The \verb|\brm@parsenumber|\marg{text}\marg{handler} command parses an integer.
% When it's finished, it calls the handler, which is expected to have the signature \callback\-\marg{number}\-\marg{remainingtext}.
% It delegates to the command command \cs{brm@parsenumber@accumulate}.
% It passes an empty string as the accumulated text, because at the start of the process nothing has accumulated yet.
% 
% This command depends on \cs{brm@headstr}, which consumes all spaces in its input.
% That means that spaces in the text will be ignored.
% For example, if the text contains \texttt{1 2  3}, then it will be parsed as if it were \texttt{123}.
%    \begin{macrocode}
\newcommand*{\brm@parsenumber}[2]{%
    \expandafter\brm@parsenumber@accumulate\expandafter%
        {\romannumeral-`0#1}{}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsenumber@accumulate}
% The work for parsing numbers is done in the \verb|\brm@parsenumber@accumulate|\-\marg{remainingtext}\marg{accumulatedtext}\-\marg{handler} command.
% It consumes the remaining text, one character at a time.
% As soon as it finds a character that is not a digit, it calls the handler.
%    \begin{macrocode}
\newcommand*{\brm@parsenumber@accumulate}[3]{%
    \brm@ifempty{#1}{%
        \brm@parsenumber@end{#1}{#2}{#3}%
    }{%
        \expandafter\brm@ifdigit\expandafter%
            {\romannumeral-`0\brm@headstr{#1}}{%
            \expandafter\brm@parsenumber@expand\expandafter%
                {\romannumeral-`0\brm@headstr{#1}}{\brm@tailstr{#1}}{#3}{#2}%
        }{%
            \brm@parsenumber@end{#1}{#2}{#3}%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsenumber@expand}
% It is difficult to fully expand two arguments to a command.
% The workaround is to expand one argument, pass everything to a helper, and then expand the other argument in the helper.
% The \verb|\brm@parsenumber@expand|\-\marg{lastaccumulatedcharacter}\marg{accumulatedtext}\-\marg{remainingtext}\-\marg{handler} command exists to expand the remaining text argument.
%    \begin{macrocode}
\newcommand*{\brm@parsenumber@expand}[4]{%
    \expandafter\brm@parsenumber@accumulate\expandafter%
        {\romannumeral-`0#2}{#4#1}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@parsenumber@end}
% When the end of the number has been reached, the command \verb|\brm@parsenumber@end|\marg{remainingtext}\marg{number}\marg{handler} is called.
% It checks to make sure that at least some digits were parsed, and fails otherwise.
% Then it passes the number and remaining text to the handler, which is expected to have the signature \callback\-\marg{number}\-\marg{remainingtext}.
%    \begin{macrocode}
\newcommand*{\brm@parsenumber@end}[3]{%
    \brm@ifempty{#2}{%
        \brm@error{Invalid number: #1}%
    }{%
        #3{#2}{#1}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Predicates}
% 
% Each of the commands in this section is a predicate, which is a command that tests whether a certain property is true.
% These commands have both a true handler, which is called with no arguments if the condition is true, and a false handler, which is called if the condition is false.
% 
% \begin{macro}{\brm@ifsamehead}
% The \verb|\brm@ifsamehead|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler} command checks whether the two given strings start with the same letter.
% It requires both strings to contain at least one character.
%    \begin{macrocode}
\newcommand*{\brm@ifsamehead}[4]{%
    \brm@ifsamechar{\brm@headstr{#1}}{\brm@headstr{#2}}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifsamechar}
% The \verb|\brm@ifsamechar|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler} command checks whether the two given characters are the same.
%    \begin{macrocode}
\newcommand*{\brm@ifsamechar}[4]{%
    \if#1#2%
        \expandafter\@firstoftwo%
    \else%
        \expandafter\@secondoftwo%
    \fi%
    {#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifsamestr}
% The \verb|\brm@ifsamestr|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler} command checks whether the two given strings are the same.
% Most implementations, such as \cs{ifstrequal}, are not expandable.
% This command checks the characters recursively one by one using \cs{if}.
%    \begin{macrocode}
\newcommand*{\brm@ifsamestr}[4]{%
    \expandafter\brm@ifsamestr@expandsecond\expandafter%
        {\romannumeral-`0#2}{#1}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifsamestr@expandsecond}
% It is difficult to fully expand two arguments to a command.
% The workaround is to expand one argument, pass everything to a helper, and then expand the other argument in the helper.
% The \verb|\brm@ifsamestr@expandsecond|\-\marg{right}\marg{left}\-\marg{truehandler}\marg{falsehandler} command exists to expand the first string argument.
%    \begin{macrocode}
\newcommand*{\brm@ifsamestr@expandsecond}[4]{%
    \expandafter\brm@ifsamestr@expanded\expandafter%
        {\romannumeral-`0#2}{#1}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifsamestr@expanded}
% After both the first and second strings have been expanded, equality is tested recursively by \verb|\brm@ifsamestr@expanded|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler}.
% If both strings are empty, then they must be equal.
% If only one string is empty, then they are inequal.
% If neither of them are empty, then the first characters are checked for equality, and the rest are checked recursively.
%    \begin{macrocode}
\newcommand*{\brm@ifsamestr@expanded}[4]{%
    \brm@ifempty{#1}{%
        \brm@ifempty{#2}{%
            #3%
        }{%
            #4%
        }%
    }{%
        \brm@ifempty{#2}{%
            #4%
        }{%
            \brm@ifsamehead{#1}{#2}{%
                \brm@ifsamestr{\brm@tailstr{#1}}{\brm@tailstr{#2}}{%
                    #3%
                }{%
                    #4%
                }%
            }{%
                #4%
            }%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ifsamebook}
% The \verb|\ifsamebook|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler} command checks whether the two given books are the same.
%    \begin{macrocode}
\newcommand*{\ifsamebook}[4]{%
    \brm@ifsamestr{\thebook{#1}}{\thebook{#2}}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ifsamechapter}
% The \verb|\ifsamechapter|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler} command checks whether the two given chapters are the same.
%    \begin{macrocode}
\newcommand*{\ifsamechapter}[4]{%
    \brm@ifsamestr{\thechapter{#1}}{\thechapter{#2}}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ifsameverse}
% The \verb|\ifsameverse|\-\marg{left}\marg{right}\-\marg{truehandler}\marg{falsehandler} command checks whether the two given verses are the same.
%    \begin{macrocode}
\newcommand*{\ifsameverse}[4]{%
    \brm@ifsamestr{\theverse{#1}}{\theverse{#2}}{#3}{#4}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ifhasbook}
% The \verb|\ifhasbook|\-\marg{passage}\-\marg{truehandler}\marg{falsehandler} command checks whether the passage contains a book.
%    \begin{macrocode}
\newcommand*{\ifhasbook}[3]{%
    \expandafter\brm@ifempty\expandafter%
        {\romannumeral-`0\thebook{#1}}{#3}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ifhaschapter}
% The \verb|\ifhaschapter|\-\marg{passage}\-\marg{truehandler}\marg{falsehandler} command checks whether the passage contains a chapter.
%    \begin{macrocode}
\newcommand*{\ifhaschapter}[3]{%
    \expandafter\brm@ifempty\expandafter%
        {\romannumeral-`0\thechapter{#1}}{#3}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\ifhasverse}
% The \verb|\ifhasverse|\-\marg{passage}\-\marg{truehandler}\marg{falsehandler} command checks whether the passage contains a verse.
%    \begin{macrocode}
\newcommand*{\ifhasverse}[3]{%
    \expandafter\brm@ifempty\expandafter%
        {\romannumeral-`0\theverse{#1}}{#3}{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifempty}
% The \verb|\brm@ifempty|\-\marg{text}\-\marg{truehandler}\marg{falsehandler} command checks whether the string is empty.
% This test was suggested at \url{http://tex.stackexchange.com/questions/2936/test-whether-token-list-is-empty}.
%    \begin{macrocode}
\newcommand*{\brm@ifempty}[3]{%
    \expandafter\ifx\expandafter\brm@nevermatch#1\brm@nevermatch%
        \expandafter\@firstoftwo%
    \else%
        \expandafter\@secondoftwo%
    \fi%
    {#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@nevermatch}
% The \verb|\brm@nevermatch| command expands to itself.
% Because of that it is a convenient token to use in the \cs{brm@ifempty} test.
%    \begin{macrocode}
\def\brm@nevermatch{\brm@nevermatch}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifnameexists}
% The \verb|\brm@ifnameexists|\-\marg{csname}\-\marg{truehandler}\marg{falsehandler} command checks whether the named is a defined command.
%    \begin{macrocode}
\newcommand*{\brm@ifnameexists}[3]{%
    \ifcsname#1\endcsname%
        \expandafter\@firstoftwo%
    \else%
        \expandafter\@secondoftwo%
    \fi%
    {#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifdigit}
% The \verb|\brm@ifdigit|\-\marg{text}\-\marg{truehandler}\marg{falsehandler} command checks whether the given character is a digit.
%    \begin{macrocode}
\newcommand*{\brm@ifdigit}[3]{%
    \ifnum 9<1\string#1 %
        \expandafter\@firstoftwo%
    \else%
        \expandafter\@secondoftwo%
    \fi%
    {#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@ifspace}
% The \verb|\brm@ifspace|\-\marg{text}\-\marg{truehandler}\marg{falsehandler} command checks whether the given character is a space.
%    \begin{macrocode}
\newcommand*{\brm@ifspace}[3]{%
    \ifcat #1 %
        \expandafter\@firstoftwo%
    \else%
        \expandafter\@secondoftwo%
    \fi%
    {#2}{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{String Manipulations}
% 
% The commands in this section are expandable versions of common string manipulations.
% They work well for the use cases that exist in this package, but they tend to behave badly if they are given anything other than ASCII characters.
% 
% \begin{macro}{\brm@uppercaseascii}
% The \verb|\brm@uppercaseascii|\marg{text} command changes an ASCII string to uppercase.
% Most implementations, such as \cs{MakeUppercase}, are not expandable.
% The limitation to ASCII characters here is not serious, since an expandable implementation is only needed when a command name is being defined, and command names can only contain ASCII characters anyway.
%    \begin{macrocode}
\newcommand*{\brm@uppercaseascii}[1]{%
    \brm@ifempty{#1}{}{%
        \expandafter\brm@uppercaseascii@char\expandafter%
            {\romannumeral-`0\brm@headstr{#1}}%
        \expandafter\brm@uppercaseascii\expandafter%
            {\romannumeral-`0\brm@tailstr{#1}}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@uppercaseascii@char}
% The \verb|\brm@uppercaseascii@char|\marg{character} command changes a single ASCII character to uppercase.
% It uses a giant \cs{ifcase} statement to separate the lowercase letters from all other characters.
%    \begin{macrocode}
\newcommand*{\brm@uppercaseascii@char}[1]{%
    \ifcase`#1%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        #1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or#1\or%
        A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or K\or L\or M\or%
        N\or O\or P\or Q\or R\or S\or T\or U\or V\or W\or X\or Y\or Z%
    \else#1%
    \fi%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@headstr}
% The \verb|\brm@headstr|\marg{text} command returns the first character from a string.
% The string is required to contain at least one character, so callers should check \cs{brm@ifempty} first.
% 
% Because of the way it pulls the first character off the string, spaces all get consumed by the \TeX parser.
% It is as if all spaces in the text get ignored.
% So really, this command returns the first non-space character in the text.
% Unfortunately, this is a deficiency of \TeX that cannot be worked around in an expandable manner.
% Even modifying the catcode of the space character does not fix this, because catcode changes and \cs{scantokens} are not themselves expandable.
% \emph{If you know how to correct this problem, by writing an expandable function that returns the first character, whether or not it is a space, please \href{mailto:spam@chad.parry.org}{contact the author}.}
%    \begin{macrocode}
\def\brm@headstr#1{\brm@headstr@helper#1\@nil}
\def\brm@headstr@helper#1#2\@nil{#1}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@tailstr}
% The \verb|\brm@tailstr|\marg{text} command returns the string with the first character removed.
% The string is required to contain at least one character, so callers should check \cs{brm@ifempty} first.
%    \begin{macrocode}
\def\brm@tailstr#1{\brm@tailstr@helper#1\@nil}
\def\brm@tailstr@helper#1#2\@nil{#2}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Exceptions}
% 
% \begin{macro}{\brm@error}
% The \verb|\brm@error|\marg{message} command raises errors to the user.
% This is useful because other implementations, such as \cs{PackageError}, are not expandable.
% This implementation was suggested at \url{http://tex.stackexchange.com/questions/17238/signalling-error-in-expandable-context}.
%    \begin{macrocode}
\begingroup
\catcode`\:=11\relax
\gdef\brm@error#1{%
    \protect\expandafter\brm@error@aux\csname#1\endcsname%
}
\gdef\brm@error@aux#1{%
    \romannumeral-`0\@firstofone{\brm@error: #1}%
}
\endgroup
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Formatting}
% 
% These commands are used to format the reference according to a customizable style.
% Those commands that are used to customized styles are defined without the \verb|brm@| prefix, to make them easier to access.
% 
% \begin{macro}{\brm@bookref}
% The \verb|\brm@bookref|\marg{stylename}\marg{book} command returns the name of the command that outputs the stylized book name.
%    \begin{macrocode}
\newcommand*{\brm@bookref}[2]{brm@bookref@#1@#2}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@bookalias}
% The \verb|\brm@bookalias|\marg{book} command returns the name of the command that outputs the actual book name for the book alias.
%    \begin{macrocode}
\newcommand*{\brm@bookalias}[1]{brm@bookalias@#1}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@style}
% The \verb|\brm@style|\marg{stylename} command returns the name of the style command that formats the reference.
%    \begin{macrocode}
\newcommand*{\brm@style}[1]{brm@style@#1}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\thebookname}
% The \verb|\thebookname|\marg{stylename}\marg{passage} outputs the stylized book name for the passage.
%    \begin{macrocode}
\newcommand*{\thebookname}[2]{%
    \brm@bookname{#1}{\thebook{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@bookname}
% The \verb|\brm@bookname|\marg{stylename}\marg{passage} outputs the stylized name for the given book.
%    \begin{macrocode}
\newcommand*{\brm@bookname}[2]{%
    \brm@ifnameexists{\brm@bookref{#1}{#2}}{%
        \csname\brm@bookref{#1}{#2}\endcsname%
    }{%
        \brm@error{Style missing book: #2}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\providebiblestyle}
% To create a new Bible style, the user calls \verb|\providebiblestyle|\marg{stylename}\marg{formatter}.
% This command creates a style with the signature \verb|\brm@style@|\meta{stylename}\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
% The formatter gets expanded from within an environment where those three arguments are defined.
%    \begin{macrocode}
\newcommand*{\providebiblestyle}[2]{%
    \expandafter\def\csname\brm@style{#1}\endcsname##1##2##3{%
        #2%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\providebiblebookalias}
% A new Bible book alias can be created with \verb|\providebiblebookalias|\marg{book}\marg{alias}.
% An alias is the name that is specified to the \cs{bibleref} command.
% Every book needs to have aliases defined for users to refer to it.
% A book can have as many different aliases as the user wishes to defined.
% This command expands the alias name and delegates to \cs{brm@providebiblebookalias}.
% 
% Because the parser ignores spaces, an alias name also cannot contain spaces. If an alias is
% created that contains spaces anyway, then it will be impossible for users to reference it.
%    \begin{macrocode}
\newcommand*{\providebiblebookalias}[2]{%
    \expandafter\brm@providebiblebookalias\expandafter%
        {\romannumeral-`0#2}{#1}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@providebiblebookalias}
% A new Bible book alias is created by \verb|\brm@providebiblebookalias|\marg{alias}\marg{book}.
%    \begin{macrocode}
\newcommand*{\brm@providebiblebookalias}[2]{%
    \expandafter\def\csname\brm@bookalias{#1}\endcsname{#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\providebiblebook}
% A new stylized Bible book name can be created with \verb|\providebiblebook|\marg{stylename}\marg{book}\marg{bookname}.
% The book name needs to be defined for each book and each style before it can be referenced by a user.
%    \begin{macrocode}
\newcommand*{\providebiblebook}[3]{%
    \expandafter\def\csname\brm@bookref{#1}{#2}\endcsname{#3}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@formatchapter}
% This helper command extracts a chapter from a passage and then formats it according to the given callback.
% The signature is \verb|\brm@formatchapter|\marg{formatter}\marg{passage}.
% It is used by partially applying the command with a formatter, creating a command that accept a passage and output a stylized chapter.
% The callback is expected to have the signature \callback\marg{chapter}.
%    \begin{macrocode}
\newcommand*{\brm@formatchapter}[2]{%
    #1{\thechapter{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@formatverse}
% This helper command extracts a verse from a passage and then formats it according to the given callback.
% The signature is \verb|\brm@formatverse|\marg{formatter}\marg{passage}.
% It is used by partially applying the command with a formatter, creating a command that accept a passage and output a stylized verse.
% The callback is expected to have the signature \callback\marg{verse}.
%    \begin{macrocode}
\newcommand*{\brm@formatverse}[2]{%
    #1{\theverse{#2}}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@identity@filter}
% The \cs{brm@standard@filtered} command accepts a filter, but some callers don't need one.
% To meet that need, callers can use this filter that doesn't modify it's arguments, \verb|\brm@identity@filter|\-\marg{lastpassage}\marg{passage}\-\marg{infixseparators}\-\marg{prefixes}\-\marg{formatters}\-\marg{delegate}.
%    \begin{macrocode}
\newcommand*{\brm@identity@filter}[6]{%
    #6{#1}{#2}{#3}{#4}{#5}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\standardbiblestyle}
% Many different Bible styles have a lot of similarities in structure.
% As long as the stylized references follow a standard pattern, they can be formatted with this command.
% There are more than nine parameters that are needed, and \TeX only allows nine parameters to any one command.
% The workaround is to consume the first nine arguments in this command and then consume the rest of the arguments in \cs{brm@standard@filtered}.
% The signature, including all parameters, is \verb|\standardbiblestyle|\-\marg{bookstyle}\-\marg{bkchsep}\marg{chvsep}\-\marg{bksep}\marg{chsep}\marg{vsep}\-\marg{rangesep}\-\marg{chapterstyle}\marg{versestyle}\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
%    \begin{macrocode}
\newcommand*{\standardbiblestyle}[9]{%
    \brm@standard@filtered%
        {\brm@packseparators{#2}{#3}}%
        {\brm@packpassage{#4}{#5}{#6}}%
        {\brm@packpassage{#7}{#7}{#7}}%
        {\brm@packpassage%
            {\thebookname{#1}}%
            {\brm@formatchapter{#8}}%
            {\brm@formatverse{#9}}}%
        {\brm@identity@filter}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@standard@filtered}
% This command helps collect arguments by consuming five arguments from \cs{standardbiblestyle} and three remaining arguments for the range begin, range end, and the remaining ranges list.
% It's signature is \verb|\brm@standard@filtered|\-\marg{infixseparators}\-\marg{listseparators}\-\marg{rangeseparators}\-\marg{formatters}\-\marg{filter}\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
%    \begin{macrocode}
\newcommand*{\brm@standard@filtered}[8]{%
    #5%
        {\brm@packpassage{}{}{}}%
        {#6}%
        {#1}%
        {\brm@packpassage{}{}{}}%
        {#4}%
        {\brm@standard@passage}%
    #5{#6}{#7}{#1}{#3}{#4}{\brm@standard@passage}%
    #8{%
        \brm@standard@list{#1}{#2}{#3}{#4}{#5}%
            {\ifhasbook{#7}{#7}{#6}}%
    }{%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@standard@list}
% A standard list of references can be formatted by \verb|\brm@standard@list|\-\marg{infixseparators}\-\marg{listseparators}\-\marg{rangeseparators}\-\marg{formatters}\-\marg{filter}\-\marg{lastpassage}\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
% It delegates to \cs{brm@standard@passage} to format passages and then recursively calls itself to continue with the next element in the list.
%    \begin{macrocode}
\newcommand*{\brm@standard@list}[9]{%
    #5{#6}{#7}{#1}{#2}{#4}{\brm@standard@passage}%
    #5{#7}{#8}{#1}{#3}{#4}{\brm@standard@passage}%
    #9{%
        \brm@standard@list{#1}{#2}{#3}{#4}{#5}%
            {\ifhasbook{#8}{#8}{#7}}%
    }{%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@standard@passage}
% A single standard passage format is created by \verb|\brm@standard@passage|\-\marg{lastpassage}\marg{passage}\-\marg{infixseparators}\-\marg{prefixes}\-\marg{formatters}.
% The last passage is not displayed, but it is used to decide which separators might be necessary, and how much of the reference to output.
% For example, if the last passage is for a different chapter in the same book compared to the current passage, then the book name will not be output, but a prefix separator for chapters will be.
% This function contains an enormous number of nested ``if-then'' blocks to cover every possible relationship between the last passage and the current passage.
%    \begin{macrocode}
\newcommand*{\brm@standard@passage}[5]{%
    \ifhasbook{#2}{%
        \ifhaschapter{#2}{%
            \ifhasverse{#2}{%
                \ifsamebook{#1}{#2}{%
                    \ifsamechapter{#1}{#2}{%
                        \ifsameverse{#1}{#2}{%
                        }{%
                            \ifhasverse{#1}{%
                                \theverse{#4}%
                                \theverse{#5}{#2}%
                            }{%
                                \thechapter{#4}%
                                \thechapter{#5}{#2}%
                                \chapterverseseparator{#3}%
                                \theverse{#5}{#2}%
                            }%
                        }%
                    }{%
                        \ifhaschapter{#1}{%
                            \thechapter{#4}%
                            \thechapter{#5}{#2}%
                            \chapterverseseparator{#3}%
                            \theverse{#5}{#2}%
                        }{%
                            \thebook{#4}%
                            \thebook{#5}{#2}%
                            \bookchapterseparator{#3}%
                            \thechapter{#5}{#2}%
                            \chapterverseseparator{#3}%
                            \theverse{#5}{#2}%
                        }%
                    }%
                }{%
                    \thebook{#4}%
                    \thebook{#5}{#2}%
                    \bookchapterseparator{#3}%
                    \thechapter{#5}{#2}%
                    \chapterverseseparator{#3}%
                    \theverse{#5}{#2}%
                }%
            }{%
                \ifsamebook{#1}{#2}{%
                    \ifsamechapter{#1}{#2}{%
                        \ifhasverse{#1}{%
                            \brm@ifsamestr
                                {\thechapter{#4}}{\theverse{#4}}{%
                                \thebook{#4}%
                                \thebook{#5}{#2}%
                                \bookchapterseparator{#3}%
                                \thechapter{#5}{#2}%
                            }{%
                                \thechapter{#4}%
                                \thechapter{#5}{#2}%
                            }%
                        }{%
                        }%
                    }{%
                        \ifhaschapter{#1}{%
                            \ifhasverse{#1}{%
                                \brm@ifsamestr
                                    {\thechapter{#4}}{\theverse{#4}}{%
                                    \thebook{#4}%
                                    \thebook{#5}{#2}%
                                    \bookchapterseparator{#3}%
                                    \thechapter{#5}{#2}%
                                }{%
                                    \thechapter{#4}%
                                    \thechapter{#5}{#2}%
                                }%
                            }{%
                                \thechapter{#4}%
                                \thechapter{#5}{#2}%
                            }%
                        }{%
                            \thebook{#4}%
                            \thebook{#5}{#2}%
                            \bookchapterseparator{#3}%
                            \thechapter{#5}{#2}%
                        }%
                    }%
                }{%
                    \thebook{#4}%
                    \thebook{#5}{#2}%
                    \bookchapterseparator{#3}%
                    \thechapter{#5}{#2}%
                }%
            }%
        }{%
            \ifsamebook{#1}{#2}{%
                \ifhaschapter{#1}{%
                    \thebook{#4}%
                    \thebook{#5}{#2}%
                }{%
                }%
            }{%
                \thebook{#4}%
                \thebook{#5}{#2}%
            }%
        }%
    }{%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@delegatestyle}
% For standard formats that want to internally delegate part of the format to a different defined style, the \verb|\brm@delegatestyle|\marg{stylename}\marg{beginpassage}\-\marg{endpassage}\-\marg{list} command is convenient.
%    \begin{macrocode}
\newcommand*{\brm@delegatestyle}[4]{%
    \brm@ifnameexists{\brm@style{#1}}{%
        \csname\brm@style{#1}\endcsname{#2}{#3}{#4}%
    }{%
        \brm@error{Unrecognized style: #1}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Standard Styles}
%
% \subsubsection{Book Names}
% \label{booknames}
%
% All the same books and book names that are supported by the \textsf{bibleref} package are supported here too.
% Each book is given a full name, an abbreviation, and an alternative abbrevation.
% Users can define more names or abbreviations if they wish.
% Examples of how to extend the collection of supported books are shown in the \textsf{bibleref-lds} package.
% 
% \begin{macro}{\brm@foreachbook@full}
% There are several different use cases for full book names.
% There are also several varieties of each full name.
% For example, the book number could be formatted with either Arabic or Roman numerals.
% To provide the greatest amount of flexibility, callback gets invoked for each formatted full name.
% This command has the signature \verb|\brm@foreachbook@full|\-\marg{callback}\-\marg{space}\-\marg{bookof}\-\marg{gospelof}\-\marg{epistleto}\marg{epistletothe}\marg{epistleof}\-\marg{booknumberstyle}\marg{epistlenumberstyle}.
% The callback argument is expected to have the signature \callback\-\marg{book}\marg{bookname}.
% The callback can do whatever it wants with the names, such as creating a permanant book alias.
% The two number style arguments are expected to be functions with the signature \callback\marg{number}.
%    \begin{macrocode}
\newcommand*{\brm@foreachbook@full}[9]{%
    #1{Genesis}{#3Genesis}%
    #1{Exodus}{#3Exodus}%
    #1{Leviticus}{#3Leviticus}%
    #1{Numbers}{#3Numbers}%
    #1{Deuteronomy}{#3Deuteronomy}%
    #1{Joshua}{#3Joshua}%
    #1{Judges}{#3Judges}%
    #1{Ruth}{#3Ruth}%
    #1{ISamuel}{#8{1}#3Samuel}%
    #1{IISamuel}{#8{2}#3Samuel}%
    #1{IKings}{#8{1}#3Kings}%
    #1{IIKings}{#8{2}#3Kings}%
    #1{IChronicles}{#8{1}#3Chronicles}%
    #1{IIChronicles}{#8{2}#3Chronicles}%
    #1{Ezra}{#3Ezra}%
    #1{Nehemiah}{#3Nehemiah}%
    #1{Tobit}{#3Tobit}%
    #1{Judith}{#3Judith}%
    #1{Esther}{#3Esther}%
    #1{IMaccabees}{#8{1}#3Maccabees}%
    #1{IIMaccabees}{#8{2}#3Maccabees}%
    #1{Job}{#3Job}%
    #1{Psalms}{#3Psalms}%
    #1{Proverbs}{#3Proverbs}%
    #1{Ecclesiastes}{#3Ecclesiastes}%
    #1{SongofSongs}{#3Song#2of#2Songs}%
    #1{Wisdom}{#3Wisdom}%
    #1{Ecclesiasticus}{#3Ecclesiasticus}%
    #1{Isaiah}{#3Isaiah}%
    #1{Jeremiah}{#3Jeremiah}%
    #1{Lamentations}{#3Lamentations}%
    #1{Baruch}{#3Baruch}%
    #1{Ezekiel}{#3Ezekiel}%
    #1{Daniel}{#3Daniel}%
    #1{Hosea}{#3Hosea}%
    #1{Joel}{#3Joel}%
    #1{Amos}{#3Amos}%
    #1{Obadiah}{#3Obadiah}%
    #1{Jonah}{#3Jonah}%
    #1{Micah}{#3Micah}%
    #1{Nahum}{#3Nahum}%
    #1{Habakkuk}{#3Habakkuk}%
    #1{Zephaniah}{#3Zephaniah}%
    #1{Haggai}{#3Haggai}%
    #1{Zechariah}{#3Zechariah}%
    #1{Malachi}{#3Malachi}%
    #1{Matthew}{#4Matthew}%
    #1{Mark}{#4Mark}%
    #1{Luke}{#4Luke}%
    #1{John}{#4John}%
    #1{Acts}{Acts}%
    #1{Romans}{#6Romans}%
    #1{ICorinthians}{#9{1}#6Corinthians}%
    #1{IICorinthians}{#9{2}#6Corinthians}%
    #1{Galatians}{#6Galatians}%
    #1{Ephesians}{#6Ephesians}%
    #1{Philippians}{#6Philippians}%
    #1{Colossians}{#6Colossians}%
    #1{IThessalonians}{#9{1}#6Thessalonians}%
    #1{IIThessalonians}{#9{2}#6Thessalonians}%
    #1{ITimothy}{#9{1}#5Timothy}%
    #1{IITimothy}{#9{2}#5Timothy}%
    #1{Titus}{#5Titus}%
    #1{Philemon}{#5Philemon}%
    #1{Hebrews}{#6Hebrews}%
    #1{James}{#7James}%
    #1{IPeter}{#9{1}#7Peter}%
    #1{IIPeter}{#9{2}#7Peter}%
    #1{IJohn}{#9{1}#7John}%
    #1{IIJohn}{#9{2}#7John}%
    #1{IIIJohn}{#9{3}#7John}%
    #1{Jude}{#7Jude}%
    #1{Revelation}{#3Revelation}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@foreachbook@abbrv}
% The abbreviated book names are accessible through \verb|\brm@foreachbook@abbrv|\-\marg{callback}\-\marg{period}\-\marg{booknumberstyle}\marg{epistlenumberstyle}
% The callback argument is expected to have the signature \callback\-\marg{book}\marg{bookname}.
% The two number style arguments are expected to be functions with the signature \callback\marg{number}.
%    \begin{macrocode}
\newcommand*{\brm@foreachbook@abbrv}[4]{%
    #1{Genesis}{Gn#2}%
    #1{Exodus}{Ex#2}%
    #1{Leviticus}{Lv#2}%
    #1{Numbers}{Nb#2}%
    #1{Deuteronomy}{Dt#2}%
    #1{Joshua}{Jos#2}%
    #1{Judges}{Jg#2}%
    #1{Ruth}{Rt#2}%
    #1{ISamuel}{#3{1}S#2}%
    #1{IISamuel}{#3{2}S#2}%
    #1{IKings}{#3{1}K#2}%
    #1{IIKings}{#3{2}K#2}%
    #1{IChronicles}{#3{1}Ch#2}%
    #1{IIChronicles}{#3{2}Ch#2}%
    #1{Ezra}{Ezr#2}%
    #1{Nehemiah}{Ne#2}%
    #1{Tobit}{Tb#2}%
    #1{Judith}{Jdt#2}%
    #1{Esther}{Est#2}%
    #1{IMaccabees}{#3{1}M#2}%
    #1{IIMaccabees}{#3{2}M#2}%
    #1{Job}{Jb#2}%
    #1{Psalms}{Ps#2}%
    #1{Proverbs}{Pr#2}%
    #1{Ecclesiastes}{Qo#2}%
    #1{SongofSongs}{Sg#2}%
    #1{Wisdom}{Ws#2}%
    #1{Ecclesiasticus}{Si#2}%
    #1{Isaiah}{Is#2}%
    #1{Jeremiah}{Jr#2}%
    #1{Lamentations}{Lm#2}%
    #1{Baruch}{Ba#2}%
    #1{Ezekiel}{Ezk#2}%
    #1{Daniel}{Dn#2}%
    #1{Hosea}{Ho#2}%
    #1{Joel}{Jl#2}%
    #1{Amos}{Am#2}%
    #1{Obadiah}{Ob#2}%
    #1{Jonah}{Jon#2}%
    #1{Micah}{Mi#2}%
    #1{Nahum}{Na#2}%
    #1{Habakkuk}{Hab#2}%
    #1{Zephaniah}{Zp#2}%
    #1{Haggai}{Hg#2}%
    #1{Zechariah}{Zc#2}%
    #1{Malachi}{Ml#2}%
    #1{Matthew}{Mt#2}%
    #1{Mark}{Mk#2}%
    #1{Luke}{Lk#2}%
    #1{John}{Jn#2}%
    #1{Acts}{Ac#2}%
    #1{Romans}{Rm#2}%
    #1{ICorinthians}{#4{1}Co#2}%
    #1{IICorinthians}{#4{2}Co#2}%
    #1{Galatians}{Ga#2}%
    #1{Ephesians}{Ep#2}%
    #1{Philippians}{Ph#2}%
    #1{Colossians}{Col#2}%
    #1{IThessalonians}{#4{1}Th#2}%
    #1{IIThessalonians}{#4{2}Th#2}%
    #1{ITimothy}{#4{1}Tm#2}%
    #1{IITimothy}{#4{2}Tm#2}%
    #1{Titus}{Tt#2}%
    #1{Philemon}{Phm#2}%
    #1{Hebrews}{Heb#2}%
    #1{James}{Jm#2}%
    #1{IPeter}{#4{1}P#2}%
    #1{IIPeter}{#4{2}P#2}%
    #1{IJohn}{#4{1}Jn#2}%
    #1{IIJohn}{#4{2}Jn#2}%
    #1{IIIJohn}{#4{3}Jn#2}%
    #1{Jude}{Jude}%
    #1{Revelation}{Rv#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@foreachbook@altabbrv}
% The alternative abbreviated book names are accessible through \verb|\brm@foreachbook@altabbrv|\-\marg{callback}\-\marg{period}\marg{space}\-\marg{booknumberstyle}\marg{epistlenumberstyle}
% The callback argument is expected to have the signature \callback\-\marg{book}\marg{bookname}.
% The two number style arguments are expected to be functions with the signature \callback\marg{number}.
%    \begin{macrocode}
\newcommand*{\brm@foreachbook@altabbrv}[5]{%
    #1{Genesis}{Gen#2}%
    #1{Exodus}{Exod#2}%
    #1{Leviticus}{Lev#2}%
    #1{Numbers}{Num#2}%
    #1{Deuteronomy}{Deut#2}%
    #1{Joshua}{Josh#2}%
    #1{Judges}{Judg#2}%
    #1{Ruth}{Ruth}%
    #1{ISamuel}{#4{1}Sam#2}%
    #1{IISamuel}{#4{2}Sam#2}%
    #1{IKings}{#4{1}Kgs#2}%
    #1{IIKings}{#4{2}Kgs#2}%
    #1{IChronicles}{#4{1}Chr#2}%
    #1{IIChronicles}{#4{2}Chr#2}%
    #1{Ezra}{Ezra}%
    #1{Nehemiah}{Neh#2}%
    #1{Tobit}{Tobit}%
    #1{Judith}{Judith}%
    #1{Esther}{Esther}%
    #1{IMaccabees}{#4{1}M#2}%
    #1{IIMaccabees}{#4{2}M#2}%
    #1{Job}{Job}%
    #1{Psalms}{Ps#2}%
    #1{Proverbs}{Prov#2}%
    #1{Ecclesiastes}{Eccles#2}%
    #1{SongofSongs}{S#2#3of#3S#2}%
    #1{Wisdom}{Wisd#2}%
    #1{Ecclesiasticus}{Ecclus#2}%
    #1{Isaiah}{Isa#2}%
    #1{Jeremiah}{Jer#2}%
    #1{Lamentations}{Lam#2}%
    #1{Baruch}{Baruch}%
    #1{Ezekiel}{Ezek#2}%
    #1{Daniel}{Dan#2}%
    #1{Hosea}{Hos#2}%
    #1{Joel}{Joel}%
    #1{Amos}{Amos}%
    #1{Obadiah}{Obad#2}%
    #1{Jonah}{Jonah}%
    #1{Micah}{Mic#2}%
    #1{Nahum}{Nah#2}%
    #1{Habakkuk}{Hab#2}%
    #1{Zephaniah}{Zeph#2}%
    #1{Haggai}{Hag#2}%
    #1{Zechariah}{Zech#2}%
    #1{Malachi}{Mal#2}%
    #1{Matthew}{Matt#2}%
    #1{Mark}{Mark}%
    #1{Luke}{Luke}%
    #1{John}{John}%
    #1{Acts}{Acts}%
    #1{Romans}{Rom#2}%
    #1{ICorinthians}{#5{1}Cor#2}%
    #1{IICorinthians}{#5{2}Cor#2}%
    #1{Galatians}{Gal#2}%
    #1{Ephesians}{Eph#2}%
    #1{Philippians}{Phil#2}%
    #1{Colossians}{Col#2}%
    #1{IThessalonians}{#5{1}Thess#2}%
    #1{IIThessalonians}{#5{2}Thess#2}%
    #1{ITimothy}{#5{1}Tim#2}%
    #1{IITimothy}{#5{2}Tim#2}%
    #1{Titus}{Tit#2}%
    #1{Philemon}{Philem#2}%
    #1{Hebrews}{Heb#2}%
    #1{James}{Jas#2}%
    #1{IPeter}{#5{1}Pet#2}%
    #1{IIPeter}{#5{2}Pet#2}%
    #1{IJohn}{#5{1}John}%
    #1{IIJohn}{#5{2}John}%
    #1{IIIJohn}{#5{3}John}%
    #1{Jude}{Jude}%
    #1{Revelation}{Rev#2}%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{Number Styles}
% 
% The book names can be customized with different number styles for book numbers and epistle numbers.
% 
% \begin{macro}{\brm@number@arabic}
% The \verb|\brm@number@arabic|\marg{number} command formats a number with Arabic numerals.
%    \begin{macrocode}
\newcommand*{\brm@number@arabic}[1]{#1}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@lowerroman}
% The \verb|\brm@number@lowerroman|\marg{number} command formats a number with lower-case Roman numerals.
%    \begin{macrocode}
\newcommand*{\brm@number@lowerroman}[1]{\romannumeral#1}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@upperroman}
% The \verb|\brm@number@upperroman|\marg{number} command formats a number with upper-case Roman numerals.
%    \begin{macrocode}
\newcommand*{\brm@number@upperroman}[1]{%
    \MakeUppercase{\romannumeral#1}%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@arabicspace}
% The \verb|\brm@number@arabicspace|\marg{number} command formats a number with Arabic numerals and a trailing space.
%    \begin{macrocode}
\newcommand*{\brm@number@arabicspace}[1]{#1~}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@arabicspaceascii}
% The \verb|\brm@number@arabicspaceascii|\marg{number} command formats a number with Arabic numerals and a trailing ASCII-safe space, which is useful when defining a command name that can only consist of ASCII characters.
%    \begin{macrocode}
\newcommand*{\brm@number@arabicspaceascii}[1]{#1 }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@lowerromanspace}
% The \verb|\brm@number@lowerromanspace|\marg{number} command formats a number with lower-case Roman numerals and a trailing space.
%    \begin{macrocode}
\newcommand*{\brm@number@lowerromanspace}[1]{\romannumeral#1~}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@upperromanspace}
% The \verb|\brm@number@upperromanspace|\marg{number} command formats a number with upper-case Roman numerals and a trailing space.
%    \begin{macrocode}
\newcommand*{\brm@number@upperromanspace}[1]{%
    \MakeUppercase{\romannumeral#1}~%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@scromanspace}
% The \verb|\brm@number@upperromanspace|\marg{number} command formats a number with small caps Roman numerals and a trailing space.
%    \begin{macrocode}
\newcommand*{\brm@number@scromanspace}[1]{\textsc{\romannumeral#1}~}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@upperromanascii}
% The \verb|\brm@number@upperromanascii|\marg{number} command formats a number with upper-case Roman numerals, in an expandable ASCII-safe way, which is useful when defining a command name that can only consist of ASCII characters.
%    \begin{macrocode}
\newcommand*{\brm@number@upperromanascii}[1]{%
    \expandafter\brm@uppercaseascii\expandafter{\romannumeral#1}%
}
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{Book Aliases}
% 
% Various book aliases are provided for all the book names, including versions of the full names, the abbreviated names, and the alternative abbreviated names.
% The book numbers can be done with either Arabic or Roman numerals.
% The abbreviations are supported both with and without periods.
% In order to support all such combinations, there are \oldstylenums{10} different definitions.
% (If there were more than \oldstylenums{24} combinations, then it would start to be necessary to programmatically keep track of them, but this small number can still be maintained manually).
%    \begin{macrocode}
\brm@foreachbook@full{\providebiblebookalias}{}{}{}{}{}{}%
    {\brm@number@upperromanascii}{\brm@number@upperromanascii}
\brm@foreachbook@full{\providebiblebookalias}{}{}{}{}{}{}%
    {\brm@number@arabic}{\brm@number@arabic}
\brm@foreachbook@abbrv{\providebiblebookalias}{}%
    {\brm@number@upperromanascii}{\brm@number@upperromanascii}
\brm@foreachbook@abbrv{\providebiblebookalias}{}%
    {\brm@number@arabic}{\brm@number@arabic}
\brm@foreachbook@abbrv{\providebiblebookalias}{.}%
    {\brm@number@upperromanascii}{\brm@number@upperromanascii}
\brm@foreachbook@abbrv{\providebiblebookalias}{.}%
    {\brm@number@arabic}{\brm@number@arabic}
\brm@foreachbook@altabbrv{\providebiblebookalias}{}{}%
    {\brm@number@arabic}{\brm@number@arabic}
\brm@foreachbook@altabbrv{\providebiblebookalias}{}{}%
    {\brm@number@upperromanascii}{\brm@number@upperromanascii}
\brm@foreachbook@altabbrv{\providebiblebookalias}{.}{}%
    {\brm@number@arabic}{\brm@number@arabic}
\brm@foreachbook@altabbrv{\providebiblebookalias}{.}{}%
    {\brm@number@upperromanascii}{\brm@number@upperromanascii}
%    \end{macrocode}
% 
% \subsubsection{Formatted Book Names}
% 
% Several different forms of book names are created by default.
% These commands define the callback as \verb|\providebiblebook{|\meta{stylename}\verb|}|, which creates a version of each book name in the given style.
% 
%    \begin{macrocode}
\brm@foreachbook@full{\providebiblebook{fullname}}{\ }{}{}{}{}{}%
    {\brm@number@arabicspace}{\brm@number@arabicspace}
\brm@foreachbook@abbrv{\providebiblebook{jerusalem}}{}%
    {\brm@number@arabicspace}{\brm@number@arabicspace}
\brm@foreachbook@altabbrv{\providebiblebook{anglosaxon}}{.}{\ }%
    {\brm@number@upperromanspace}{\brm@number@upperromanspace}
\brm@foreachbook@altabbrv{\providebiblebook{JEH}}{.}{\ }%
    {\brm@number@arabicspace}{\brm@number@arabicspace}
\brm@foreachbook@full{\providebiblebook{MHRA}}{\ }{}{}{}{}{}%
    {\brm@number@scromanspace}{\brm@number@scromanspace}
\brm@foreachbook@altabbrv{\providebiblebook{NTG}}{}{\ }%
    {\brm@number@arabicspace}{\brm@number@arabicspace}
\brm@foreachbook@altabbrv{\providebiblebook{MLA}}{.}{\ }%
    {\brm@number@arabicspace}{\brm@number@arabicspace}
\brm@foreachbook@altabbrv{\providebiblebook{chicago}}{.}{\ }%
    {\brm@number@arabicspace}{\brm@number@arabicspace}
%    \end{macrocode}
% 
% \subsubsection{Default Styles}
% \label{defaultstyles}
% 
% All the styles that are supported by the \textsf{bibleref} package are supported here too.
% Each one is made to delegate to the \cs{standardbiblestyle} helper.
% The parameters to that helper determine the punctuation and spacing that will be used in the formatted output.
% The helper's signature is \verb|\standardbiblestyle|\-\marg{bookstyle}\-\marg{bkchsep}\marg{chvsep}\-\marg{bksep}\marg{chsep}\marg{vsep}\-\marg{rangesep}\-\marg{chapterstyle}\marg{versestyle}\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
% The \cs{providebiblestyle} command creates an environment in which three parameters are defined, for the begin passage, the end passage, and the passage list.
% 
% New styles can be defined by creating new definitions like the ones here, except with different punctuation or spacing.
% Examples of how to define custom styles are found in the \textsf{bibleref-lds} package.
% 
%    \begin{macrocode}
\providebiblestyle{fullname}{\standardbiblestyle{fullname}%
    {\ }{:}{; }{;}{,}{--}%
    {\brm@number@arabic}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{jerusalem}{\standardbiblestyle{jerusalem}%
    {\ }{:}{; }{; }{,}{--}%
    {\brm@number@arabic}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{anglosaxon}{\standardbiblestyle{anglosaxon}%
    {\ }{.}{; }{; }{,}{--}%
    {\brm@number@upperroman}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{JEH}{\standardbiblestyle{JEH}%
    {\ }{.~}{; }{; }{,}{--}%
    {\brm@number@lowerroman}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{MHRA}{\standardbiblestyle{MHRA}%
    {\ }{.~}{; }{; }{,}{--}%
    {\brm@number@lowerroman}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{NTG}{\standardbiblestyle{NTG}%
    {\ }{,}{; }{; }{,}{--}%
    {\brm@number@lowerroman}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{MLA}{\standardbiblestyle{MLA}%
    {\ }{.}{; }{; }{,}{--}%
    {\brm@number@lowerroman}{\brm@number@arabic}%
    {#1}{#2}{#3}}
\providebiblestyle{chicago}{\standardbiblestyle{chicago}%
    {\ }{\,:\,}{; }{; }{,}{--}%
    {\brm@number@lowerroman}{\brm@number@arabic}%
    {#1}{#2}{#3}}
%    \end{macrocode}
% 
% \subsection{Text Style}
% 
% The style that outputs a textual description of the reference is more complicated than the others.
% It needs to be defined here in its own section.
% 
% \emph{This style depends on commands that are not expandable, so this style is unique in that it may not be used in an expandable context.}
% It would be possible to implement this style in an expandable manner, by providing replacements for the \textsf{fmtcount} package's \cs{numberstringnum} and \cs{Ordinalstringnum} commands.
% The effort required would be large, however, and this style is less likely to be needed in an expandable context anyway.
% 
% \begin{macro}{\brm@number@stomach@text}
% This special number format spells out the number using its English word.
% \emph{This command relies on \cs{numberstringnum}, which is not expandable.}
%    \begin{macrocode}
\newcommand*{\brm@number@stomach@text}[1]{\numberstringnum{#1}}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@stomach@ordspace}
% This special number format spells out the number using its English ordinal word.
% This is useful for book numbers, such as \texttt{First}.
% \emph{This command relies on \cs{Ordinalstringnum}, which is not expandable.}
%    \begin{macrocode}
\newcommand*{\brm@number@stomach@ordspace}[1]{\Ordinalstringnum{#1}~}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@stomach@chapter}
% This special number format prefixes the text with the label ``chapter.''
% It delegates to \cs{brm@number@stomach@text}.
% \emph{This command relies on \cs{numberstringnum}, which is not expandable.}
%    \begin{macrocode}
\newcommand*{\brm@number@stomach@chapter}[1]{chapter\ %
    \brm@number@stomach@text{#1}}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@number@stomach@verse}
% This special number format prefixes the text with the label ``verse.''
% It delegates to \cs{brm@number@stomach@text}.
% \emph{This command relies on \cs{numberstringnum}, which is not expandable.}
%    \begin{macrocode}
\newcommand*{\brm@number@stomach@verse}[1]{verse\ %
    \brm@number@stomach@text{#1}}
%    \end{macrocode}
% \end{macro}
% 
% The style of book names spells out all the titles and uses English text instead of numerals.
%    \begin{macrocode}
\brm@foreachbook@full{\providebiblebook{text}}%
    {\ }%
    {Book of }{Gospel according to St.~}%
    {Epistle to }{Epistle to the }{Epistle of }%
    {\brm@number@stomach@ordspace}%
    {\brm@number@stomach@ordspace}
%    \end{macrocode}
% 
% The style of references spells out all the conjunctions and numbers.
%    \begin{macrocode}
\providebiblestyle{text}{\standardbiblestyle{text}%
    {,\ }{\ }{,\ and\ }{,\ and\ }{,\ and\ }{\ to\ }%
    {\brm@number@stomach@chapter}{\brm@number@stomach@verse}%
    {#1}{#2}{#3}}
%    \end{macrocode}
% 
% \subsection{Bible Gateway Style}
% 
% Since the parsing commands are all expandable, it is possible to parse a reference within a \cs{url} command.
% Then, the formatted output can be made to look like a valid URL rather than a human-readable style.
% So creating a link to a Bible reference uses all the same implementation machinery as the non-linked styles.
% The URL could be made to point to a variety of targets.
% This section illustrates how to create a link to \href{http://www.biblegateway.com/}{Bible Gateway}.
% The user may add other styles to link to any other sites.
% 
% First, the book names are defined.
% Bible Gateway uses the full names of the books and Arabic numerals.
% The ASCII version of the space character must be used, because a non-breaking space would not URL-encode to a space character correctly.
%    \begin{macrocode}
\brm@foreachbook@full{\providebiblebook{biblegateway.com}}%
    {\ }{}{}{}{}{}%
    {\brm@number@arabicspaceascii}{\brm@number@arabicspaceascii}
%    \end{macrocode}
% 
% \begin{macro}{\providebiblegatewayurl}
% The Bible Gateway site publishes \href{http://www.biblegateway.com/versions/}{many versions of the Bible}, (e.g. \href{http://www.biblegateway.com/versions/New-International-Version-NIV-Bible/}{NIV}).
% A user that wishes to have their preferred version embedded in the URL should use \verb|\providebiblegatewayurl|\-\marg{urlstylename}\-\marg{version}.
%    \begin{macrocode}
\newcommand*{\providebiblegatewayurl}[2]{%
    \providebiblestyle{#1}{%
        \brm@biblegatewayurl@style{#2}{##1}{##2}{##3}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@biblegatewayurl@style}
% The Bible Gateway URL is constructed by the style \verb|\brm@biblegatewayurl@style|\-\marg{version}\-\marg{beginpassage}\-\marg{endpassage}\-\marg{list}.
% Note that ampersands in the address are represented as \verb|\&| instead of \verb|&|, so it will still work in contexts where the ampersand is an active character.
% It uses the standard Bible style by calling \cs{brm@standard@filtered}, and it is allowed to specify a filter.
% The filter is \cs{brm@biblegatewayurl@filter}, which alters the standard format to meet the Bible Gateway requirements.
%    \begin{macrocode}
\newcommand*{\brm@biblegatewayurl@style}[4]{%
    http://www.biblegateway.com/passage/?search=%
    \brm@standard@filtered%
        {\brm@packseparators{ }{:}}%
        {\brm@packpassage{; }{; }{,}}%
        {\brm@packpassage{-}{-}{-}}%
        {\brm@packpassage%
            {\thebookname{biblegateway.com}}%
            {\brm@formatchapter{\brm@number@arabic}}%
            {\brm@formatverse{\brm@number@arabic}}}%
        {\brm@biblegatewayurl@filter{-}}%
        {#2}{#3}{#4}%
    \&version=#1%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\brm@biblegatewayurl@filter}
% A filter for standard formats is \verb|\brm@biblegatewayurl@filter|\-\marg{rangesep}\-\marg{lastpassage}\marg{passage}\-\marg{infixseparators}\-\marg{prefixes}\-\marg{formatters}\-\marg{delegate}.
% It makes sure that each new passage in a list contains the book name, not just the chapter or verse.
% This is because Bible Gateway sometimes behaves unexpectedly if a number is specified without a book name, and it is ambiguous whether the number represents a chapter or a verse.
% Ranges, (as opposed to lists), behave normally and are not altered.
%    \begin{macrocode}
\newcommand*{\brm@biblegatewayurl@filter}[7]{%
    \brm@ifsamestr{\theverse{#5}}{#1}{%
        % Show ranges like normal.
        #7{#2}{#3}{#4}{#5}{#6}%
    }{%
        % List the book explicitly for each list element.
        #7{\brm@packpassage{}{}{}}{#3}{#4}{#5}{#6}%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% A URL style that creates links for Bible Gateway references is defined.
% Users may define other styles for different versions of the Bible at Bible Gateway.
%    \begin{macrocode}
\providebiblegatewayurl{biblegateway.com-url}{NIV}
%    \end{macrocode}
% 
% \begin{macro}{\providebiblegatewaystyle}
% Different users might wish to define links for a variety of styles.
% For example, a user might wish to have either full text or abbreviated text being linked.
% To provide this flexibility, there is the \verb|\providebiblegatewaystyle|\-\marg{stylename}\-\marg{urlstylename}\marg{textstylename} command.
% The user first gives the name for this new style, and then gives the name of the existing style that will be used to format the link, and then finally the name of the existing style that will be used to format the linked text.
%    \begin{macrocode}
\newcommand*{\providebiblegatewaystyle}[3]{%
    \providebiblestyle{#1}{%
        \href{%
            \brm@delegatestyle{#2}{##1}{##2}{##3}%
        }{%
            \brm@delegatestyle{#3}{##1}{##2}{##3}%
        }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% A style is defined that uses a link style for the Bible Gatway NIV version and full names for the linked text.
% Users may define other styles for different link formats or different linked text formats.
%    \begin{macrocode}
\providebiblegatewaystyle{biblegateway.com}%
    {biblegateway.com-url}{fullname}
%    \end{macrocode}
% \Finale
\endinput