ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* process list items differently depending on position in list
@ 2022-05-02  7:14 Denis Maier via ntg-context
  2022-05-02 10:09 ` Denis Maier via ntg-context
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Maier via ntg-context @ 2022-05-02  7:14 UTC (permalink / raw)
  To: ntg-context; +Cc: denis.maier


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

Hi,

I need to process a comma separated list, and adapt the formatting according to the position of an item in the list (example below). Expected output would be:
{\bf foo
}, {\em bar} and baz.

Is that possible? Can I somehow get the position of an item in the list? (My understanding is that \processcommalist and \processcommacommand apply a command to each list item, but there is no such thing as <position in the list>... I hope I'm wrong here.)

Or is that something where using Lua would be a better choice?

Best,
Denis

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\define[1]\command
  {#1}

\define\somelist
  {foo, bar, baz}

\starttext

  \processcommacommand
    [\somelist]
    \command

\stoptext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

[-- Attachment #2: Type: text/plain, Size: 493 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] 4+ messages in thread

* Re: process list items differently depending on position in list
  2022-05-02  7:14 process list items differently depending on position in list Denis Maier via ntg-context
@ 2022-05-02 10:09 ` Denis Maier via ntg-context
  2022-05-02 10:33   ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 4+ messages in thread
From: Denis Maier via ntg-context @ 2022-05-02 10:09 UTC (permalink / raw)
  To: ntg-context; +Cc: denis.maier


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

Ok, in lua it's indeed simple:

\startluacode
list = {"foo", "bar", "baz"}
\stopluacode

\starttext

\startluacode
for i, v in ipairs(list) do
    if i == #list then
        tex.print("last:")
        tex.print(v)
    elseif i == 1 then
        tex.print("first:")
        tex.print(v)
    else
        tex.print("in between:")
        tex.print(v)
    end
end
\stopluacode

\stoptext

But, it'd still love to hear whether there is a solution on the tex side.

Denis

Von: Maier, Denis Christian (UB)
Gesendet: Montag, 2. Mai 2022 09:15
An: 'mailing list for ConTeXt users' <ntg-context@ntg.nl>
Betreff: process list items differently depending on position in list

Hi,

I need to process a comma separated list, and adapt the formatting according to the position of an item in the list (example below). Expected output would be:
{\bf foo
}, {\em bar} and baz.

