mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
move syntax_highlight doc filter in compatibility module and define it conditionally
This commit is contained in:
parent
a8f1881d9b
commit
cf759a6fe8
|
@ -273,6 +273,32 @@ except ImportError:
|
||||||
def pygments_css(style):
|
def pygments_css(style):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
if markdown is not None and pygments is not None:
|
||||||
|
class CodeBlockPreprocessor(Preprocessor):
|
||||||
|
pattern = re.compile(
|
||||||
|
r'^\s*@@ (.+?) @@\s*(.+?)^\s*@@', re.M|re.S)
|
||||||
|
|
||||||
|
formatter = HtmlFormatter()
|
||||||
|
|
||||||
|
def run(self, lines):
|
||||||
|
def repl(m):
|
||||||
|
try:
|
||||||
|
lexer = get_lexer_by_name(m.group(1))
|
||||||
|
except (ValueError, NameError):
|
||||||
|
lexer = TextLexer()
|
||||||
|
code = m.group(2).replace('\t',' ')
|
||||||
|
code = pygments.highlight(code, lexer, self.formatter)
|
||||||
|
code = code.replace('\n\n', '\n \n').replace('\n', '<br />').replace('\\@','@')
|
||||||
|
return '\n\n%s\n\n' % code
|
||||||
|
ret = self.pattern.sub(repl, "\n".join(lines))
|
||||||
|
return ret.split("\n")
|
||||||
|
|
||||||
|
def md_filter_add_syntax_highlight(md):
|
||||||
|
md.preprocessors.add('highlight', CodeBlockPreprocessor(), "_begin")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
def md_filter_add_syntax_highlight(md):
|
||||||
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import pytz
|
import pytz
|
||||||
|
@ -374,3 +400,4 @@ def include(module, namespace=None, app_name=None):
|
||||||
return include(module, namespace, app_name)
|
return include(module, namespace, app_name)
|
||||||
else:
|
else:
|
||||||
return include((module, app_name), namespace)
|
return include((module, app_name), namespace)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,8 @@ 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, pygments_highlight, reverse, template_render
|
NoReverseMatch, markdown, pygments_highlight, reverse, template_render,
|
||||||
|
md_filter_add_syntax_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
|
||||||
|
@ -66,44 +67,15 @@ def form_for_link(link):
|
||||||
return mark_safe(coreschema.render_to_form(schema))
|
return mark_safe(coreschema.render_to_form(schema))
|
||||||
|
|
||||||
|
|
||||||
from markdown.preprocessors import Preprocessor
|
|
||||||
from pygments.formatters import HtmlFormatter
|
|
||||||
from pygments.lexers import get_lexer_by_name, TextLexer
|
|
||||||
from pygments import highlight
|
|
||||||
import pygments
|
|
||||||
import re
|
|
||||||
|
|
||||||
# starting from this blogpost and modified to support current markdown extensions API
|
|
||||||
# https://zerokspot.com/weblog/2008/06/18/syntax-highlighting-in-markdown-with-pygments/
|
|
||||||
class CodeBlockPreprocessor(Preprocessor):
|
|
||||||
pattern = re.compile(
|
|
||||||
r'^\s*@@ (.+?) @@\s*(.+?)^\s*@@', re.M|re.S)
|
|
||||||
|
|
||||||
formatter = HtmlFormatter()
|
|
||||||
|
|
||||||
def run(self, lines):
|
|
||||||
def repl(m):
|
|
||||||
try:
|
|
||||||
lexer = get_lexer_by_name(m.group(1))
|
|
||||||
except (ValueError, NameError):
|
|
||||||
lexer = TextLexer()
|
|
||||||
code = m.group(2).replace('\t',' ')
|
|
||||||
code = pygments.highlight(code, lexer, self.formatter)
|
|
||||||
code = code.replace('\n\n', '\n \n').replace('\n', '<br />').replace('\\@','@')
|
|
||||||
return '\n\n%s\n\n' % code
|
|
||||||
# import ipdb ; ipdb.set_trace()
|
|
||||||
ret = self.pattern.sub(repl, "\n".join(lines))
|
|
||||||
return ret.split("\n")
|
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def render_markdown(markdown_text):
|
def render_markdown(markdown_text):
|
||||||
if not markdown:
|
if not markdown:
|
||||||
return markdown_text
|
return markdown_text
|
||||||
md = markdown.Markdown()
|
md = markdown.Markdown()
|
||||||
md.preprocessors.add('highlight', CodeBlockPreprocessor(), "_begin")
|
|
||||||
|
|
||||||
a = md.convert(markdown_text)
|
# add pygments syntax highlight if pygments package is available
|
||||||
|
md_filter_add_syntax_highlight(md)
|
||||||
|
|
||||||
return mark_safe(md.convert(markdown_text))
|
return mark_safe(md.convert(markdown_text))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user