summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/ducksay/ducksay.sty
blob: 98d096c4175740827e979a8b32ef4637454ca6dc (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
%%
%% This is file `ducksay.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% ducksay.dtx  (with options: `pkg')
%% 
%% --------------------------------------------------------------
%% ducksay -- cowsay for LaTeX
%% E-mail: jspratte@yahoo.de
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% --------------------------------------------------------------
%% 
%% Copyright (C) 2017-2019 Jonathan P. Spratte
%% 
%% This  work may be  distributed and/or  modified under  the conditions  of the
%% LaTeX Project Public License (LPPL),  either version 1.3c  of this license or
%% (at your option) any later version.  The latest version of this license is in
%% the file:
%% 
%%   http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status) by
%%   Jonathan P. Spratte.
%% 
%% This work consists of the file  ducksay.dtx
%% and the derived files           ducksay.pdf
%%                                 ducksay.sty
%%                                 ducksay.code.v1.tex
%%                                 ducksay.code.v2.tex and
%%                                 ducksay.animals.tex.
%% 
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{xparse,l3keys2e}

\def\ducksay@version{2.3}
\def\ducksay@date{2019-01-13}

\ProvidesExplPackage
  {ducksay}           {\ducksay@date}
  {\ducksay@version}  {cowsay for LaTeX}

\int_new:N \l_ducksay_msg_width_int
\int_new:N \l_ducksay_msg_height_int
\int_new:N \l_ducksay_tail_symbol_count_int
\seq_new:N \l_ducksay_msg_lines_seq
\tl_new:N \l_ducksay_align_tl
\tl_new:N \l_ducksay_msg_align_tl
\tl_new:N \l_ducksay_animal_tl
\tl_new:N \l_ducksay_body_tl
\tl_new:N \l_ducksay_bubble_tl
\tl_new:N \l_ducksay_tmpa_tl
\tl_new:N \l_ducksay_tail_symbol_out_one_tl
\tl_new:N \l_ducksay_tail_symbol_out_two_tl
\tl_new:N \l_ducksay_tail_symbol_in_tl
\bool_new:N \l_ducksay_version_one_bool
\bool_new:N \l_ducksay_version_two_bool
\box_new:N \l_ducksay_tmpa_box
\regex_const:Nn \c_ducksay_textcolor_regex
  { \cO(?:\\textcolor\{(.*?)\}\{(.*?)\}) }
\regex_const:Nn \c_ducksay_color_delim_regex
  { \cO(?:\\bgroup\\color\{(.*?)\}(.*)\\egroup) }
\regex_const:Nn \c_ducksay_color_regex
  { \cO(?:\\color\{(.*?)\}) }
\msg_new:nnn { ducksay } { load-time-only }
  { The~`#1`~key~is~to~be~used~only~during~package~load~time. }
\msg_new:nnn { ducksay } { deprecated-key }
  { The~`\l_keys_key_tl`~key~is~deprecated.~Sorry~for~the~inconvenience. }
