Example #1
0
    def transform(self, stream=None, filters=[]):
        """
        Execute the template and apply any match transformations.

        If stream is specified, it must be one of the following:

        Element
          A kid.Element.
        ElementStream
          An `pull.ElementStream` instance or other iterator that yields
          stream events.
        string
          A file or URL unless the string starts with
          '<' in which case it is considered an XML document
          and processed as if it had been an Element.

        By default, the `pull` method is called to obtain the stream.
        """
        if stream is None:
            stream = self.pull()
        elif isinstance(stream, basestring):
            if xml_sniff(stream):
                stream = XML(stream, fragment=False)
            else:
                stream = document(stream)
        elif hasattr(stream, 'tag'):
            stream = ElementStream(stream)
        else:
            stream = ElementStream.ensure(stream)
        for f in filters + self._filters:
            stream = f(stream, self)
        return stream
 def extract_kid_strings(self):
     if not self.__curfile:
         return
     if not kid_parser:
         raise ImportError("Kid templating is not installed.")
     tag = None
     tags = []
     for ev, item in kid_parser.document(self.__curfile):
         if ev == kid_parser.TEXT:
             if tag:
                 item = item.strip()
                 if item and not self.__contains_inline_python(item):
                     self.__addentry(item, tag, istemplatestring=1)
         elif ev == kid_parser.START:
             tag = item.tag
             if isinstance(tag, basestring):
                 tag = self.__strip_namespace_uri(tag)
                 if tag in ("script", "style"):
                     tag = None
             else:
                 tag = None
             tags.append(tag)
         elif ev == kid_parser.END:
             if tags:
                 tag = tags.pop()
    def transform(self, stream=None, filters=[]):
        """
        Execute the template and apply any match transformations.

        If stream is specified, it must be one of the following:

        Element
          A kid.Element.
        ElementStream
          An `pull.ElementStream` instance or other iterator that yields
          stream events.
        string
          A file or URL unless the string starts with
          '<' in which case it is considered an XML document
          and processed as if it had been an Element.

        By default, the `pull` method is called to obtain the stream.
        """
        if stream is None:
            stream = self.pull()
        elif isinstance(stream, basestring):
            if xml_sniff(stream):
                stream = XML(stream, fragment=False)
            else:
                stream = document(stream)
        elif hasattr(stream, 'tag'):
            stream = ElementStream(stream)
        else:
            stream = ElementStream.ensure(stream)
        for f in filters + self._filters:
            stream = f(stream, self)
        return stream
Example #4
0
 def extract_kid_strings(self):
     if not self.__curfile:
         return
     if not kid_parser:
         raise ImportError, "Kid templating is not installed."
     tag = None
     tags = []
     for ev, item in kid_parser.document(self.__curfile):
         if ev == kid_parser.TEXT:
             if tag:
                 item = item.strip()
                 if item and not self.__contains_inline_python(item):
                     self.__addentry(item, tag, iskidstring=1)
         elif ev == kid_parser.START:
             tag = item.tag
             if isinstance(tag, basestring):
                 tag = self.__strip_namespace_uri(tag)
                 if tag in ('script', 'style'):
                     tag = None
             else:
                 tag = None
             tags.append(tag)
         elif ev == kid_parser.END:
             if tags:
                 tag = tags.pop()
Example #5
0
def error_location(filename, encoding=None, entity_map=None, lineno=None):
    if lineno:
        try:
            source = open(filename, "rb")
            try:
                doc = document(source, encoding=encoding, filename=filename, entity_map=entity_map, debug=True)
                writer = KidWriter(doc, encoding, filename, lineno)
                return writer.parse()
            finally:
                source.close()
        except Exception:
            pass
def error_location(filename, encoding=None, entity_map=None, lineno=None):
    if lineno:
        try:
            source = open(filename, 'rb')
            try:
                doc = document(source, encoding=encoding,
                    filename=filename, entity_map=entity_map, debug=True)
                writer = KidWriter(doc, encoding, filename, lineno)
                return writer.parse()
            finally:
                source.close()
        except Exception:
            pass
def xinclude_filter(stream, template):
    xi = Namespace('http://www.w3.org/2001/XInclude')
    include = xi.include
    fallback = xi.fallback
    for ev, item in stream:
        if ev == START and item.tag == include:
            item = item.expand()
            href = item.get('href')
            try:
                doc = document(href, template._get_assume_encoding())
            except:
                fallback_elm = item.find(fallback)
                for ev, item in ElementStream(fallback_elm).strip(1):
                    yield ev, item
            else:
                for ev, item in doc:
                    if ev != XML_DECL:
                        yield ev
Example #8
0
def xinclude_filter(stream, template):
    xi = Namespace('http://www.w3.org/2001/XInclude')
    include = xi.include
    fallback = xi.fallback
    for ev, item in stream:
        if ev == START and item.tag == include:
            item = item.expand()
            href = item.get('href')
            try:
                doc = document(href, template._get_assume_encoding())
            except:
                fallback_elm = item.find(fallback)
                for ev, item in ElementStream(fallback_elm).strip(1):
                    yield ev, item
            else:
                for ev, item in doc:
                    if ev != XML_DECL:
                        yield ev
def parse(source, encoding=None, filename=None, entity_map=None):
    doc = document(source, encoding=encoding,
        filename=filename, entity_map=entity_map)
    return KidWriter(doc, encoding, filename).parse()