mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
DocumentationRenderer fixes
This commit is contained in:
parent
9f3237207d
commit
9dbdf34621
|
@ -808,7 +808,7 @@ class DocumentationRenderer(BaseRenderer):
|
||||||
code_style = formatter.get_style_defs('.highlight')
|
code_style = formatter.get_style_defs('.highlight')
|
||||||
langs = ['shell', 'javascript', 'python']
|
langs = ['shell', 'javascript', 'python']
|
||||||
codec = coreapi.codecs.CoreJSONCodec()
|
codec = coreapi.codecs.CoreJSONCodec()
|
||||||
schema = mark_safe(codec.encode(data))
|
schema = mark_safe(json.dumps(codec.encode(data)))
|
||||||
return {
|
return {
|
||||||
'document': data,
|
'document': data,
|
||||||
'langs': langs,
|
'langs': langs,
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import re
|
import re
|
||||||
import coreschema
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
|
@ -14,7 +13,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework import exceptions, renderers, serializers
|
from rest_framework import exceptions, renderers, serializers
|
||||||
from rest_framework.compat import (
|
from rest_framework.compat import (
|
||||||
RegexURLPattern, RegexURLResolver, coreapi, uritemplate, urlparse
|
RegexURLPattern, RegexURLResolver, coreapi, coreschema, uritemplate, urlparse
|
||||||
)
|
)
|
||||||
from rest_framework.request import clone_request
|
from rest_framework.request import clone_request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
@ -279,6 +278,7 @@ class SchemaGenerator(object):
|
||||||
|
|
||||||
def __init__(self, title=None, url=None, patterns=None, urlconf=None):
|
def __init__(self, title=None, url=None, patterns=None, urlconf=None):
|
||||||
assert coreapi, '`coreapi` must be installed for schema support.'
|
assert coreapi, '`coreapi` must be installed for schema support.'
|
||||||
|
assert coreschema, '`coreschema` must be installed for schema support.'
|
||||||
|
|
||||||
if url and not url.endswith('/'):
|
if url and not url.endswith('/'):
|
||||||
url += '/'
|
url += '/'
|
||||||
|
@ -522,6 +522,7 @@ class SchemaGenerator(object):
|
||||||
for variable in uritemplate.variables(path):
|
for variable in uritemplate.variables(path):
|
||||||
title = ''
|
title = ''
|
||||||
description = ''
|
description = ''
|
||||||
|
schema_cls = coreschema.String
|
||||||
if model is not None:
|
if model is not None:
|
||||||
# Attempt to infer a field description if possible.
|
# Attempt to infer a field description if possible.
|
||||||
try:
|
try:
|
||||||
|
@ -537,11 +538,14 @@ class SchemaGenerator(object):
|
||||||
elif model_field is not None and model_field.primary_key:
|
elif model_field is not None and model_field.primary_key:
|
||||||
description = get_pk_description(model, model_field)
|
description = get_pk_description(model, model_field)
|
||||||
|
|
||||||
|
if isinstance(model_field, models.AutoField):
|
||||||
|
schema_cls = coreschema.Integer
|
||||||
|
|
||||||
field = coreapi.Field(
|
field = coreapi.Field(
|
||||||
name=variable,
|
name=variable,
|
||||||
location='path',
|
location='path',
|
||||||
required=True,
|
required=True,
|
||||||
schema=coreschema.String(title=title, description=description)
|
schema=schema_cls(title=title, description=description)
|
||||||
)
|
)
|
||||||
fields.append(field)
|
fields.append(field)
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@
|
||||||
const coreapi = window.coreapi
|
const coreapi = window.coreapi
|
||||||
const codec = new coreapi.codecs.CoreJSONCodec()
|
const codec = new coreapi.codecs.CoreJSONCodec()
|
||||||
const schema = {{ schema }}
|
const schema = {{ schema }}
|
||||||
const doc = codec.decode(schema, {preloaded: true})
|
const doc = codec.decode(schema)
|
||||||
const client = new coreapi.Client(null, null, csrf)
|
const client = new coreapi.Client(null, null, csrf)
|
||||||
|
|
||||||
$('body').scrollspy({ target: '#toc' })
|
$('body').scrollspy({ target: '#toc' })
|
||||||
|
|
|
@ -39,13 +39,8 @@ class FencedCodeExtension(markdown.Extension):
|
||||||
">normalize_whitespace")
|
">normalize_whitespace")
|
||||||
|
|
||||||
|
|
||||||
from pygments import highlight
|
|
||||||
from pygments.lexers import get_lexer_by_name
|
|
||||||
from pygments.formatters import HtmlFormatter
|
|
||||||
|
|
||||||
|
|
||||||
@register.tag(name='code')
|
@register.tag(name='code')
|
||||||
def do_code(parser,token):
|
def highlight_code(parser,token):
|
||||||
code = token.split_contents()[-1]
|
code = token.split_contents()[-1]
|
||||||
nodelist = parser.parse(('endcode',))
|
nodelist = parser.parse(('endcode',))
|
||||||
parser.delete_first_token()
|
parser.delete_first_token()
|
||||||
|
@ -60,6 +55,9 @@ class CodeNode(template.Node):
|
||||||
self.nodelist = code
|
self.nodelist = code
|
||||||
|
|
||||||
def render(self, context):
|
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)
|
body = self.nodelist.render(context)
|
||||||
lexer = get_lexer_by_name(self.lang, stripall=False)
|
lexer = get_lexer_by_name(self.lang, stripall=False)
|
||||||
formatter = HtmlFormatter(nowrap=True, style=self.style)
|
formatter = HtmlFormatter(nowrap=True, style=self.style)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user