Nested docs: use lists for paths instead of strings

This way we don't need to do any string manipulation (split_keys is gone),
and snippet code generation should be more flexible.
This commit is contained in:
Aleksey Zhukov 2017-05-23 18:22:02 +03:00
parent 3960328a29
commit f0e65d2637
No known key found for this signature in database
GPG Key ID: E1C3A2857EFC7C66
11 changed files with 38 additions and 44 deletions

View File

@ -1,15 +1,16 @@
{% load rest_framework %}
{% for section_key, section in items %}
{% if section_key %}
<h{{level}} id="{{prefix}}{{ section_key }}" class="coredocs-section-title">{{ section_key }} <a href="#{{ prefix }}{{ section_key }}"><i class="fa fa-link" aria-hidden="true"></i>
</a></h{{level}}>
{% endif %}
{% if section_key %}
<h{{level}} id="{{ path|list_add:section_key|join:'-' }}" class="coredocs-section-title">
{{ section_key }}
<a href="#{{ path|list_add:section_key|join:'-' }}"><i class="fa fa-link" aria-hidden="true"></i></a>
</h{{level}}>
{% endif %}
{% if section.links|items %}
{% for link_key, link in section.links|items %}
{% include "rest_framework/docs/link.html" with prefix=prefix level=level|add:1%}
{% include "rest_framework/docs/link.html" with path=path %}
{% endfor %}
{% endif %}
{% include 'rest_framework/docs/_recursive_document.html' with items=section.data|items prefix=prefix|add:section_key|add:'/' level=level|add:1 %}
{% include 'rest_framework/docs/_recursive_document.html' with items=section.data|items path=path|list_add:section_key level=level|add:1 %}
{% endfor %}

View File

@ -1,22 +1,18 @@
{% load rest_framework %}
{% if items %}
<ul id="{% if prefix %}{{prefix}}-dropdown{% else %}menu-content{% endif %}" class="menu-content collapse out">
<ul id="{% if path %}{{ path|join:"-" }}-dropdown{% else %}menu-content{% endif %}" class="menu-content collapse out">
{% for section_key, section in items %}
<li data-toggle="collapse" data-target="#{{prefix}}{{ section_key }}-dropdown" class="collapsed">
<a><i class="fa fa-dot-circle-o fa-lg"></i> {% if prefix %}{{prefix}}/{% endif %}{% if section_key %}{{ section_key }}{% else %}API Endpoints{% endif %} <span class="arrow"></span></a>
<li data-toggle="collapse" data-target="#{{ path|list_add:section_key|join:"-" }}-dropdown" class="collapsed">
<a><i class="fa fa-dot-circle-o fa-lg"></i> {% for p in path %}{{ p }}/{% endfor %}{% if section_key %}{{ section_key }}{% else %}API Endpoints{% endif %} <span class="arrow"></span></a>
</li>
{% if section.links|items %}
<ul class="sub-menu {% if section_key %}collapse{% endif %}" id="{{prefix}}{{ section_key }}-dropdown">
<ul class="sub-menu {% if section_key %}collapse{% endif %}" id="{{ path|list_add:section_key|join:'-' }}-dropdown">
{% for link_key, link in section.links|items %}
<li><a href="#{% if prefix %}{{prefix}}/{% endif %}{{ section_key }}-{{ link_key }}">{{ link.title|default:link_key }}</a></li>
<li><a href="#{{ path|list_add:section_key|list_add:link_key|join:"-" }}">{{ link.title|default:link_key }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if prefix %}
{% include 'rest_framework/docs/_recursive_menu.html' with items=section.data|items prefix=prefix|add:'/'|add:section_key %}
{% else %}
{% include 'rest_framework/docs/_recursive_menu.html' with items=section.data|items prefix=section_key %}
{% endif %}
{% include "rest_framework/docs/_recursive_menu.html" with items=section.data|items path=path|list_add:section_key %}
{% endfor %}
</ul>
{% endif %}

View File

@ -14,8 +14,8 @@
</div>
</div>
{% include 'rest_framework/docs/_recursive_document.html' with items=document.data|items level=2 %}
{% include "rest_framework/docs/_recursive_document.html" with items=document.data|items path=""|make_list level=2 %}
{% for link_key, link in document.links|items %}
{% include "rest_framework/docs/link.html" %}
{% include "rest_framework/docs/link.html" with path=""|make_list %}
{% endfor %}

View File

