ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Values from Lua back to ConTeXt
@ 2024-10-02 13:44 Thomas Meyer
  2024-10-02 17:44 ` [NTG-context] " Wolfgang Schuster
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Meyer @ 2024-10-02 13:44 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1004 bytes --]

Hello,
what do I have to do so that the values (d1, m1, y1) from the Lua block 
are returned with Return and the commented out line
\date[d=d1,m=m1,y=y1][weekday,{,~},day,{.~},month,{~},year]
works in ConTeXt.
In other words: how do I do it right?

Thank you in advance
Greetings
Thomas

PS. Sorry, I'm a totally novice in Lua.


\starttext

\startluacode

function ddate(s)
i = 1
j = 4
y1 = string.sub(s,i,j)
i = 5
j = 6
m1 = string.sub(s,i,j)
i = 7
j = 8
d1 = string.sub(s,i,j)
context("Geht das:  %s.%s.%s", d1, m1, y1)         -- that works!
--return d1, m1, y1                                           -- that 
works not! Why?
end

\stopluacode

\def\DDate#1{\ctxlua{ddate("#1")}}

\DDate{20241005}
%\date[d=d1,m=m1,y=y1][weekday,{,~},day,{.~},month,{~},year]
\blank
\date[dd,mm,yyyy][weekday,{,~},day,{.~},month,{~},year]
\blank
\date[d=11,m=7,y=2024][weekday,{,~},day,{.~},month,{~},year]


\stoptext

[-- Attachment #1.2: Type: text/html, Size: 1622 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [NTG-context] Re: Values from Lua back to ConTeXt
  2024-10-02 13:44 [NTG-context] Values from Lua back to ConTeXt Thomas Meyer
@ 2024-10-02 17:44 ` Wolfgang Schuster
  2024-10-03  8:59   ` Thomas Meyer
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfgang Schuster @ 2024-10-02 17:44 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Thomas Meyer

Thomas Meyer schrieb am 02.10.2024 um 15:44:
> Hello,
> what do I have to do so that the values (d1, m1, y1) from the Lua block 
> are returned with Return and the commented out line
> \date[d=d1,m=m1,y=y1][weekday,{,~},day,{.~},month,{~},year]
> works in ConTeXt.
> In other words: how do I do it right?

To call function or pass information from the TeX side to Lua you use 
the \directlua command (although ConTeXt provides many helpers and you 
won't use \directlua itself, e.g. the luacode environment is a wrapper 
for \directlua with a few special features).

To get output back from the Lua side to TeX you use the tex.sprint (or 
tex.print) function.

%%%% begin example
\startluacode

function Date(str)
   local year  = string.sub(str,1,4)
   local month = string.sub(str,5,6)
   local day   = string.sub(str,7,8)
   tex.sprint(day,".",month,".",year)
   -- tex.sprint(day .. "." .. month .. "." .. year)
end

\stopluacode

\def\Date#1{\directlua{Date("#1")}}

\starttext

\Date{20241005}

\stoptext
%%%% end example

To pass information from Lua to the value of the \date command you need 
a separate function call for each of them but ConTeXt makes it easier 
because it provides the possibility to call a TeX command from Lua 
itself where you now can pass Lua data to the command.

%%%% begin example
\startluacode

function userdata.ddate(str)
   local year  = string.sub(str,1,4)
   local month = string.sub(str,5,6)
   local day   = string.sub(str,7,8)
 
context.date({d=day,y=year,m=month},{"weekday,space,day,space,month,space,year"})
end

\stopluacode

\def\DDate#1{\ctxlua{userdata.ddate("#1")}}

\starttext

\DDate{20241005}

\stoptext
%%%% end example

Wolfgang
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [NTG-context] Re: Values from Lua back to ConTeXt
  2024-10-02 17:44 ` [NTG-context] " Wolfgang Schuster
@ 2024-10-03  8:59   ` Thomas Meyer
  2024-10-03  9:27     ` Wolfgang Schuster
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Meyer @ 2024-10-03  8:59 UTC (permalink / raw)
  To: ntg-context


