Two pass tag processing example

From Wiki
Jump to navigation Jump to search

Two pass XML tag processing example

Imagine the next XML structure:

<float>
  <figure file="hacker.jpg" context="width=3cm" />
  <figure file="cow.pdf" context="width=.5ctx-var-textwidth;" />
  <caption>Cow and hacker.</caption>
</float>

There can be arbitrary number of figures inside a <float> tag. All figures share a single caption. It is desired to typeset them using \placefigure and several \externalfigure. See also relative width specification for th second figure (.5\textwidth).

Solution is by two pass <float> processing (saving via \defineXMLenvironmentsave and flushes via \XMLflush) with different caption and figure definitions:

\starttext

\defineXMLentity[ctx-var-textwidth] {\textwidth}

\defineXMLenvironmentsave
  [float]
  {\bgroup
   \defineXMLsave[caption]%
   \defineXMLignore[figure]}
  {\XMLflush{float}%
   \defineXMLignore
     [caption]
   \defineXMLcommand
     [figure]
     [file=dummy,context=]
     {\expanded{\externalfigure
       [\XMLflush{dir-fig}\XMLop{file}][\XMLop{context}]}%
      \kern1em
      \ignorespaces}%
   \placefigure
     {\XMLflush{caption}}
     {\hbox{\ignorespaces\XMLflush{float}\removeunwantedspaces}}%
   \egroup}

\startXMLdata
<float>
  <figure file="cow.pdf" context="width=.5ctx-var-textwidth;" />
  <figure file="hacker.jpg" context="width=3cm" />
  <caption>Cow and hacker.</caption>
</float>
\stopXMLdata

\stoptext