DocumentationRenderer fixes

This commit is contained in:
Tom Christie 2017-02-01 15:09:49 +00:00
parent 9f3237207d
commit 9dbdf34621
4 changed files with 13 additions and 11 deletions

View File

@ -808,7 +808,7 @@ class DocumentationRenderer(BaseRenderer):
code_style = formatter.get_style_defs('.highlight')
langs = ['shell', 'javascript', 'python']
codec = coreapi.codecs.CoreJSONCodec()
schema = mark_safe(codec.encode(data))
schema = mark_safe(json.dumps(codec.encode(data)))
return {
'document': data,
'langs': langs,

View File

@ -1,5 +1,4 @@
import re
import coreschema
from collections import OrderedDict
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.compat import (
RegexURLPattern, RegexURLResolver, coreapi, uritemplate, urlparse
RegexURLPattern, RegexURLResolver, coreapi, coreschema, uritemplate, urlparse
)
from rest_framework.request import clone_request
from rest_framework.response import Response
@ -279,6 +278,7 @@ class SchemaGenerator(object):
def __init__(self, title=None, url=None, patterns=None, urlconf=None):
assert coreapi, '`coreapi` must be installed for schema support.'
assert coreschema, '`coreschema` must be installed for schema support.'
if url and not url.endswith('/'):
url += '/'
@ -522,6 +522,7 @@ class SchemaGenerator(object):
for variable in uritemplate.variables(path):
title = ''
description = ''
schema_cls = coreschema.String
if model is not None:
# Attempt to infer a field description if possible.
try:
@ -537,11 +538,14 @@ class SchemaGenerator(object):
elif model_field is not None and model_field.primary_key:
description = get_pk_description(model, model_field)
if isinstance(model_field, models.AutoField):
schema_cls = coreschema.Integer
field = coreapi.Field(
name=variable,
location='path',
required=True,
schema=coreschema.String(title=title, description=description)
schema=schema_cls(title=title, description=description)
)
fields.append(field)

View File

@ -58,7 +58,7 @@
const coreapi = window.coreapi
const codec = new coreapi.codecs.CoreJSONCodec()
const schema = {{ schema }}
const doc = codec.decode(schema, {preloaded: true})
const doc = codec.decode(schema)
const client = new coreapi.Client(null, null, csrf)
$('body').scrollspy({ target: '#toc' })

View File

@ -39,13 +39,8 @@ class FencedCodeExtension(markdown.Extension):
">normalize_whitespace")
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
@register.tag(name='code')
def do_code(parser,token):
def highlight_code(parser,token):
code = token.split_contents()[-1]
nodelist = parser.parse(('endcode',))
parser.delete_first_token()
@ -60,6 +55,9 @@ 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)