|
| 1 | +%%% -*- coding: utf-8 -*- |
| 2 | +%%% ---------------------------------------------------------------------------- |
| 3 | +%%% Codehigh : Highlight codes and demos with LaTeX3 |
| 4 | +%%% Author : Jianrui Lyu <[email protected]> |
| 5 | +%%% Repository: https://github.com/lvjr/codehigh |
| 6 | +%%% License : The LaTeX Project Public License 1.3c |
| 7 | +%%% ---------------------------------------------------------------------------- |
| 8 | + |
| 9 | +\NeedsTeXFormat{LaTeX2e} |
| 10 | +\RequirePackage{expl3} |
| 11 | +\ProvidesExplPackage{codehigh}{2021-04-11}{2021A} |
| 12 | + {Highlight codes and demos with LaTeX3} |
| 13 | + |
| 14 | +%\RequirePackage{xparse} |
| 15 | +\RequirePackage{xcolor} |
| 16 | +\RequirePackage{ninecolors} |
| 17 | + |
| 18 | +\ExplSyntaxOn |
| 19 | + |
| 20 | +\int_new:N \l__codehigh_a_int |
| 21 | +\int_new:N \l__codehigh_b_int |
| 22 | +\tl_new:N \l__codehigh_a_tl |
| 23 | +\tl_new:N \l__codehigh_b_tl |
| 24 | +\tl_new:N \l__codehigh_c_tl |
| 25 | +\tl_new:N \l__codehigh_d_tl |
| 26 | +\tl_new:N \l__codehigh_m_tl |
| 27 | + |
| 28 | +\cs_generate_variant:Nn \regex_set:Nn { cn } |
| 29 | +\cs_generate_variant:Nn \str_remove_once:Nn { NV } |
| 30 | +\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV } |
| 31 | + |
| 32 | +%\prg_generate_conditional_variant:Nnn \regex_extract_once:nnN { vVN } { TF } |
| 33 | +\prg_generate_conditional_variant:Nnn \regex_extract_once:NnN { cVN } { T, TF } |
| 34 | +\prg_generate_conditional_variant:Nnn \regex_split:NnN { cVN } { T, TF } |
| 35 | + |
| 36 | +\NewDocumentEnvironment { codehigh } { } |
| 37 | + { |
| 38 | + \bgroup |
| 39 | + \__codehigh_do_specials: |
| 40 | + \begin{codehigh@aux} |
| 41 | + } |
| 42 | + { |
| 43 | + \end{codehigh@aux} |
| 44 | + \egroup |
| 45 | + \__codehigh_typeset_code: |
| 46 | + } |
| 47 | + |
| 48 | +\NewDocumentEnvironment { demohigh } { } |
| 49 | + { |
| 50 | + \bgroup |
| 51 | + \__codehigh_do_specials: |
| 52 | + \begin{codehigh@aux} |
| 53 | + } |
| 54 | + { |
| 55 | + \end{codehigh@aux} |
| 56 | + \egroup |
| 57 | + \__codehigh_typeset_code: |
| 58 | + \__codehigh_typeset_demo: |
| 59 | + } |
| 60 | + |
| 61 | +\cs_new_protected:Npn \__codehigh_do_specials: |
| 62 | + { |
| 63 | + \obeylines |
| 64 | + \obeyspaces |
| 65 | + \catcode `\% = 12 |
| 66 | + } |
| 67 | + |
| 68 | +\tl_new:N \g__codehigh_code_tl |
| 69 | + |
| 70 | +\NewDocumentEnvironment { codehigh@aux } { +b } |
| 71 | + { |
| 72 | + \tl_gset:Nn \g__codehigh_code_tl { #1 } |
| 73 | + \tl_log:N \g__codehigh_code_tl |
| 74 | + } |
| 75 | + { } |
| 76 | + |
| 77 | +\cs_new_protected:Npn \__codehigh_typeset_code: |
| 78 | + { |
| 79 | + \tl_set_eq:NN \l_tmpa_tl \g__codehigh_code_tl |
| 80 | + \regex_replace_all:nnN { . } { \c{string} \0 } \l_tmpa_tl |
| 81 | + \tl_set:Nx \l_tmpa_tl { \l_tmpa_tl } |
| 82 | + \tl_set_eq:NN \l_tmpb_tl \l_tmpa_tl |
| 83 | + %\__codehigh_typeset_code_plain:N \l_tmpa_tl |
| 84 | + \group_begin: |
| 85 | + \setlength { \parindent } { 0pt } |
| 86 | + \ttfamily |
| 87 | + \__codehigh_parse_code:nN { latex } \l_tmpb_tl |
| 88 | + \group_end: |
| 89 | + } |
| 90 | + |
| 91 | +\cs_new_protected:Npn \__codehigh_typeset_code_plain:N #1 |
| 92 | + { |
| 93 | + \group_begin: |
| 94 | + \regex_replace_all:nnN { \^^M } { \c{par} } #1 |
| 95 | + \setlength { \parindent } { 0pt } |
| 96 | + \ttfamily |
| 97 | + \tl_use:N #1 |
| 98 | + \group_end: |
| 99 | + } |
| 100 | + |
| 101 | +\cs_new_protected:Npn \__codehigh_typeset_demo: |
| 102 | + { |
| 103 | + \tl_set_eq:NN \l_tmpb_tl \g__codehigh_code_tl |
| 104 | + \tl_set_rescan:NnV \l_tmpb_tl |
| 105 | + { |
| 106 | + \catcode `\% = 14 \relax |
| 107 | + \catcode `\^^M = 10 \relax |
| 108 | + } |
| 109 | + \l_tmpb_tl |
| 110 | + \tl_log:N \l_tmpb_tl |
| 111 | + \par |
| 112 | + \tl_use:N \l_tmpb_tl |
| 113 | + } |
| 114 | + |
| 115 | +\int_new:N \l__codehigh_latex_syntax_count_int |
| 116 | + |
| 117 | +%% #1: language name, #2: regex, #3: style name |
| 118 | +\cs_new_protected:Npn \__codehigh_add_syntax:nnn #1 #2 #3 |
| 119 | + { |
| 120 | + \int_incr:c { l__codehigh_ #1 _syntax_count_int } |
| 121 | + \regex_set:cn |
| 122 | + { l__codehigh_ #1 _ \int_use:c { l__codehigh_ #1 _syntax_count_int } _regex } |
| 123 | + { #2 } |
| 124 | + \tl_set:cn |
| 125 | + { l__codehigh_ #1 _ \int_use:c { l__codehigh_ #1 _syntax_count_int } _tl } |
| 126 | + { #3 } |
| 127 | + } |
| 128 | + |
| 129 | +\__codehigh_add_syntax:nnn { latex } { \% .*? \^^M } { Comment } |
| 130 | +\__codehigh_add_syntax:nnn { latex } { \& } { Alignment } |
| 131 | +\__codehigh_add_syntax:nnn { latex } { \\ (begin|end) } { BeginEnd } |
| 132 | +\__codehigh_add_syntax:nnn { latex } { \\ \\ } { Newline } |
| 133 | +\__codehigh_add_syntax:nnn { latex } { \\ [A-Za-z]+ } { Command } |
| 134 | + |
| 135 | +%% #1: style name, #2: commands |
| 136 | +\cs_new_protected:Npn \__codehigh_add_style:nn #1 #2 |
| 137 | + { |
| 138 | + \tl_set:cn { l__codehigh_style_ #1 _tl } { #2 } |
| 139 | + } |
| 140 | + |
| 141 | +\__codehigh_add_style:nn { Default } { \color{black} } |
| 142 | +\__codehigh_add_style:nn { Alignment } { \color{brown3} } |
| 143 | +\__codehigh_add_style:nn { BeginEnd } { \color{blue3} } |
| 144 | +\__codehigh_add_style:nn { Command } { \color{teal3} } |
| 145 | +\__codehigh_add_style:nn { Comment } { \color{gray3} } |
| 146 | +\__codehigh_add_style:nn { Newline } { \color{purple3} } |
| 147 | + |
| 148 | +\int_new:N \l__codehigh_item_count_int |
| 149 | +\tl_new:N \l__codehigh_code_to_parse_tl |
| 150 | +\tl_new:N \l__codehigh_regex_match_name_tl |
| 151 | +\tl_new:N \l__codehigh_regex_match_text_tl |
| 152 | +\tl_new:N \l__codehigh_regex_before_text_tl |
| 153 | + |
| 154 | +\cs_new_protected:Npn \__codehigh_parse_code:nN #1 #2 |
| 155 | + { |
| 156 | + \tl_set_eq:NN \l__codehigh_code_to_parse_tl #2 |
| 157 | + \bool_do_until:nn { \tl_if_empty_p:N \l__codehigh_code_to_parse_tl } |
| 158 | + { |
| 159 | + \__codehigh_parse_code_once:nN { #1 } \l__codehigh_code_to_parse_tl |
| 160 | + \int_compare:nNnTF { \l__codehigh_item_count_int } = { -1 } |
| 161 | + { |
| 162 | + \__codehigh_typeset_text:nN { Default } \l__codehigh_code_to_parse_tl |
| 163 | + \tl_clear:N \l__codehigh_code_to_parse_tl |
| 164 | + } |
| 165 | + { |
| 166 | + \tl_concat:NNN \l__codehigh_a_tl |
| 167 | + \l__codehigh_regex_before_text_tl \l__codehigh_regex_match_text_tl |
| 168 | + \str_remove_once:NV \l__codehigh_code_to_parse_tl \l__codehigh_a_tl |
| 169 | + \tl_log:N \l__codehigh_code_to_parse_tl |
| 170 | + \__codehigh_typeset_text:nN { Default } \l__codehigh_regex_before_text_tl |
| 171 | + \__codehigh_typeset_text:VN \l__codehigh_regex_match_name_tl \l__codehigh_regex_match_text_tl |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | +\cs_new_protected:Npn \__codehigh_parse_code_once:nN #1 #2 |
| 177 | + { |
| 178 | + \int_set:Nn \l__codehigh_item_count_int { -1 } |
| 179 | + \tl_clear:N \l__codehigh_regex_match_text_tl |
| 180 | + \tl_clear:N \l__codehigh_regex_before_text_tl |
| 181 | + \int_step_inline:nn { \l__codehigh_latex_syntax_count_int } |
| 182 | + { |
| 183 | + \regex_extract_once:cVNT { l__codehigh_ #1 _ ##1 _regex } #2 \l_tmpa_seq |
| 184 | + { |
| 185 | + \seq_get:NN \l_tmpa_seq \l__codehigh_m_tl |
| 186 | + \regex_split:cVNT { l__codehigh_ #1 _ ##1 _regex } #2 \l_tmpb_seq |
| 187 | + { |
| 188 | + \seq_get:NN \l_tmpb_seq \l__codehigh_b_tl |
| 189 | + \tl_set:Nx \l__codehigh_c_tl { \str_count:N \l__codehigh_b_tl } |
| 190 | + \bool_lazy_or:nnT |
| 191 | + { \int_compare_p:nNn { \l__codehigh_item_count_int } = { -1 } } |
| 192 | + { |
| 193 | + \int_compare_p:nNn |
| 194 | + { \l__codehigh_item_count_int } > { \l__codehigh_c_tl } |
| 195 | + } |
| 196 | + { |
| 197 | + \int_set:Nn \l__codehigh_item_count_int { \l__codehigh_c_tl } |
| 198 | + \tl_set_eq:NN \l__codehigh_regex_before_text_tl \l__codehigh_b_tl |
| 199 | + \tl_set_eq:NN \l__codehigh_regex_match_text_tl \l__codehigh_m_tl |
| 200 | + \tl_set_eq:Nc \l__codehigh_regex_match_name_tl { l__codehigh_ #1 _ ##1 _tl } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | +%% #1: style name, #2: text |
| 208 | +\cs_new_protected:Npn \__codehigh_typeset_text:nN #1 #2 |
| 209 | + { |
| 210 | + \group_begin: |
| 211 | + \regex_replace_all:nnN { \^^M } { \c{par} } #2 |
| 212 | + \regex_replace_all:nnN { \ } { \c{relax} \c{space} } #2 |
| 213 | + \tl_use:c { l__codehigh_ style_ #1 _tl } |
| 214 | + #2 |
| 215 | + \group_end: |
| 216 | + } |
| 217 | +\cs_generate_variant:Nn \__codehigh_typeset_text:nN { VN } |
| 218 | + |
| 219 | +\ExplSyntaxOff |
| 220 | + |
0 commit comments