diff options
author | Waylan Limberg <waylan@gmail.com> | 2011-06-07 10:48:55 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2011-06-07 10:48:55 -0400 |
commit | 11124d9f8ddd547c93078dcf6380deacec7fc32f (patch) | |
tree | fd6279145e7dae3ef02505c8ffd74ac0d106fcf0 | |
parent | bca0d6bf8b26707431c936ed51419358b9e57749 (diff) | |
download | markdown-11124d9f8ddd547c93078dcf6380deacec7fc32f.tar.gz markdown-11124d9f8ddd547c93078dcf6380deacec7fc32f.tar.bz2 markdown-11124d9f8ddd547c93078dcf6380deacec7fc32f.zip |
Added smart_strong extension. Fixes Issue #17. Like PHP Markdown Extra, we now include smart handling of double underscores (not just single underscores). The new behavior may be called seperately as the 'smart_strong' extension or as part of the 'extra' extension.
-rw-r--r-- | docs/extensions/Smart_Strong.txt | 30 | ||||
-rw-r--r-- | docs/extensions/extra.txt | 1 | ||||
-rw-r--r-- | markdown/extensions/extra.py | 3 | ||||
-rw-r--r-- | markdown/extensions/smart_strong.py | 45 |
4 files changed, 78 insertions, 1 deletions
diff --git a/docs/extensions/Smart_Strong.txt b/docs/extensions/Smart_Strong.txt new file mode 100644 index 0000000..c939016 --- /dev/null +++ b/docs/extensions/Smart_Strong.txt @@ -0,0 +1,30 @@ +Smart_Strong +------------ + +Summary + +The Markdown Smart_Strong Extension adds smarter handling of double underscores +within words. This does for double underscores what 'smart_emphasis' does for +single underscores. + +The Smart_Strong Extension is included in the standard Markdown library. + +Usage +----- + + >>> import markdown + >>> markdown.markdown('Text with double__underscore__words.', \ + extensions=['smart_strong']) + u'<p>Text with double__underscore__words.</p>' + >>> markdown.markdown('__Strong__ still works.', \ + extensions=['smart_strong']) + u'<p><strong>Strong</strong> still works.</p>' + >>> markdown.markdown('__this__works__too__.', \ + extensions=['smart_strong']) + u'<p><strong>this__works__too</strong>.</p>' + +This extension is also included with the [[Extra]] Extension. You may call that +extension to get this behavior with all the other features of 'Extra'. + + >>> markdown.markdown(text, extensions=['extra']) + diff --git a/docs/extensions/extra.txt b/docs/extensions/extra.txt index 817d58f..018a849 100644 --- a/docs/extensions/extra.txt +++ b/docs/extensions/extra.txt @@ -15,6 +15,7 @@ The supported extensions include: * [[Footnotes]] * [[HeaderId]] * [[Tables]] +* [[Smart_Strong]] See each individual extension for syntax documentation. Extra and all it's supported extensions are included in the standard Markdown library. diff --git a/markdown/extensions/extra.py b/markdown/extensions/extra.py index e569029..60ad55e 100644 --- a/markdown/extensions/extra.py +++ b/markdown/extensions/extra.py @@ -29,7 +29,8 @@ when you upgrade to any future version of Python-Markdown. import markdown -extensions = ['fenced_code', +extensions = ['smart_strong', + 'fenced_code', 'footnotes', 'headerid', 'def_list', diff --git a/markdown/extensions/smart_strong.py b/markdown/extensions/smart_strong.py new file mode 100644 index 0000000..4fb3280 --- /dev/null +++ b/markdown/extensions/smart_strong.py @@ -0,0 +1,45 @@ +''' +Smart_Strong Extension for Python-Markdown +========================================== + +This extention as a smarter handleing of double underscores within words. + +Simple Usage: + + >>> import markdown + >>> markdown.markdown('Text with double__underscore__words.', \ + extensions=['smart_strong']) + u'<p>Text with double__underscore__words.</p>' + >>> markdown.markdown('__Strong__ still works.', \ + extensions=['smart_strong']) + u'<p><strong>Strong</strong> still works.</p>' + >>> markdown.markdown('__this__works__too__.', \ + extensions=['smart_strong']) + u'<p><strong>this__works__too</strong>.</p>' + +Copyright 2011 +[Waylan Limberg](http://achinghead.com) + +''' + +import re +import markdown +from markdown.inlinepatterns import SimpleTagPattern + +SMART_STRONG_RE = r'(?<!\w)(_{2})(?!_)(.+?)(?<!_)\2(?!\w)' +STRONG_RE = r'(\*{2})(.+?)\2' + +class SmartEmphasisExtension(markdown.extensions.Extension): + """ Add smart_emphasis extension to Markdown class.""" + + def extendMarkdown(self, md, md_globals): + """ Modify inline patterns. """ + md.inlinePatterns['strong'] = SimpleTagPattern(STRONG_RE, 'strong') + md.inlinePatterns['strong2'] = SimpleTagPattern(SMART_STRONG_RE, 'strong') + +def makeExtension(configs={}): + return SmartEmphasisExtension(configs=dict(configs)) + +if __name__ == '__main__': + import doctest + doctest.testmod() |