File: //usr/lib/python3/dist-packages/genshi/__pycache__/core.cpython-310.pyc
o
!�b�d � @ sH d Z ddlmZ ddlZddlmZ ddlZddlZddlm Z ddl
mZmZ g d�Z
dZG d d
� d
e�ZG dd� de�ZejZejZejZejZejZejZejZejZejZejZejZd
d� ZG dd� de�Z G dd� dej!�Z"zddl#m"Z" W n e$y� Y nw e"j%Z%dd� Z&G dd� de�Z'e'd�Z(G dd� dej!�Z)dS )z#Core classes for markup processing.� )�reduceN)�chain)�
stringrepr)�
stripentities� striptags)�Stream�Markup�escape�unescape�Attrs� Namespace�QNamezrestructuredtext enc @ s e Zd ZdZg Zi Zdd� ZdS )�StreamEventKindz#A kind of event on a markup stream.c C s | j �|t�| |��S �N)�
_instances�
setdefault�str�__new__)�cls�val� r �-/usr/lib/python3/dist-packages/genshi/core.pyr $ � zStreamEventKind.__new__N)�__name__�
__module__�__qualname__�__doc__� __slots__r r r r r r r s
r c @ s� e Zd ZdZddgZed�Zed�Zed�Zed�Z ed�Z
ed �Zed
�Zed�Z
ed�Zed
�Zed�Zd%dd�Zdd� Zdd� Zdd� Zd&dd�Zd'dd�Zd(dd�Zdd � Zd!d"� Zd#d$� ZdS ))r a. Represents a stream of markup events.
This class is basically an iterator over the events.
Stream events are tuples of the form::
(kind, data, position)
where ``kind`` is the event kind (such as `START`, `END`, `TEXT`, etc),
``data`` depends on the kind of event, and ``position`` is a
``(filename, line, offset)`` tuple that contains the location of the
original element or text in the input. If the original location is unknown,
``position`` is ``(None, -1, -1)``.
Also provided are ways to serialize the stream to text. The `serialize()`
method will return an iterator over generated strings, while `render()`
returns the complete generated text at once. Both accept various parameters
that impact the way the stream is serialized.
�events�
serializer�START�END�TEXT�XML_DECL�DOCTYPE�START_NS�END_NS�START_CDATA� END_CDATA�PI�COMMENTNc C s || _ || _dS )a: Initialize the stream with a sequence of markup events.
:param events: a sequence or iterable providing the events
:param serializer: the default serialization method to use for this
stream
:note: Changed in 0.5: added the `serializer` argument
N)r r )�selfr r r r r �__init__J s
zStream.__init__c C �
t | j�S r )�iterr �r+ r r r �__iter__V �
zStream.__iter__c C s t t|| ��| jd�S )a� Override the "bitwise or" operator to apply filters or serializers
to the stream, providing a syntax similar to pipes on Unix shells.
Assume the following stream produced by the `HTML` function:
>>> from genshi.input import HTML
>>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''', encoding='utf-8')
>>> print(html)
<p onclick="alert('Whoa')">Hello, world!</p>
A filter such as the HTML sanitizer can be applied to that stream using
the pipe notation as follows:
>>> from genshi.filters import HTMLSanitizer
>>> sanitizer = HTMLSanitizer()
>>> print(html | sanitizer)
<p>Hello, world!</p>
Filters can be any function that accepts and produces a stream (where
a stream is anything that iterates over events):
>>> def uppercase(stream):
... for kind, data, pos in stream:
... if kind is TEXT:
... data = data.upper()
... yield kind, data, pos
>>> print(html | sanitizer | uppercase)
<p>HELLO, WORLD!</p>
Serializers can also be used with this notation:
>>> from genshi.output import TextSerializer
>>> output = TextSerializer()
>>> print(html | sanitizer | uppercase | output)
HELLO, WORLD!
Commonly, serializers should be used at the end of the "pipeline";
using them somewhere in the middle may produce unexpected results.
:param function: the callable object that should be applied as a filter
:return: the filtered stream
:rtype: `Stream`
)r )r �_ensurer )r+ �functionr r r �__or__Y s ,z
Stream.__or__c G s t tj| f| �S )aL Apply filters to the stream.
This method returns a new stream with the given filters applied. The
filters must be callables that accept the stream object as parameter,
and return the filtered stream.
The call::
stream.filter(filter1, filter2)
is equivalent to::
stream | filter1 | filter2
:param filters: one or more callable objects that should be applied as
filters
:return: the filtered stream
:rtype: `Stream`
)r �operator�or_)r+ �filtersr r r �filter� s z
Stream.filterc K sB ddl m} |du r| jpd}| jdd|i|��}|||||d�S )a� Return a string representation of the stream.
Any additional keyword arguments are passed to the serializer, and thus
depend on the `method` parameter value.
:param method: determines how the stream is serialized; can be either
"xml", "xhtml", "html", "text", or a custom serializer
class; if `None`, the default serialization method of
the stream is used
:param encoding: how the output string should be encoded; if set to
`None`, this method returns a `unicode` object
:param out: a file-like object that the output should be written to
instead of being returned as one big string; note that if
this is a file or socket (or similar), the `encoding` must
not be `None` (that is, the output must be encoded)
:return: a `str` or `unicode` object (depending on the `encoding`
parameter), or `None` if the `out` parameter is provided
:rtype: `basestring`
:see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer
:note: Changed in 0.5: added the `out` parameter
r )�encodeN�xml�method)r; �encoding�outr )�
genshi.outputr9 r � serialize)r+ r; r<