Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
%%
%% This is file 'wtxsection.sty'.
%%
%% Copyright 2019 Takuto ASAKURA (wtsnjp)
%% GitHub: https://github.com/wtsnjp
%% Twitter: @wtsnjp
%%
%% This package is distributed under the MIT License.
%%
% Note: This package is a conceptual TOY package. Don't be so serious.
% -------------------------------------------------- declarations
\RequirePackage{xparse}
\ProvidesExplPackage {wtxsection} {2019/09/23} {0.1.0}
{LaTeX section extencion for begin-end balancing}
% -------------------------------------------------- variables
\seq_new:N \g_wtxs_sections_seq
\str_new:N \l_wtxs_cur_type_str
\str_new:N \l_wtxs_cur_name_str
% -------------------------------------------------- messages
\msg_new:nnn { wtxsection } { already-declared }
{
Section~type~"#1"~is~already~declared.
}
\msg_new:nnn { wtxsection } { not-declared }
{
Section~type~"#1"~has~not~been~declared.
}
\msg_new:nnn { wtxsection } { seq-emtpy }
{
End~of~section~is~declared~\msg_line_context:,~
but~no~section~has~been~begun.
}
\msg_new:nnn { wtxsection } { bad-end }
{
End~of~#1~is~declared~\msg_line_context:,~
but~you~must~declare~the~end~of~#2~"#3"~first.
}
\msg_new:nnn { wtxsection } { unclosed-section }
{
End~of~document~is~declared~before~the~end~of~
#1~"#2"~is~declared.
}
% -------------------------------------------------- utility
% LaTeX2e stuff
%% \__wtxs_only_preamble:N \CS
\cs_new_eq:Nc \__wtxs_only_preamble:N { @onlypreamble }
%% \__wtxs_at_end_document:n { <code> }
\cs_new_eq:NN \__wtxs_at_end_document:n \AtEndDocument
% -------------------------------------------------- LaTeX2e interface
%% \xDeclareNewSection { <type> } { <command> }
\NewDocumentCommand \xDeclareNewSection { m m }
{
\str_if_exist:cTF { c_wtxs_#1_str }
{
\msg_critical:nnn { wtxsection } { already-declared } {#1}
}
{
\wtxs_declare_new_section:nn {#1} {#2}
}
}
\__wtxs_only_preamble:N \xDeclareNewSection
%% \xBegin { <type> } (*) [<toc name>] { <name> }
\NewDocumentCommand \xBegin { m s o m }
{
\str_if_exist:cTF { c_wtxs_#1_str }
{
\IfBooleanTF {#2}
{
% begin xsection
\use:c { wtxs_begin_#1:n } {#4}
% execute actual LaTeX2e section with star
\use:c { wtxs_cmd_#1: } * {#4}
}
{
\IfNoValueTF {#3}
{
% begin xsection
\use:c { wtxs_begin_#1:n } {#4}
% execute actual LaTeX2e section
\use:c { wtxs_cmd_#1: } {#4}
}
{
% begin xsection
\use:c { wtxs_begin_#1:n } {#3}
% execute actual LaTeX2e section with an optional argument
\use:c { wtxs_cmd_#1: } [#3] {#4}
}
}
}
{
\msg_critical:nnn { wtxsection } { not-declared } {#1}
}
}
%% \xEnd { <type> }
\NewDocumentCommand \xEnd { m }
{
\str_if_exist:cTF { c_wtxs_#1_str }
{
\use:c { wtxs_end_#1: }
}
{
\msg_critical:nnn { wtxsection } { not-declared } {#1}
}
}
% -------------------------------------------------- implementation
%% \wtxs_declare_new_section:nn { <type> } { <command> }
\cs_new:Npn \wtxs_declare_new_section:nn #1#2
{
% declare the section
\str_const:cn { c_wtxs_#1_str } { #1 }
% copy the actual command
\cs_new_eq:cN { wtxs_cmd_#1: } #2
% begin section
\cs_new:cpn { wtxs_begin_#1:n } ##1
{
\seq_gput_left:Nn \g_wtxs_sections_seq { {#1} ##1 }
}
% end section
\cs_new:cn { wtxs_end_#1: }
{
\seq_gpop_left:NNTF \g_wtxs_sections_seq \l_tmpa_tl
{
\str_set:Nx \l_wtxs_cur_type_str { \tl_head:V \l_tmpa_tl }
\str_set:Nx \l_wtxs_cur_name_str { \tl_tail:V \l_tmpa_tl }
\str_if_eq:NcF \l_wtxs_cur_type_str { c_wtxs_#1_str }
{
\msg_critical:nnxxx { wtxsection } { bad-end } { #1 }
{ \str_use:N \l_wtxs_cur_type_str }
{ \str_use:N \l_wtxs_cur_name_str }
}
}
{ \msg_critical:nn { wtxsection } { seq-emtpy } }
}
}
\cs_new:Nn \wtxs_check_end_of_document:
{
\seq_gpop_left:NNT \g_wtxs_sections_seq \l_tmpa_tl
{
\str_set:Nx \l_wtxs_cur_type_str { \tl_head:V \l_tmpa_tl }
\str_set:Nx \l_wtxs_cur_name_str { \tl_tail:V \l_tmpa_tl }
\msg_critical:nnxx { wtxsection } { unclosed-section }
{ \str_use:N \l_wtxs_cur_type_str }
{ \str_use:N \l_wtxs_cur_name_str }
}
}
\__wtxs_at_end_document:n { \wtxs_check_end_of_document: }
% vim: ft=expl3 nospell:
%% EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.