Given a piece of text, I would like to replace lines start with a date string yyyy-mm-dd
with a command. For this, my idea is first to split the text by \par
with \regex_split
, save the result in a sequence, then do the regex replacement in each sequence item, and finally put all these items in a token list. However, I'm getting TeX capacity exceeded, sorry [input stack size=5000]
error.
Below is a MWE (it should color the date in red and title in blue):
\documentclass{article}
\usepackage{xcolor}
\ExplSyntaxOn
\NewDocumentCommand \JournalEntry { m m }
{
\textcolor { red } { #1 }
\textcolor { blue } { #2 }
}
\tl_new:N \g_myjournal_contents_tl
\NewDocumentEnvironment { myjournal } { +b }
{
\tl_gset:Nn \g_myjournal_contents_tl { #1 }
\myjournal_parse_contents:N \g_myjournal_contents_tl
\tl_use:N \g_myjournal_contents_tl
}
{}
\seq_new:N \l_myjournal_tmp_seq
\cs_generate_variant:Nn \regex_split:nnN { nVN }
\cs_new:Nn \myjournal_parse_contents:N
{
\regex_split:nVN { \c{par} } #1 \l_myjournal_tmp_seq
\tl_set:Nn \g_myjournal_contents_tl {}
\seq_map_inline:Nn \l_myjournal_tmp_seq
{
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_replace_all:nnN
{ (\ *)? (\d{4}-\d{1,2}-\d{1,2}) (\ *)? (.*) }
{ \c{JournalEntry} {\2} {\4} }
\l_tmpa_tl
\tl_gput_right:NV \g_myjournal_contents_tl \l_tmpa_tl
\tl_gput_right:Nn \g_myjournal_contents_tl { \par }
}
}
\ExplSyntaxOff
\usepackage{blindtext}
\begin{document}
\begin{myjournal}
2022-02-01 Sunny - Some title
\blindtext
2022-02-01 Not sunny titles
\blindtext
\end{myjournal}
-- Text in case of empty --
\end{document}
#1
(the name of a token list) to the regex every time. That won't help: you need##1
due to the nesting.