Skip to content

Commit 69a3084

Browse files
committed
version 2021A
0 parents  commit 69a3084

File tree

3 files changed

+317
-0
lines changed

3 files changed

+317
-0
lines changed

README

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Package: Highlight codes and demos with LaTeX3
2+
Author: Jianrui Lyu <[email protected]>
3+
Repository: https://github.com/lvjr/codehigh
4+
License: The LaTeX Project Public License 1.3c

codehigh.sty

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+

codehigh.tex

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
% -*- coding: utf-8 -*-
2+
\documentclass{article}
3+
\usepackage[a4paper,margin=2cm]{geometry}
4+
5+
\usepackage{codehigh}
6+
%\usepackage{arev}
7+
\usepackage{hyperref}
8+
\hypersetup{
9+
colorlinks=true,
10+
urlcolor=blue3,
11+
}
12+
13+
\newcommand*{\myversion}{2021A}
14+
\newcommand*{\mydate}{\the\year/\mylpad\month/\mylpad\day\ (v\myversion)}
15+
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi}
16+
17+
\begin{document}
18+
19+
\title{\textsf{\color{green3}Codehigh Package: Highlight Codes and Demos with LaTeX3}%
20+
\footnote{\url{https://github.com/lvjr/codehigh}}}
21+
\author{Jianrui Lyu ([email protected])}
22+
\date{\mydate}
23+
\maketitle
24+
25+
\verb!Codehigh! package uses \verb!l3regex! package in \LaTeX3 Programming Layer
26+
to parse and highlight source codes and demos.
27+
In theory it would be more powerful than \verb!listings! package,
28+
and more easy to use than \verb!minted! package.
29+
At present, this package is in \underline{\color{red3}\textbf{experimental}} status.
30+
Don’t use it in important documents, unless you have time
31+
to update them for the newer versions of \verb!codehigh! package in the future.
32+
33+
\bigskip
34+
\hrule
35+
\bigskip
36+
37+
The following is typeset with \verb!verbatim! environment.
38+
39+
\begin{verbatim}
40+
\begin{tabular}[b]{|lc|r|}
41+
\hline
42+
One & Two & Three \\ %[4pt]
43+
%\hline
44+
Four & Five & Six \\
45+
\hline%\hline\hline
46+
Seven & Eight & Nine \\ %* [1em]
47+
\hline
48+
\end{tabular}
49+
\end{verbatim}
50+
51+
\bigskip
52+
\hrule
53+
\bigskip
54+
55+
The following is typeset with \verb!codehigh! environment.
56+
57+
\begin{codehigh}
58+
\begin{tabular}[b]{|lc|r|}
59+
\hline
60+
One & Two & Three \\ %[4pt]
61+
%\hline
62+
Four & Five & Six \\
63+
\hline%\hline\hline
64+
Seven & Eight & Nine \\ %* [1em]
65+
\hline
66+
\end{tabular}
67+
\end{codehigh}
68+
69+
\bigskip
70+
\hrule
71+
\bigskip
72+
73+
The following is typeset with \verb!demohigh! environment.
74+
75+
\begin{demohigh}
76+
\begin{tabular}[b]{|lc|r|}
77+
\hline
78+
One & Two & Three \\ %[4pt]
79+
%\hline
80+
Four & Five & Six \\
81+
\hline%\hline\hline
82+
Seven & Eight & Nine \\ %* [1em]
83+
\hline
84+
\end{tabular}
85+
\end{demohigh}
86+
87+
\bigskip
88+
\hrule
89+
\bigskip
90+
91+
Please read the source file \verb!codehigh.tex! of this document for reference.
92+
93+
\end{document}

0 commit comments

Comments
 (0)