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: Tue, 20 Feb 2024 19:10:18 +0000	[thread overview]
Message-ID: <744177EC-F639-400E-BFB4-07731DE86B9F@scorecrow.com> (raw)
In-Reply-To: <170843395043.3180173.12511363966774513204@cgl.ntg.nl>

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



> On 20 Feb 2024, at 12:59, madiazm.eoicc@gmail.com wrote:
> 
> no, I just want to split at the end of each sentence to get the four arguments: now I pass this four lines to my macro \tareaAbc (with the dirty trick of ñ)
> 
> In den Büchereien gibt es auch …ñ 
> … Kuchen.ñ       
> … Theater.ñ       
> … Workshops.ñ 
> 
> and I wish your context.foo(lines[i]) iteration would become a single context.tareaAbc(the four arguments somehow separated so that I can manage each line with the corresponding context formatting)
> 
> As you see my definition is: \def\tareaAbc #1ñ#2ñ#3ñ#4ñ{...context formating for each #)}; Its the clue to pass each sentence as an independent argument that I don't get to work.
> thanks again
> 

A variation on Hans original suggestion is to use a buffer instead of a separate text file, combined with Lua.

\startluacode
  userdata = userdata or {}
  
  function userdata.formatTestQuestions()
    local the_buffer = buffers.getlines("TestQuestions")
    local tracker = 0
    local the_question = {}
    local letters = {"-", "a", "b", "c"}
    
    -- 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.startlines()
        context("{\\bf Beispiel:} %s", the_question[1])
        context(true)
        for answer = 2,4 do
          context("\\qquad %s) %s", letters[answer], the_question[answer])
          context(true)
        end
        context.stoplines()
                  
        -- 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()}}

\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

How do you think you did on that test? Here's another one.

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

\stoptext

—
Bruce Horrocks
Hampshire, UK

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

\startluacode
  userdata = userdata or {}
  
  function userdata.formatTestQuestions()
    local the_buffer = buffers.getlines("TestQuestions")
    local tracker = 0
    local the_question = {}
    local letters = {"-", "a", "b", "c"}
    
    -- 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.startlines()
        context("{\\bf Beispiel:} %s", the_question[1])
        context(true)
        for answer = 2,4 do
          context("\\qquad %s) %s", letters[answer], the_question[answer])
          context(true)
        end
        context.stoplines()
                  
        -- 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()}}

\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

How do you think you did on that test? Here's another one.

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

\stoptext

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



[-- Attachment #4: 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-20 19:15 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 [this message]
2024-02-21  8:17           ` madiazm.eoicc
2024-02-21 14:27             ` Bruce Horrocks
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=744177EC-F639-400E-BFB4-07731DE86B9F@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).