summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/codehigh/codehigh.sty
diff options
context:
space:
mode:
Diffstat (limited to 'macros/latex/contrib/codehigh/codehigh.sty')
-rw-r--r--macros/latex/contrib/codehigh/codehigh.sty155
1 files changed, 123 insertions, 32 deletions
diff --git a/macros/latex/contrib/codehigh/codehigh.sty b/macros/latex/contrib/codehigh/codehigh.sty
index a075bd1b33..d9bfaec0e7 100644
--- a/macros/latex/contrib/codehigh/codehigh.sty
+++ b/macros/latex/contrib/codehigh/codehigh.sty
@@ -12,7 +12,7 @@
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
-\ProvidesExplPackage{codehigh}{2022-02-07}{2022D}
+\ProvidesExplPackage{codehigh}{2022-04-29}{2022E}
{Highlight codes and demos with l3regex and lpeg}
%\RequirePackage{xparse}
@@ -21,9 +21,8 @@
\RequirePackage{xcolor}
\RequirePackage{ninecolors}
\RequirePackage{varwidth}
-\ifluatex
- \RequirePackage{luatexbase}
-\fi
+\RequirePackage{iftex}
+\legacy_if:nT {LuaTeX} {\RequirePackage{luatexbase}}
\int_new:N \l__codehigh_a_int
\int_new:N \l__codehigh_b_int
@@ -36,6 +35,8 @@
\cs_generate_variant:Nn \regex_set:Nn {cn}
\cs_generate_variant:Nn \seq_set_split:Nnn {NVV}
\cs_generate_variant:Nn \str_remove_once:Nn {NV}
+\cs_generate_variant:Nn \tl_log:n {e}
+\cs_generate_variant:Nn \tl_set:Nn {Ne}
\cs_generate_variant:Nn \tl_set_rescan:Nnn {NnV}
\prg_generate_conditional_variant:Nnn \str_if_eq:nn {en} {T, TF}
@@ -69,41 +70,76 @@
}
%%% -------------------------------------------------------
-%%> \section{CodeHigh Environments and Commands}
+%%> \section{Environments and Commands}
%%% -------------------------------------------------------
+%% Since every codehigh environment has an optional argument,
+%% we need to make ^^M active first to keep the leading spaces in the first line
\NewDocumentCommand \NewCodeHighEnv {mm}
{
- \NewDocumentEnvironment {#1} {O{}}
+ \exp_args:Nc \NewDocumentCommand {#1} {}
{
- \keys_set:nn {codehigh} {#2, ##1}
- \bgroup
- \__codehigh_do_specials:
- \begin{codehigh@aux}
+ %% it is \^^M but not `\^^M
+ \char_set_catcode_active:N \^^M
+ \__codehigh_begin:nw {#2}
+ }
+ \exp_args:Nc \NewDocumentCommand {end#1} {}
+ {
+ \__codehigh_end:
+ }
+ }
+
+%% Now we need to replace each ^^M with a space character
+\NewDocumentCommand \__codehigh_begin:nw {mO{}}
+ {
+ \char_set_catcode_end_line:N \^^M
+ \tl_set:Nn \l_tmpa_tl {#1,#2}
+ \__codehigh_replace_endlines:N \l_tmpa_tl
+ \keys_set:nV {codehigh} \l_tmpa_tl
+ \tl_set:Ne \__codehigh_collect_end_tl
+ {
+ \c_backslash_str end \c_left_brace_str \@currenvir \c_right_brace_str
}
+ \exp_last_unbraced:NNNNo
+ \cs_set:Npn \__codehigh_collect:w ##1 \__codehigh_collect_end_tl
{
- \end{codehigh@aux}
- \egroup
- \__codehigh_typeset:
+ \__codehigh_store:n {##1}
+ \exp_args:No \end \@currenvir
}
+ \group_begin:
+ \__codehigh_do_specials:
+ \__codehigh_collect:w
}
+\cs_new_protected:Npn \__codehigh_end:
+ {
+ \group_end:
+ \__codehigh_typeset:
+ }
+
+\group_begin:
+ \char_set_catcode_active:N \^^M
+ \cs_new_protected:Npn \__codehigh_replace_endlines:N #1
+ {
+ \tl_replace_all:Nnn #1 {^^M} {~}
+ }
+\group_end:
+
\cs_new_protected:Npn \__codehigh_do_specials:
{
+ \cs_set_eq:NN \do \char_set_catcode_other:N
+ \dospecials
\obeylines
\obeyspaces
- \catcode `\% = 12
- \catcode `\^ = 12
}
\tl_new:N \g__codehigh_code_tl
-\NewDocumentEnvironment {codehigh@aux} {+b}
+\cs_new_protected:Npn \__codehigh_store:n #1
{
\tl_gset:Nn \g__codehigh_code_tl { #1 }
- %\tl_log:N \g__codehigh_code_tl
+ \__codehigh_tracing:nn {code} {\__codehigh_log:N \g__codehigh_code_tl}
}
- { }
\cs_new_protected:Npn \__codehigh_typeset:
{
@@ -124,6 +160,7 @@
\group_begin:
\keys_set:nn {codehigh} {#2, ##1}
\CatchFileDef \l__codehigh_input_tl {##2} {\__codehigh_do_specials:}
+ \setlength \parskip {0pt plus 1pt}
\__codehigh_typeset_input:N \l__codehigh_input_tl
\group_end:
}
@@ -145,7 +182,7 @@
}
}
-\regex_const:Nn \l__codehigh_comment_regex { ^ \% \% ( [\%>] ) ( [^\r]+ ) [\r] }
+\regex_const:Nn \l__codehigh_comment_regex { ^ \% \% ( [\%>] ) ( [^\r]+ ) \r }
\tl_new:N \l__codehigh_comment_tl % comment lines that need to be typeset
\bool_new:N \l__codehigh_comment_bool
@@ -223,6 +260,7 @@
\regex_replace_once:nnN {\r $} {} #1
\regex_replace_all:nnN { . } { \c{string} \0 } #1
\tl_set:Nx #1 { #1 }
+ \__codehigh_tracing:nn {code} {\__codehigh_log:N #1}
}
\cs_new_protected:Npn \__codehigh_put_code_box:
@@ -275,7 +313,8 @@
\__codehigh_build_demo:
\dim_set:Nn \l_tmpa_dim { \box_wd:N \g__codehigh_code_box }
\dim_set:Nn \l_tmpb_dim { \box_wd:N \g__codehigh_demo_box }
- %\tl_log:x { \dim_use:N \l_tmpa_dim + \dim_use:N \l_tmpb_dim }
+ \__codehigh_tracing:nn {demo}
+ { \tl_log:x { \dim_use:N \l_tmpa_dim + \dim_use:N \l_tmpb_dim } }
\par\addvspace{0.5em}\noindent
\setlength \fboxsep {\l__codehigh_main_boxsep_dim}
\GetCodeHighStyle{main}
@@ -316,7 +355,7 @@
\catcode `\^^M = 10 \relax
}
\l_tmpb_tl
- %\tl_log:N \l_tmpb_tl
+ \__codehigh_tracing:nn {demo} { \tl_log:N \l_tmpb_tl }
\__codehigh_get_demo_box:nN \l_tmpb_tl \g__codehigh_demo_box
}
@@ -455,11 +494,9 @@
\cs_new_protected:Npn \__codehigh_parse_code:nN #1 #2
{
- \ifluatex
- \__codehigh_parse_code_luatex:nN {#1} #2
- \else
- \__codehigh_parse_code_normal:nN {#1} #2
- \fi
+ \legacy_if:nTF {LuaTeX}
+ { \__codehigh_parse_code_luatex:nN {#1} #2 }
+ { \__codehigh_parse_code_normal:nN {#1} #2 }
}
\cs_generate_variant:Nn \__codehigh_parse_code:nN {VN}
@@ -478,7 +515,8 @@
\tl_concat:NNN \l__codehigh_a_tl
\l__codehigh_regex_before_text_tl \l__codehigh_regex_match_text_tl
\str_remove_once:NV \l__codehigh_code_to_parse_tl \l__codehigh_a_tl
- %\tl_log:N \l__codehigh_code_to_parse_tl
+ \__codehigh_tracing:nn {parser}
+ {\tl_log:N \l__codehigh_code_to_parse_tl}
\__codehigh_typeset_text:nN {0}
\l__codehigh_regex_before_text_tl
\__codehigh_typeset_text:VN \l__codehigh_regex_match_type_tl
@@ -521,12 +559,13 @@
}
}
-\ifluatex \directlua{require("codehigh.lua")} \fi
+\legacy_if:nT {LuaTeX} { \directlua{require("codehigh.lua")} }
\cs_new_protected:Npn \__codehigh_parse_code_luatex:nN #1 #2
{
\directlua{ParseCode(token.scan_argument(), token.scan_argument())}{#1}{#2}
- %\tl_log:N \l__codehigh_parse_code_count_tl
+ \__codehigh_tracing:nn {parser}
+ {\tl_log:N \l__codehigh_parse_code_count_tl}
\int_step_inline:nn {\l__codehigh_parse_code_count_tl}
{
\__codehigh_typeset_text:vc
@@ -537,11 +576,11 @@
%% #1: rule type, #2: text
\cs_new_protected:Npn \__codehigh_typeset_text:nN #1 #2
{
+ \__codehigh_tracing:nn {parser} { \tl_log:e { type: #1; ~ text: #2 } }
\group_begin:
\regex_replace_all:nnN { \r } { \c{par} \c{leavevmode} } #2
- \ifluatex\else
- \regex_replace_all:nnN { \ } { \c{relax} \c{space} } #2
- \fi
+ \legacy_if:nF {LuaTeX}
+ { \regex_replace_all:nnN { \ } { \c{leavevmode} \c{space} } #2 }
\color{\tl_use:c {l__codehigh_style_ \l__codehigh_theme_name_tl _#1_tl}}
%\obeyspaces
#2
@@ -560,3 +599,55 @@
\tl_use:N #1
}
+%%% -------------------------------------------------------
+%%> \section{Tracing CodeHigh}
+%%% -------------------------------------------------------
+
+\NewDocumentCommand \SetCodeHighTracing { m }
+ {
+ \keys_set:nn { codehigh-set-tracing } {#1}
+ }
+
+\bool_new:N \g__codehigh_tracing_code_bool
+\bool_new:N \g__codehigh_tracing_demo_bool
+\bool_new:N \g__codehigh_tracing_parser_bool
+
+\keys_define:nn { codehigh-set-tracing }
+ {
+ +code .code:n = \bool_gset_true:N \g__codehigh_tracing_code_bool,
+ -code .code:n = \bool_gset_false:N \g__codehigh_tracing_code_bool,
+ +demo .code:n = \bool_gset_true:N \g__codehigh_tracing_demo_bool,
+ -demo .code:n = \bool_gset_false:N \g__codehigh_tracing_demo_bool,
+ +parser .code:n = \bool_gset_true:N \g__codehigh_tracing_parser_bool,
+ -parser .code:n = \bool_gset_false:N \g__codehigh_tracing_parser_bool,
+ all .code:n = \__codehigh_enable_all_tracings:,
+ none .code:n = \__codehigh_disable_all_tracings:,
+ }
+
+\cs_new_protected_nopar:Npn \__codehigh_enable_all_tracings:
+ {
+ \bool_gset_true:N \g__codehigh_tracing_code_bool
+ \bool_gset_true:N \g__codehigh_tracing_demo_bool
+ \bool_gset_true:N \g__codehigh_tracing_parser_bool
+ }
+
+\cs_new_protected_nopar:Npn \__codehigh_disable_all_tracings:
+ {
+ \bool_gset_false:N \g__codehigh_tracing_code_bool
+ \bool_gset_false:N \g__codehigh_tracing_demo_bool
+ \bool_gset_false:N \g__codehigh_tracing_parser_bool
+ }
+
+\cs_new_protected:Npn \__codehigh_tracing:nn #1 #2
+ {
+ \bool_if:cT { g__codehigh_tracing_ #1 _bool } {#2}
+ }
+
+\group_begin:
+ \char_set_catcode_active:N \^^M
+ \cs_new_protected:Npn \__codehigh_log:N #1
+ {
+ \tl_replace_all:Nnn #1 {^^M} {^^J}
+ \tl_log:N #1
+ }
+\group_end: