aboutsummaryrefslogtreecommitdiffstats
path: root/markdown/extensions/nl2br.py
diff options
context:
space:
mode:
Diffstat (limited to 'markdown/extensions/nl2br.py')
-rw-r--r--markdown/extensions/nl2br.py35
1 files changed, 35 insertions, 0 deletions
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)
+