consume

function

Library: Markup utilities (OMMARKUPUTILITIES)
Import : ommarkuputilities.xmd

Returns: the content stored in the markup-buffer argument


Declaration
export markup source function
   consume value markup-buffer b
    


Purpose

While a markup stream stored in a markup-buffer can be read out directly by using the markup-buffer as a markup source, this operation does not change the buffer. The markup source function consume, in contrast, removes the markup stream from the buffer at the same time it outputs the stream. In effect, a markup-buffer can be used as a queue.

This can be convenient in cases where parts of markup need to be moved around:

  global markup-buffer queue
  
  element "append"
     using output as queue
        output #content
  
  element "flush"
     output consume queue
     suppress
          

If the consumer of the markup-buffer content throws or returns before it consumes the entire available input, the use of consume allows us to keep track exactly how much content still remains in the buffer. For example, the following function consumes its input only up to and including the first markup region:

  define markup source function
     first-region (value markup source s)
  as
     repeat
        output s take any*
        exit
  
      catch #markup-start e
        signal rethrow
        repeat
           output first-region (s)
         catch #markup-end e2
           assert e == e2
           signal rethrow
           return
        again
  
      catch #markup-point e
        signal rethrow
     again
          

We can now use the function first-region to consume and output only the first element accumulated in queue:

  element "first"
     output first-region (consume queue)
     suppress
          

With these rules in place and another element #implied rule that simply reproduces the element that triggered it, the example XML input

  <doc>
    <append><p>Title</p><p>Body</p></append>
    <section>
      <title><first/></title>
      <flush/>
    </section>
  </doc>
          

would be transformed into

  <doc>
  
    <section>
      <title><p>Title</p></title>
      <p>Text</p>
    </section>
  </doc>
          

Related Topics