Work on DocumentationRenderer

This commit is contained in:
Tom Christie 2017-02-01 10:29:09 +00:00
parent 9cb3c8ec35
commit 49a294558d
5 changed files with 30 additions and 22 deletions

View File

@ -803,10 +803,18 @@ class DocumentationRenderer(BaseRenderer):
def get_context(self, data): def get_context(self, data):
from pygments.formatters import HtmlFormatter from pygments.formatters import HtmlFormatter
from django.utils.html import mark_safe
formatter = HtmlFormatter(style=self.code_style) formatter = HtmlFormatter(style=self.code_style)
code_style = formatter.get_style_defs('.highlight') code_style = formatter.get_style_defs('.highlight')
langs = ['shell', 'javascript', 'python'] langs = ['shell', 'javascript', 'python']
return {'document': data, 'langs': langs, 'code_style': code_style} codec = coreapi.codecs.CoreJSONCodec()
schema = mark_safe(codec.encode(data))
return {
'document': data,
'langs': langs,
'code_style': code_style,
'schema': schema
}
def render(self, data, accepted_media_type=None, renderer_context=None): def render(self, data, accepted_media_type=None, renderer_context=None):
#from coredocs.main import render as render_docs #from coredocs.main import render as render_docs

View File

@ -38,28 +38,28 @@
<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script> <script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
<script src="{% static 'rest_framework/docs/js/bootstrap-3.0.3.min.js' %}"></script> <script src="{% static 'rest_framework/docs/js/bootstrap-3.0.3.min.js' %}"></script>
<script> <script>
// function getCookie(name) { function getCookie(name) {
// var cookieValue = null; var cookieValue = null;
// if (document.cookie && document.cookie !== '') { if (document.cookie && document.cookie !== '') {
// var cookies = document.cookie.split(';'); var cookies = document.cookie.split(';');
// for (var i = 0; i < cookies.length; i++) { for (var i = 0; i < cookies.length; i++) {
// var cookie = jQuery.trim(cookies[i]); var cookie = jQuery.trim(cookies[i]);
// // Does this cookie string begin with the name we want? // Does this cookie string begin with the name we want?
// if (cookie.substring(0, name.length + 1) === (name + '=')) { if (cookie.substring(0, name.length + 1) === (name + '=')) {
// cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
// break; break;
// } }
// } }
// } }
// return cookieValue; return cookieValue;
// } }
// const csrf = {'X-CSRFToken': getCookie('csrftoken')} const csrf = {'X-CSRFToken': getCookie('csrftoken')}
const coreapi = window.coreapi const coreapi = window.coreapi
const codec = new coreapi.codecs.CoreJSONCodec() const codec = new coreapi.codecs.CoreJSONCodec()
const schema = '{"_type": "document"}' {{ schema }} const schema = {{ schema }}
const doc = codec.decode(schema, {preloaded: true}) const doc = codec.decode(schema, {preloaded: true})
const client = new coreapi.Client() const client = new coreapi.Client(null, null, csrf)
$('body').scrollspy({ target: '#toc' }) $('body').scrollspy({ target: '#toc' })

View File

@ -4,7 +4,7 @@
// Initialize a client & load the schema document // Initialize a client & load the schema document
var client = new coreapi.Client() var client = new coreapi.Client()
var document = null var document = null
client.get("{{ schema_url }}").then(function(result) { client.get("{{ document.url }}").then(function(result) {
document = result document = result
}) })

View File

@ -3,7 +3,7 @@
# Initialize a client & load the schema document # Initialize a client & load the schema document
client = coreapi.Client() client = coreapi.Client()
document = client.get("{{ schema_url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %}) document = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
# Interact with the API endpoint # Interact with the API endpoint
action = [{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"] action = [{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]

View File

@ -1,6 +1,6 @@
{% load rest_framework %} {% load rest_framework %}
<pre class="highlight shell" data-language="shell"><code>{% code bash %}# Load the schema document <pre class="highlight shell" data-language="shell"><code>{% code bash %}# Load the schema document
$ coreapi get {{ schema_url }}{% if schema_format %} --format {{ schema_format }}{% endif %} $ coreapi get {{ document.url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
# Interact with the API endpoint # 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 %}</code></pre> $ coreapi action {% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>