diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 60d7f7097..3bb3f7897 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -294,7 +294,7 @@ class PageNumberPagination(BasePagination): name=self.page_query_param, required=False, location='query', - description=force_text(self.page_query_description) + #description=force_text(self.page_query_description) ) ] if self.page_size_query_param is not None: @@ -303,7 +303,7 @@ class PageNumberPagination(BasePagination): name=self.page_size_query_param, required=False, location='query', - description=force_text(self.page_size_query_description) + #description=force_text(self.page_size_query_description) ) ) return fields @@ -444,13 +444,13 @@ class LimitOffsetPagination(BasePagination): name=self.limit_query_param, required=False, location='query', - description=force_text(self.limit_query_description) + #description=force_text(self.limit_query_description) ), coreapi.Field( name=self.offset_query_param, required=False, location='query', - description=force_text(self.offset_query_description) + #description=force_text(self.offset_query_description) ) ] diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 97ec8164c..4b9ba3eb0 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -798,10 +798,22 @@ class DocumentationRenderer(BaseRenderer): media_type = 'text/html' format = 'html' charset = 'utf-8' + template = 'rest_framework/docs/index.html' + code_style = 'default' + + def get_context(self, data): + from pygments.formatters import HtmlFormatter + formatter = HtmlFormatter(style=self.code_style) + code_style = formatter.get_style_defs('.highlight') + langs = ['shell', 'javascript', 'python'] + return {'document': data, 'langs': langs, 'code_style': code_style} def render(self, data, accepted_media_type=None, renderer_context=None): - from coredocs.main import render as render_docs - return render_docs(data, theme='cerulean', highlight='emacs', static=lambda path: '/static/rest_framework/docs/' + path) + #from coredocs.main import render as render_docs + #return render_docs(data, theme='cerulean', highlight='emacs', static=lambda path: '/static/rest_framework/docs/' + path) + template = loader.get_template(self.template) + context = self.get_context(data) + return template_render(template, context, request=renderer_context['request']) class MultiPartRenderer(BaseRenderer): diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index fb7d12985..3a35db2cc 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -514,8 +514,8 @@ class SchemaGenerator(object): name=variable, location='path', required=True, - title='' if (title is None) else title, - description='' if (description is None) else description + #title='' if (title is None) else title, + #description='' if (description is None) else description ) fields.append(field) @@ -540,7 +540,7 @@ class SchemaGenerator(object): name='data', location='body', required=True, - type='array' + #type='array' ) ] @@ -559,11 +559,11 @@ class SchemaGenerator(object): name=field.field_name, location='form', required=required, - title=title, - description=description, - type=types_lookup[field], - input=determine_input(field), - choices=getattr(field, 'choices', None) + #title=title, + #description=description, + #type=types_lookup[field], + #input=determine_input(field), + #choices=getattr(field, 'choices', None) ) fields.append(field) diff --git a/rest_framework/templates/rest_framework/docs/document.html b/rest_framework/templates/rest_framework/docs/document.html new file mode 100644 index 000000000..e7cde659e --- /dev/null +++ b/rest_framework/templates/rest_framework/docs/document.html @@ -0,0 +1,19 @@ +{% load rest_framework %} +
{% render_markdown document.description %}
+{% code bash %}# Load the schema document
+$ coreapi get {{ schema_url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
+
+# Interact with the API endpoint
+$ coreapi action {% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}
diff --git a/rest_framework/templates/rest_framework/docs/link.html b/rest_framework/templates/rest_framework/docs/link.html
new file mode 100644
index 000000000..2d0f1879a
--- /dev/null
+++ b/rest_framework/templates/rest_framework/docs/link.html
@@ -0,0 +1,119 @@
+{% load rest_framework %}
+
+ {{ link.action|upper }} {{ link.url }}
+
{% render_markdown link.description %}
+ +{% if link.fields|with_location:'path' %} +The following parameters should be included in the URL path.
+Parameter | Description |
---|---|
{{ field.name }} {% if field.required %} required{% endif %} | {% if field.description %}{{ field.description }}{% endif %} |
The following parameters should be included as part of a URL query string.
+Parameter | Description |
---|---|
{{ field.name }} {% if field.required %} required{% endif %} | {% if field.description %}{{ field.description }}{% endif %} |
The following parameters should be included as HTTP headers.
+Parameter | Description |
---|---|
{{ field.name }} {% if field.required %} required{% endif %} | {% if field.description %}{{ field.description }}{% endif %} |
The request body should be "{{ link.encoding }}"
encoded, and should contain a single item.
Parameter | Description |
---|---|
{{ field.name }} {% if field.required %} required{% endif %} | {% if field.description %}{{ field.description }}{% endif %} |
The request body should be a "{{ link.encoding }}"
encoded object, containing the following items.
Parameter | Description |
---|---|
{{ field.name }} {% if field.required %} required{% endif %} | {% if field.description %}{{ field.description }}{% endif %} |
%s
'
+ LANG_TAG = ' class="highlight %s"'
+
+
+class FencedCodeExtension(markdown.Extension):
+
+ def extendMarkdown(self, md, md_globals):
+ """ Add FencedBlockPreprocessor to the Markdown instance. """
+ md.registerExtension(self)
+
+ md.preprocessors.add('fenced_code_block',
+ CustomFencedBlockPreprocessor(md),
+ ">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):
+ code = token.split_contents()[-1]
+ nodelist = parser.parse(('endcode',))
+ parser.delete_first_token()
+ return CodeNode(code, nodelist)
+
+
+class CodeNode(template.Node):
+ style = 'default'
+
+ def __init__(self, lang, code):
+ self.lang = lang
+ self.nodelist = code
+
+ def render(self, context):
+ 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()
+def with_location(fields, location):
+ return [
+ field for field in fields
+ if field.location == location
+ ]
+
+
+@register.simple_tag
+def form_for_link(link):
+ import coreschema
+ properties = {
+ field.name: field.schema or coreschema.String()
+ for field in link.fields
+ }
+ required = [
+ field.name
+ for field in link.fields
+ if field.required
+ ]
+ schema = coreschema.Object(properties=properties, required=required)
+ return coreschema.render_to_form(schema)
+
+
+@register.simple_tag
+def render_markdown(markdown_text):
+ return markdown.markdown(markdown_text, extensions=[FencedCodeExtension(), "tables"])
+
+
@register.simple_tag
def get_pagination_html(pager):
return pager.to_html()