XIST.publishers

Module with classes for publishing trees

Home › Python software › ll.xist › publishersText · XIST · Python

This module contains classes that may be used as publishing handlers in ll.xist.xsc.Node.publish.

class Publisher​(object):

A Publisher object is used for serializing an XIST tree into a byte sequence.

def __init__​(self, encoding=None, xhtml=1, validate=True, prefixes={}, prefixdefault=False, hidexmlns=(), showxmlns=()):

Create a publisher. Arguments have the following meaning:

encoding (string or None)

Specifies the encoding to be used for the byte sequence. If None is used the encoding in the XML declaration will be used. If there is no XML declaration, UTF-8 will be used.

xhtml (int)

With the parameter xhtml you can specify if you want HTML output:

HTML (xhtml==0)

Elements with a empty content model will be published as <foo>.

HTML browser compatible XML (xhtml==1)

Elements with an empty content model will be published as <foo /> and others that just happen to be empty as <foo></foo>. This is the default.

Pure XML (xhtml==2)

All empty elements will be published as <foo/>.

validate (bool)

Specifies whether validation should be done before publishing.

prefixes (mapping)

A dictionary that specifies which namespace prefixes should be used for publishing. Keys in the dictionary are either namespace names or objects that have an xmlns attribute which is the namespace name. Values can be:

False

Treat elements in this namespace as if they are not in any namespace (if global attributes from this namespace are encountered, a non-empty prefix will be used nonetheless).

None

Treat the namespace as the default namespaces (i.e. use unprefixed element names). Global attributes will again result in a non-empty prefix.

True

The publisher uses a unique non-empty prefix for this namespace.

A string

Use this prefix for the namespace.

prefixdefault (string or None)

If an element or attribute is encountered whose namespace name is not in prefixes prefixdefault is used as the fallback.

hidexmlns (list or set)

hidexmlns can be a list or set that contains namespace names for which no xmlns attributes should be published. (This can be used to hide the namespace declarations for e.g. Java taglibs.)

showxmlns (list or set)

showxmlns can be a list or set that contains namespace names for which xmlns attributes will be published, even if there are no elements from this namespace in the tree.

def encode​(self, text):

Encode text with the encoding and error handling currently active and return the resulting byte string.

def encodetext​(self, text):

Encode test as text data. text must be a unicode object. The publisher will apply the configured encoding, error handling and the current text filter (which escapes characters that can't appear in text data (like < etc.)) and returns the resulting str object.

def pushtextfilter​(self, filter):

Pushes a new text filter function ontp the text filter stack. This function is responsible for escaping characters that can't appear in text data (like <)). This is used to switch on escaping of " inside attribute values.

def poptextfilter​(self):

Pops the current text filter function from the stack.

def pusherrors​(self, errors):

Pushes a new error handling scheme onto the error handling stack.

def poperrors​(self):

Pop the current error handling scheme from the error handling stack.

def _newprefix​(self):

def getencoding​(self):

Return the encoding currently in effect.

def getnamespaceprefix​(self, xmlns):

Return (and register) a namespace prefix for the namespace name xmlns. This honors the namespace configuration from self.prefixes and self.prefixdefault. Furthermore the same prefix will be returned from now on (except when the empty prefix becomes invalid once global attributes are encountered)

def getobjectprefix​(self, object):

Get and register a namespace prefix for the namespace object lives in (specified by the xmlns attribute of object). Similar to getnamespaceprefix this honors the namespace configuration from self.prefixes and self.prefixdefault (except when a global attribute requires a non-empty prefix).

def iterbytes​(self, node, base=None):

Output the node node. This method is a generator that will yield the resulting XML byte sequence in fragments.

def bytes​(self, node, base=None):

Return a byte string in XML format for the XIST node node.

def iterstring​(self, node, base=None):

A generator that will produce a serialized string of node.

def string​(self, node, base=None):

Return a unicode string for node.

def write​(self, stream, node, base=None):

Write node to the file-like object stream (which must provide a write method).