Exemplo n.º 1
0
    def _prettifyETree(self, elem):
        """ Recursively add linebreaks to ElementTree children. """

        i = "\n"
        if markdown.isBlockLevel(elem.tag) and elem.tag not in ['code', 'pre']:
            if (not elem.text or not elem.text.strip()) \
                    and len(elem) and markdown.isBlockLevel(elem[0].tag):
                elem.text = i
            for e in elem:
                if markdown.isBlockLevel(e.tag):
                    self._prettifyETree(e)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
Exemplo n.º 2
0
    def _prettifyETree(self, elem):
        """ Recursively add linebreaks to ElementTree children. """

        i = "\n"
        if markdown.isBlockLevel(elem.tag) and elem.tag not in ['code', 'pre']:
            if (not elem.text or not elem.text.strip()) \
                    and len(elem) and markdown.isBlockLevel(elem[0].tag):
                elem.text = i
            for e in elem:
                if markdown.isBlockLevel(e.tag):
                    self._prettifyETree(e)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
Exemplo n.º 3
0
    def run(self, lines):
        text = "\n".join(lines)
        new_blocks = []
        text = text.split("\n\n")
        items = []
        left_tag = ''
        right_tag = ''
        in_tag = False # flag

        while text:
            block = text[0]
            if block.startswith("\n"):
                block = block[1:]
            text = text[1:]

            if block.startswith("\n"):
                block = block[1:]

            if not in_tag:
                if block.startswith("<"):
                    left_tag = self._get_left_tag(block)
                    right_tag, data_index = self._get_right_tag(left_tag, block)

                    if data_index < len(block):
                        text.insert(0, block[data_index:])
                        block = block[:data_index]

                    if not (markdown.isBlockLevel(left_tag) \
                        or block[1] in ["!", "?", "@", "%"]):
                        new_blocks.append(block)
                        continue

                    if self._is_oneliner(left_tag):
                        new_blocks.append(block.strip())
                        continue

                    if block[1] == "!":
                        # is a comment block
                        left_tag = "--"
                        right_tag, data_index = self._get_right_tag(left_tag, block)
                        # keep checking conditions below and maybe just append

                    if block.rstrip().endswith(">") \
                        and self._equal_tags(left_tag, right_tag):
                        new_blocks.append(
                            self.markdown.htmlStash.store(block.strip()))
                        continue
                    else: #if not block[1] == "!":
                        # if is block level tag and is not complete

                        if markdown.isBlockLevel(left_tag) or left_tag == "--" \
                        and not block.rstrip().endswith(">"):
                            items.append(block.strip())
                            in_tag = True
                        else:
                            new_blocks.append(
                            self.markdown.htmlStash.store(block.strip()))

                        continue

                new_blocks.append(block)

            else:
                items.append(block.strip())

                right_tag, data_index = self._get_right_tag(left_tag, block)

                if self._equal_tags(left_tag, right_tag):
                    # if find closing tag
                    in_tag = False
                    new_blocks.append(
                        self.markdown.htmlStash.store('\n\n'.join(items)))
                    items = []

        if items:
            new_blocks.append(self.markdown.htmlStash.store('\n\n'.join(items)))
            new_blocks.append('\n')

        new_text = "\n\n".join(new_blocks)
        return new_text.split("\n")
Exemplo n.º 4
0
    def run(self, lines):
        text = "\n".join(lines)
        new_blocks = []
        text = text.split("\n\n")
        items = []
        left_tag = ''
        right_tag = ''
        in_tag = False  # flag

        while text:
            block = text[0]
            if block.startswith("\n"):
                block = block[1:]
            text = text[1:]

            if block.startswith("\n"):
                block = block[1:]

            if not in_tag:
                if block.startswith("<"):
                    left_tag = self._get_left_tag(block)
                    right_tag, data_index = self._get_right_tag(
                        left_tag, block)

                    if data_index < len(block):
                        text.insert(0, block[data_index:])
                        block = block[:data_index]

                    if not (markdown.isBlockLevel(left_tag) \
                        or block[1] in ["!", "?", "@", "%"]):
                        new_blocks.append(block)
                        continue

                    if self._is_oneliner(left_tag):
                        new_blocks.append(block.strip())
                        continue

                    if block[1] == "!":
                        # is a comment block
                        left_tag = "--"
                        right_tag, data_index = self._get_right_tag(
                            left_tag, block)
                        # keep checking conditions below and maybe just append

                    if block.rstrip().endswith(">") \
                        and self._equal_tags(left_tag, right_tag):
                        new_blocks.append(
                            self.markdown.htmlStash.store(block.strip()))
                        continue
                    else:  #if not block[1] == "!":
                        # if is block level tag and is not complete

                        if markdown.isBlockLevel(left_tag) or left_tag == "--" \
                        and not block.rstrip().endswith(">"):
                            items.append(block.strip())
                            in_tag = True
                        else:
                            new_blocks.append(
                                self.markdown.htmlStash.store(block.strip()))

                        continue

                new_blocks.append(block)

            else:
                items.append(block.strip())

                right_tag, data_index = self._get_right_tag(left_tag, block)

                if self._equal_tags(left_tag, right_tag):
                    # if find closing tag
                    in_tag = False
                    new_blocks.append(
                        self.markdown.htmlStash.store('\n\n'.join(items)))
                    items = []

        if items:
            new_blocks.append(self.markdown.htmlStash.store(
                '\n\n'.join(items)))
            new_blocks.append('\n')

        new_text = "\n\n".join(new_blocks)
        return new_text.split("\n")
