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 %} {% load rest_framework %}
{% for section_key, section in items %} {% for section_key, section in items %}
{% if section_key %} {% 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> <h{{level}} id="{{ path|list_add:section_key|join:'-' }}" class="coredocs-section-title">
</a></h{{level}}> {{ section_key }}
{% endif %} <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 %} {% if section.links|items %}
{% for link_key, link in 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 %} {% endfor %}
{% endif %} {% 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 %} {% endfor %}

View File

@ -1,22 +1,18 @@
{% load rest_framework %} {% load rest_framework %}
{% if items %} {% 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 %} {% for section_key, section in items %}
<li data-toggle="collapse" data-target="#{{prefix}}{{ section_key }}-dropdown" class="collapsed"> <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> {% if prefix %}{{prefix}}/{% endif %}{% if section_key %}{{ section_key }}{% else %}API Endpoints{% endif %} <span class="arrow"></span></a> <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> </li>
{% if section.links|items %} {% 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 %} {% 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 %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% if prefix %} {% include "rest_framework/docs/_recursive_menu.html" with items=section.data|items path=path|list_add:section_key %}
{% 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 %}
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}

View File

@ -14,8 +14,8 @@
</div> </div>
</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 %} {% 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 %} {% endfor %}

View File

@ -16,7 +16,7 @@
</div> </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="modal-body">
<div class="row"> <div class="row">
<div class="col-lg-6 request"> <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() var client = new coreapi.Client()
// Interact with the API endpoint // 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 = { {% if link.fields %}var params = {
{% for field in link.fields %} {{ field.name }}: ...{% if not loop.last %},{% endif %} {% for field in link.fields %} {{ field.name }}: ...{% if not loop.last %},{% endif %}
{% endfor %}} {% endfor %}}

View File

@ -6,7 +6,7 @@ client = coreapi.Client()
schema = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %}) schema = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
# Interact with the API endpoint # 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 = { {% if link.fields %}params = {
{% for field in link.fields %} "{{ field.name }}": ...{% if not loop.last %},{% endif %} {% for field in link.fields %} "{{ field.name }}": ...{% if not loop.last %},{% endif %}
{% endfor %}} {% endfor %}}

View File

@ -3,4 +3,4 @@
$ coreapi get {{ document.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 {{ 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 <i class="fa fa-exchange"></i> Interact
</button> </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> <h3 id="{{ path|list_add:section_key|list_add:link_key|join:"-" }}" class="coredocs-link-title">
</a></h3> {{ 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"> <div class="meta">
<span class="label label-primary">{{ link.action|upper }}</span> <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> <i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
<div class="menu-list"> <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"> <ul class="menu-list menu-list-bottom">
<li data-toggle="collapse" data-target="#auth-control" class="collapsed"> <li data-toggle="collapse" data-target="#auth-control" class="collapsed">

View File

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

View File

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