Hi, Is there a more elegant way to feed an xml tree into a context() command that what I have below? \startluacode function xml.functions.heading(t) context.section("{") lxml.flush(t) context("}") end \stopluacode The subtree in ’t’ could have embedded xml tags that should be processed, so just stripping it clean will not do. (this is not about \section as such, I just needed an example. \startsection would be more modern, but it would not help fix the issue ) Thanks for any help, Taco
On 9/19/2018 2:50 PM, Taco Hoekwater wrote:
Hi,
Is there a more elegant way to feed an xml tree into a context() command that what I have below?
\startluacode function xml.functions.heading(t) context.section("{") lxml.flush(t) context("}") end \stopluacode
The subtree in ’t’ could have embedded xml tags that should be processed, so just stripping it clean will not do.
(this is not about \section as such, I just needed an example. \startsection would be more modern, but it would not help fix the issue ) it actually depends on what you do ... anyway here is some insight (as xml-tex old-timer you'll probably recognize the madness)
% \enabletrackers[context*] \starttext % here is your missing mwe \startbuffer[test] <document> <section>some <b>bold</b> title</section> </document> \stopbuffer % this will assume xml and relate some handlers \setuphead[section] [coding=xml, xmlsetup=xml:flush] \startxmlsetups xml:flush \xmlflush{#1} \stopxmlsetups % comment this one for your amusement \startxmlsetups xml:b {\bf \xmlflush{#1}} \stopxmlsetups % here is the magic: you pass a reference \startluacode function xml.finalizers.heading(t) context.formatted.section("main::%i",t[1].ix) end \stopluacode % this loads only (can happen at the lua end ... you can sort that out) \xmlloadbuffer{main}{test}{} % this indexes the nodes (the ix keys, basically all these #1 argumehnts in setups use that trickery) \xmladdindex{main} % to be sure \xmlsetsetup{main}{b}{xml:b} % now we filter / apply your finalizer \xmlfilter{main}{section/heading()} % done \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
My 2 cents: local xmlflush = lxml.flush local function text_or_xml(...) for i,v in ipairs(arg) do if "table" == type(v) then xmlflush(v) else context(v) end end end function xml.functions.heading(t) text_or_xml( "\\section{" , t , "}" ) end Massimiliano Il giorno mer, 19/09/2018 alle 15.56 +0200, Hans Hagen ha scritto:
On 9/19/2018 2:50 PM, Taco Hoekwater wrote:
Hi,
Is there a more elegant way to feed an xml tree into a context() command that what I have below?
\startluacode function xml.functions.heading(t) context.section("{") lxml.flush(t) context("}") end \stopluacode
The subtree in ’t’ could have embedded xml tags that should be processed, so just stripping it clean will not do.
(this is not about \section as such, I just needed an example. \startsection would be more modern, but it would not help fix the issue )
it actually depends on what you do ... anyway here is some insight (as xml-tex old-timer you'll probably recognize the madness)
% \enabletrackers[context*]
\starttext
% here is your missing mwe
\startbuffer[test] <document> <section>some <b>bold</b> title</section> </document> \stopbuffer
% this will assume xml and relate some handlers
\setuphead[section] [coding=xml, xmlsetup=xml:flush]
\startxmlsetups xml:flush \xmlflush{#1} \stopxmlsetups
% comment this one for your amusement
\startxmlsetups xml:b {\bf \xmlflush{#1}} \stopxmlsetups
% here is the magic: you pass a reference
\startluacode function xml.finalizers.heading(t) context.formatted.section("main::%i",t[1].ix) end \stopluacode
% this loads only (can happen at the lua end ... you can sort that out)
\xmlloadbuffer{main}{test}{}
% this indexes the nodes (the ix keys, basically all these #1 argumehnts in setups use that trickery)
\xmladdindex{main}
% to be sure
\xmlsetsetup{main}{b}{xml:b}
% now we filter / apply your finalizer
\xmlfilter{main}{section/heading()}
% done
\stoptext
----------------------------------------------------------------- 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 _____________________________________________________________________ ______________
Hi,
it actually depends on what you do ... anyway here is some insight (as xml-tex old-timer you'll probably recognize the madness)
What I am mostly trying to do is to have as much of the xml processing in lua as possible.
% \enabletrackers[context*]
\starttext
% here is your missing mwe
Yes, sorry about that. My question was intended to be more of an documentation-related one. Using your MWE as guideline, my minimal file looks like this: % BEGIN ENVIRONMENT \startluacode function xml.functions.document(t) lxml.flush(t) end function xml.functions.b(t) context.start() context.bf() lxml.flush(t) context.stop() context("{}") end function xml.functions.section(t) context.section("{") lxml.flush(t) context("}") end \stopluacode \startxmlsetups mysetups % to be converted to lua later \xmlsetfunction {main}{document}{xml.functions.document} \xmlsetfunction {main}{section} {xml.functions.section} \xmlsetfunction {main}{b} {xml.functions.b} \stopxmlsetups \xmlregistersetup{mysetups} % to be converted to lua later % XML normally in separate file \starttext \startbuffer[test] <document> <section>some <b>bold</b> title</section> </document> \stopbuffer \xmlprocessbuffer{main}{test}{} \stoptext % END EXAMPLE I know it would be simple to write e.g. \startsetups xml:section \section{\xmlflush{#1}} \stopsetups but I was aiming for a simple, clean-looking CLD as style setup of the XML document. I doubt I could explain your MWE to one of my coworkers, as I hardly understand it myself :) What I was really hoping for was something like the fictional: function xml.functions.section(t) context.section(lxml.process_into_context_string(t)) end or some CLD extension so that it would actually understand function xml.functions.section(t) context.section(lxml.flush(t)) end I hope this makes more sense now. Unless something magical is possible, it seems my best bet is to stick with \startsetups instead of lua for these cases, just to make sure I can explain to the rest of the office what is going on. Thanks, Taco
participants (3)
-
Hans Hagen
-
mf
-
Taco Hoekwater