Changes

Jump to navigation Jump to search
Merge sections
< [[Inside ConTeXt]]
== Using Lua == There is a lovely set of functions in the <tt>utilities.parsers</tt> table that turns strings into nice Lua-accessible tables of various kinds. An example is given below; the functions are documented on the [[String_manipulation#util-prs.lua|string manipulation]] page. <texcode>\startluacodeuserdata = userdata or { } function userdata.mycommand(keywords, keyvals) keyword_options = utilities.parsers.settings_to_array(keywords) named_values = utilities.parsers.settings_to_hash(keyvals) -- do stuff based on that array and that hashtable.end\stopluacode \def\mycommand[#1][#2]{\ctxlua{ userdata.mycommand('#1', '#2')}  \mycommand[top, inmargin, now][color=green, roof=gabled]</texcode> == Using ConTeXt/TeX == This section is based on a post on the mailing list by Taco Hoekwater from [[http://www.mail-archive.com/ntg-context@ntg.nl/msg03235.html 2004-06-28]]and Hans Hagen from [[https://www.mail-archive.com/ntg-context@ntg.nl/msg100795.html 2021-12-14]]. === Example 1 === To parse your own optional keyval argument from a command. You may want to adapt the logic according to your needs. Note that in the example below, <tt>[#1]</tt> is not optional.  <texcode>\tolerant\protected\def\MyCommand[#1]#2 {\ifempty{#1}% I only got \quote{#2}. \orelse\ifhastok={#1}% \getdummyparameters[MyOwnKeyOne={},MyOwnKeyTwo={B},#1] % this is to define default values; overwritten when mentioned as part of ",#1" \ifcstok{\dummyparameter{MyOwnKeyOne}}\emptytoks \else % if this one is not empty, we do ... Here is #1! \fi And I also got \quote{#2}. \else #1 and #2 % whatever \fi}</texcode> Of course, one can pass on <tt>#1</tt> as argument to other commands. If <tt>MyOwnKeyOne</tt> is not matched by its argument list, it's ignored. See also https://www.mail-archive.com/ntg-context@ntg.nl/msg100795.html for more complex cases and other variants. === Example 2 === <texcode>\unprotect % enable exclamations in macro names % Helper function:if the user specifies nothing, throw an error\def\errorDir{% \def\Dir{0}% error recovery \message{Please supply "Dir" argument}%} % Helper function: process Dir=... other than Up/Down/Left/Right% Accept numbers, else throw an error\def\checkDir#1{% \doifnumberelse {#1} {\def\Dir{#1}} {\message{Invalid "Dir" argument! (#1)}}} % Defining our command that accepts key=val\def\MyZigzag#1[#2]{% % the #1 makes sure we allow a space before the bracket  % create commands \ZZDir, \ZZLinewidth (default 1pt), etc. \getparameters[ZZ][Dir=,Linewidth=1pt,Color=Red,Width=3em,#2]  % define an alias \edef\mywidth{\ZZWidth}%  % accept keywords for Dir= (and map those keywords to numbers) %\expandafter\processaction\expandafter[\ZZDir] % mkii \processaction[\ZZDir] % mkiv [ Down=>\def\Dir{270}, Left=>\def\Dir{180}, Up=>\def\Dir{90}, Right=>\def\Dir{0}, \s!default=>\errorDir, \s!unknown=>\checkDir{\ZZDir}]} % this brace belongs to \def! \protect % end of definitions</texcode> === Example 2 with commentary ===
The 'key' to the keyval functionality in ConTeXt are two macros called
[[{{cmd:getparameters|\getparameters]] }} and [[{{cmd:processaction|\processaction]]. Here is a 'quickstart', assuming you want to define <tt>\MyZigzag</tt>. The code example is interrupted with explantory running text}}.
<texcode>
</texcode>
If you want some of the variables to accept keyword values, then you also need to use [[{{cmd:processaction|\processaction]] }} on the ZZ variable in question to map the keywords onto actual values.
Say you want "Dir" to be mandatory and that it accepts 4 directional keywords, as well as a direct angle specification. I've used all mixed case keywords, because otherwise you might run into conflicts with the multilingual interface:
<texcode>
%\expandafter\processaction\expandafter[\ZZDir]% mkii \processaction[\ZZDir] % mkiv [ Down =>\def\Dir{270}, Left =>\def\Dir{180}, Up =>\def\Dir{90}, Right =>\def\Dir{0}, \s!default =>\errorDir, \s!unknown =>\checkDir{\ZZDir}]
} % this brace belongs to \def!
</texcode>
<tt>\s!default</tt> may be triggered because <tt>\ZZDir</tt>'s expansion is empty unless the user supplied something.
The In MkII, the first argument to [[{{cmd:processaction|\processaction]] }} has to be expanded, so you need the [[{{cmd:expandafter|\expandafter]]}}s. In MkIV, {{cmd|processaction|link=no}} does this automatically.
for For completeness, here is an example definition of <tt>\checkDir</tt> and <tt>\errorDir</tt>:
<texcode>
</texcode>
[[Category:Inside ConTeXtProgramming and Databases]][[Category:Tools]]
57

edits

Navigation menu