\keys_define:nn { ducksay }
  {
    ,bubble .tl_set:N      = \l_ducksay_bubble_tl
    ,body   .tl_set:N      = \l_ducksay_body_tl
    ,align  .tl_set:N      = \l_ducksay_align_tl
    ,align  .value_required:n = true
    ,wd     .int_set:N     = \l_ducksay_msg_width_int
    ,wd     .initial:n     = -\c_max_int
    ,wd     .value_required:n = true
    ,ht     .int_set:N     = \l_ducksay_msg_height_int
    ,ht     .initial:n     = -\c_max_int
    ,ht     .value_required:n = true
    ,animal .code:n        =
      { \keys_define:nn { ducksay } { default_animal .meta:n = { #1 } } }
    ,animal .initial:n     = duck
    ,msg-align .tl_set:N   = \l_ducksay_msg_align_tl
    ,msg-align .initial:n  = l
    ,msg-align .value_required:n = true
    ,rel-align .tl_set:N   = \l_ducksay_rel_align_tl
    ,rel-align .initial:n  = l
    ,rel-align .value_required:n = true
    ,ligatures .tl_set:N   = \l_ducksay_ligatures_tl
    ,ligatures .initial:n  = { `<>,'- }
    ,tail-1    .tl_set:N   = \l_ducksay_tail_symbol_out_one_tl
    ,tail-1    .initial:x  = \c_backslash_str
    ,tail-2    .tl_set:N   = \l_ducksay_tail_symbol_out_two_tl
    ,tail-2    .initial:x  = \c_backslash_str
    ,no-tail   .meta:n     = { tail-1 = { ~ }, tail-2 = { ~ } }
    ,think     .meta:n     = { tail-1 = { O }, tail-2 = { o } }
    ,say       .code:n     =
      {
        \exp_args:Nx \DucksayOptions
          { tail-1 = { \c_backslash_str }, tail-2 = { \c_backslash_str } }
      }
    ,version   .choice:
    ,version / 1 .code:n   =
      {
        \bool_set_false:N \l_ducksay_version_two_bool
        \bool_set_true:N  \l_ducksay_version_one_bool
      }
    ,version / 2 .code:n   =
      {
        \bool_set_false:N \l_ducksay_version_one_bool
        \bool_set_true:N  \l_ducksay_version_two_bool
      }
    ,version   .initial:n  = 2
    ,add-think .code:n     = \msg_error:nn { ducksay } { deprecated-key }
  }
\ProcessKeysOptions { ducksay }
\keys_define:nn { ducksay }
  {
    version .code:n = \msg_error:nnn { ducksay } { load-time-only } { version }
  }
\keys_define:nn { ducksay / add-animal }
  {
    ,tail-symbol .code:n    =
      \tl_set:Nx \l_ducksay_tail_symbol_in_tl { \tl_to_str:n { #1 } }
    ,tail-symbol .initial:o = \c_backslash_str
    ,tail-count  .int_set:N = \l_ducksay_tail_symbol_count_int
    ,tail-count  .initial:n = 2
  }
\cs_generate_variant:Nn \tl_replace_once:Nnn { NVn }
\cs_generate_variant:Nn \tl_replace_all:Nnn { NVn }
\cs_new_protected:Npx \ducksay_replace_verb_newline:Nn #1 #2
  {
    \tl_replace_all:Nnn #1 { \char_generate:nn { 13 } { 12 } } { #2 }
  }
\cs_new_protected:Npx \ducksay_replace_verb_newline_newline:Nn #1 #2
  {
    \tl_replace_all:Nnn #1
      { \char_generate:nn { 13 } { 12 } \char_generate:nn { 13 } { 12 } } { #2 }
  }
\cs_new_protected:Npn \ducksay_process_verb_newline:nnn #1 #2 #3
  {
    \tl_set:Nn \ProcessedArgument { #3 }
    \ducksay_replace_verb_newline_newline:Nn \ProcessedArgument { #2 }
    \ducksay_replace_verb_newline:Nn \ProcessedArgument { #1 }
  }
\cs_new_protected:Npn \ducksay_add_animal_inner:nnnn #1 #2 #3 #4
  {
    \group_begin:
      \keys_set:nn { ducksay / add-animal } { #1 }
      \tl_set:Nn \l_ducksay_tmpa_tl { \ #3 }
      \int_compare:nNnTF { \l_ducksay_tail_symbol_count_int } < { \c_zero_int }
        {
          \tl_replace_once:NVn
            \l_ducksay_tmpa_tl
            \l_ducksay_tail_symbol_in_tl
            \l_ducksay_tail_symbol_out_one_tl
          \tl_replace_all:NVn
            \l_ducksay_tmpa_tl
            \l_ducksay_tail_symbol_in_tl
            \l_ducksay_tail_symbol_out_two_tl
        }
        {
          \int_compare:nNnT { \l_ducksay_tail_symbol_count_int } >
            { \c_zero_int }
            {
              \tl_replace_once:NVn
                \l_ducksay_tmpa_tl
                \l_ducksay_tail_symbol_in_tl
                \l_ducksay_tail_symbol_out_one_tl
              \int_step_inline:nnn { 2 } { \l_ducksay_tail_symbol_count_int }
                {
                  \tl_replace_once:NVn
                    \l_ducksay_tmpa_tl
                    \l_ducksay_tail_symbol_in_tl
                    \l_ducksay_tail_symbol_out_two_tl
                }
            }
        }
      \tl_map_inline:Nn \l_ducksay_ligatures_tl
        { \tl_replace_all:Nnn \l_ducksay_tmpa_tl { ##1 } { { ##1 } } }
      \ducksay_replace_verb_newline:Nn \l_ducksay_tmpa_tl
        { \tabularnewline\null }
      \exp_args:NNnV
    \group_end:
    \tl_set:cn { l_ducksay_animal_#2_tl } \l_ducksay_tmpa_tl
    \exp_args:Nnx \keys_define:nn { ducksay }
      {
        #2 .code:n =
          {
            \exp_not:n { \tl_set_eq:NN \l_ducksay_animal_tl }
            \exp_after:wN \exp_not:N \cs:w l_ducksay_animal_#2_tl \cs_end:
            \exp_not:n { \exp_args:NV \DucksayOptions }
            \exp_after:wN
            \exp_not:N \cs:w l_ducksay_animal_#2_options_tl \cs_end:
          }
      }
    \tl_if_exist:cF { l_ducksay_animal_#2_options_tl }
      { \tl_new:c { l_ducksay_animal_#2_options_tl } }
    \IfBooleanT { #4 }
      { \keys_define:nn { ducksay } { default_animal .meta:n = { #2 } } }
  }
\cs_generate_variant:Nn \ducksay_add_animal_inner:nnnn { nnVn }
\NewDocumentCommand \DefaultAnimal { m }
  {
    \keys_define:nn { ducksay } { default_animal .meta:n = { #1 } }
  }
\NewDocumentCommand \DucksayOptions { m }
  {
    \keys_set:nn { ducksay } { #1 }
  }
\NewDocumentCommand \AddAnimal { s O{} m +v }
  {
    \ducksay_add_animal_inner:nnnn { #2 } { #3 } { #4 } { #1 }
  }
\NewDocumentCommand \AddColoredAnimal { s O{} m +v }
  {
    \tl_set:Nn \l_ducksay_tmpa_tl { #4 }
    \regex_replace_all:NnN \c_ducksay_color_delim_regex
      { \c{bgroup}\c{color}\cB\{\1\cE\}\2\c{egroup} }
      \l_ducksay_tmpa_tl
    \regex_replace_all:NnN \c_ducksay_color_regex
      { \c{color}\cB\{\1\cE\} }
      \l_ducksay_tmpa_tl
    \regex_replace_all:NnN \c_ducksay_textcolor_regex
      { \c{textcolor}\cB\{\1\cE\}\cB\{\2\cE\} }
      \l_ducksay_tmpa_tl
    \ducksay_add_animal_inner:nnVn { #2 } { #3 } \l_ducksay_tmpa_tl { #1 }
  }
\NewDocumentCommand \AnimalOptions { s m m }
  {
    \tl_if_exist:cTF { l_ducksay_animal_#2_options_tl }
      {
        \IfBooleanTF { #1 }
          { \tl_set:cn }
          { \tl_put_right:cn }
      }
      { \tl_set:cn }
    { l_ducksay_animal_#2_options_tl } { #3, }
  }
\bool_if:NT \l_ducksay_version_one_bool
  { \file_input:n { ducksay.code.v1.tex } }
\bool_if:NT \l_ducksay_version_two_bool
  { \file_input:n { ducksay.code.v2.tex } }
\ExplSyntaxOff
\input{ducksay.animals.tex}
%% 
%%
%% End of file `ducksay.sty'.