Created
September 3, 2020 17:31
-
-
Save jonsterling/a18c5863325acfca8126d1159ffb3a8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\RequirePackage{expl3} | |
\RequirePackage{xparse} | |
\ProvidesExplPackage {namespace} {2020/08/31} {0.1} {Compositional namespaces for labels} | |
\tl_new:N \l_ns_stk | |
\cs_new:Npn \ns_push:n #1 { | |
\tl_put_right:Nn \l_ns_stk {#1} | |
\tl_put_right:Nn \l_ns_stk {/} | |
} | |
\cs_new:Npn \ns_resolve:Nn #1 #2 { | |
\regex_match:nnTF {^/} {#2} { | |
\tl_put_right:Nn #1 {\tl_tail:n {#2}} | |
} { | |
\tl_put_right:Nn #1 {\l_ns_stk} | |
\tl_put_right:Nn #1 {#2} | |
} | |
} | |
% Map over a sequence with special behavior for the last item | |
\cs_new:Npn \ns_seq_map_inline_last:Nnn #1 #2 #3 { | |
\int_zero:N \l_tmpa_int | |
\seq_map_inline:Nn #1 { | |
\int_incr:N \l_tmpa_int | |
\int_compare:nNnTF {\l_tmpa_int} < {\seq_count:N #1} {#2} {#3} | |
} | |
} | |
\seq_new:N \l_ns_resolve_multi_split | |
\cs_new:Npn \ns_resolve_multi:Nn #1 #2 { | |
\seq_set_split:Nnn \l_ns_resolve_multi_split {,} {#2} | |
\ns_seq_map_inline_last:Nnn \l_ns_resolve_multi_split { | |
\ns_resolve:Nn #1 {##1} | |
\tl_put_right:Nn #1 {,} | |
} { | |
\ns_resolve:Nn #1 {##1} | |
} | |
} | |
\NewDocumentEnvironment{namespace}{m}{ | |
\group_begin: | |
\ns_push:n {#1} | |
}{ | |
\group_end: | |
} | |
\tl_new:N \l_ns_resolve_tl | |
\NewDocumentCommand\nslabel{om}{ | |
\tl_clear:N \l_ns_resolve_tl | |
\ns_resolve:Nn \l_ns_resolve_tl {#2} | |
\IfValueTF{#1}{\label[#1]{\l_ns_resolve_tl}}{\label{\l_ns_resolve_tl}} | |
} | |
\NewDocumentCommand\nsref{m}{ | |
\tl_clear:N \l_ns_resolve_tl | |
\ns_resolve_multi:Nn \l_ns_resolve_tl {#1} | |
\ref{\l_ns_resolve_tl} | |
} | |
\NewDocumentCommand\nscref{m}{ | |
\tl_clear:N \l_ns_resolve_tl | |
\ns_resolve_multi:Nn \l_ns_resolve_tl {#1} | |
%[\tl_use:N \l_ns_resolve_tl] | |
\cref{\l_ns_resolve_tl} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment