ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Bruce Horrocks <ntg@scorecrow.com>
To: ntg-context mailing list <ntg-context@ntg.nl>
Subject: [NTG-context] Re: using an end of line as parameter
Date: Wed, 21 Feb 2024 14:27:58 +0000	[thread overview]
Message-ID: <8D4B172E-A832-4F16-ACBB-B6E57A771285@scorecrow.com> (raw)
In-Reply-To: <170850347690.3180173.11581736153351775295@cgl.ntg.nl>

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



> On 21 Feb 2024, at 08:17, madiazm.eoicc@gmail.com wrote:
> 
> Thanks a lot Bruce, that did the trick! definitely I will have to learn a bit lua in summer.
> I just got an error on \qquad and found searching in internet that it was because escape character conflicts between tex and lua. For the time i removed it and since I will create itemizes I hope I dont get that error if I use \item instead of \qquad.
> Thanks again, I spent hours of trial and error, error, error... on this matter.

Yes, the \'s need to be escaped in Lua. And it's made me realise that the process of identifying the four lines of the question is separate from the typesetting of the question later. The amended version below uses your \tareaABC macro to format the question so you can more easily change the layout without having to understand ConTeXT Lua Documents in detail.

\startluacode
  userdata = userdata or {}
  
  function userdata.formatTestQuestions()
    local the_buffer = buffers.getlines("TestQuestions")
    local tracker = 0
    local the_question = {}
    
    -- Go through the buffer of questions one line at a time
    for i = 1,#the_buffer do

      -- Skip blank lines but 'collect' non-blank lines until we have four
      -- (which is assumed to be a whole question)
      if  string.strip(the_buffer[i]) == "" then
        tracker = 0
        the_question = {}
      else
        tracker = tracker + 1
        the_question[tracker] = the_buffer[i]
      end
      
      -- If tracker has got to 4 then we've read four lines
      if tracker == 4 then
        context.tareaAbc(
          the_question[1],
          the_question[2], 
          the_question[3],
          the_question[4])
                  
        -- Reset for the next question (in case no blank line)
        tracker = 0
        the_question = {}
      end
      
    end
  end
\stopluacode

\def\startTestQuestions
  {\dostartbuffer[TestQuestions][startTestQuestions][stopTestQuestions]}
\def\stopTestQuestions
  {\ctxlua{userdata.formatTestQuestions()}}

\define[4]\tareaAbc{\item #1
   \startitemgroup[itemize][a]
   \item #2
   \item #3
   \item #4
   \stopitemgroup}
   
\starttext
Here are some questions:

\startTestQuestions
In den Büchereien gibt es auch … 
… Kuchen.
… Theater.
… Workshops.

In den Büchereien gibt es auch … 
… Kuchen.
… Theater.
… Workshops.
In den Büchereien gibt es auch … 
… Kuchen.
… Theater.
… Workshops.
\stopTestQuestions

\stoptext

—
Bruce Horrocks
Hampshire, UK

[-- Attachment #2: tt2.tex --]
[-- Type: application/octet-stream, Size: 1681 bytes --]

\startluacode
  userdata = userdata or {}
  
  function userdata.formatTestQuestions()
    local the_buffer = buffers.getlines("TestQuestions")
    local tracker = 0
    local the_question = {}
    
    -- Go through the buffer of questions one line at a time
    for i = 1,#the_buffer do

      -- Skip blank lines but 'collect' non-blank lines until we have four
      -- (which is assumed to be a whole question)
      if  string.strip(the_buffer[i]) == "" then
        tracker = 0
        the_question = {}
      else
        tracker = tracker + 1
        the_question[tracker] = the_buffer[i]
      end
      
      -- If tracker has got to 4 then we've read four lines
      if tracker == 4 then
        context.tareaAbc(
          the_question[1],
          the_question[2], 
          the_question[3],
          the_question[4])
                  
        -- Reset for the next question (in case no blank line)
        tracker = 0
        the_question = {}
      end
      
    end
  end
\stopluacode

\def\startTestQuestions
  {\dostartbuffer[TestQuestions][startTestQuestions][stopTestQuestions]}
\def\stopTestQuestions
  {\ctxlua{userdata.formatTestQuestions()}}

\define[4]\tareaAbc{\item #1
   \startitemgroup[itemize][a]
   \item #2
   \item #3
   \item #4
   \stopitemgroup}
   
\starttext
Here are some questions:

\startTestQuestions
In den Büchereien gibt es auch … 
… Kuchen.
… Theater.
… Workshops.

In den Büchereien gibt es auch … 
… Kuchen.
… Theater.
… Workshops.
In den Büchereien gibt es auch … 
… Kuchen.
… Theater.
… Workshops.
\stopTestQuestions

\stoptext

[-- Attachment #3: 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
___________________________________________________________________________________

  reply	other threads:[~2024-02-21 14:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19 12:49 [NTG-context] " Miguel Diaz
2024-02-19 22:36 ` [NTG-context] " Hans Hagen
2024-02-20 11:26   ` madiazm.eoicc
2024-02-20 12:13     ` Hans Hagen
2024-02-20 12:59       ` madiazm.eoicc
2024-02-20 19:10         ` Bruce Horrocks
2024-02-21  8:17           ` madiazm.eoicc
2024-02-21 14:27             ` Bruce Horrocks [this message]
2024-02-22 10:17               ` madiazm.eoicc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8D4B172E-A832-4F16-ACBB-B6E57A771285@scorecrow.com \
    --to=ntg@scorecrow.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).