ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Sending data from lua to context and back to lua
@ 2018-02-07 17:39 Rudolf Bahr
  2018-02-08  5:51 ` Rudolf Bahr
  2018-02-08  8:30 ` Thomas A. Schmitz
  0 siblings, 2 replies; 8+ messages in thread
From: Rudolf Bahr @ 2018-02-07 17:39 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 2228 bytes --]


Hello Contexteers!

Sometimes the necessity could arise to send data from lua to context
and answers back to lua. I tried this by using "read" and
"immediate\write" commands in context. These commands are described
in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".

My question is: Do there exist better and more simple solutions?
Here is my WE which is admittedly a little bit ugly, but it works.
Appended the pdf-output:

========================================================================
\starttext

\startluacode

tex.print("Here is lua.")
tex.print(" ")
tex.print("I'm sending some picture names to Context in order to get back their sizes.")

local imageA = "2007-01-01---Intro.png"
local imageB = "2007-01-01---Neujahrsballade---1.png"
local imageC = "2007-01-01---Neujahrsballade---2.png"

local f = assert(io.open("contextin.txt", "w"))
f:write(imageA .. "\n" .. imageB .. "\n" .. imageC)
f:close()

\stopluacode

% -------------------------------------------------------------

\openin15=contextin.txt

\read15 to \zeileA
\read15 to \zeileB
\read15 to \zeileC

\closein15

\vskip\lineheight
=====================================================
\vskip\lineheight
Here is \CONTEXT\ and what I got from Lua:
\vskip\lineheight

\zeileA \\
\zeileB \\
\zeileC

\vskip\lineheight
Now I'm working ... and write my answers to a file for lua: \\

\zeileA\ width=3000pt height=2000pt \\
\zeileB\ width=1200pt height=900pt  \\
\zeileC\ width=1600pt height=1200pt \\

\immediate\openout7=contextout.txt

\immediate\write7{\zeileA\ width=3000pt height=2000pt \\}
\immediate\write7{\zeileB\ width=1200pt height=900pt  \\}
\immediate\write7{\zeileC\ width=1600pt height=1200pt \\}

\immediate\closeout7

\vskip\lineheight
=====================================================
\vskip\lineheight

% -------------------------------------------------------------

\startluacode

tex.print("Here is lua again and what I got back from Context:")
tex.print(" ")
tex.print(" ")

local f = assert(io.open("contextout.txt", "r"))
local contexts_answer = f:read("*a")
f:close()

tex.print(contexts_answer)

\stopluacode


\stoptext

========================================================================

Best regards,

Rudolf


