| 1 |
""" |
| 2 |
The `compat` module provides support for backwards compatibility with older |
| 3 |
versions of Django/Python, and compatibility wrappers around optional packages. |
| 4 |
""" |
| 5 |
import django |
| 6 |
from django.views.generic import View |
| 7 |
|
| 8 |
|
| 9 |
def unicode_http_header(value): |
| 10 |
# Coerce HTTP header value to unicode. |
| 11 |
if isinstance(value, bytes): |
| 12 |
return value.decode('iso-8859-1') |
| 13 |
return value |
| 14 |
|
| 15 |
|
| 16 |
# django.contrib.postgres requires psycopg |
| 17 |
try: |
| 18 |
from django.contrib.postgres import fields as postgres_fields |
| 19 |
except ImportError: |
| 20 |
postgres_fields = None |
| 21 |
|
| 22 |
|
| 23 |
# uritemplate is required for OpenAPI schema generation |
| 24 |
try: |
| 25 |
import uritemplate |
| 26 |
except ImportError: |
| 27 |
uritemplate = None |
| 28 |
|
| 29 |
|
| 30 |
# pyyaml is optional |
| 31 |
try: |
| 32 |
import yaml |
| 33 |
except ImportError: |
| 34 |
yaml = None |
| 35 |
|
| 36 |
# inflection is optional |
| 37 |
try: |
| 38 |
import inflection |
| 39 |
except ImportError: |
| 40 |
inflection = None |
| 41 |
|
| 42 |
|
| 43 |
# requests is optional |
| 44 |
try: |
| 45 |
import requests |
| 46 |
except ImportError: |
| 47 |
requests = None |
| 48 |
|
| 49 |
|
| 50 |
# PATCH method is not implemented by Django |
| 51 |
if 'patch' not in View.http_method_names: |
| 52 |
View.http_method_names = View.http_method_names + ['patch'] |
| 53 |
|
| 54 |
|
| 55 |
# Markdown is optional (version 3.0+ required) |
| 56 |
try: |
| 57 |
import markdown |
| 58 |
|
| 59 |
HEADERID_EXT_PATH = 'markdown.extensions.toc' |
| 60 |
LEVEL_PARAM = 'baselevel' |
| 61 |
|
| 62 |
def apply_markdown(text): |
| 63 |
""" |
| 64 |
Simple wrapper around :func:`markdown.markdown` to set the base level |
| 65 |
of '#' style headers to <h2>. |
| 66 |
""" |
| 67 |
extensions = [HEADERID_EXT_PATH] |
| 68 |
extension_configs = { |
| 69 |
HEADERID_EXT_PATH: { |
| 70 |
LEVEL_PARAM: '2' |
| 71 |
} |
| 72 |
} |
| 73 |
md = markdown.Markdown( |
| 74 |
extensions=extensions, extension_configs=extension_configs |
| 75 |
) |
| 76 |
md_filter_add_syntax_highlight(md) |
| 77 |
return md.convert(text) |
| 78 |
except ImportError: |
| 79 |
apply_markdown = None |
| 80 |
markdown = None |
| 81 |
|
| 82 |
|
| 83 |
try: |
| 84 |
import pygments |
| 85 |
from pygments.formatters import HtmlFormatter |
| 86 |
from pygments.lexers import TextLexer, get_lexer_by_name |
| 87 |
|
| 88 |
def pygments_highlight(text, lang, style): |
| 89 |
lexer = get_lexer_by_name(lang, stripall=False) |
| 90 |
formatter = HtmlFormatter(nowrap=True, style=style) |
| 91 |
return pygments.highlight(text, lexer, formatter) |
| 92 |
|
| 93 |
def pygments_css(style): |
| 94 |
formatter = HtmlFormatter(style=style) |
| 95 |
return formatter.get_style_defs('.highlight') |
| 96 |
|
| 97 |
except ImportError: |
| 98 |
pygments = None |
| 99 |
|
| 100 |
def pygments_highlight(text, lang, style): |
| 101 |
return text |
| 102 |
|
| 103 |
def pygments_css(style): |
| 104 |
return None |
| 105 |
|
| 106 |
if markdown is not None and pygments is not None: |
| 107 |
# starting from this blogpost and modified to support current markdown extensions API |
| 108 |
# https://zerokspot.com/weblog/2008/06/18/syntax-highlighting-in-markdown-with-pygments/ |
| 109 |
|
| 110 |
import re |
| 111 |
|
| 112 |
from markdown.preprocessors import Preprocessor |
| 113 |
|
| 114 |
class CodeBlockPreprocessor(Preprocessor): |
| 115 |
pattern = re.compile( |
| 116 |
r'^\s*``` *([^\n]+)\n(.+?)^\s*```', re.M | re.S) |
| 117 |
|
| 118 |
formatter = HtmlFormatter() |
| 119 |
|
| 120 |
def run(self, lines): |
| 121 |
def repl(m): |
| 122 |
try: |
| 123 |
lexer = get_lexer_by_name(m.group(1)) |
| 124 |
except (ValueError, NameError): |
| 125 |
lexer = TextLexer() |
| 126 |
code = m.group(2).replace('\t', ' ') |
| 127 |
code = pygments.highlight(code, lexer, self.formatter) |
| 128 |
code = code.replace('\n\n', '\n \n').replace('\n', '<br />').replace('\\@', '@') |
| 129 |
return '\n\n%s\n\n' % code |
| 130 |
ret = self.pattern.sub(repl, "\n".join(lines)) |
| 131 |
return ret.split("\n") |
| 132 |
|
| 133 |
def md_filter_add_syntax_highlight(md): |
| 134 |
md.preprocessors.register(CodeBlockPreprocessor(), 'highlight', 40) |
| 135 |
return True |
| 136 |
else: |
| 137 |
def md_filter_add_syntax_highlight(md): |
| 138 |
return False |
| 139 |
|
| 140 |
|
| 141 |
if django.VERSION >= (6, 1): |
| 142 |
# `split_header_value` was added in Django 6.2 (backported to 6.1b1+), |
| 143 |
# replacing the `cc_delim_re` regular expression. |
| 144 |
# https://github.com/django/django/commit/526b1b414d8e215bf627b5722df12a09346dbf6b |
| 145 |
from django.utils.http import split_header_value |
| 146 |
else: |
| 147 |
|
| 148 |
def split_header_value(value, sep=","): |
| 149 |
for part in value.split(sep): |
| 150 |
if stripped := part.strip(): |
| 151 |
yield stripped |
| 152 |
|
| 153 |
|
| 154 |
# `separators` argument to `json.dumps()` differs between 2.x and 3.x |
| 155 |
# See: https://bugs.python.org/issue22767 |
| 156 |
SHORT_SEPARATORS = (',', ':') |
| 157 |
LONG_SEPARATORS = (', ', ': ') |
| 158 |
INDENT_SEPARATORS = (',', ': ') |