* [NTG-context] XML: flush to lua function @ 2023-09-11 14:47 denis.maier 2023-09-12 6:56 ` [NTG-context] " denis.maier 0 siblings, 1 reply; 3+ messages in thread From: denis.maier @ 2023-09-11 14:47 UTC (permalink / raw) To: ntg-context [-- Attachment #1.1: Type: text/plain, Size: 608 bytes --] Hi, I have the following xml setup with a lua function: \startluacode function markingLength(s) _,n = s:gsub("%S+","") if n > 3 then context("yes" .. " ...") else context("no") end end \stopluacode \startxmlsetups xml:ink:ink-title:marking %\ctxlua{markingLength(\xmlflush{#1})} % does not work \ctxlua{markingLength{"1 2 3 4"}} \stopxmlsetups While the function works, I cannot use xmlflush here. How can I properly pass the xml content to the lua function? Best, Denis [-- Attachment #1.2: Type: text/html, Size: 3664 bytes --] [-- Attachment #2: Type: text/plain, Size: 495 bytes --] ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context webpage : https://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : https://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 3+ messages in thread
* [NTG-context] Re: XML: flush to lua function 2023-09-11 14:47 [NTG-context] XML: flush to lua function denis.maier @ 2023-09-12 6:56 ` denis.maier 2023-09-12 7:52 ` Taco Hoekwater 0 siblings, 1 reply; 3+ messages in thread From: denis.maier @ 2023-09-12 6:56 UTC (permalink / raw) To: ntg-context [-- Attachment #1.1: Type: text/plain, Size: 3062 bytes --] Ok, I've found a way, see below. But maybe someone has some nice lua (or other) tricks to share... (I'm not sure accessing t.dt[1] is particularly safe when operating on nested structures.) Best, Denis \startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <chapter> <title>A short title</title> <p>asdf asdf asdf jklö jklö jklö</p> </chapter> <chapter> <title>A longer title bla bla bla bla bla bla bla bla</title> <p> asdf asdf asdf jklö jklö jklö </p> </chapter> </doc> \stopbuffer \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|chapter|p}{xml:*} \stopxmlsetups \xmlregisterdocumentsetup{test}{xml:test} \startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups \setuphead[chapter][expansion=yes] % make sure the stuff gets expanded on time \startxmlsetups xml:chapter \startchapter [ title={\xmlfilter{#1}{/title/command(xml:chapter:title)}}, marking={\xmlfilter{#1}{/title/command(xml:chapter:title:marking)}}, ] \xmlflush{#1} \stopchapter \stopxmlsetups \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \startluacode function xml.functions.getMarking(t) _,n = t.dt[1]:gsub("%S+","") if n > 10 then local words = {} for word in t.dt[1]:gmatch("%S+") do table.insert(words, word) end local resWords = {} resWords = { unpack( words, 1, 7 ) } res = table.concat(resWords, " ") .. " ..." else res = t.dt[1] end context(res) end \stopluacode \startxmlsetups xml:chapter:title:marking \xmlfunction{#1}{getMarking} \stopxmlsetups \startxmlsetups xml:chapter:title \xmlflush{#1} \stopxmlsetups \setupheadertexts[chapter] \starttext \xmlprocessbuffer{test}{test}{} \stoptext Von: denis.maier@unibe.ch <denis.maier@unibe.ch> Gesendet: Montag, 11. September 2023 16:47 An: ntg-context@ntg.nl Betreff: [NTG-context] XML: flush to lua function Hi, I have the following xml setup with a lua function: \startluacode function markingLength(s) _,n = s:gsub("%S+","") if n > 3 then context("yes" .. " ...") else context("no") end end \stopluacode \startxmlsetups xml:ink:ink-title:marking %\ctxlua{markingLength(\xmlflush{#1})} % does not work \ctxlua{markingLength{"1 2 3 4"}} \stopxmlsetups While the function works, I cannot use xmlflush here. How can I properly pass the xml content to the lua function? Best, Denis [-- Attachment #1.2: Type: text/html, Size: 14517 bytes --] [-- Attachment #2: Type: text/plain, Size: 495 bytes --] ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context webpage : https://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : https://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 3+ messages in thread
* [NTG-context] Re: XML: flush to lua function 2023-09-12 6:56 ` [NTG-context] " denis.maier @ 2023-09-12 7:52 ` Taco Hoekwater 0 siblings, 0 replies; 3+ messages in thread From: Taco Hoekwater @ 2023-09-12 7:52 UTC (permalink / raw) To: mailing list for ConTeXt users Hi, xml.text(t) is safer than directly accessing t.dt[1]. Note: you may need to strip some XML tags from that content. If the element is not straight-up data but has nested elements, then this will return the full xml representation instead: <title>A short title</title> => ‘A short title' <title><A short title></title> => ‘<A short title>' <title><A <b>short</b> title></title> => '<title><A <b>short</b> title></title>' Best wishes, Taco > On 12 Sep 2023, at 08:56, <denis.maier@unibe.ch> <denis.maier@unibe.ch> wrote: > > Ok, I’ve found a way, see below. > But maybe someone has some nice lua (or other) tricks to share… (I’m not sure accessing t.dt[1] is particularly safe when operating on nested structures.) > Best, > Denis > \startbuffer[test] > <?xml version="1.0" encoding="UTF-8"?> > <doc> > <chapter> > <title>A short title</title> > <p>asdf asdf asdf jklö jklö jklö</p> > </chapter> > <chapter> > <title>A longer title bla bla bla bla bla bla bla bla</title> > <p> > asdf asdf asdf jklö jklö jklö > </p> > </chapter> > </doc> > \stopbuffer > \startxmlsetups xml:test > \xmlsetsetup{#1}{*}{-} > \xmlsetsetup{#1}{doc|chapter|p}{xml:*} > \stopxmlsetups > \xmlregisterdocumentsetup{test}{xml:test} > \startxmlsetups xml:doc > \xmlflush{#1} > \stopxmlsetups > \setuphead[chapter][expansion=yes] % make sure the stuff gets expanded on time > \startxmlsetups xml:chapter > \startchapter > [ > title={\xmlfilter{#1}{/title/command(xml:chapter:title)}}, > marking={\xmlfilter{#1}{/title/command(xml:chapter:title:marking)}}, > ] > \xmlflush{#1} > \stopchapter > \stopxmlsetups > \startxmlsetups xml:p > \xmlflush{#1}\par > \stopxmlsetups > \startluacode > function xml.functions.getMarking(t) > _,n = t.dt[1]:gsub("%S+","") > if n > 10 then local words = {} for word in t.dt[1]:gmatch("%S+") do table.insert(words, word) end > local resWords = {} > resWords = { unpack( words, 1, 7 ) } > res = table.concat(resWords, " ") .. " …" > else res = t.dt[1] > end > context(res) > end > \stopluacode > \startxmlsetups xml:chapter:title:marking > \xmlfunction{#1}{getMarking} > \stopxmlsetups > \startxmlsetups xml:chapter:title > \xmlflush{#1} > \stopxmlsetups > \setupheadertexts[chapter] > \starttext > \xmlprocessbuffer{test}{test}{} > \stoptext > Von: denis.maier@unibe.ch <denis.maier@unibe.ch> > Gesendet: Montag, 11. September 2023 16:47 > An: ntg-context@ntg.nl > Betreff: [NTG-context] XML: flush to lua function > Hi, > I have the following xml setup with a lua function: > \startluacode > function markingLength(s) > _,n = s:gsub("%S+","") > if n > 3 then context("yes" .. " …") else context("no") end > end > \stopluacode > \startxmlsetups xml:ink:ink-title:marking > %\ctxlua{markingLength(\xmlflush{#1})} % does not work > \ctxlua{markingLength{"1 2 3 4"}} > \stopxmlsetups > While the function works, I cannot use xmlflush here. How can I properly pass the xml content to the lua function? > Best, > Denis > ___________________________________________________________________________________ > If your question is of interest to others as well, please add an entry to the Wiki! > > maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context > webpage : https://www.pragma-ade.nl / http://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > wiki : https://contextgarden.net > ___________________________________________________________________________________ — Taco Hoekwater E: taco@bittext.nl genderfluid (all pronouns) ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context webpage : https://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : https://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-12 7:54 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-09-11 14:47 [NTG-context] XML: flush to lua function denis.maier 2023-09-12 6:56 ` [NTG-context] " denis.maier 2023-09-12 7:52 ` Taco Hoekwater
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).