Fixed exception with invalid rst syntax

This commit is contained in:
Martin Mahner 2018-04-12 23:28:16 +02:00
parent 1db58301da
commit d5cdffc2f2
2 changed files with 27 additions and 7 deletions

View file

@ -45,7 +45,7 @@ class Highlighter(object):
return fallback return fallback
def render(self, code_string, lexer_name, **kwargs): def render(self, code_string, lexer_name, **kwargs):
highlighted_string = self.highlight(code_string, lexer_name) highlighted_string = self.highlight(code_string, lexer_name=lexer_name)
context = { context = {
'highlighted': highlighted_string, 'highlighted': highlighted_string,
'highlighted_splitted': highlighted_string.splitlines(), 'highlighted_splitted': highlighted_string.splitlines(),
@ -60,7 +60,7 @@ class PlainTextHighlighter(Highlighter):
"""Plain Text. Just replace linebreaks.""" """Plain Text. Just replace linebreaks."""
template_name = 'dpaste/highlight/text.html' template_name = 'dpaste/highlight/text.html'
def highlight(self, code_string, lexer_name=None): def highlight(self, code_string, **kwargs):
return linebreaksbr(code_string) return linebreaksbr(code_string)
@ -72,7 +72,7 @@ class MarkdownHighlighter(PlainTextHighlighter):
'math') 'math')
render_flags = ('skip-html',) render_flags = ('skip-html',)
def highlight(self, code_string, lexer_name=None): def highlight(self, code_string, **kwargs):
import misaka import misaka
return mark_safe(misaka.html(code_string, return mark_safe(misaka.html(code_string,
extensions=self.extensions, extensions=self.extensions,
@ -87,12 +87,13 @@ class RestructuredTextHighlighter(PlainTextHighlighter):
'settings_overrides': { 'settings_overrides': {
'raw_enabled': False, 'raw_enabled': False,
'file_insertion_enabled': False, 'file_insertion_enabled': False,
'report_level': 3, 'halt_level': 5,
'report_level': 2,
'warning_stream': '/dev/null', 'warning_stream': '/dev/null',
} }
} }
def highlight(self, code_string, lexer_name=None): def highlight(self, code_string, **kwargs):
from docutils.core import publish_parts from docutils.core import publish_parts
self.publish_args['source'] = code_string self.publish_args['source'] = code_string
parts = publish_parts(**self.publish_args) parts = publish_parts(**self.publish_args)
@ -105,7 +106,7 @@ class RestructuredTextHighlighter(PlainTextHighlighter):
class PlainCodeHighlighter(Highlighter): class PlainCodeHighlighter(Highlighter):
"""Plain Code. No highlighting but Pygments like span tags around each line.""" """Plain Code. No highlighting but Pygments like span tags around each line."""
def highlight(self, code_string, lexer_name=None): def highlight(self, code_string, **kwargs):
return '\n'.join(['<span class="plain">{}</span>'.format(escape(l) or '&#8203;') return '\n'.join(['<span class="plain">{}</span>'.format(escape(l) or '&#8203;')
for l in code_string.splitlines()]) for l in code_string.splitlines()])

View file

@ -1,10 +1,12 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from textwrap import dedent
from django.test import TestCase from django.test import TestCase
from dpaste.highlight import PLAIN_CODE, PygmentsHighlighter, \ from dpaste.highlight import PLAIN_CODE, PygmentsHighlighter, \
PlainCodeHighlighter PlainCodeHighlighter, RestructuredTextHighlighter
class HighlightAPITestCase(TestCase): class HighlightAPITestCase(TestCase):
@ -77,3 +79,20 @@ class HighlightAPITestCase(TestCase):
' <span class="n">var</span>\n') ' <span class="n">var</span>\n')
value = PygmentsHighlighter().highlight(input, 'python') value = PygmentsHighlighter().highlight(input, 'python')
self.assertEqual(value, expected) self.assertEqual(value, expected)
def test_broken_rst_syntax(self):
"""
rst Syntax thats not valid must not raise an exception (SystemMessage)
"""
# (SEVERE/4) Missing matching underline for section title overline.
input = dedent("""
=========================
Generate 15 random numbers
70 180 3 179 192 117 75 72 90 190 49 159 63 14 55
=========================
""")
try:
RestructuredTextHighlighter().highlight(input)
except Exception as e:
self.fail('rst syntax raised unexpected exception: {}'.format(e))