ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] How to change the way one input file's data is displayed, depending on where it is in the document.
       [not found] <1708463364.6078783.1727133586176.ref@mail.yahoo.com>
@ 2024-09-23 23:19 ` Joel via ntg-context
  2024-09-25 17:37   ` [NTG-context] " Bruce Horrocks
  0 siblings, 1 reply; 2+ messages in thread
From: Joel via ntg-context @ 2024-09-23 23:19 UTC (permalink / raw)
  To: Mailing List for ConTeXt Users; +Cc: Joel


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

I have some code like this--it is a very reduced minimum-working example, from 10,000+ lines of code:
fileA.tex------------------------------

\datatypeA{fish}{an animal that lives in the sea}
\datatypeA{bear}{a big animal with claws and fur}
\datatypeA{squid}{a sea creature with many legs}

fileB.tex------------------------------

\datatypeB{fish}{an animal that lives in the sea}
\datatypeB{bear}{a big animal with claws and fur}
\datatypeB{squid}{a sea creature with many legs}

main.tex------------------------------

\datatypeA[2]{\startitemize \item #1 #2 \stopitemize}
\datatypeA[B]{#1 -- #2}

\define[8]\activity{
    \subject{#1}
        #2
        \startitemize
            \item #3
            \item #4
            \item #5
            \item #6
            \item #7
        \stopitemize
    #8
}

\define[2]\balloongame{
    \activity{Balloon Toss Game}{Instructions}{scissors}{plates}{construction paper}{#1}{#2}{\input fileA}
}
\define[2]\fishinggame{
    \activity{Fun Fishing Game}{Instructions}{scissors}{plates}{construction paper}{#1}{#2}{\input fileB}
}

\starttext

    \balloongame{balloons}{tape}
    \fishinggame{crayons}{glue}

\stoptext
Basically, I have different classroom activities, defined as macros. \balloongame gives all details to the teacher about a "Balloon Toss Game" and \fishinggame defines a "Fun Fishing Game". (code has been reduced and simplified significantly).
The main issue I have is I have data being stored in filesA.tex, fileB.tex, etc. (there are often a dozen such files). Note that strangly the datasets are all identical, but a different file is called, and the only difference in the dataset is they call different macros. So "Balloon Game" pulls the list, but displays them in datatypeA format, the other game takes the same data, but its in a different file, and since the macros are different, it displays the information differently.
Basically, I have tons of data files sharing basically the same information, but each one defined inside the file as using a different macro, according to how the information is to be displayed (maybe sometimes as a list, sometimes in a table, etc.)
This method seems horribly inefficient. I should be able to store that data in one file, but display the data in different ways. The problem is if I use one format, like this...

 \datatype{fish}{an animal that lives in the sea}
\datatype{bear}{a big animal with claws and fur}
\datatype{squid}{a sea creature with many legs}
...how do I get that dataset to be included, but displayed in a different format each time?
My thinking is perhaps constantly redefine the macro \datatype, but that seems still inefficient and prone to lead to errors in the code. Is that the best solution?

Any suggestion for the most proper way to go about displaying the information from this file, but in different ways in the document?
NOTE: My event code already uses 8 of the #1 #2 variables, so there's not really room to include that information there.

--Joel


[-- Attachment #1.2: Type: text/html, Size: 4824 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] 2+ messages in thread

* [NTG-context] Re: How to change the way one input file's data is displayed, depending on where it is in the document.
  2024-09-23 23:19 ` [NTG-context] How to change the way one input file's data is displayed, depending on where it is in the document Joel via ntg-context
@ 2024-09-25 17:37   ` Bruce Horrocks
  0 siblings, 0 replies; 2+ messages in thread
From: Bruce Horrocks @ 2024-09-25 17:37 UTC (permalink / raw)
  To: ntg-context mailing list

Hi Joel,

Your MWE fails to compile for me because \datatypeA is not defined.

> On 24 Sep 2024, at 00:19, Joel via ntg-context <ntg-context@ntg.nl> wrote:
> 
> I have some code like this--it is a very reduced minimum-working example, from 10,000+ lines of code:
> 
> fileA.tex------------------------------
> 
> \datatypeA{fish}{an animal that lives in the sea}
> \datatypeA{bear}{a big animal with claws and fur}
> \datatypeA{squid}{a sea creature with many legs}
> 
> fileB.tex------------------------------
> 
> \datatypeB{fish}{an animal that lives in the sea}
> \datatypeB{bear}{a big animal with claws and fur}
> \datatypeB{squid}{a sea creature with many legs}
> 
> main.tex------------------------------
> 
> \datatypeA[2]{\startitemize \item #1 #2 \stopitemize}
> \datatypeA[B]{#1 -- #2}

Did you mean to \def or \define \datatypeA and \datatypeB here?

> 
> \define[8]\activity{
>     \subject{#1}
>         #2
>         \startitemize
>             \item #3
>             \item #4
>             \item #5
>             \item #6
>             \item #7
>         \stopitemize
>     #8
> }
> 
> \define[2]\balloongame{
>     \activity{Balloon Toss Game}{Instructions}{scissors}{plates}{construction paper}{#1}{#2}{\input fileA}
> }
> \define[2]\fishinggame{
>     \activity{Fun Fishing Game}{Instructions}{scissors}{plates}{construction paper}{#1}{#2}{\input fileB}
> }
> 
> \starttext
> 
>     \balloongame{balloons}{tape}
>     \fishinggame{crayons}{glue}
> 
> \stoptext
> 
> Basically, I have different classroom activities, defined as macros. \balloongame gives all details to the teacher about a "Balloon Toss Game" and \fishinggame defines a "Fun Fishing Game". (code has been reduced and simplified significantly).
> 
> The main issue I have is I have data being stored in filesA.tex, fileB.tex, etc. (there are often a dozen such files). Note that strangly the datasets are all identical, but a different file is called, and the only difference in the dataset is they call different macros. So "Balloon Game" pulls the list, but displays them in datatypeA format, the other game takes the same data, but its in a different file, and since the macros are different, it displays the information differently.
> 
> Basically, I have tons of data files sharing basically the same information, but each one defined inside the file as using a different macro, according to how the information is to be displayed (maybe sometimes as a list, sometimes in a table, etc.)

> 
> This method seems horribly inefficient. I should be able to store that data in one file, but display the data in different ways. The problem is if I use one format, like this...
> 
> \datatype{fish}{an animal that lives in the sea}
> \datatype{bear}{a big animal with claws and fur}
> \datatype{squid}{a sea creature with many legs}
> 
> ...how do I get that dataset to be included, but displayed in a different format each time?

There is a facility called a dataset which allows you to store key-value pairs.

Use \definedataset to create one:

  \definedataset [animals] [ ]

then in your data file populate it using:

  \setdataset [animals] [fish] [desc={an animal that lives in the sea}]
  \setdataset [animals] [bear] [desc={a big animal with claws and fur}]
  \setdataset [animals] [squid] [desc={a sea creature with many legs}]

To retrieve a single value you use:

  \datasetvariable {animals} {fish} {desc}

If you want to be able to retrieve the name of the animal and do something with it then populate the dataset using an anonymous table instead:

  \setdataset [animals] [name=fish,desc={an animal that lives in the sea}]
  \setdataset [animals] [name=bear,desc={a big animal with claws and fur}]
  \setdataset [animals] [name=squid,desc={a sea creature with many legs}]

And retrieve using:

  The first animal I saw was a \datasetvariable{animals}{1}{name} which is \datasetvariable{animals}{1}{desc}.
  The second animal I saw was a \datasetvariable{animals}{2}{name} which is \datasetvariable{animals}{2}{desc}.
  etc

To mimic the \input{fileA} behaviour where you get the whole file included at that point, you can use \processdataset to iterate through the whole set. Unfortunately, it's not very well documented in the Wiki. You need to use a setup to do the formatting, so something like:

  \startsetups[formatA]
    \startitemize
      \item \datasetentry{name} \datasetentry{desc}
    \stopitemize
  \stopsetups

  \processdataset[animals][formatA]

Even this might not be right because it does the \startitemize ... \stopitemize once per database entry. You probably only want that done once for all the rows in fileA.

If you can fix the MWE then we can probably help more.

Lastly, and at the risk of confusing you further, Context has the ability to process XML data and it may make more sense for you to create the main datafile as XML.


> 
> My thinking is perhaps constantly redefine the macro \datatype, but that seems still inefficient and prone to lead to errors in the code. Is that the best solution?
> 
> Any suggestion for the most proper way to go about displaying the information from this file, but in different ways in the document?
> 
> NOTE: My event code already uses 8 of the #1 #2 variables, so there's not really room to include that information there.
> 
> --Joel



—
Bruce Horrocks
Hampshire, UK

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

end of thread, other threads:[~2024-09-25 17:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1708463364.6078783.1727133586176.ref@mail.yahoo.com>
2024-09-23 23:19 ` [NTG-context] How to change the way one input file's data is displayed, depending on where it is in the document Joel via ntg-context
2024-09-25 17:37   ` [NTG-context] " Bruce Horrocks

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