[luatex] Passing verbatim text to a function
Aditya Mahajan
adityam at umich.edu
Mon Sep 27 03:09:33 CEST 2010
On Mon, 27 Sep 2010, Sietse Brouwer wrote:
> I'm using ConTeXt Mark IV, and trying to write a function that uses
> Lua to dedent a block of verbatim text before handing it off to
> \starttyping. My current problem: I have not yet succeeded in passing
> the block of text to Lua with newlines and spaces preserved.
>
> I started out with the usual pattern for defining a macro that calls Lua:
>
> %%%% ---- begin usual pattern ---- %%%%
> [snip]
> %%%% ---- end usual pattern ---- %%%%
>
> That pattern, however, got me my spaces and newlines discarded. I next
> tried a two-macro structure, as described in
> http://tinyurl.com/mbp2ub (which leads to
> http://wiki.contextgarden.net/Inside_ConTeXt#Passing_verbatim_text_as_macro_parameter,
> but I'm not convinced that URL won't get broken).
>
> %%%% ---- start two-macro pattern ---- %%%%
> % Lua code remains the same
>
> % new two-macro structure
> \def\Dedent{\bgroup\obeylines\obeyspaces\doDedent}
> \def\doDedent#1{\directlua{dedent([[#1]])}}
>
> % test remains the same
> %%%% ---- start two-macro pattern ---- %%%%
>
> Alas! This preserves spaces, but not lines. I am now at the end of my
> wits. Does anyone on this list know a solution?
If you do not mind a slight change in the user syntax, here is an
alternative:
\def\startDedent
{\dostartbuffer[dedent][startDedent][stopDedent]}
\def\stopDedent{\directlua{dedent("dedent")}}
The function dedent(...) now takes the name of the buffer rather than its
contents. So
dedent = function(name)
local code = buffers.content(name)
....
end
(I also had to change the line
lead = string.match(code, '^ +')
to
lead = string.match(code, '^ +') or ''
)
Then you can use
\startDedent andra
moi
ennepe\stopDedent
Aditya
More information about the luatex
mailing list