[-- Attachment #2: input-test.pdf --]
[-- Type: application/pdf, Size: 9806 bytes --]

[-- Attachment #3: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-07 17:39 Sending data from lua to context and back to lua Rudolf Bahr
@ 2018-02-08  5:51 ` Rudolf Bahr
  2018-02-08  8:30 ` Thomas A. Schmitz
  1 sibling, 0 replies; 8+ messages in thread
From: Rudolf Bahr @ 2018-02-08  5:51 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Wed, Feb 07, 2018 at 06:39:53PM +0100, Bahr Rudolf wrote:
> 
> Hello Contexteers!
> 
> Sometimes the necessity could arise to send data from lua to context
> and answers back to lua. I tried this by using "read" and
> "immediate\write" commands in context. These commands are described
> in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
> 
> My question is: Do there exist better and more simple solutions?
> Here is my WE which is admittedly a little bit ugly, but it works.
> Appended the pdf-output:
> 
> ========================================================================
> \starttext
> 
> \startluacode
> 
> tex.print("Here is lua.")
> tex.print(" ")
> tex.print("I'm sending some picture names to Context in order to get back their sizes.")
> 
> local imageA = "2007-01-01---Intro.png"
> local imageB = "2007-01-01---Neujahrsballade---1.png"
> local imageC = "2007-01-01---Neujahrsballade---2.png"
> 
> local f = assert(io.open("contextin.txt", "w"))
> f:write(imageA .. "\n" .. imageB .. "\n" .. imageC)
> f:close()
> 
> \stopluacode
> 
> % -------------------------------------------------------------
> 
> \openin15=contextin.txt
> 
> \read15 to \zeileA
> \read15 to \zeileB
> \read15 to \zeileC
> 
> \closein15
> 
> \vskip\lineheight
> =====================================================
> \vskip\lineheight
> Here is \CONTEXT\ and what I got from Lua:
> \vskip\lineheight
> 
> \zeileA \\
> \zeileB \\
> \zeileC
> 
> \vskip\lineheight
> Now I'm working ... and write my answers to a file for lua: \\
> 
> \zeileA\ width=3000pt height=2000pt \\
> \zeileB\ width=1200pt height=900pt  \\
> \zeileC\ width=1600pt height=1200pt \\
> 
> \immediate\openout7=contextout.txt
> 
> \immediate\write7{\zeileA\ width=3000pt height=2000pt \\}
> \immediate\write7{\zeileB\ width=1200pt height=900pt  \\}
> \immediate\write7{\zeileC\ width=1600pt height=1200pt \\}
> 
> \immediate\closeout7
> 
> \vskip\lineheight
> =====================================================
> \vskip\lineheight
> 
> % -------------------------------------------------------------
> 
> \startluacode
> 
> tex.print("Here is lua again and what I got back from Context:")
> tex.print(" ")
> tex.print(" ")
> 
> local f = assert(io.open("contextout.txt", "r"))
> local contexts_answer = f:read("*a")
> f:close()
> 
> tex.print(contexts_answer)
> 
> \stopluacode
> 
> 
> \stoptext
> 
> ========================================================================
> 
> Best regards,
> 
> Rudolf
> 

Sorry, Donald E. Knuth's macros aren't in chapters 21 and 22, but in
chapters 20 and 21!

Rudolf 

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-07 17:39 Sending data from lua to context and back to lua Rudolf Bahr
  2018-02-08  5:51 ` Rudolf Bahr
@ 2018-02-08  8:30 ` Thomas A. Schmitz
  2018-02-08 14:34   ` Rudolf Bahr
  1 sibling, 1 reply; 8+ messages in thread
From: Thomas A. Schmitz @ 2018-02-08  8:30 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 02/07/2018 06:39 PM, Rudolf Bahr wrote:
> Sometimes the necessity could arise to send data from lua to context
> and answers back to lua. I tried this by using "read" and
> "immediate\write" commands in context. These commands are described
> in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
> 
> My question is: Do there exist better and more simple solutions?

You can print (typeset) information from inside lua code with the 
"context" function. Have you seen the manual cld-mkiv.pdf? It's in the 
minimals.

Thomas
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-08  8:30 ` Thomas A. Schmitz
@ 2018-02-08 14:34   ` Rudolf Bahr
  2018-02-08 15:18     ` Thomas A. Schmitz
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Rudolf Bahr @ 2018-02-08 14:34 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, Feb 08, 2018 at 09:30:48AM +0100, Thomas A. Schmitz wrote:
> On 02/07/2018 06:39 PM, Rudolf Bahr wrote:
> > Sometimes the necessity could arise to send data from lua to context
> > and answers back to lua. I tried this by using "read" and
> > "immediate\write" commands in context. These commands are described
> > in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
> > 
> > My question is: Do there exist better and more simple solutions?
> 
> You can print (typeset) information from inside lua code with the "context"
> function. Have you seen the manual cld-mkiv.pdf? It's in the minimals.
> 
> Thomas

Yes, thank you. I tried "context()" several times, but without success.
Could be, that I have a false meaning how to use it: If I transfer data
from context to lua and vice versa then in my imagination there must be 
variables in both languages  where the sent data is to be found.

With the use of "\immediate\write" and "\read" in context and the read and write
functions in lua I know exactly from where to where the data goes. How to do 
the same with "context()"?

Rudolf
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-08 14:34   ` Rudolf Bahr
@ 2018-02-08 15:18     ` Thomas A. Schmitz
  2018-02-08 15:25     ` Hans Hagen
  2018-02-08 15:26     ` Henning Hraban Ramm
  2 siblings, 0 replies; 8+ messages in thread
From: Thomas A. Schmitz @ 2018-02-08 15:18 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 08.02.2018 15:34, Rudolf Bahr wrote:
> With the use of "\immediate\write" and "\read" in context and the read and write
> functions in lua I know exactly from where to where the data goes. How to do
> the same with "context()"?

This question is impossible to answer - what is "the same"? Please make 
a minimal example to show what you have tried and where you had trouble 
- the manual I referred to provides good starting points.

Thomas
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-08 14:34   ` Rudolf Bahr
  2018-02-08 15:18     ` Thomas A. Schmitz
@ 2018-02-08 15:25     ` Hans Hagen
  2018-02-08 15:26     ` Henning Hraban Ramm
  2 siblings, 0 replies; 8+ messages in thread
From: Hans Hagen @ 2018-02-08 15:25 UTC (permalink / raw)
  To: ntg-context

On 2/8/2018 3:34 PM, Rudolf Bahr wrote:
> On Thu, Feb 08, 2018 at 09:30:48AM +0100, Thomas A. Schmitz wrote:
>> On 02/07/2018 06:39 PM, Rudolf Bahr wrote:
>>> Sometimes the necessity could arise to send data from lua to context
>>> and answers back to lua. I tried this by using "read" and
>>> "immediate\write" commands in context. These commands are described
>>> in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
>>>
>>> My question is: Do there exist better and more simple solutions?
>>
>> You can print (typeset) information from inside lua code with the "context"
>> function. Have you seen the manual cld-mkiv.pdf? It's in the minimals.
>>
>> Thomas
> 
> Yes, thank you. I tried "context()" several times, but without success.
> Could be, that I have a false meaning how to use it: If I transfer data
> from context to lua and vice versa then in my imagination there must be
> variables in both languages  where the sent data is to be found.
> 
> With the use of "\immediate\write" and "\read" in context and the read and write
> functions in lua I know exactly from where to where the data goes. How to do
> the same with "context()"?
I have no clue what you want to achieve but Thomas means:

\setupwhitespace[line]

\starttext

\startluacode
     context("Here is lua.")
     context.par()
     context("I'm sending some picture names to Context in order to get 
back their sizes.")

     local imageA = "2007-01-01---Intro.png"
     local imageB = "2007-01-01---Neujahrsballade---1.png"
     local imageC = "2007-01-01---Neujahrsballade---2.png"

     local f = io.open("contextin.txt", "w")
     f:write(imageA,"\n",imageB,"\n",imageC)
     f:close()
\stopluacode

\openin15=contextin.txt
\read15 to \zeileA
\read15 to \zeileB
\read15 to \zeileC
\closein15

Here is \CONTEXT\ and what I got from Lua:

\startlines
\zeileA
\zeileB
\zeileC
\stoplines

Now I'm working ... and write my answers to a file for lua:

\startlines
\zeileA\ width=3000pt height=2000pt
\zeileB\ width=1200pt height=900pt
\zeileC\ width=1600pt height=1200pt
\stoplines

\immediate\openout7=contextout.txt
\immediate\write7{\zeileA\ width=3000pt height=2000pt \\}
\immediate\write7{\zeileB\ width=1200pt height=900pt  \\}
\immediate\write7{\zeileC\ width=1600pt height=1200pt \\}
\immediate\closeout7

\startluacode
     context("Here is lua again and what I got back from Context:")
     context.par()
     context(io.loaddata("contextout.txt"))
\stopluacode

\stoptext


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | 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  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-08 14:34   ` Rudolf Bahr
  2018-02-08 15:18     ` Thomas A. Schmitz
  2018-02-08 15:25     ` Hans Hagen
@ 2018-02-08 15:26     ` Henning Hraban Ramm
  2018-02-08 16:28       ` Rudolf Bahr
  2 siblings, 1 reply; 8+ messages in thread
From: Henning Hraban Ramm @ 2018-02-08 15:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Am 2018-02-08 um 15:34 schrieb Rudolf Bahr <quasi@quasi.de>:

> On Thu, Feb 08, 2018 at 09:30:48AM +0100, Thomas A. Schmitz wrote:
>> On 02/07/2018 06:39 PM, Rudolf Bahr wrote:
>>> Sometimes the necessity could arise to send data from lua to context
>>> and answers back to lua. I tried this by using "read" and
>>> "immediate\write" commands in context. These commands are described
>>> in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
>>> 
>>> My question is: Do there exist better and more simple solutions?
>> 
>> You can print (typeset) information from inside lua code with the "context"
>> function. Have you seen the manual cld-mkiv.pdf? It's in the minimals.
>> 
>> Thomas
> 
> Yes, thank you. I tried "context()" several times, but without success.
> Could be, that I have a false meaning how to use it: If I transfer data
> from context to lua and vice versa then in my imagination there must be 
> variables in both languages  where the sent data is to be found.
> 
> With the use of "\immediate\write" and "\read" in context and the read and write
> functions in lua I know exactly from where to where the data goes. How to do 
> the same with "context()"?

\startluacode

function SomeFunction()
  context("Value")
end

\stopluacode

\def\MyVar{\ctxlua{SomeFunction()}}

Greetlings, Hraban
---
http://www.fiee.net
http://wiki.contextgarden.net
GPG Key ID 1C9B22FD

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Sending data from lua to context and back to lua
  2018-02-08 15:26     ` Henning Hraban Ramm
@ 2018-02-08 16:28       ` Rudolf Bahr
  0 siblings, 0 replies; 8+ messages in thread
From: Rudolf Bahr @ 2018-02-08 16:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, Feb 08, 2018 at 04:26:16PM +0100, Henning Hraban Ramm wrote:
> Am 2018-02-08 um 15:34 schrieb Rudolf Bahr <quasi@quasi.de>:
> 
> > On Thu, Feb 08, 2018 at 09:30:48AM +0100, Thomas A. Schmitz wrote:
> >> On 02/07/2018 06:39 PM, Rudolf Bahr wrote:
> >>> Sometimes the necessity could arise to send data from lua to context
> >>> and answers back to lua. I tried this by using "read" and
> >>> "immediate\write" commands in context. These commands are described
> >>> in chapter 21 and 22 of Donald E. Knuth's "The TeXbook".
> >>> 
> >>> My question is: Do there exist better and more simple solutions?
> >> 
> >> You can print (typeset) information from inside lua code with the "context"
> >> function. Have you seen the manual cld-mkiv.pdf? It's in the minimals.
> >> 
> >> Thomas
> > 
> > Yes, thank you. I tried "context()" several times, but without success.
> > Could be, that I have a false meaning how to use it: If I transfer data
> > from context to lua and vice versa then in my imagination there must be 
> > variables in both languages  where the sent data is to be found.
> > 
> > With the use of "\immediate\write" and "\read" in context and the read and write
> > functions in lua I know exactly from where to where the data goes. How to do 
> > the same with "context()"?

 
> \startluacode
> 
> function SomeFunction()
>   context("Value")
> end
> 
> \stopluacode
> 
> \def\MyVar{\ctxlua{SomeFunction()}}
> 

YES! That's what I needed to get back data from lua to context!
Many thanks to Hraban, Hans and Thomas for your help!

Rudolf 

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2018-02-08 16:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-07 17:39 Sending data from lua to context and back to lua Rudolf Bahr
2018-02-08  5:51 ` Rudolf Bahr
2018-02-08  8:30 ` Thomas A. Schmitz
2018-02-08 14:34   ` Rudolf Bahr
2018-02-08 15:18     ` Thomas A. Schmitz
2018-02-08 15:25     ` Hans Hagen
2018-02-08 15:26     ` Henning Hraban Ramm
2018-02-08 16:28       ` Rudolf Bahr

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).