mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Ensure pygments is optional.
This commit is contained in:
parent
2c4ad8c0e2
commit
e8cf623cee
|
@ -251,6 +251,29 @@ except ImportError:
|
||||||
apply_markdown = None
|
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
|
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
|
||||||
# See: http://bugs.python.org/issue22767
|
# See: http://bugs.python.org/issue22767
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
|
|
|
@ -25,7 +25,7 @@ from django.utils.html import mark_safe
|
||||||
from rest_framework import VERSION, exceptions, serializers, status
|
from rest_framework import VERSION, exceptions, serializers, status
|
||||||
from rest_framework.compat import (
|
from rest_framework.compat import (
|
||||||
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi,
|
INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi,
|
||||||
template_render
|
pygments_css, template_render
|
||||||
)
|
)
|
||||||
from rest_framework.exceptions import ParseError
|
from rest_framework.exceptions import ParseError
|
||||||
from rest_framework.request import is_form_media_type, override_method
|
from rest_framework.request import is_form_media_type, override_method
|
||||||
|
@ -802,16 +802,13 @@ class DocumentationRenderer(BaseRenderer):
|
||||||
charset = 'utf-8'
|
charset = 'utf-8'
|
||||||
template = 'rest_framework/docs/index.html'
|
template = 'rest_framework/docs/index.html'
|
||||||
code_style = 'emacs'
|
code_style = 'emacs'
|
||||||
|
languages = ['shell', 'javascript', 'python']
|
||||||
|
|
||||||
def get_context(self, data, request):
|
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 {
|
return {
|
||||||
'document': data,
|
'document': data,
|
||||||
'langs': langs,
|
'langs': self.languages,
|
||||||
'code_style': code_style,
|
'code_style': pygments_css(self.code_style),
|
||||||
'request': request
|
'request': request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
<link href="{% static 'rest_framework/docs/img/favicon.ico' %}" rel="shortcut icon">
|
<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="{% static 'rest_framework/js/coreapi-0.0.20.js' %}"></script>
|
||||||
<script src="{% url 'api-docs:schema-js' %}"></script>
|
<script src="{% url 'api-docs:schema-js' %}"></script>
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ from django.utils.html import escape, format_html, smart_urlquote
|
||||||
from django.utils.safestring import SafeData, mark_safe
|
from django.utils.safestring import SafeData, mark_safe
|
||||||
|
|
||||||
from rest_framework.compat import (
|
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.renderers import HTMLFormRenderer
|
||||||
from rest_framework.utils.urls import replace_query_param
|
from rest_framework.utils.urls import replace_query_param
|
||||||
|
@ -39,14 +39,8 @@ class CodeNode(template.Node):
|
||||||
self.nodelist = code
|
self.nodelist = code
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
from pygments import highlight
|
text = self.nodelist.render(context)
|
||||||
from pygments.lexers import get_lexer_by_name
|
return pygments_highlight(text)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
@register.filter()
|
@register.filter()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user