Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6681 -- Don't break docutils when rendering reStructuredText.

Don't set a global default interpreted role function for reStructuredText.
Instead, use the `default-role` directive to change the default only within
the `parse_rst()` function.

Thanks Malcolm Tredinnick for the report.
  • Loading branch information...
commit bcd4c3f27d5bcf701337ac7c74c042d90792bdc3 1 parent 3895d88
authored June 18, 2013 timgraham committed October 04, 2013
16  django/contrib/admindocs/utils.py
@@ -67,9 +67,18 @@ def parse_rst(text, default_reference_context, thing_being_parsed=None):
67 67
     }
68 68
     if thing_being_parsed:
69 69
         thing_being_parsed = force_bytes("<%s>" % thing_being_parsed)
70  
-    parts = docutils.core.publish_parts(text, source_path=thing_being_parsed,
71  
-                destination_path=None, writer_name='html',
72  
-                settings_overrides=overrides)
  70
+    # Wrap ``text`` in some reST that sets the default role to ``cmsreference``,
  71
+    # then restores it.
  72
+    source = """
  73
+.. default-role:: cmsreference
  74
+
  75
+%s
  76
+
  77
+.. default-role::
  78
+"""
  79
+    parts = docutils.core.publish_parts(source % text,
  80
+                source_path=thing_being_parsed, destination_path=None,
  81
+                writer_name='html', settings_overrides=overrides)
73 82
     return mark_safe(parts['fragment'])
74 83
 
75 84
 #
@@ -100,7 +109,6 @@ def default_reference_role(name, rawtext, text, lineno, inliner, options=None, c
100 109
 
101 110
 if docutils_is_available:
102 111
     docutils.parsers.rst.roles.register_canonical_role('cmsreference', default_reference_role)
103  
-    docutils.parsers.rst.roles.DEFAULT_INTERPRETED_ROLE = 'cmsreference'
104 112
 
105 113
     for name, urlbase in ROLES.items():
106 114
         create_reference_role(name, urlbase)
44  tests/admin_docs/tests.py
... ...
@@ -1,3 +1,11 @@
  1
+import unittest
  2
+
  3
+try:
  4
+    import docutils
  5
+except ImportError:
  6
+    docutils = None
  7
+
  8
+from django.contrib.admindocs import utils
1 9
 from django.contrib.auth.models import User
2 10
 from django.test import TestCase
3 11
 from django.test.utils import override_settings
@@ -43,3 +51,39 @@ def test_xview_class(self):
43 51
         user.save()
44 52
         response = self.client.head('/xview/class/')
45 53
         self.assertFalse('X-View' in response)
  54
+
  55
+
  56
+@unittest.skipUnless(docutils, "no docutils installed.")
  57
+class DefaultRoleTest(TestCase):
  58
+    urls = 'admin_docs.urls'
  59
+
  60
+    def test_parse_rst(self):
  61
+        """
  62
+        Tests that ``django.contrib.admindocs.utils.parse_rst`` uses
  63
+        ``cmsreference`` as the default role.
  64
+        """
  65
+        markup = ('<p><a class="reference external" href="/admindocs/%s">'
  66
+                  'title</a></p>\n')
  67
+        self.assertEqual(utils.parse_rst('`title`', 'model'),
  68
+                         markup % 'models/title/')
  69
+        self.assertEqual(utils.parse_rst('`title`', 'view'),
  70
+                         markup % 'views/title/')
  71
+        self.assertEqual(utils.parse_rst('`title`', 'template'),
  72
+                         markup % 'templates/title/')
  73
+        self.assertEqual(utils.parse_rst('`title`', 'filter'),
  74
+                         markup % 'filters/#title')
  75
+        self.assertEqual(utils.parse_rst('`title`', 'tag'),
  76
+                         markup % 'tags/#title')
  77
+
  78
+    def test_publish_parts(self):
  79
+        """
  80
+        Tests that Django hasn't broken the default role for interpreted text
  81
+        when ``publish_parts`` is used directly, by setting it to
  82
+        ``cmsreference``. See #6681.
  83
+        """
  84
+        self.assertNotEqual(docutils.parsers.rst.roles.DEFAULT_INTERPRETED_ROLE,
  85
+                            'cmsreference')
  86
+        source = 'reST, `interpreted text`, default role.'
  87
+        markup = '<p>reST, <cite>interpreted text</cite>, default role.</p>\n'
  88
+        parts = docutils.core.publish_parts(source=source, writer_name="html4css1")
  89
+        self.assertEqual(parts['fragment'], markup)
3  tests/admin_docs/urls.py
... ...
@@ -1,8 +1,9 @@
1  
-from django.conf.urls import patterns
  1
+from django.conf.urls import include, patterns
2 2
 
3 3
 from . import views
4 4
 
5 5
 urlpatterns = patterns('',
  6
+    (r'^admindocs/', include('django.contrib.admindocs.urls')),
6 7
     (r'^xview/func/$', views.xview_dec(views.xview)),
7 8
     (r'^xview/class/$', views.xview_dec(views.XViewClass.as_view())),
8 9
 )

0 notes on commit bcd4c3f

Please sign in to comment.
Something went wrong with that request. Please try again.