I have some Lua code which searches for "①" and "②" within some text and prints only the text displayed between:
\startluacode
userdata = userdata or {}
function userdata.printbetween(str, m1, m2)
m1 = m1 or "①"
m2 = m2 or "②"
mat_pat = string.format('%s(.*)%s', m1, m2)
str = string.match(str, mat_pat) or str
context(str)
end
\stopluacode
\define\sentence{This is ① a final ② test sentence.}
\define[1]\textbetween{%
\ctxlua{userdata.printbetween([===[#1]===])}%
}
\starttext
\startitemize[n]
\item \textbetween{This is ① a test ② sentence.}
\item \textbetween{This is ① another test ② sentence.}
\item \textbetween{\sentence}
\stopitemize
\stoptext
This should print:
1. a test
2. another test
3. a final
Instead, this is printing:
1. a test
2. another test
3. This is ① a final ② test sentence.
When I give Lua a variable, such as \sentence
, it appears to process "\sentence" and not the data contained inside \sentence
.
How can I let Lua examine the contents inside the variable rather than just examining the variable's name?
\define
makes the definition unexpandable. Try\def\sentence{....}
instead