开发者

python-markdown htmlStash placeholder not being replaced

I am currently developing a web application using django and and using python-markdown to convert markdown into HTML. There are a couple of situations that markdown currently doesn't handle, and as such have written a couple of basic extensions.

"""

Helps make paras for Less framework

@div large-column float-left

# This is an H1

this is a paragraph right here!

and a new one

## Heading 2

and yet another one

--> becomes -->

<div class="large-column float left">
    <h1>This is an H1</h1>
    <p>this is a paragraph right here!</p>
    <p>and a new one</p>
    <h2>Heading 2</h2>
    <p>and yet another one</p>
</div>

"""

import re
import markdown

# Global vars

LESS_BLOCK_RE = re.compile( \
    r'@(?P<tag>div|span)[ ]*(?P<class>[a-zA-z0-9-\ ^\n]+)[ ]*\n(?P<inner>.*)(?=div|span)?',
    re.MULTILINE|re.DOTALL
    )

class LessFrameworkExtension(markdown.Extension):

    def extendMarkdown(self, md, md_globals):
        md.registerExtension(self)

        md.preprocessors.add('less_fr开发者_JS百科amework', LessBlockPreprocessor(md),'_begin')

    def reset(self):
        print 'resetting'

class LessBlockPreprocessor(markdown.preprocessors.Preprocessor):

    def __init__(self, md):
        markdown.preprocessors.Preprocessor.__init__(self, md)

    def getConfig(self, key):
        if key in self.config:
            return self.config[key][0]
        else:
            return None

    def run(self, lines):
        """ Match and store Less Framework Blocks in the HTML Stash """

        text = "\n".join(lines)

        while 1:
            m = LESS_BLOCK_RE.search(text)
            if m:
                less_tag = m.group('tag')
                less_class = m.group('class')
                less_inner = m.group('inner')

                print less_tag
                print less_class
                print less_inner

                placeholder = self.markdown.htmlStash.store(less_inner, safe=True)
                text = '<%s class="%s">\n%s\n</%s>' % (less_tag, less_class, placeholder, less_tag)
            else:
                break
        return text.split("\n")

    def _escape(self, txt):
        """ basic html escaping """
        txt = txt.replace('&', '&amp;')
        txt = txt.replace('<', '&lt;')
        txt = txt.replace('>', '&gt;')
        txt = txt.replace('"', '&quot;')
        return txt

def makeExtension(configs):
    return LessFrameworkExtension(configs)

The above extension works partially, but the output is:

<div class="large-column float-left
">
wzxhzdk:0
</div>'

This appears to be the htmlStash store placeholder. Perhaps I am missing a call to python-markdown? Looking at similar extensions in the python-markdown project, it appears that my approach is consistent.

Any help would be much appreciated!

Example Input and Expected Output

@div large-column float-left

# This is an H1

this is a paragraph right here!

and a new one

## Heading 2

and yet another one

Extended Markdown --> becomes --> HTML

<div class="large-column float left">
    <h1>This is an H1</h1>
    <p>this is a paragraph right here!</p>
    <p>and a new one</p>
    <h2>Heading 2</h2>
    <p>and yet another one</p>
</div>


I know this is from a long time ago, but for any others (like me) who run into this issue and see this post, you need to make sure to register the preprocessor after at least the normalize_whitespace step (which is stripping unicode characters -- and what the htmlstash function is using as delimiters).

in this case

md.preprocessors.add('less_framework', LessBlockPreprocessor(md),'_begin')

should be:

md.preprocessors.add('less_framework', LessBlockPreprocessor(md),'>normalize_whitespace')

More info here: https://github.com/Python-Markdown/markdown/issues/222

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