Is that possible? Can I somehow get the position of an item in the list? (My understanding is that \processcommalist and \processcommacommand apply a command to each list item, but there is no such thing as <position in the list>... I hope I'm wrong here.)

Or is that something where using Lua would be a better choice?

Best,
Denis

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\define[1]\command
  {#1}

\define\somelist
  {foo, bar, baz}

\starttext

  \processcommacommand
    [\somelist]
    \command

\stoptext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

[-- Attachment #2: Type: text/plain, Size: 493 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] 4+ messages in thread

* Re: process list items differently depending on position in list
  2022-05-02 10:09 ` Denis Maier via ntg-context
@ 2022-05-02 10:33   ` Hans Hagen via ntg-context
  2022-05-02 11:50     ` Denis Maier via ntg-context
  0 siblings, 1 reply; 4+ messages in thread
From: Hans Hagen via ntg-context @ 2022-05-02 10:33 UTC (permalink / raw)
  To: Denis Maier via ntg-context; +Cc: Hans Hagen

On 5/2/2022 12:09 PM, Denis Maier via ntg-context wrote:

> But, it’d still love to hear whether there is a solution on the tex side.
\processtokens {[before]} {[between]} {[after]} {[space]} 
{{one}{two}{three}}

\def\whatever{a,b,c,d}

\getcommacommandsize[\whatever]
\scratchcounterone \zerocount
\scratchcountertwo \commalistsize

\processcommacommand[\whatever]
   {\advance\scratchcounterone\plusone
    \ifnum\scratchcounterone=\scratchcountertwo
      \space and\space
    \orelse\ifnum\scratchcounterone>\plusone
      ,\space
    \fi
    \commalistelement}

but ... there's also:

\startlines
\commalistsentence[aap,noot,mies]
\commalistsentence[aap,noot]
\commalistsentence[aap]
\commalistsentence[a,b,c]
\commalistsentence[a,b,c][{ \& },{ and }]
\commalistsentence[a,b,c][+,-]
\stoplines

which uses presets like

\setuplabeltext [nl] [and-1={{, }}, and-2={{ en }}]   % 1, 2 en 3
\setuplabeltext [en] [and-1={{, }}, and-2={{, }}]     % 1, 2, 3
\setuplabeltext [de] [and-1={{, }}, and-2={{ und }}]  % 1, 2 und 3
\setuplabeltext [hr] [and-1={{, }}, and-2={{ i }}]    % 1, 2 i 3

maybe wikify


-----------------------------------------------------------------
                                           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] 4+ messages in thread

* Re: process list items differently depending on position in list
  2022-05-02 10:33   ` Hans Hagen via ntg-context
@ 2022-05-02 11:50     ` Denis Maier via ntg-context
  0 siblings, 0 replies; 4+ messages in thread
From: Denis Maier via ntg-context @ 2022-05-02 11:50 UTC (permalink / raw)
  To: ntg-context; +Cc: denis.maier

> -----Ursprüngliche Nachricht-----
> Von: ntg-context <ntg-context-bounces@ntg.nl> Im Auftrag von Hans Hagen
> via ntg-context
> Gesendet: Montag, 2. Mai 2022 12:34
> An: Denis Maier via ntg-context <ntg-context@ntg.nl>
> Cc: Hans Hagen <j.hagen@xs4all.nl>
> Betreff: Re: [NTG-context] process list items differently depending on
> position in list
> 
> On 5/2/2022 12:09 PM, Denis Maier via ntg-context wrote:
> 
> > But, it’d still love to hear whether there is a solution on the tex side.
> \processtokens {[before]} {[between]} {[after]} {[space]} {{one}{two}{three}}
> 
> \def\whatever{a,b,c,d}
> 
> \getcommacommandsize[\whatever]
> \scratchcounterone \zerocount
> \scratchcountertwo \commalistsize
> 
> \processcommacommand[\whatever]
>    {\advance\scratchcounterone\plusone
>     \ifnum\scratchcounterone=\scratchcountertwo
>       \space and\space
>     \orelse\ifnum\scratchcounterone>\plusone
>       ,\space
>     \fi
>     \commalistelement}

Thanks.

> 
> but ... there's also:
> 
> \startlines
> \commalistsentence[aap,noot,mies]
> \commalistsentence[aap,noot]
> \commalistsentence[aap]
> \commalistsentence[a,b,c]
> \commalistsentence[a,b,c][{ \& },{ and }] \commalistsentence[a,b,c][+,-]
> \stoplines
> 
> which uses presets like
> 
> \setuplabeltext [nl] [and-1={{, }}, and-2={{ en }}]   % 1, 2 en 3
> \setuplabeltext [en] [and-1={{, }}, and-2={{, }}]     % 1, 2, 3
> \setuplabeltext [de] [and-1={{, }}, and-2={{ und }}]  % 1, 2 und 3
> \setuplabeltext [hr] [and-1={{, }}, and-2={{ i }}]    % 1, 2 i 3
> 
> maybe wikify

I've just checked, \commalistsentence is already on the wiki, but you have to know it exists. (I'll check whether I can link to that page from somewhere.)

But there's no (high-level) way to apply certain commands to this list elements, right? 
Background: My real use case is a bit more complex. I use comma separated lists to store author lists, but the authors themselves are saved in structured variables. So, I'll need to reassemble the different name parts first.
So, nothing like 
\commalistprocessandmakesentence[a,b,c]\commandforfirst\commandforinbetween\commandforlast
Probably to specific, right?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startbuffer[test]
<?xml version='1.0' standalone='yes?>
<document>
<title>This is the title</title>
<author>
<family-name>Doe</family-name>
<given-name>John</given-name>
</author>
<author>
<family-name>Smith</family-name>
<given-name>Jane</given-name>
</author>
<p>This is a first sentence</p>
</document>
\stopbuffer

\startxmlsetups xml:setup
  \xmlsetsetup{\xmldocument}{*}{-}
  \xmlsetsetup{\xmldocument}{document|p}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:setup}

\startxmlsetups xml:document
  \xmlfilter{#1}{/title/command(xml:title)}
  \xmlfilter{#1}{/author/command(xml:author)}
  \startdocument
  \xmlflush{#1}
  \stopdocument
\stopxmlsetups

\startxmlsetups xml:title
  \setupdocument[title={\xmlflush{#1}},author={\AuthorList}]
\stopxmlsetups

\startxmlsetups xml:author:family-name
	\xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:author:given-name
	\xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:author
  \defineauthor[\xmlfilter{#1}{/family-name/command(xml:author:family-name)}\xmlfilter{#1}{/given-name/command(xml:author:given-name)}][family-name={\xmlfilter{#1}{/family-name/command(xml:author:family-name)}},given-name={\xmlfilter{#1}{/given-name/command(xml:author:given-name)}}]
  \addtocommalist {\xmlfilter{#1}{/family-name/command(xml:author:family-name)}\xmlfilter{#1}{/given-name/command(xml:author:given-name)}} \AuthorList
\stopxmlsetups

\startxmlsetups xml:p
  \xmlflush{#1}\par
\stopxmlsetups

\definenamespace
   [documentauthor] % name of internal varialbles
   [type=module,
    name=author, 
    command=yes, % Create \defineauthor
    style=yes, % Create \useauthorstyleandcolor 
    setup=list, % Create \setupauthor
    parent=documentauthor,
  ]
  
\def\AuthorList{}

\define[1]\useauthor
    {\edef\currentauthor{#1}%
    {\authorparameter{given-name}
         \space
          \authorparameter{family-name}}}

\startsetups document:start
    \documentvariable{title}\endgraf
    \blank[medium]
    \processcommacommand[\documentvariable{author}]\useauthor
    \endgraf
\stopsetups

\xmlprocessbuffer{main}{test}{}




___________________________________________________________________________________
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] 4+ messages in thread

end of thread, other threads:[~2022-05-02 11:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-02  7:14 process list items differently depending on position in list Denis Maier via ntg-context
2022-05-02 10:09 ` Denis Maier via ntg-context
2022-05-02 10:33   ` Hans Hagen via ntg-context
2022-05-02 11:50     ` Denis Maier via ntg-context

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