@ -16,7 +16,7 @@
</div>
<form data-key='[{{ prefix|split_keys }} "{{ section_key }}", "{{ link_key }}"]' class="api-interaction">
<form data-key='[{% for p in path %}"{{ p }}", {% endfor %}"{{ section_key }}", "{{ link_key }}"]' class="api-interaction">
<div class="modal-body">
<div class="row">
<div class="col-lg-6 request">

View File

@ -6,7 +6,7 @@ var schema = window.schema // Loaded by `schema.js`
var client = new coreapi.Client()
// Interact with the API endpoint
var action = [{{ prefix|split_keys }}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
var action = [{% for p in path %}"{{ p }}", {% endfor %}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
{% if link.fields %}var params = {
{% for field in link.fields %} {{ field.name }}: ...{% if not loop.last %},{% endif %}
{% endfor %}}

View File

@ -6,7 +6,7 @@ client = coreapi.Client()
schema = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
# Interact with the API endpoint
action = [{{ prefix|split_keys }}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
action = [{% for p in path %}"{{ p }}", {% endfor %}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
{% if link.fields %}params = {
{% for field in link.fields %} "{{ field.name }}": ...{% if not loop.last %},{% endif %}
{% endfor %}}

View File

@ -3,4 +3,4 @@
$ coreapi get {{ document.url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
# Interact with the API endpoint
$ coreapi action {{ prefix|split_keys:'cmd' }}{% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>
$ coreapi action {% for p in path %}{{ p }} {% endfor %}{% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>

View File

@ -10,8 +10,10 @@
<i class="fa fa-exchange"></i> Interact
</button>
<h3 id="{{prefix}}{{ section_key }}-{{ link_key }}" class="coredocs-link-title">{{ link.title|default:link_key }} <a href="#{{prefix}}{{ section_key }}-{{ link_key }}"><i class="fa fa-link" aria-hidden="true"></i>
</a></h3>
<h3 id="{{ path|list_add:section_key|list_add:link_key|join:"-" }}" class="coredocs-link-title">
{{ link.title|default:link_key }}
<a href="#{{ path|list_add:section_key|list_add:link_key|join:"-" }}"><i class="fa fa-link" aria-hidden="true"></i></a>
</h3>
<div class="meta">
<span class="label label-primary">{{ link.action|upper }}</span>

View File

@ -4,7 +4,7 @@
<i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
<div class="menu-list">
{% include 'rest_framework/docs/_recursive_menu.html' with items=document.data|items %}
{% include "rest_framework/docs/_recursive_menu.html" with items=document.data|items path=""|make_list %}
<ul class="menu-list menu-list-bottom">
<li data-toggle="collapse" data-target="#auth-control" class="collapsed">

View File

@ -365,19 +365,14 @@ def break_long_headers(header):
@register.filter
def split_keys(keys, cmd_type='script'):
if not keys:
return ''
def list_add(lst, item):
"""
Appends a item to a list or a tuple. Original value is not modified.
if cmd_type == 'script':
joiner = ', '
wrapper = '"'
else:
joiner = ' '
wrapper = ''
return joiner.join([
'{wrapper}{k}{wrapper}'.format(k=k, wrapper=wrapper)
if k != '' else ''
for k in keys.split('/')
])
Can be only applied to lists or tuples, raises TypeError otherwise.
"""
if isinstance(lst, list):
return lst + [item]
elif isinstance(lst, tuple):
return lst + (item,)
raise TypeError("list_add only accepts lists or tuples")

View File

@ -27,16 +27,16 @@ class TestRecursiveUrlViewSets(TestCase):
)
def test_documentation(self):
header_re = 'h{level}\s+id="{path}".*>{title} <a href="#{path}"'
header_re = 'h{level}\s+id="{path}".*>\s*{title}\s*<a href="#{path}"'
for route in (('not_dummies',), ('dummy', 'aaaas'), ('dummy', 'bbbbs')):
path = "/".join(route)
path = "-".join(route)
self.assertTrue(
re.search(header_re.format(level=1+len(route), path=path, title=route[-1]), self.content),
'unable to find documentation section for {}'.format(path)
)
for method in ('read', 'create'):
subpath = "{}/retrieve_alt-{}".format(path, method)
subpath = "{}-retrieve_alt-{}".format(path, method)
self.assertTrue(
re.search(header_re.format(level=3, path=subpath, title=method), self.content),
'unable to find documentation section for {}'.format(subpath)