Commit ebd25985 authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Removed django.contrib.markup.

parent f27a4ee3
Loading
Loading
Loading
Loading

django/contrib/markup/__init__.py

deleted100644 → 0
+0 −0

Empty file deleted.

django/contrib/markup/models.py

deleted100644 → 0
+0 −0

Empty file deleted.

+0 −0

Empty file deleted.

+0 −90
Original line number Diff line number Diff line
"""
Set of "markup" template filters for Django.  These filters transform plain text
markup syntaxes to HTML; currently there is support for:

    * Textile, which requires the PyTextile library available at
      http://loopcore.com/python-textile/

    * Markdown, which requires the Python-markdown library from
      http://www.freewisdom.org/projects/python-markdown

    * reStructuredText, which requires docutils from http://docutils.sf.net/
"""

from django import template
from django.conf import settings
from django.utils.encoding import force_bytes, force_text
from django.utils.safestring import mark_safe

register = template.Library()

@register.filter(is_safe=True)
def textile(value):
    try:
        import textile
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in 'textile' filter: The Python textile library isn't installed.")
        return force_text(value)
    else:
        return mark_safe(force_text(textile.textile(force_bytes(value), encoding='utf-8', output='utf-8')))

@register.filter(is_safe=True)
def markdown(value, arg=''):
    """
    Runs Markdown over a given value, optionally using various
    extensions python-markdown supports.

    Syntax::

        {{ value|markdown:"extension1_name,extension2_name..." }}

    To enable safe mode, which strips raw HTML and only returns HTML
    generated by actual Markdown syntax, pass "safe" as the first
    extension in the list.

    If the version of Markdown in use does not support extensions,
    they will be silently ignored.

    """
    import warnings
    warnings.warn('The markdown filter has been deprecated',
                  category=DeprecationWarning)
    try:
        import markdown
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in 'markdown' filter: The Python markdown library isn't installed.")
        return force_text(value)
    else:
        markdown_vers = getattr(markdown, "version_info", 0)
        if markdown_vers < (2, 1):
            if settings.DEBUG:
                raise template.TemplateSyntaxError(
                    "Error in 'markdown' filter: Django does not support versions of the Python markdown library < 2.1.")
            return force_text(value)
        else:
            extensions = [e for e in arg.split(",") if e]
            if extensions and extensions[0] == "safe":
                extensions = extensions[1:]
                return mark_safe(markdown.markdown(
                    force_text(value), extensions, safe_mode=True, enable_attributes=False))
            else:
                return mark_safe(markdown.markdown(
                    force_text(value), extensions, safe_mode=False))

@register.filter(is_safe=True)
def restructuredtext(value):
    import warnings
    warnings.warn('The restructuredtext filter has been deprecated',
                  category=DeprecationWarning)
    try:
        from docutils.core import publish_parts
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in 'restructuredtext' filter: The Python docutils library isn't installed.")
        return force_text(value)
    else:
        docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
        parts = publish_parts(source=force_bytes(value), writer_name="html4css1", settings_overrides=docutils_settings)
        return mark_safe(force_text(parts["fragment"]))

django/contrib/markup/tests.py

deleted100644 → 0
+0 −108
Original line number Diff line number Diff line
# Quick tests for the markup templatetags (django.contrib.markup)
import re
import warnings

from django.template import Template, Context
from django import test
from django.utils import unittest
from django.utils.html import escape

try:
    import textile
except ImportError:
    textile = None

try:
    import markdown
    markdown_version = getattr(markdown, "version_info", 0)
except ImportError:
    markdown = None

try:
    import docutils
except ImportError:
    docutils = None

class Templates(test.TestCase):

    textile_content = """Paragraph 1

Paragraph 2 with "quotes" and @code@"""

    markdown_content = """Paragraph 1

## An h2"""

    rest_content = """Paragraph 1

Paragraph 2 with a link_

.. _link: http://www.example.com/"""

    def setUp(self):
        self.save_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.contrib.markup')

    def tearDown(self):
        self.restore_warnings_state()

    @unittest.skipUnless(textile, 'textile not installed')
    def test_textile(self):
        t = Template("{% load markup %}{{ textile_content|textile }}")
        rendered = t.render(Context({'textile_content':self.textile_content})).strip()
        self.assertEqual(rendered.replace('\t', ''), """<p>Paragraph 1</p>

<p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>""")

    @unittest.skipIf(textile, 'textile is installed')
    def test_no_textile(self):
        t = Template("{% load markup %}{{ textile_content|textile }}")
        rendered = t.render(Context({'textile_content':self.textile_content})).strip()
        self.assertEqual(rendered, escape(self.textile_content))

    @unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
    def test_markdown(self):
        t = Template("{% load markup %}{{ markdown_content|markdown }}")
        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
        pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
        self.assertTrue(pattern.match(rendered))

    @unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
    def test_markdown_attribute_disable(self):
        t = Template("{% load markup %}{{ markdown_content|markdown:'safe' }}")
        markdown_content = "{@onclick=alert('hi')}some paragraph"
        rendered = t.render(Context({'markdown_content':markdown_content})).strip()
        self.assertTrue('@' in rendered)

    @unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
    def test_markdown_attribute_enable(self):
        t = Template("{% load markup %}{{ markdown_content|markdown }}")
        markdown_content = "{@onclick=alert('hi')}some paragraph"
        rendered = t.render(Context({'markdown_content':markdown_content})).strip()
        self.assertFalse('@' in rendered)

    @unittest.skipIf(markdown, 'markdown is installed')
    def test_no_markdown(self):
        t = Template("{% load markup %}{{ markdown_content|markdown }}")
        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
        self.assertEqual(rendered, self.markdown_content)

    @unittest.skipUnless(docutils, 'docutils not installed')
    def test_docutils(self):
        t = Template("{% load markup %}{{ rest_content|restructuredtext }}")
        rendered = t.render(Context({'rest_content':self.rest_content})).strip()
        # Different versions of docutils return slightly different HTML
        try:
            # Docutils v0.4 and earlier
            self.assertEqual(rendered, """<p>Paragraph 1</p>
<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""")
        except AssertionError:
            # Docutils from SVN (which will become 0.5)
            self.assertEqual(rendered, """<p>Paragraph 1</p>
<p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""")

    @unittest.skipIf(docutils, 'docutils is installed')
    def test_no_docutils(self):
        t = Template("{% load markup %}{{ rest_content|restructuredtext }}")
        rendered = t.render(Context({'rest_content':self.rest_content})).strip()
        self.assertEqual(rendered, self.rest_content)
Loading