Hi all here, I have a question in the area of the json module and in general about the interaction between Lua and ConTeXt. In m-json.mkxl we have for example: interfaces.implement { name = "tablefield", actions = function(namespace,name) local data = ""> if data then context(data) end end, arguments = "2 arguments", public = true, protected = true, } Means, that, if I ask for a table field from json file the value will be typeset through context(data). Is there any possibility to check the value passed from Lua to ConTeXt on ConTeXt level? I like to use the json module for getting addresses for a mass lettering. Prominent examples, where a check of the value from a json-database is needed is the title of a person (typeset it plus a space or do nothing if empty), or if you need to have alternatives for an opening sentence depending on the salutation or gender. Or whatever you can think of in a mass letter depending on the dataset ... The idea is to do the special handlings on ConTeXt level, but I didn’t manage to get it running together with the existing json module. Also checks against non empty field values don’t work for me. I know that context(data) is not really returning a value, like a return value in procedural languages. But I hope there is a solution to do the conditionals on ConTeXt level. What I also don’t understand is, why different variants of how the lua code is called, behave different, my expectation was that they simply run under a different catcode regime. % typsets: equal (as expected) \edef\compareVal{A} \edef\temp{\ctxlua{context("A")}} \ifx\temp\compareVal equal\else not equal\fi But in the following variants I always branch to the \else path: \edef\temp{\ctxluacode{context("A")}} \ifx\temp\compareVal equal\else not equal\fi \edef\temp{\startluacode context("A") \stopluacode} \ifx\temp\compareVal equal\else not equal\fi \edef\temp{\startlua context("A") \stoplua} \ifx\temp\compareVal equal\else not equal\fi The following mwe (files are also attached), shows one use case (address field in the letter) I hope someone here, is able to help. \usemodule[json] \loadjsonfile[mglist][addr.json] \showmakeup[space] \starttext % read all adresses \dorecurse{\jsonlength{mglist}{records}}{% % create macros for later use ... \edef\Salutaion{\jsonfield{mglist}{records[\recurselevel].salutation}} \edef\Title{\jsonfield{mglist}{records[\recurselevel].title}} \edef\Name{\jsonfield{mglist}{records[\recurselevel].name}} \edef\Firstname{\jsonfield{mglist}{records[\recurselevel].firstname}} \edef\Street{\jsonfield{mglist}{records[\recurselevel].street}} \edef\ZIP{\jsonfield{mglist}{records[\recurselevel].zip}} \edef\Location{\jsonfield{mglist}{records[\recurselevel].location}} % print the Adress ... \Salutaion\space\ifx\Title\empty\else\Title\space\fi\Firstname\space\Name \blank[small] \Street \blank[small] \ZIP\space\Location \blank[line] } % works ... \edef\temp{\ctxlua{context("")}} \ifx\temp\empty empty\else not empty\fi % works ... \edef\compareVal{A} \edef\temp{\ctxlua{context("A")}} \ifx\temp\compareVal equal\else not equal\fi \stoptext With a sample Json file (addr.json): { "records": [ { "salutation": "Mrs.", "title": "", "name": "von Bingen", "firstname": "Hildegard", "street": "past road 1", "zip": "00000", "location": "Bingen am Rhein" }, { "salutation": "Mr.", "title": "Dr.", "name": "von Kos", "firstname": "Hippokrates", "street": "long ago road 1", "zip": "00001", "location": "Kos" } ] } Thanks for help … Andreas |
addr.json
Description: application/json
jsontest.tex
Description: Binary data
jsontest.pdf
Description: Adobe PDF document
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : [email protected] / 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 ___________________________________________________________________________________
