Skip to content

Commit

Permalink
Handle errors from publish_parts
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfischer committed Apr 4, 2018
1 parent 786edd4 commit fd5ddf1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions readthedocs/core/templatetags/core_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def make_document_url(project, version=None, page=''):
def restructuredtext(value, short=False):
try:
from docutils.core import publish_parts
from docutils import ApplicationError
except ImportError:
if settings.DEBUG:
raise template.TemplateSyntaxError(
Expand All @@ -59,8 +60,15 @@ def restructuredtext(value, short=False):
'file_insertion_enabled': False,
}
docutils_settings.update(getattr(settings, 'RESTRUCTUREDTEXT_FILTER_SETTINGS', {}))
parts = publish_parts(source=force_bytes(value), writer_name="html4css1",
settings_overrides=docutils_settings)
try:
parts = publish_parts(
source=force_bytes(value),
writer_name="html4css1",
settings_overrides=docutils_settings,
)
except ApplicationError:
return force_text(value)

out = force_text(parts["fragment"])
try:
if short:
Expand Down
14 changes: 14 additions & 0 deletions readthedocs/rtd_tests/tests/test_core_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,17 @@ def test_mkdocs_index_no_directory_urls(self):
proj.documentation_type = 'mkdocs'
url = core_tags.make_document_url(proj, LATEST, 'index.html')
self.assertEqual(url, 'http://readthedocs.org/docs/pip/en/latest/')

def test_restructured_text(self):
value = '*test*'
result = core_tags.restructuredtext(value)
self.assertIn('<em>test</em>', result)

def test_restructured_text_invalid(self):
value = (
'*******\n'
'Test\n'
'****\n\n'
)
result = core_tags.restructuredtext(value)
self.assertEqual(result, value)

0 comments on commit fd5ddf1

Please sign in to comment.