summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/xsim/code/xsim.environments.code.tex
diff options
context:
space:
mode:
Diffstat (limited to 'macros/latex/contrib/xsim/code/xsim.environments.code.tex')
-rw-r--r--macros/latex/contrib/xsim/code/xsim.environments.code.tex171
1 files changed, 142 insertions, 29 deletions
diff --git a/macros/latex/contrib/xsim/code/xsim.environments.code.tex b/macros/latex/contrib/xsim/code/xsim.environments.code.tex
index 49976aa53f..b9235ba555 100644
--- a/macros/latex/contrib/xsim/code/xsim.environments.code.tex
+++ b/macros/latex/contrib/xsim/code/xsim.environments.code.tex
@@ -29,6 +29,20 @@
\xsim_load_modules:n {templates,verbwrite}
+\xsim_if_write_to_files:F
+ {
+ \xsim_declare_property:nnnn
+ { \c_false_bool }
+ { \c_false_bool }
+ { \c_false_bool }
+ {solution-body}
+ \xsim_declare_property:nnnn
+ { \c_false_bool }
+ { \c_false_bool }
+ { \c_false_bool }
+ {exercise-body}
+ }
+
\tl_new:N \l__xsim_file_name_signature_tl
\tl_new:N \l__xsim_file_path_tl
\tl_new:N \l__xsim_file_path_and_name_tl
@@ -40,6 +54,9 @@
\tl_new:N \ExerciseText
\tl_set:Nn \ExerciseText { \msg_error:nn {xsim} {only-in-solution} }
+\cs_generate_variant:Nn \tl_set_rescan:Nnn {Nnx}
+\cs_generate_variant:Nn \prop_put:Nnn {Nnx}
+
% ----------------------------------------------------------------------------
\msg_new:nnn {xsim} {only-in-solution}
@@ -52,7 +69,7 @@
\keys_define:nn {xsim}
{
- path .tl_set:N = \l__xsim_file_path_tl ,
+ path .tl_set:N = \l__xsim_file_path_tl ,
file-extension .tl_set:N = \l__xsim_file_extension_tl ,
file-extension .initial:n = tex
}
@@ -139,11 +156,29 @@
\cs_generate_variant:Nn \__xsim_file_info:nnnnn {nnnVV}
% ----------------------------------------------------------------------------
+% #1: type
+% #2: id
+% #3: exercise|solution
+% #4: potential environment body
+\cs_new_protected:Npn \xsim_save_environment_body:nnnn #1#2#3#4
+ {
+ \xsim_if_write_to_files:TF
+ { \__xsim_write_environment_body:nnn {#1} {#2} {#3} }
+ { \__xsim_save_environment_body:nnnn {#1} {#2} {#3} {#4} }
+ }
+\cs_new_protected:Npn \xsim_save_environment_body_end:
+ {
+ \xsim_if_write_to_files:TF
+ { \__xsim_write_environment_body_end: }
+ { \__xsim_save_environment_body_end: }
+ }
+
+% writing to files:
% #1: type
% #2: id
% #3: exercise|solution
-\cs_new_protected:Npn \xsim_save_environment_body:nnn #1#2#3
+\cs_new_protected:Npn \__xsim_write_environment_body:nnn #1#2#3
{
\xsim_verbose:n
{ Starting~ to~ write~ #3~ environment~ type~ `#1'~ id~ `#2' }
@@ -157,9 +192,22 @@
\l__xsim_file_path_and_name_tl
}
-\cs_new_protected:Npn \xsim_save_environment_body_end:
+\cs_new_protected:Npn \__xsim_write_environment_body_end:
{ \xsim_file_write_stop: }
+% saving to property lists:
+% #1: type
+% #2: id
+% #3: exercise|solution
+% #4: environment body
+\cs_new_protected:Npn \__xsim_save_environment_body:nnnn #1#2#3#4
+ {
+ \xsim_verbose:n { Saving~ #3~ environment~ type~ `#1'~ id~ `#2' }
+ \xsim_set_property:nnnn {#1} {#2} {#3-body} {#4}
+ }
+
+\cs_new:Npn \__xsim_save_environment_body_end: { }
+
% ----------------------------------------------------------------------------
% #1: type
% #2: exercise|solution
@@ -206,9 +254,10 @@
% #1: type
% #2: id
% #3: exercise|solution
-\cs_new_protected:Npn \xsim_start_environment:nnn #1#2#3
- { \xsim_save_environment_body:nnn {#1} {#2} {#3} }
-\cs_generate_variant:Nn \xsim_start_environment:nnn {nV}
+% #4: potential enironment body
+\cs_new_protected:Npn \xsim_start_environment:nnnn #1#2#3#4
+ { \xsim_save_environment_body:nnnn {#1} {#2} {#3} {#4} }
+\cs_generate_variant:Nn \xsim_start_environment:nnnn {nV}
% #1: type
% #2: id
@@ -237,12 +286,16 @@
{begin}
{ \xsim_get_parameter:nn {#1} {#3-template} }
\xsim_use_hook:nnn {#1} {#3} {begin}
- \file_if_exist:nTF { \exp_not:V \l__xsim_file_path_and_name_tl }
- { \file_input:n { \exp_not:V \l__xsim_file_path_and_name_tl } }
+ \xsim_if_write_to_files:TF
{
- \xsim_verbose:n
- { File~ `\l__xsim_file_path_and_name_tl'~ doesn't~ exist. }
+ \file_if_exist:nTF { \exp_not:V \l__xsim_file_path_and_name_tl }
+ { \file_input:n { \exp_not:V \l__xsim_file_path_and_name_tl } }
+ {
+ \xsim_verbose:n
+ { File~ `\l__xsim_file_path_and_name_tl'~ doesn't~ exist. }
+ }
}
+ { \xsim_get_property:nnn {#1} {#2} {#3-body} }
\xsim_use_hook:nnn {#1} {#3} {end}
\xsim_use_template:nn
{end}
@@ -254,12 +307,34 @@
% #1: type
% #2: id
+% #3: exercise|solution
+\prg_new_protected_conditional:Npnn \xsim_if_exists:nnn #1#2#3 {T,F,TF}
+ {
+ \xsim_if_write_to_files:TF
+ {
+ \__xsim_set_file_signature:nnn {#1} {#2} {#3}
+ \file_if_exist:VTF \l__xsim_file_path_and_name_tl
+ { \prg_return_true: }
+ { \prg_return_false: }
+ }
+ {
+ \xsim_if_property_exist:nTF {#3-body}
+ {
+ \xsim_if_property_set:nnnTF {#1} {#2} {#3-body}
+ { \prg_return_true: }
+ { \prg_return_false: }
+ }
+ { \prg_return_false: }
+ }
+ }
+
+% #1: type
+% #2: id
% #3: options
% #4: exercise|solution
\cs_new_protected:Npn \xsim_insert:nnnn #1#2#3#4
{
- \__xsim_set_file_signature:nnn {#1} {#2} {#4}
- \file_if_exist:VTF \l__xsim_file_path_and_name_tl
+ \xsim_if_exists:nnnTF {#1} {#2} {#4}
{
\xsim_verbose:n
{ Inserting~ #4~ type~ `#1'~ id~ `#2'. }
@@ -294,9 +369,16 @@
}
\str_if_eq:nnT {#4} {solution}
{
- \__xsim_set_file_signature:nnn {#1} {#2} {exercise}
- \file_get:nnN {\l__xsim_file_path_and_name_tl} {} \ExerciseText
- \tl_trim_spaces:N \ExerciseText
+ \xsim_if_write_to_files:TF
+ {
+ \__xsim_set_file_signature:nnn {#1} {#2} {exercise}
+ \file_get:nnN {\l__xsim_file_path_and_name_tl} {} \ExerciseText
+ \tl_trim_spaces:N \ExerciseText
+ }
+ {
+ \tl_set:Nx \ExerciseText
+ { \xsim_get_property:nnn {#1} {#2} {exercise-body} }
+ }
}
\use:x
{
@@ -315,8 +397,11 @@
\group_end:
}
{
- \xsim_verbose:x
- { File~ `\l__xsim_file_path_and_name_tl'~ doesn't~ exist. }
+ \xsim_if_write_to_files:T
+ {
+ \xsim_verbose:x
+ { File~ `\l__xsim_file_path_and_name_tl'~ doesn't~ exist. }
+ }
}
}
\cs_generate_variant:Nn \xsim_insert:nnnn {nV,VV,nnnV}
@@ -327,8 +412,12 @@
\cs_new_protected:Npn \xsim_input:nnn #1#2#3
{
\group_begin:
- \__xsim_set_file_signature:nnn {#1} {#2} {#3}
- \file_input:n { \l__xsim_file_path_and_name_tl }
+ \xsim_if_write_to_files:TF
+ {
+ \__xsim_set_file_signature:nnn {#1} {#2} {#3}
+ \file_input:n { \l__xsim_file_path_and_name_tl }
+ }
+ { \xsim_get_property:nnn {#1} {#2} {#3-body} }
\group_end:
}
\cs_generate_variant:Nn \xsim_input:nnn {oo}
@@ -355,23 +444,46 @@
\xsim_new_hook:nnn {#1} {#2} {end}
\xsim_new_hook:nnn {#1} {#2} {post}
\xsim_new_hook:nnn {#1} {#2} {print-false}
- \NewDocumentEnvironment
- { \xsim_get_parameter:nn {#1} {#2-env} } {o}
+ \xsim_if_write_to_files:TF
{
- \IfNoValueTF {##1}
+ \NewDocumentEnvironment
+ { \xsim_get_parameter:nn {#1} {#2-env} } {!o}
{
- \bool_set_false:N \l__xsim_options_given_bool
- \use:c {xsim_start_#2:nn} {#1} {}
+ \IfNoValueTF {##1}
+ {
+ \bool_set_false:N \l__xsim_options_given_bool
+ \use:c {xsim_start_#2:nn} {#1} {}
+ }
+ {
+ \bool_set_true:N \l__xsim_options_given_bool
+ \use:c {xsim_start_#2:nn} {#1} {##1}
+ }
+ \xsim_start_environment:nVnn {#1} \g_xsim_exercise_id_tl {#2} { }
}
{
- \bool_set_true:N \l__xsim_options_given_bool
- \use:c {xsim_start_#2:nn} {#1} {##1}
+ \xsim_stop_environment:nVn {#1} \g_xsim_exercise_id_tl {#2}
+ \use:c {xsim_stop_#2:n} {#1}
}
- \xsim_start_environment:nVn {#1} \g_xsim_exercise_id_tl {#2}
}
{
- \xsim_stop_environment:nVn {#1} \g_xsim_exercise_id_tl {#2}
- \use:c {xsim_stop_#2:n} {#1}
+ \NewDocumentEnvironment
+ { \xsim_get_parameter:nn {#1} {#2-env} } {o+b}
+ {
+ \IfNoValueTF {##1}
+ {
+ \bool_set_false:N \l__xsim_options_given_bool
+ \use:c {xsim_start_#2:nn} {#1} {}
+ }
+ {
+ \bool_set_true:N \l__xsim_options_given_bool
+ \use:c {xsim_start_#2:nn} {#1} {##1}
+ }
+ \xsim_start_environment:nVnn {#1} \g_xsim_exercise_id_tl {#2} {##2}
+ }
+ {
+ \xsim_stop_environment:nVn {#1} \g_xsim_exercise_id_tl {#2}
+ \use:c {xsim_stop_#2:n} {#1}
+ }
}
}
@@ -379,3 +491,4 @@
\file_input_stop:
2019/02/17 - fix issue #43
+2019/10/03 - add possibility to save environment bodies to property list