Difference between revisions of "CLD"

From ConTeXt wiki
m
(Basic reading: fix link)
Line 6: Line 6:
  
 
== Basic reading ==
 
== Basic reading ==
* [www.pragma-ade.com/general/manuals/cld-mkiv.pdf The CLD manual] by Hans Hagen.
+
* [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf The CLD manual] by Hans Hagen.
 
* [[source:mult-cld.lua| The source code.]]
 
* [[source:mult-cld.lua| The source code.]]
  

Revision as of 20:49, 12 March 2010

CLD (ConTeXt Lua Documents) are way to access TeX from inside Lua scripts; they provide means to typeset documents using little to no TeX code at all. This makes them especially helpful for automated content generation and scripting. Internally, the Lua commands are mapped onto their ConTeXt and TeX counterparts. Knowledge of the latter is, therefore, a prerequisite to writing CLDs. If you ever dreamed of proper typesetting without backslashes galore, here is where to start at.

Basic reading

An Example

Writing CLDs is straightforward although there are some pitfalls. Suppose we want to typeset the following minimal example:

\starttext
\placefigure {none} {%                                                                                                     
  \framed [frame=on, align=middle] {%
    \input knuth
  }%
}%
\stoptext

With CLDs this will require more verbosity because nesting -- in the present example a framed inside a placefigure -- has to be expressed through functions. Optional and key-value style arguments are expressed as arrays and dictionaries respectively, which is a transparent representation of their ConTeXt ancestors. Mandatory (grouped) arguments are given either as strings or as functions.

The following example shows two ways of rewriting the above TeX code: the first one closely resembles its structure while the second calls the function test from inside the placefigure macro -- NB: it is compulsory to call a declared function by means of another function, otherwise ConTeXt can't process it. (Works with latest ConTeXt beta as of 2010-03-10.)

context.placefigure(
    "none",
    function()
        context.framed( {
            frame="on",
            align="middle"
        },  
            function() context.input("knuth") end 
        )   
    end 
)

context.par()

function text ()
    context.framed( {
            frame="on",
            align="middle"
        },  
        function() context.input("knuth") end 
    )   
end

context.placefigure(
    "none",
    function () text() end                                                                                                 
)