[NTG-context] How to get parameters in lua

2014-09-19 Thread Christoph Reller
Hi,

\installcommandhandler nicely provides (among others) \definename and
\setupname commands. Thus it is easy to provide a key with
\setup...[key=value] and to read the value of such a key with
\nameparameter{key}.

My question is: How do I read the value of a key defined with
\setup...[key=value] on the lua side? If I use
  context.nameparameter(key)
then the value of key is printed directly. I want to be able to read that
value and use it in lua with something like
  myvariable = context.?whatfunction?(key)

Here is some example code:

\unprotect

\startluacode
  function myfun(parameters,content)
context(From lua: Parameters: )
context(parameters)
context.elemparameter(gkey) -- directly prints the parameter
context.par()
  end
\stopluacode

\installnamespace{elem}
\installcommandhandler \elem {elem} \elem
\appendtoks
  \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
\to \everydefineelem

\unexpanded\def\elem_cmd#environment{%
  \edef\currentelem{#environment}%
  \dosingleempty\elem_cmd_parameters}

\def\elem_cmd_parameters[#parameters]#content{%
  \setupcurrentelem[#parameters]
  From \TeX\: Parameters: key=\elemparameter{key},
gkey=\elemparameter{gkey}\par

\ctxlua{myfun(\!!bs#parameters\!!es,\!!bs\normalunexpanded{#content}\!!es)}
}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext

Thank you for any feedback,

Christoph Reller
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___

Re: [NTG-context] How to get parameters in lua

2014-09-19 Thread Hans Hagen

On 9/19/2014 9:30 AM, Christoph Reller wrote:

Hi,

\installcommandhandler nicely provides (among others) \definename and
\setupname commands. Thus it is easy to provide a key with
\setup...[key=value] and to read the value of such a key with
\nameparameter{key}.

My question is: How do I read the value of a key defined with
\setup...[key=value] on the lua side? If I use
   context.nameparameter(key)
then the value of key is printed directly. I want to be able to read
that value and use it in lua with something like
   myvariable = context.?whatfunction?(key)

Here is some example code:

\unprotect

\startluacode
   function myfun(parameters,content)
 context(From lua: Parameters: )
 context(parameters)
 context.elemparameter(gkey) -- directly prints the parameter
 context.par()
   end
\stopluacode

\installnamespace{elem}
\installcommandhandler \elem {elem} \elem
\appendtoks
   \setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
\to \everydefineelem

\unexpanded\def\elem_cmd#environment{%
   \edef\currentelem{#environment}%
   \dosingleempty\elem_cmd_parameters}

\def\elem_cmd_parameters[#parameters]#content{%
   \setupcurrentelem[#parameters]
   From \TeX\: Parameters: key=\elemparameter{key},
gkey=\elemparameter{gkey}\par

\ctxlua{myfun(\!!bs#parameters\!!es,\!!bs\normalunexpanded{#content}\!!es)}
}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext

Thank you for any feedback,


something like this ... keep in mind that there is no grouping in lua

% macros=mkvi

\unprotect

\installnamespace{elem}

\installcommandhandler \elem {elem} \elem

\startluacode
userdata.elem = { data = { [] = { } } }

function userdata.elem.define(namespace,parent)
print(define,namespace,parent)
userdata.elem.data[namespace] = { }
if namespace ~= parent then

table.setmetatableindex(userdata.elem.data[namespace],userdata.elem.data[parent])
end
end

function userdata.elem.setup(namespace,key,gkey)
print(setup,namespace,key,gkey)
userdata.elem.data[namespace].key = key
userdata.elem.data[namespace].gkey = gkey
end

function userdata.myfun(namespace,one,two)
context.par()
context(elem: one=%s,userdata.elem.data[namespace][one])
context.par()
context(elem: two=%s,userdata.elem.data[namespace][two])
context.par()
end
\stopluacode

\appendtoks
\setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
\ctxlua{userdata.elem.define(\currentelem,\currentelemparent)}%
\to \everydefineelem

\appendtoks

\ctxlua{userdata.elem.setup(\currentelem,\elemparameter{key},\elemparameter{gkey})}%
\to \everysetupelem

\unexpanded\def\elem_cmd#environment%
  {\edef\currentelem{#environment}%
   \dosingleempty\elem_cmd_parameters}

\def\elem_cmd_parameters[#parameters]#content%
  {\setupelem[\currentelem][#parameters]
   From \TeX\: Parameters: key=\elemparameter{key}, 
gkey=\elemparameter{gkey}\par

   \ctxlua{userdata.myfun(\currentelem,key,gkey)}}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext

A simpler alternative is:

% macros=mkvi

\unprotect

\installnamespace{elem}

\installcommandhandler \elem {elem} \elem

\startluacode
userdata.elem = { data = {  } }

function userdata.elem.sync(key,gkey)
userdata.elem.data.key = key
userdata.elem.data.gkey = gkey
end

function userdata.myfun(one,two)
context.par()
context(elem: one=%s,userdata.elem.data[one])
context.par()
context(elem: two=%s,userdata.elem.data[two])
context.par()
end
\stopluacode

\appendtoks
\setuevalue{\currentelem}{\elem_cmd{\currentelem}}%
\to \everydefineelem

\unexpanded\def\elem_cmd#environment%
  {\edef\currentelem{#environment}%
   \dosingleempty\elem_cmd_parameters}

\unexpanded\def\elem_sync

{\ctxlua{userdata.elem.sync(\elemparameter{key},\elemparameter{gkey})}}

\def\elem_cmd_parameters[#parameters]#content%
  {\setupcurrentelem[#parameters]
   \elem_sync
   From \TeX\: Parameters: key=\elemparameter{key}, 
gkey=\elemparameter{gkey}\par

   \ctxlua{userdata.myfun(key,gkey)}}

\defineelem[base]
\setupelem[gkey=gval]

\protect

\starttext

\base[key=val]{Hello}

\stoptext


-
  Hans Hagen | PRAGMA ADE
  Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
 | www.pragma-pod.nl
-
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  :