Exemplo n.º 5
0
    def run(self, lines):
        text = "\n".join(lines)
        new_blocks = []
        text = text.split("\n\n")
        items = []
        left_tag = ''
        right_tag = ''
        in_tag = False # flag

        while text:
            block = text[0]
            text = text[1:]

            if not in_tag:

                if not block.startswith("<"):
                    new_blocks.append(block)
                    continue

                left_tag = self._get_left_tag(block)
                # data_index 是 right_tag 结束的地方
                right_tag, data_index = self._get_right_tag(left_tag, block)

                if block[1] == "!":
                    # 处理 "<!--html注释-->"
                    left_tag = "--"
                    right_tag, data_index = self._get_right_tag(left_tag, block)

                if data_index < len(block) and \
                        markdown.isBlockLevel(left_tag):
                    # 处理这样的 block: "<pre>内容</pre>其他"
                    # 这样 data_index 的值会小于 block 长度
                    # 需先将 pre 块后面的放回 text 列表,下次处理
                    text.insert(0, block[data_index:])
                    block = block[:data_index]

                if not markdown.isBlockLevel(left_tag) and \
                        block[1] not in ["!", "?", "@", "%"]:
                    # 既不是合法html块标签,又不是特殊标签
                    new_blocks.append(block)
                    continue

                if self._is_oneliner(left_tag):
                    # 处理 "<hr/>","<hr>" 这样的行分割符
                    new_blocks.append(block.strip())
                    continue

                if not block.rstrip().endswith(">") or not\
                        self._equal_tags(left_tag, right_tag) and\
                        markdown.isBlockLevel(left_tag) or\
                        left_tag == "--" :
                    # block 里的 html 标签对还未结束
                    items.append(block.strip())
                    in_tag = True
                    continue

                # 现在 block 里一定是 html 块处理
                new_blocks.append(self.markdown.htmlStash.store(block.strip()))

            else:
                # 进入跨 block 的 html 块标签
                right_tag, data_index = self._get_right_tag(left_tag, block)

                if data_index < len(block) and \
                        markdown.isBlockLevel(left_tag):
                    text.insert(0, block[data_index:])
                    block = block[:data_index]

                items.append(block.strip())

                if self._equal_tags(left_tag, right_tag):
                    # 如果左右标签成对,就准备结束 in_tag 情况
                    in_tag = False
                    new_blocks.append(self.markdown.htmlStash.store('\n\n'.join(items)))
                    items = []

        if items:
            # 如果 items 里面还有内容,说明有 html 块标签未配对,直接加上
            new_blocks.append(self.markdown.htmlStash.store('\n\n'.join(items)))
            new_blocks.append('\n')

        return "\n\n".join(new_blocks).split("\n")
Exemplo n.º 6
0
    def run(self, lines):
        text = "\n".join(lines)
        new_blocks = []
        text = text.split("\n\n")
        items = []
        left_tag = ''
        right_tag = ''
        in_tag = False  # flag

        while text:
            block = text[0]
            text = text[1:]

            if not in_tag:

                if not block.startswith("<"):
                    new_blocks.append(block)
                    continue

                left_tag = self._get_left_tag(block)
                # data_index 是 right_tag 结束的地方
                right_tag, data_index = self._get_right_tag(left_tag, block)

                if block[1] == "!":
                    # 处理 "<!--html注释-->"
                    left_tag = "--"
                    right_tag, data_index = self._get_right_tag(
                        left_tag, block)

                if data_index < len(block) and \
                        markdown.isBlockLevel(left_tag):
                    # 处理这样的 block: "<pre>内容</pre>其他"
                    # 这样 data_index 的值会小于 block 长度
                    # 需先将 pre 块后面的放回 text 列表,下次处理
                    text.insert(0, block[data_index:])
                    block = block[:data_index]

                if not markdown.isBlockLevel(left_tag) and \
                        block[1] not in ["!", "?", "@", "%"]:
                    # 既不是合法html块标签,又不是特殊标签
                    new_blocks.append(block)
                    continue

                if self._is_oneliner(left_tag):
                    # 处理 "<hr/>","<hr>" 这样的行分割符
                    new_blocks.append(block.strip())
                    continue

                if not block.rstrip().endswith(">") or not\
                        self._equal_tags(left_tag, right_tag) and\
                        markdown.isBlockLevel(left_tag) or\
                        left_tag == "--" :
                    # block 里的 html 标签对还未结束
                    items.append(block.strip())
                    in_tag = True
                    continue

                # 现在 block 里一定是 html 块处理
                new_blocks.append(self.markdown.htmlStash.store(block.strip()))

            else:
                # 进入跨 block 的 html 块标签
                right_tag, data_index = self._get_right_tag(left_tag, block)

                if data_index < len(block) and \
                        markdown.isBlockLevel(left_tag):
                    text.insert(0, block[data_index:])
                    block = block[:data_index]

                items.append(block.strip())

                if self._equal_tags(left_tag, right_tag):
                    # 如果左右标签成对,就准备结束 in_tag 情况
                    in_tag = False
                    new_blocks.append(
                        self.markdown.htmlStash.store('\n\n'.join(items)))
                    items = []

        if items:
            # 如果 items 里面还有内容,说明有 html 块标签未配对,直接加上
            new_blocks.append(self.markdown.htmlStash.store(
                '\n\n'.join(items)))
            new_blocks.append('\n')

        return "\n\n".join(new_blocks).split("\n")
