Ejemplo n.º 1
0
 def run(self, parent, blocks):
     block = blocks.pop(0)
     m = self.RE.match(block)
     if m:
         # Add remaining line to master blocks for later.
         blocks.insert(0, block[m.end():])
         sibling = self.lastChild(parent)
         if sibling and sibling.tag == 'pre' and sibling[0] and \
                 sibling[0].tag == 'code':
             # Last block is a codeblock. Append to preserve whitespace.
             sibling[0].text = util.AtomicString('%s/n/n/n' % sibling[0].text )
Ejemplo n.º 2
0
 def run(self, parent, blocks):
     sibling = self.lastChild(parent)
     block = blocks.pop(0)
     theRest = ''
     if sibling and sibling.tag == "pre" and len(sibling) \
                 and sibling[0].tag == "code":
         # The previous block was a code block. As blank lines do not start
         # new code blocks, append this block to the previous, adding back
         # linebreaks removed from the split into a list.
         code = sibling[0]
         block, theRest = self.detab(block)
         code.text = util.AtomicString('%s\n%s\n' % (code.text, block.rstrip()))
     else:
         # This is a new codeblock. Create the elements and insert text.
         pre = util.etree.SubElement(parent, 'pre')
         code = util.etree.SubElement(pre, 'code')
         block, theRest = self.detab(block)
         code.text = util.AtomicString('%s\n' % block.rstrip())
     if theRest:
         # This block contained unindented line(s) after the first indented 
         # line. Insert these lines as the first block of the master blocks
         # list for future processing.
         blocks.insert(0, theRest)
Ejemplo n.º 3
0
    def handleMatch(self, m):
        el = util.etree.Element('a')
        email = self.unescape(m.group(2))
        if email.startswith("mailto:"):
            email = email[len("mailto:"):]

        def codepoint2name(code):
            """Return entity definition by code, or the code if not defined."""
            entity = htmlentitydefs.codepoint2name.get(code)
            if entity:
                return "%s%s;" % (util.AMP_SUBSTITUTE, entity)
            else:
                return "%s#%d;" % (util.AMP_SUBSTITUTE, code)

        letters = [codepoint2name(ord(letter)) for letter in email]
        el.text = util.AtomicString(''.join(letters))

        mailto = "mailto:" + email
        mailto = "".join(
            [util.AMP_SUBSTITUTE + '#%d;' % ord(letter) for letter in mailto])
        el.set('href', mailto)
        return el
Ejemplo n.º 4
0
 def handleMatch(self, m):
     el = util.etree.Element("a")
     el.set('href', self.unescape(m.group(2)))
     el.text = util.AtomicString(m.group(2))
     return el
Ejemplo n.º 5
0
 def handleMatch(self, m):
     el = util.etree.Element(self.tag)
     el.text = util.AtomicString(m.group(3).strip())
     return el
Ejemplo n.º 6
0
    def __processPlaceholders(self, data, parent):
        """
        Process string with placeholders and generate ElementTree tree.

        Keyword arguments:

        * data: string with placeholders instead of ElementTree elements.
        * parent: Element, which contains processing inline data

        Returns: list with ElementTree elements with applied inline patterns.
        
        """
        def linkText(text):
            if text:
                if result:
                    if result[-1].tail:
                        result[-1].tail += text
                    else:
                        result[-1].tail = text
                else:
                    if parent.text:
                        parent.text += text
                    else:
                        parent.text = text

        result = []
        strartIndex = 0
        while data:
            index = data.find(self.__placeholder_prefix, strartIndex)
            if index != -1:
                id, phEndIndex = self.__findPlaceholder(data, index)

                if id in self.stashed_nodes:
                    node = self.stashed_nodes.get(id)

                    if index > 0:
                        text = data[strartIndex:index]
                        linkText(text)

                    if not isString(node):  # it's Element
                        for child in [node] + node.getchildren():
                            if child.tail:
                                if child.tail.strip():
                                    self.__processElementText(
                                        node, child, False)
                            if child.text:
                                if child.text.strip():
                                    self.__processElementText(child, child)
                    else:  # it's just a string
                        linkText(node)
                        strartIndex = phEndIndex
                        continue

                    strartIndex = phEndIndex
                    result.append(node)

                else:  # wrong placeholder
                    end = index + len(self.__placeholder_prefix)
                    linkText(data[strartIndex:end])
                    strartIndex = end
            else:
                text = data[strartIndex:]
                if isinstance(data, util.AtomicString):
                    # We don't want to loose the AtomicString
                    text = util.AtomicString(text)
                linkText(text)
                data = ""

        return result