[luatex] Passing verbatim text to a function
Sietse Brouwer
sbbrouwer at gmail.com
Mon Sep 27 00:16:02 CEST 2010
Dear list,
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 ---- %%%%
\startluacode
dedent = function(code)
-- Create a logfile.
logfile = io.open("debug.txt", "w")
-- Prove that strings with newlines get written correctly
logfile:write("--\n" .. " test\n" .. "--\n")
-- Now write to the logfile that what ConTeXt gave us.
logfile:write(code)
logfile:close()
-- Actual dedenting code. Not relevant yet.
lead = string.match(code, '^ +')
code = string.gsub(code, '^' .. lead, '')
code = string.gsub(code, '\n' .. lead, '\n')
-- Placeholder. Messing with context.type()
-- or context.starttyping() comes later.
context(code)
end
\stopluacode
\def\Dedent#1{ \directlua{ dedent( [[#1]] ) } }
% Try it out. If all goes well, logfile.txt should contain
% the below with linebreaks and spaces preserved.
\starttext
\Dedent{ andra
moi
ennepe}
\stoptext
%%%% ---- 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?
Kind regards,
SIetse
Sietse Brouwer
More information about the luatex
mailing list