Ensure pygments is optional.

This commit is contained in:
Tom Christie 2017-03-07 11:09:09 +00:00
parent 2c4ad8c0e2
commit e8cf623cee
4 changed files with 31 additions and 17 deletions

View File

@ -251,6 +251,29 @@ except ImportError:
apply_markdown = None
try:
import pygments
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
def pygments_highlight(text, lang, style):
lexer = get_lexer_by_name(lang, stripall=False)
formatter = HtmlFormatter(nowrap=True, style=style)
return pygments.highlight(text, lexer, formatter)
def pygments_css(style):
formatter = HtmlFormatter(style=style)
return formatter.get_style_defs('.highlight')
except ImportError:
pygments = None
def pygments_highlight(text, lang, style):
return text
def pygments_css(style):
return None
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
# See: http://bugs.python.org/issue22767
if six.PY3:

View File

@ -25,7 +25,7 @@ from django.utils.html import mark_safe
from rest_framework import VERSION, exceptions, serializers, status
from rest_framework.compat import (
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi,
template_render
pygments_css, template_render
)
from rest_framework.exceptions import ParseError
from rest_framework.request import is_form_media_type, override_method
@ -802,16 +802,13 @@ class DocumentationRenderer(BaseRenderer):
charset = 'utf-8'
template = 'rest_framework/docs/index.html'
code_style = 'emacs'
languages = ['shell', 'javascript', 'python']
def get_context(self, data, request):
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter(style=self.code_style)
code_style = formatter.get_style_defs('.highlight')
langs = ['shell', 'javascript', 'python']
return {
'document': data,
'langs': langs,
'code_style': code_style,
'langs': self.languages,
'code_style': pygments_css(self.code_style),
'request': request
}

View File

@ -16,7 +16,7 @@
<link href="{% static 'rest_framework/docs/img/favicon.ico' %}" rel="shortcut icon">
<style>{{ code_style }}</style>
{% if code_style %}<style>{{ code_style }}</style>{% endif %}
<script src="{% static 'rest_framework/js/coreapi-0.0.20.js' %}"></script>
<script src="{% url 'api-docs:schema-js' %}"></script>

View File

@ -11,7 +11,7 @@ from django.utils.html import escape, format_html, smart_urlquote
from django.utils.safestring import SafeData, mark_safe
from rest_framework.compat import (
NoReverseMatch, markdown, reverse, template_render
NoReverseMatch, markdown, reverse, template_render, pygments_highlight
)
from rest_framework.renderers import HTMLFormRenderer
from rest_framework.utils.urls import replace_query_param
@ -39,14 +39,8 @@ class CodeNode(template.Node):
self.nodelist = code
def render(self, context):
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
body = self.nodelist.render(context)
lexer = get_lexer_by_name(self.lang, stripall=False)
formatter = HtmlFormatter(nowrap=True, style=self.style)
code = highlight(body, lexer, formatter)
return code
text = self.nodelist.render(context)
return pygments_highlight(text)
@register.filter()