1

How could I write something like the following converter using only the Lua interface?

\startbuffer[test]
  <html>
    <body>
      <p>Some stuff</p>
      <p>More stuff, with <em>emphasis</em></p>
    </body>
  </html>
\stopbuffer

\startxmlsetups xml:test:*
  \xmlsetsetup{\xmldocument}{*}{-}
  \xmlsetsetup{\xmldocument}{html|body|p|em}{xml:test:*}
\stopxmlsetups

\xmlregisterdocumentsetup{testxml}{xml:test:setup}

\startxmlsetups xml:test:html
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:test:body
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:test:p
  \xmlflush{#1}\endgraf
\stopxmlsetups

\startxmlsetups xml:test:em
  {\em\xmlflush{#1}}
\stopxmlsetups

\starttext
  \xmlprocessbuffer{testxml}{test}{}
\stoptext

I can get the other stuff working with context.whatever or lxml.whatever; the one thing that is holding me back is the (start/stop)xmlsetups command, so I cannot replace, for example,

\startxmlsetups xml:test:html
  \xmlflush{#1}
\stopxmlsetups

with

\startluacode
  context.startxmlsetups('xml:test:html')
    lxml.flush('#1')
  context.stopxmlsetups()
\stopluacode

I have tried context.startxmlsetups(xml:test:html), context.delayed.startxmlsetups(xml:test:html), context.nested.startxmlsetups(xml:test:html), function() context.startxmlsetups(xml:test:html) end, and function() context.startxmlsetups(xml:test:html) return true end; they all seem to either do nothing or give errors about \csname. The macro definition seems to be in core-env.mkiv, but I cannot make sense of it. Is there a way to call these start/stopxmlsetups commands from Lua, or to avoid them entirely?

Edit (7/9/18):

I have tried using #1 and '#1' as arguments to a Lua function outside of the xmlsetups environment, but they do not seem to mean anything useful there. This works:

\startxmlsetups xml:test:html
  \ctxlua{lxml.flush('#1')}
\stopxmlsetups

But not \ctxlua{lxml.flush('#1')} on its own, or \ctxlua{lxml.flush(#1)}, or \ctxlua{lxml.flush('xml:test:html')}. Even \ctxlua{lxml.flush('testxml::2')} fails to do anything. (testxml::2 is the result of calling print('#1') inside that particular xmlsetups environment. Interestingly, outside of that, print('#1') gives ##1; I do not know if it has any significance, but the standard Lua interpreter does not behave that way.)

I also tried the following:

\def\setupxmlwith[#xml][#luafunction]%
  {\startxmlsetups{#xml}\xmlfunction{#1}{#luafunction}\endcsname\stopxmlsetups}

\startluacode
  function xml.functions.test()
    lxml.flush('#1')
  end
\stopluacode

\setupxmlwith[xml:test:html][test]

Which resulted in:

Missing \endcsname inserted

<to be read again>
\stopxmlsetups
\syst_setups_start_xml_indeed ...me \??setup #1:#2
                                              \expandafter \endcsname \e...

When I added \endcsname before \stopxmlsetups in the macro definition, I got Parameters must be named consecutively.

  • 1
    Please add a complete example so any would-be-helper can easily check out your problem. – vonbrand Jul 6 '18 at 16:11
  • Can you just call a lua function with #1 as an argument? – Aditya Jul 6 '18 at 23:45
  • 2
    @vonbrand This is a MWE even though it might not look like it because it's ConTeXt, not LaTeX. – Henri Menke Jul 7 '18 at 0:15
  • @Aditya See the edit. – Keiser Jul 9 '18 at 20:51
  • @Keiser Put the name of the setups environment in brackets, e.g. \startxmlsetups [xml:...] ... \stopxmlsetups. – Wolfgang Schuster Jul 14 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.