Logo Search packages:      
Sourcecode: calibre version File versions

def calibre::utils::genshi::filters::transform::Transformer::copy (   self,
  buffer,
  accumulate = False 
)

Copy selection into buffer.

The buffer is replaced by each *contiguous* selection before being passed
to the next transformation. If accumulate=True, further selections will
be appended to the buffer rather than replacing it.

>>> from calibre.utils.genshi.builder import tag
>>> buffer = StreamBuffer()
>>> html = HTML('<html><head><title>Some Title</title></head>'
...             '<body>Some <em>body</em> text.</body></html>')
>>> print html | Transformer('title/text()').copy(buffer) \\
...     .end().select('body').prepend(tag.h1(buffer))
<html><head><title>Some Title</title></head><body><h1>Some
Title</h1>Some <em>body</em> text.</body></html>

This example illustrates that only a single contiguous selection will
be buffered:

>>> print html | Transformer('head/title/text()').copy(buffer) \\
...     .end().select('body/em').copy(buffer).end().select('body') \\
...     .prepend(tag.h1(buffer))
<html><head><title>Some Title</title></head><body><h1>Some
Title</h1>Some <em>body</em> text.</body></html>
>>> print buffer
<em>body</em>

Element attributes can also be copied for later use:

>>> html = HTML('<html><head><title>Some Title</title></head>'
...             '<body><em>Some</em> <em class="before">body</em>'
...             '<em>text</em>.</body></html>')
>>> buffer = StreamBuffer()
>>> def apply_attr(name, entry):
...     return list(buffer)[0][1][1].get('class')
>>> print html | Transformer('body/em[@class]/@class').copy(buffer) \\
...     .end().buffer().select('body/em[not(@class)]') \\
...     .attr('class', apply_attr)
<html><head><title>Some Title</title></head><body><em
class="before">Some</em> <em class="before">body</em><em
class="before">text</em>.</body></html>


:param buffer: the `StreamBuffer` in which the selection should be
       stored
:rtype: `Transformer`
note: Copy (and cut) copy each individual selected object into the
       buffer before passing to the next transform. For example, the
       XPath ``*|text()`` will select all elements and text, each
       instance of which will be copied to the buffer individually
       before passing to the next transform. This has implications for
       how ``StreamBuffer`` objects can be used, so some
       experimentation may be required.

Definition at line 486 of file transform.py.

00486                                             :
        """Copy selection into buffer.

        The buffer is replaced by each *contiguous* selection before being passed
        to the next transformation. If accumulate=True, further selections will
        be appended to the buffer rather than replacing it.

        >>> from calibre.utils.genshi.builder import tag
        >>> buffer = StreamBuffer()
        >>> html = HTML('<html><head><title>Some Title</title></head>'
        ...             '<body>Some <em>body</em> text.</body></html>')
        >>> print html | Transformer('title/text()').copy(buffer) \\
        ...     .end().select('body').prepend(tag.h1(buffer))
        <html><head><title>Some Title</title></head><body><h1>Some
        Title</h1>Some <em>body</em> text.</body></html>

        This example illustrates that only a single contiguous selection will
        be buffered:

        >>> print html | Transformer('head/title/text()').copy(buffer) \\
        ...     .end().select('body/em').copy(buffer).end().select('body') \\
        ...     .prepend(tag.h1(buffer))
        <html><head><title>Some Title</title></head><body><h1>Some
        Title</h1>Some <em>body</em> text.</body></html>
        >>> print buffer
        <em>body</em>

        Element attributes can also be copied for later use:

        >>> html = HTML('<html><head><title>Some Title</title></head>'
        ...             '<body><em>Some</em> <em class="before">body</em>'
        ...             '<em>text</em>.</body></html>')
        >>> buffer = StreamBuffer()
        >>> def apply_attr(name, entry):
        ...     return list(buffer)[0][1][1].get('class')
        >>> print html | Transformer('body/em[@class]/@class').copy(buffer) \\
        ...     .end().buffer().select('body/em[not(@class)]') \\
        ...     .attr('class', apply_attr)
        <html><head><title>Some Title</title></head><body><em
        class="before">Some</em> <em class="before">body</em><em
        class="before">text</em>.</body></html>


        :param buffer: the `StreamBuffer` in which the selection should be
                       stored
        :rtype: `Transformer`
        note: Copy (and cut) copy each individual selected object into the
               buffer before passing to the next transform. For example, the
               XPath ``*|text()`` will select all elements and text, each
               instance of which will be copied to the buffer individually
               before passing to the next transform. This has implications for
               how ``StreamBuffer`` objects can be used, so some
               experimentation may be required.

        """
        return self.apply(CopyTransformation(buffer, accumulate))

    def cut(self, buffer, accumulate=False):


Generated by  Doxygen 1.6.0   Back to index