summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/xsim/xsim.verbwrite.code.tex
blob: 748550657d5eeff769976d7dfa034f694a920f92 (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
% ----------------------------------------------------------------------------
% the XSIM package - verbwrite module
% 
%   eXercise Sheets IMproved
% 
% ----------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://github.com/cgnieder/xsim
% E-Mail: contact@mychemistry.eu
% ----------------------------------------------------------------------------
% Copyright 2017 Clemens Niederberger
% 
% 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.
% 
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Clemens Niederberger.
% ----------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% ----------------------------------------------------------------------------
\XSIMmodule{verbwrite}{write contents of environments verbatim to files}

\xsim_load_modules:n {base}

\iow_new:N  \l__file_contents_iow
\tl_new:N   \l_xsim_file_begin_tl
\tl_new:N   \l_xsim_file_end_tl
\int_new:N  \l_xsim_line_gobble_int
\int_zero:N \l_xsim_line_gobble_int

\tl_const:Nx \c__xsim_backslash_char_tl { \cs_to_str:N \\ }

\group_begin:
  \char_set_catcode_other:n {37}
  \tl_const:Nn \c__xsim_percent_char_tl {%}
\group_end:

% ----------------------------------------------------------------------------

% the `final' option will prevent writing to files:
% #1: write stream
% #2: file name
\cs_new_protected:Npn \__xsim_open_stream:Nn #1#2
  {
    \file_if_exist:nTF {#2}
      { \xsim_if_final:F { \iow_open:Nn #1 {#2} } }
      {
        % \g_xsim_final_bool is a global variable; however, if it is true and
        % the exercise file doesn't exist, yet, we need to set it locally to
        % false in order to ensure that the file is correctly written and the
        % stream closed afterwards
        \bool_set_false:N \g_xsim_final_bool
        \iow_open:Nn #1 {#2}
      }
  }

% #1: write stream
\cs_new_protected:Npn \__xsim_close_stream:N #1
  { \xsim_if_final:F { \iow_close:N #1 } }

% #1: write stream
% #1: contents
\cs_new_protected:Npn \__xsim_write_to_stream:Nn #1#2
  {
    \xsim_if_final:F
      {
        % remove the /one/ space token with catcode 10 that is inserted if
        % no options are given to the surrounding environment:
        \tl_set:Nn \l__xsim_tmpa_tl {#2}
        \tl_remove_once:Nn \l__xsim_tmpa_tl {~}
        \int_zero:N \l__xsim_tmpa_int
        \int_while_do:nn { \l__xsim_tmpa_int < \l_xsim_line_gobble_int }
          {
            \int_incr:N \l__xsim_tmpa_int
            \tl_set:Nx \l__xsim_tmpa_tl { \tl_tail:N \l__xsim_tmpa_tl }
          }
        \iow_now:NV #1 \l__xsim_tmpa_tl
      }
  }
\cs_generate_variant:Nn \__xsim_write_to_stream:Nn {Nx}

\cs_new:Npn \__xsim_tab: { \c_space_tl \c_space_tl }
\cs_new:Npn \__xsim_par: { ^^J ^^J }

% the following is inspired by the definition of the `filecontents'
% environment:
% #1: boolean - if true an active eol needs to be inserted before
%               starting to write
% #2: file name
\cs_new_protected:Npn \xsim_file_write_start:nn #1#2
  {
    % we need to insert an active ^^M if no options are given
    % see http://tex.stackexchange.com/q/9035/5049 reasons
    \use:nx
      { \__xsim_file_write_start:n {#2} }
      { \bool_if:nF {#1} { \c__xsim_active_eol_tl } }
  }
\cs_generate_variant:Nn \xsim_file_write_start:nn {nV}

\group_begin:
\char_set_catcode_active:n {13} % ^^M (carriage return, endlinechar)
\char_set_catcode_active:n {12} % ^^L (form feed)
\char_set_catcode_active:n {9}  % ^^I (horizontal tab)
%
\tl_const:Nn \c__xsim_active_eol_tl {^^M} %
%
% #1: file name
\cs_new_protected:Npn \__xsim_file_write_start:n #1 %
  { %
    \group_begin: %
    \xsim_if_final:TF %
      { \xsim_verbose:x { Not~ (re-)writing~ file~ `#1'. } } %
      { \xsim_verbose:x { (Re-)writing~ file~ `#1'. } } %
    \__xsim_open_stream:Nn \l__file_contents_iow {#1} %
    \tl_if_blank:VF \l_xsim_file_begin_tl %
      { %
        \xsim_if_final:F %
          { %
            \iow_now:Nx \l__file_contents_iow  %
              { \l_xsim_file_begin_tl } %
          } %
      } %
    \seq_map_inline:Nn \l_char_special_seq %
      { \char_set_catcode_other:N ##1 } %
    \int_step_inline:nnnn {128} {1} {255} %
      { \char_set_catcode_letter:n {##1} } %
    \tl_set:Nx \l__xsim_tmpa_tl %
      { \c__xsim_backslash_char_tl end \cs_to_str:N \{ \@currenvir \cs_to_str:N \} } %
    \use:x %
      { %
        \cs_set:cpn {__xsim_tmpa:www} %
          ####1 \l__xsim_tmpa_tl %
          ####2 \l__xsim_tmpa_tl %
          ####3 \exp_not:N \q_stop: %
      } %
        { %
          \tl_if_blank:nTF {##3} %
            { \__xsim_write_to_stream:Nn \l__file_contents_iow {##1} } %
            { %
              \cs_set:Npx \__xsim_M:w { \exp_not:N \end {\@currenvir} } %
              \char_set_active_eq:nN {13} \__xsim_M:w %
              % the last line is `##1 \end{\@currenvir} ##2':
              % \tl_if_blank:nF {##1} %
              %   { \__xsim_write_to_stream:Nn \l__file_contents_iow {##1} }%
              % \tl_if_blank:nF {##2} %
              %   {}%
            } %
          ^^M %
        } %
    \char_set_catcode_active:n {13} %
    \char_set_catcode_active:n {12} %
    \char_set_catcode_active:n {9} %
    \cs_set:Npx \__xsim_M:w ##1 ^^M %
      { %
        \exp_not:N \__xsim_tmpa:www ##1 %
        \l__xsim_tmpa_tl %
        \l__xsim_tmpa_tl %
        \exp_not:N \q_stop: %
      } %
    \char_set_active_eq:nN {13} \__xsim_M:w %
    \char_set_active_eq:nN {12} \__xsim_par: %
    \char_set_active_eq:nN {9}  \__xsim_tab: %
  } %
\group_end:

\cs_new_protected:Npn \xsim_file_write_stop:
  {
    \tl_if_blank:VF \l_xsim_file_end_tl
      {
        \xsim_if_final:F
          {
            \iow_now:Nx \l__file_contents_iow
              { \l_xsim_file_end_tl }
          }
      }
    \__xsim_close_stream:N \l__file_contents_iow
    \group_end:
  }

% ----------------------------------------------------------------------------

\NewDocumentCommand \XSIMfilewritestart {sm}
  {
    \IfBooleanTF {#1}
      { \xsim_file_write_start:nn { \c_false_bool } {#2} }
      { \xsim_file_write_start:nn { \c_true_bool } {#2} }
  }

\NewDocumentCommand \XSIMfilewritestop {}
  { \xsim_file_write_stop: }

\NewDocumentCommand \XSIMsetfilebegin {+m}
  { \tl_set:Nn \l_xsim_file_begin_tl {#1} }

\NewDocumentCommand \XSIMsetfileend {+m}
  { \tl_set:Nn \l_xsim_file_end_tl {#1} }

\NewDocumentCommand \XSIMgobblechars {m}
  { \int_set:Nn \l_xsim_line_gobble_int {#1} }
  
% ----------------------------------------------------------------------------
\file_input_stop: