Commits

Anonymous committed e2ca41f

Deprecate ``webhelpers.markdown``; users should migrate the separate Markdown package.
Add a ``markdown`` argument to ``markdown()`` to specify which implementation
to use.

Comments (0)

Files changed (4)

   - Fix HTML overescaping and underescaping in auto_link().  Patch by Marius
     Gedminas.  A parsing bug remains: 
     http://pylonshq.com/project/pylonshq/ticket/657
+* webhelpers.markdown / webhelpers.html.converters:
+  - Deprecate ``webhelpers.markdown``, which is Markdown 1.7 without its
+    extensions. Users can upgrade to Markdown 2 in PyPi, or use the alternative
+    Markdown2 package. The ``markdown()`` helper in
+    ``webhelpers.html.converters`` now takes a ``markdown`` argument to specify
+    which implementation to use, or it will attempt to import ``markdown``, or
+    else fall back to ``webhelpers.markdown`` (with a deprecation warning).
 * webhelpers.media:
   - Bugfix in ``get_dimensions_pil``.
 * webhelpers.paginate:

tests/test_converters.py

 
 from webhelpers.html import HTML, literal
 from webhelpers.html.converters import *
+from webhelpers.html.converters import _get_markdown_module
 
 def test_textilize():
     eq_('<h1>This is a test of textile</h1>\n\n<p>Paragraph</p>\n\n<p>Another paragraph</p>\n\n<ul>\n<li>Bullets</li>\n</ul>',
         textilize("h1. This is a test of textile\n\nParagraph\n\nAnother paragraph\n\n* Bullets"))
 
 def test_markdown():
+    _markdown = _get_markdown_module()
     markdown_text = """
 Introduction
 ------------
 
 [Michel Fortin][] has ported to Markdown to PHP.
     """
-    eq_(markdown(markdown_text), literal(u'<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.\n</p>\n\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.\n</p>'))
+    if _markdown.version_info >= (2, 0):
+        expected =  literal(u'<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.</p>\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.</p>')
+    else:
+        expected =  literal(u'<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.\n</p>\n\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.\n</p>')
+    eq_(markdown(markdown_text, markdown=_markdown), expected)
 
 def test_markdown_embedded_html():
+    _markdown = _get_markdown_module()
     markdown_text = 'This text includes <script>Javascript</script> & stuff.'
-    expected = literal(u'<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.\n</p>')
-    eq_(markdown(markdown_text, safe_mode="escape"), expected)
+    if _markdown.version_info >= (2, 0):
+        expected = literal(u'<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.</p>')
+    else:
+        expected = literal(u'<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.\n</p>')
+    eq_(markdown(markdown_text, safe_mode="escape", markdown=_markdown), expected)
 
 
 def test_nl2br():

webhelpers/html/converters.py

 from webhelpers.html import HTML, escape, literal, lit_sub
 from webhelpers.html.render import render, sanitize
 import webhelpers.textile as textile
-import webhelpers.markdown as _markdown
 
 __all__ = [
     "format_paragraphs",
 _paragraph_rx = re.compile(R"\n{2,}")  # Paragraph break: 2 or more newlines.
 br = HTML.br() + "\n"
 
-def markdown(text, **kwargs):
-    """Format the text to HTML with MarkDown formatting.
-    
-    This function uses the `Python MarkDown library 
-    <http://www.freewisdom.org/projects/python-markdown/>`_
-    which is included with WebHelpers.  It does not include extensions
-    due to circular import issues.  If you need the footnotes or RSS
-    extensions, use the full Markdown package instead.  
+def markdown(text, markdown=None, **kwargs):
+    """Format the text to HTML with Markdown formatting.
+
+    Markdown is a wiki-like text markup language, originally written by
+    John Gruber for Perl.  The helper converts Markdown text to HTML.
+
+    There are at least two Python implementations of Markdown.
+    Markdown <http://www.freewisdom.org/projects/python-markdown/>`_is the
+    original port, and version 2.x contains extensions for footnotes, RSS, etc. 
+    `Markdown2 <http://code.google.com/p/python-markdown2/>`_ is another port
+    which claims to be faster and to handle edge cases better. 
+    WebHelpers itself contains an old version of Markdown
+    (``webhelpers.markdown`` == Markdown 1.7 without the extensions), but it's 
+    deprecated.  Most WebHelpers users use Markdown from Freewisdom.com.
+
+    You can pass the desired Markdown module as the ``markdown``
+    argument, or the helper will try to import ``markdown`` and fall back to
+    ``webhelpers.markdown`` if it can't. (The latter will trigger a deprecation
+    warning.)
     
     IMPORTANT:
     If your source text is untrusted and may contain malicious HTML markup,
     pass ``safe_mode="escape"`` to escape it, ``safe_mode="replace"`` to
     replace it with a scolding message, or ``safe_mode="remove"`` to strip it.
+    """
+    if not markdown:
+        markdown = _get_markdown_module()
+    return literal(markdown.markdown(text, **kwargs))
 
-    There is at least one other Markdown package for Python.  python-markdown2
-    (http://code.google.com/p/python-markdown2/), which claims to be faster and
-    to handle edge cases better.  WebHelpers is sticking to the original Python
-    Markdown for now for backward compatibility and because nobody has
-    complained about the speed.
-    """
-    return literal(_markdown.markdown(text, **kwargs))
+def _get_markdown_module():
+    try:
+        import markdown
+    except ImportError:
+        import webhelpers.markdown as markdown
+    return markdown
 
 def textilize(text, sanitize=False):
     """Format the text to HTML with Textile formatting.

webhelpers/markdown.py

 #!/usr/bin/env python
 
+import warnings
+warnings.warn("``webhelpers.markdown`` is deprecated, please install the "
+    "current Markdown package (http://python.org/pypi/Markdown)",
+    DeprecationWarning)
+
 version = "1.7"
 version_info = (1,7,0,"rc-2")
 __revision__ = "$Rev: 72 $"