summaryrefslogtreecommitdiff
path: root/biblio/citation-style-language/citation-style-language.sty
blob: af8a8ea903e551c8c6fab2c1fa7f4fbe61a38193 (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
%
% Copyright (c) 2021-2023 Zeping Lee
% Released under the MIT License.
% Repository: https://github.com/zepinglee/citeproc-lua
%

\NeedsTeXFormat{LaTeX2e}

\RequirePackage{expl3}
\RequirePackage{xparse}

\ProvidesExplPackage {citation-style-language} {2023-10-05} {0.4.5}
  {Citation Style Language for LaTeX}

\RequirePackage { l3keys2e }
\RequirePackage { url }


% Check incompatible packages
\msg_new:nnn { citation-style-language } { incompatible-package }
  { The~ `#1'~ package~ is~ incompatible~ with~ `citation-style-language'. }

\clist_map_inline:nn
  {
    babelbib , biblatex , bibtopic , bibunits , chapterbib ,
    cite , citeref , inlinebib , jurabib , mcite , mciteplus ,
    multibib , natbib , splitbib
  }
  {
    \hook_gput_code:nnn { package / #1 / before } { . }
      { \msg_error:nnn { citation-style-language } { incompatible-package } {#1} }
  }


% Load Lua module in LuaTeX
\sys_if_engine_luatex:T
  { \lua_now:n { csl = require("citeproc-latex") } }


% Global options

\NewDocumentCommand \cslsetup { m }
  { \keys_set:nn { csl } {#1} }


\bool_new:N \l__csl_regression_test_bool
\tl_new:N \l__csl_style_tl
\newcommand \csl@style {}  % For latex2e interface
\tl_new:N \l__csl_class_tl  % TODO: This should be removed
\bool_new:N \l__csl_note_bool
\clist_new:N \l__csl_bib_resources_clist
\tl_new:N \l__csl_locale_tl
\bool_new:N \l__csl_back_ref_bool
\str_new:N \l__csl_back_ref_type_str
% Citation formatting
\tl_new:N \l__csl_prefix_separator_tl
\tl_new:N \l__csl_suffix_separator_tl
% Bibliography formatting
\tl_new:N \bibsection
\tl_new:N \l__csl_bib_font_tl
\bool_new:N \l__csl_bib_entry_page_break_bool
\tl_new:N \l__csl_bib_item_sep_tl
\tl_new:N \l__csl_bib_hang_tl


\keys_define:nn { csl }
  {
    regression-test .bool_set:N = \l__csl_regression_test_bool ,
    style .code:n =
      {
        \tl_set:Nn \l__csl_style_tl {#1}
        \renewcommand \csl@style {#1}
      } ,
    class .code:n =
      {
        \tl_set:Nn \l__csl_class_tl {#1}
        \tl_if_eq:NnT \l__csl_class_tl { note }
          { \bool_set_true:N \l__csl_note_bool }
      } ,
    locale .tl_set:N = \l__csl_locale_tl ,
    backref .choices:nn =
      { true, page, section, false }
      { \exp_args:NV \__csl_set_back_ref:n \l_keys_choice_tl } ,
    % Citation formatting
    prefix-separator .tl_set:N = \l__csl_prefix_separator_tl ,
    suffix-separator .tl_set:N = \l__csl_suffix_separator_tl ,
    % Bibliography formatting
    bib-section .tl_set:N = \bibsection ,
    bib-font .tl_set:N = \l__csl_bib_font_tl ,
    bib-entry-page-break .bool_set:N = \l__csl_bib_entry_page_break_bool ,
    bib-item-sep .tl_set:N = \l__csl_bib_item_sep_tl ,
    bib-hang .tl_set:N = \l__csl_bib_hang_tl ,
    % The following keys are for easier interaction between LaTeX and
    % citeproc-lua engine and they are not designed for end-users.
    undefined-cites .code:n =
      {
        \clist_map_inline:nn {#1}
          { \__csl_warn_citation_undefined:n {##1} }
      } ,
  }

\cs_new:Npn \__csl_set_back_ref:n #1
  {
    \str_if_eq:nnTF {#1} {false}
      {
        \bool_set_false:N \l__csl_back_ref_bool
      }
      {
        \bool_set_true:N \l__csl_back_ref_bool
        \str_if_eq:nnTF {#1} {true}
          { \str_set:Nn \l__csl_back_ref_type_str { page } }
          { \str_set:Nn \l__csl_back_ref_type_str {#1} }
      }
  }

% Default settings
\keys_set:nn { csl }
  {
    backref = false ,
    prefix-separator = { ~ } ,
    suffix-separator = { , ~ } ,
    bib-entry-page-break = true ,
    bib-hang = { 1 em } ,
  }


\ProcessKeysPackageOptions { csl }


\DeclareDocumentCommand \bibliographystyle { m }
  {
    \__csl_if_preamble:F
      { \__csl_write_aux_bibstyle:n {#1} }
    \tl_set:Nn \l__csl_style_tl {#1}
  }

\tl_new:N \g__csl_aux_bibstyle_tl
\cs_set:Npn \csl@aux@style #1
  { \tl_gset:Nn \g__csl_aux_bibstyle_tl {#1} }


% Write to an empty `.ccf` file for use with latexmk
\iow_new:N \l__csl_ccf_iow
\iow_open:Nn \l__csl_ccf_iow { \c_sys_jobname_str .ccf }
\iow_now:Nx \l__csl_ccf_iow { }
\iow_close:N \l__csl_ccf_iow


% Load other modules
\input{citation-style-language-init.sty}
\input{citation-style-language-data.sty}
\input{citation-style-language-cite.sty}
\input{citation-style-language-bib.sty}
\input{citation-style-language-compatible.sty}


\endinput