Changes

Jump to navigation Jump to search
2,510 bytes added ,  13:13, 27 September 2010
Added a section on processing verbatim text, with an example of an auto-dedenting \starttyping block.
The lua functions such as <code>context.bTABLE()</code> and <code>context.bTR()</code> are just abbreviations for running <code>context ("\\bTABLE")</code>, <code>context("\\bTR")</code>, etc. See the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf ConTeXt lua document] manual for more details about such functions. The rest of the code is a simple nested for-loop that computes the sum of two dice. We do not need to worry about macro expansion at all!
 
= Getting text into LuaTex verbatim =
 
Ordinarily, getting text into a TeX macro with spaces and newlines
intact requires mucking about with <code>\obeylines</code> and
<code>\obeyspaces</code> &mdash; see
[[Inside_ConTeXt#Passing_verbatim_text_as_macro_parameter]]. Luckily for
us, however, ConTeXt provides the
<code>\dostartbuffer[</code><i>name</i><code>][</code><i>bufferstart</i><code>][<i>bufferstop</i><code>]</code>
command, which creates a pair of buffer commands that write their contents verbatim to a named buffer. This buffer can then be accessed in LuaTeX via <code>buffers.content(</code><i>name</i><code>)</code>. Here is an example that shows how the buffer definition and the LuaTex call are combined.
 
<texcode>
%% Define a buffer \startDedent ... \stopDedent that feeds its contents
%% to LuaTex for processing.
%% LuaTex finds out by how much the first line is indented, and dedents
%% the entire block by that much.
 
\startluacode
dedent = function(name)
% N.B. This function does not handle tabs.
 
%% read out the buffer
local code = buffers.content(name)
%% First some debugging stuff: dump the input to a text file
% to check that we got what we wanted.
logfile = io.open("debug.txt", "w")
logfile:write(code)
logfile:close()
 
%% Now for the meat of it.
% How many leading spaces in the first line?
lead = string.match(code, '^ +') or ''
% remove lead from every line
code = string.gsub(code, '^' .. lead, '')
code = string.gsub(code, '\n' .. lead, '\n')
 
%% print the resulting starttyping environment to the TeX stream
% (context.starttyping() doesn't seem to work)
tex.sprint("\\starttyping" .. "\n" ..
code ..
"\\stoptyping" .. "\n")
end
\stopluacode
 
% Tell TeX what strings the dedentBuffer starts and ends with.
\def\startDedent
{\dostartbuffer[dedentBuffer][startDedent][stopDedent]}
% Make the ending command (which will also signal the buffer's end)
% call the processing LuaTeX.
\def\stopDedent
{\directlua{dedent("dedentBuffer")}}
 
% Try it out:
\starttext
\startDedent
andra
moi
ennepe
\stopDedent
\stoptext
</texcode>
 
Each <code>\stopDedent</code> immediately uses the <code>dedentBuffer</code>'s
contents, and each <code>\startDedent</code> flushes the buffer's contents, so
the construction can safely be used multiple times throughout the document.
 
= Parsing input without exploding your head =
Anonymous user

Navigation menu