Exemplo n.º 7
0
    def run(self, lines):
        text = "\n".join(lines)
        new_blocks = []
        text = text.split("\n\n")
        items = []
        left_tag = ''
        right_tag = ''
        in_tag = False # flag
        
        while text:
            block = text[0]
            if block.startswith("\n"):
                block = block[1:]
            text = text[1:]

            if block.startswith("\n"):
                block = block[1:]

            if not in_tag:
                if block.startswith("<"):
                    left_tag, left_index, attrs = self._get_left_tag(block)
                    right_tag, data_index = self._get_right_tag(left_tag, 
                                                                left_index,
                                                                block)

                    if block[1] == "!":
                        # is a comment block
                        left_tag = "--"
                        right_tag, data_index = self._get_right_tag(left_tag, 
                                                                    left_index,
                                                                    block)
                        # keep checking conditions below and maybe just append
                    
                    if data_index < len(block) \
                        and markdown.isBlockLevel(left_tag): 
                        text.insert(0, block[data_index:])
                        block = block[:data_index]

                    if not (markdown.isBlockLevel(left_tag) \
                        or block[1] in ["!", "?", "@", "%"]):
                        new_blocks.append(block)
                        continue

                    if self._is_oneliner(left_tag):
                        new_blocks.append(block.strip())
                        continue
                    
                    if block.rstrip().endswith(">") \
                        and self._equal_tags(left_tag, right_tag):
                        if self.markdown_in_raw and 'markdown' in attrs.keys():
                            start = re.sub(r'\smarkdown(=[\'"]?[^> ]*[\'"]?)?', 
                                           '', block[:left_index])
                            end = block[-len(right_tag)-2:]
                            block = block[left_index:-len(right_tag)-2]
                            new_blocks.append(
                                self.markdown.htmlStash.store(start))
                            new_blocks.append(block)
                            new_blocks.append(
                                self.markdown.htmlStash.store(end))
                        else:
                            new_blocks.append(
                                self.markdown.htmlStash.store(block.strip()))
                        continue
                    else: 
                        # if is block level tag and is not complete

                        if markdown.isBlockLevel(left_tag) or left_tag == "--" \
                            and not block.rstrip().endswith(">"):
                            items.append(block.strip())
                            in_tag = True
                        else:
                            new_blocks.append(
                            self.markdown.htmlStash.store(block.strip()))

                        continue

                new_blocks.append(block)

            else:
                items.append(block)

                right_tag, data_index = self._get_right_tag(left_tag, 
                                                            left_index, 
                                                            block)

                if self._equal_tags(left_tag, right_tag):
                    # if find closing tag
                    in_tag = False
                    if self.markdown_in_raw and 'markdown' in attrs.keys():
                        start = re.sub(r'\smarkdown(=[\'"]?[^> ]*[\'"]?)?', 
                                       '', items[0][:left_index])
                        items[0] = items[0][left_index:]
                        end = items[-1][-len(right_tag)-2:]
                        items[-1] = items[-1][:-len(right_tag)-2]
                        new_blocks.append(
                            self.markdown.htmlStash.store(start))
                        new_blocks.extend(items)
                        new_blocks.append(
                            self.markdown.htmlStash.store(end))
                    else:
                        new_blocks.append(
                            self.markdown.htmlStash.store('\n\n'.join(items)))
                    items = []

        if items:
            if self.markdown_in_raw and 'markdown' in attrs.keys():
                start = re.sub(r'\smarkdown(=[\'"]?[^> ]*[\'"]?)?', 
                               '', items[0][:left_index])
                items[0] = items[0][left_index:]
                end = items[-1][-len(right_tag)-2:]
                items[-1] = items[-1][:-len(right_tag)-2]
                new_blocks.append(
                    self.markdown.htmlStash.store(start))
                new_blocks.extend(items)
                new_blocks.append(
                    self.markdown.htmlStash.store(end))
            else:
                new_blocks.append(
                    self.markdown.htmlStash.store('\n\n'.join(items)))
            #new_blocks.append(self.markdown.htmlStash.store('\n\n'.join(items)))
            new_blocks.append('\n')

        new_text = "\n\n".join(new_blocks)
        return new_text.split("\n")