diff options
author | Waylan Limberg <waylan@gmail.com> | 2011-06-21 07:57:59 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2011-06-21 07:57:59 -0400 |
commit | 8525f57ba7fc10b89c1a679aea1400d258fd0a02 (patch) | |
tree | 922b5ca9ce0d216c8454baa96518a27ded8442d5 | |
parent | 95e2264dc5cacce6041d94ba731c5b15460b37ac (diff) | |
download | markdown-8525f57ba7fc10b89c1a679aea1400d258fd0a02.tar.gz markdown-8525f57ba7fc10b89c1a679aea1400d258fd0a02.tar.bz2 markdown-8525f57ba7fc10b89c1a679aea1400d258fd0a02.zip |
Added nl2br extension. Closes #13. Thanks to Brian Neal for writing the code and allowing us to use it.
-rw-r--r-- | docs/extensions/index.txt | 19 | ||||
-rw-r--r-- | docs/extensions/nl2br.txt | 19 | ||||
-rw-r--r-- | markdown/extensions/nl2br.py | 35 |
3 files changed, 56 insertions, 17 deletions
diff --git a/docs/extensions/index.txt b/docs/extensions/index.txt index 71d857c..6f43519 100644 --- a/docs/extensions/index.txt +++ b/docs/extensions/index.txt @@ -17,28 +17,13 @@ available to you. * [[Footnotes]] * [[HeaderId]] * [[Tables]] +* [[attr_list]] * [[CodeHilite]] * [[HTML_Tidy]] * [[ImageLinks]] * [[Meta-Data]] +* [[nl2br]] * [[RSS]] * [[Table_of_Contents]] * [[WikiLinks]] -Unofficially Supported Extensions ---------------------------------- - -These extensions have not yet been included in any official Python-Markdown -release. However, the code is maintained in the projects -[mainline git repository](http://gitorious.org/projects/python-markdown/repos/mainline) -by the Python-Markdown developers and the official documentation is maintained -here. All bug reports should be made to the project. It is anticipated that -these extensions will be included with some future official release, at which -time they will be moved to the above list of official extensions. - -* [[Legacy]] - - - - - diff --git a/docs/extensions/nl2br.txt b/docs/extensions/nl2br.txt new file mode 100644 index 0000000..27030f7 --- /dev/null +++ b/docs/extensions/nl2br.txt @@ -0,0 +1,19 @@ +NL2BR Extension +=============== + +A Python-Markdown extension to treat newlines as hard breaks; like +StackOverflow and [GitHub][] flavored Markdown do. + +Usage: + + >>> import markdown + >>> text = """ + ... Line 1 + ... Line 2 + ... """ + >>> html = markdown.markdown(text, extensions=['nl2br']) + >>> print html + <p>Line 1<br /> + Line 2</p> + +[Github]: http://github.github.com/github-flavored-markdown/ diff --git a/markdown/extensions/nl2br.py b/markdown/extensions/nl2br.py new file mode 100644 index 0000000..bd11a74 --- /dev/null +++ b/markdown/extensions/nl2br.py @@ -0,0 +1,35 @@ +""" +NL2BR Extension +=============== + +A Python-Markdown extension to treat newlines as hard breaks; like +StackOverflow and GitHub flavored Markdown do. + +Usage: + + >>> import markdown + >>> markdown.markdown('line 1\\nline 2', extensions=['nl2br']) + u'<p>line 1<br />\\nline 2</p>' + +Copyright 2011 [Brian Neal](http://deathofagremmie.com/) + +Dependencies: +* [Python 2.4+](http://python.org) +* [Markdown 2.1+](http://www.freewisdom.org/projects/python-markdown/) + +""" + +import markdown + +BR_RE = r'\n' + +class Nl2BrExtension(markdown.Extension): + + def extendMarkdown(self, md, md_globals): + br_tag = markdown.inlinepatterns.SubstituteTagPattern(BR_RE, 'br') + md.inlinePatterns.add('nl', br_tag, '_end') + + +def makeExtension(configs=None): + return Nl2BrExtension(configs) + |