Two pass tag processing example

From Wiki
Revision as of 10:36, 22 September 2005 by Patrick Gundlach (talk | contribs) (Reverted edit of 67.159.5.117, changed back to last version by Mojca Miklavec)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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