diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index 16e894e1f..dbc5731cb 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -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:
diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py
index ed67c8363..206dc0ccf 100644
--- a/rest_framework/renderers.py
+++ b/rest_framework/renderers.py
@@ -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
}
diff --git a/rest_framework/templates/rest_framework/docs/index.html b/rest_framework/templates/rest_framework/docs/index.html
index 2eef1d0c7..5ce0dbb2a 100644
--- a/rest_framework/templates/rest_framework/docs/index.html
+++ b/rest_framework/templates/rest_framework/docs/index.html
@@ -16,7 +16,7 @@
-
+ {% if code_style %}{% endif %}
diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py
index 280b084e1..999b43b71 100644
--- a/rest_framework/templatetags/rest_framework.py
+++ b/rest_framework/templatetags/rest_framework.py
@@ -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()