[-- Attachment #1.1: Type: text/plain, Size: 2772 bytes --]

Thank you very much, Wolfgang,

that helps me a lot.
But how can I change the formatting
{“weekday,space,day,space,month,space,year”}
so that I get a comma after weekday and a period after day? Where can I 
find something about this?

Thanks again and have a nice day
Thomas



Am 02.10.24 um 19:44 schrieb Wolfgang Schuster:
> Thomas Meyer schrieb am 02.10.2024 um 15:44:
>> Hello,
>> what do I have to do so that the values (d1, m1, y1) from the Lua 
>> block are returned with Return and the commented out line
>> \date[d=d1,m=m1,y=y1][weekday,{,~},day,{.~},month,{~},year]
>> works in ConTeXt.
>> In other words: how do I do it right?
>
> To call function or pass information from the TeX side to Lua you use 
> the \directlua command (although ConTeXt provides many helpers and you 
> won't use \directlua itself, e.g. the luacode environment is a wrapper 
> for \directlua with a few special features).
>
> To get output back from the Lua side to TeX you use the tex.sprint (or 
> tex.print) function.
>
> %%%% begin example
> \startluacode
>
> function Date(str)
>   local year  = string.sub(str,1,4)
>   local month = string.sub(str,5,6)
>   local day   = string.sub(str,7,8)
>   tex.sprint(day,".",month,".",year)
>   -- tex.sprint(day .. "." .. month .. "." .. year)
> end
>
> \stopluacode
>
> \def\Date#1{\directlua{Date("#1")}}
>
> \starttext
>
> \Date{20241005}
>
> \stoptext
> %%%% end example
>
> To pass information from Lua to the value of the \date command you 
> need a separate function call for each of them but ConTeXt makes it 
> easier because it provides the possibility to call a TeX command from 
> Lua itself where you now can pass Lua data to the command.
>
> %%%% begin example
> \startluacode
>
> function userdata.ddate(str)
>   local year  = string.sub(str,1,4)
>   local month = string.sub(str,5,6)
>   local day   = string.sub(str,7,8)
>
> context.date({d=day,y=year,m=month},{"weekday,space,day,space,month,space,year"}) 
>
> end
>
> \stopluacode
>
> \def\DDate#1{\ctxlua{userdata.ddate("#1")}}
>
> \starttext
>
> \DDate{20241005}
>
> \stoptext
> %%%% end example
>
> Wolfgang
> ___________________________________________________________________________________ 
>
> If your question is of interest to others as well, please add an entry 
> to the Wiki!
>
> maillist : ntg-context@ntg.nl / 
> https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
> webpage  : https://www.pragma-ade.nl / https://context.aanhet.net 
> (mirror)
> archive  : https://github.com/contextgarden/context
> wiki     : https://wiki.contextgarden.net
> ___________________________________________________________________________________ 
>

[-- Attachment #1.2: Type: text/html, Size: 4720 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [NTG-context] Re: Values from Lua back to ConTeXt
  2024-10-03  8:59   ` Thomas Meyer
@ 2024-10-03  9:27     ` Wolfgang Schuster
  2024-10-03  9:44       ` Thomas Meyer
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfgang Schuster @ 2024-10-03  9:27 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Thomas Meyer

Thomas Meyer schrieb am 03.10.2024 um 10:59:
> Thank you very much, Wolfgang,
> 
> that helps me a lot.
> But how can I change the formatting
> {“weekday,space,day,space,month,space,year”}
> so that I get a comma after weekday and a period after day? Where can I 
> find something about this?

Aside from keywords like day, weekday etc. the \date (and \currentdate) 
commands allows free input which used as separator between these 
keywords. To set a period as separator just put . as entry in the list 
but ensure to use braces around , because it will otherwise be 
interpreted as list separator.

When you use TeX command within Lua use double backslashes (\\) because 
a backslash has special meaning here and with \\ you tell Lua to put a 
single \ in the output.

%%%% begin example
\starttext

\currentdate[weekday,{, },day,. ,month,space,year]
%\currentdate[weekday,{, },day,.\ ,month,space,year]

\currentdate[weekday,\textcomma\ ,day,\textperiod\ ,month,space,year]

\startluacode
context.currentdate{ "weekday,{, },day,. ,month,space,year" }
\stopluacode

\startluacode
context.currentdate{ "weekday,\\textcomma\\ ,day,\\textperiod\\ 
,month,space,year" }
\stopluacode

\stoptext
%%%% end example

Wolfgang
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [NTG-context] Re: Values from Lua back to ConTeXt
  2024-10-03  9:27     ` Wolfgang Schuster
@ 2024-10-03  9:44       ` Thomas Meyer
  0 siblings, 0 replies; 5+ messages in thread
From: Thomas Meyer @ 2024-10-03  9:44 UTC (permalink / raw)
  To: Wolfgang Schuster, mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1486 bytes --]

Ah, thank you, Wolfgang,

I tried {, } and not {, },. The result is that the rest vanishes.


Thomas


Am 03.10.24 um 11:27 schrieb Wolfgang Schuster:
> Thomas Meyer schrieb am 03.10.2024 um 10:59:
>> Thank you very much, Wolfgang,
>>
>> that helps me a lot.
>> But how can I change the formatting
>> {“weekday,space,day,space,month,space,year”}
>> so that I get a comma after weekday and a period after day? Where can 
>> I find something about this?
>
> Aside from keywords like day, weekday etc. the \date (and 
> \currentdate) commands allows free input which used as separator 
> between these keywords. To set a period as separator just put . as 
> entry in the list but ensure to use braces around , because it will 
> otherwise be interpreted as list separator.
>
> When you use TeX command within Lua use double backslashes (\\) 
> because a backslash has special meaning here and with \\ you tell Lua 
> to put a single \ in the output.
>
> %%%% begin example
> \starttext
>
> \currentdate[weekday,{, },day,. ,month,space,year]
> %\currentdate[weekday,{, },day,.\ ,month,space,year]
>
> \currentdate[weekday,\textcomma\ ,day,\textperiod\ ,month,space,year]
>
> \startluacode
> context.currentdate{ "weekday,{, },day,. ,month,space,year" }
> \stopluacode
>
> \startluacode
> context.currentdate{ "weekday,\\textcomma\\ ,day,\\textperiod\\ 
> ,month,space,year" }
> \stopluacode
>
> \stoptext
> %%%% end example
>
> Wolfgang

[-- Attachment #1.2: Type: text/html, Size: 2464 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-10-03  9:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-02 13:44 [NTG-context] Values from Lua back to ConTeXt Thomas Meyer
2024-10-02 17:44 ` [NTG-context] " Wolfgang Schuster
2024-10-03  8:59   ` Thomas Meyer
2024-10-03  9:27     ` Wolfgang Schuster
2024-10-03  9:44       ` Thomas Meyer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).