Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support restructuredtext #121

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ You can find detailed information about the package's settings at [the docs](htt

REST_FRAMEWORK_DOCS = {
'HIDE_DOCS': True # Default: False
'DOCSTRING_FORMAT': 'rst' # Default: 'text'
}


Expand Down
13 changes: 9 additions & 4 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ You can use hidden to prevent your docs from showing up in different environment

Then set the value of the environment variable `HIDE_DRFDOCS` for each environment (ie. Use `.env` files)

##### DOCSTRING_FORMAT
Use DOCSTRING_FORMAT to configure the format you follow for the docstrings. Supported formats are:
- *text*: Plain text
- *rst*: reStructuredText

### List of Settings

| Setting | Type | Options | Default |
|---------|---------|-----------------|---------|
|HIDE_DOCS| Boolean | `True`, `False` | `False` |
| | | | |
| Setting | Type | Options | Default |
|----------------|---------|-----------------|---------|
|HIDE_DOCS | Boolean | `True`, `False` | `False` |
|DOCSTRING_FORMAT| String | 'text', 'rst' | 'text' |
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ djangorestframework==3.3.2
coverage==4.0.3
flake8==2.5.1
mkdocs==0.15.3
docutils==0.12
5 changes: 3 additions & 2 deletions rest_framework_docs/api_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

class ApiDocumentation(object):

def __init__(self, drf_router=None):
def __init__(self, drf_router=None, docstring_format=None):
self.endpoints = []
self.drf_router = drf_router
self.docstring_format = docstring_format
try:
root_urlconf = import_string(settings.ROOT_URLCONF)
except ImportError:
Expand All @@ -27,7 +28,7 @@ def get_all_view_names(self, urlpatterns, parent_pattern=None):
parent_pattern = None if pattern._regex == "^" else pattern
self.get_all_view_names(urlpatterns=pattern.url_patterns, parent_pattern=parent_pattern)
elif isinstance(pattern, RegexURLPattern) and self._is_drf_view(pattern) and not self._is_format_endpoint(pattern):
api_endpoint = ApiEndpoint(pattern, parent_pattern, self.drf_router)
api_endpoint = ApiEndpoint(pattern, parent_pattern, self.drf_router, self.docstring_format)
self.endpoints.append(api_endpoint)

def _is_drf_view(self, pattern):
Expand Down
12 changes: 10 additions & 2 deletions rest_framework_docs/api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from django.contrib.admindocs.views import simplify_regex
from django.utils.encoding import force_str
from rest_framework.serializers import BaseSerializer
from django.utils.safestring import mark_safe
from docutils import core


class ApiEndpoint(object):

def __init__(self, pattern, parent_pattern=None, drf_router=None):
def __init__(self, pattern, parent_pattern=None, drf_router=None, docstring_format=None):
self.drf_router = drf_router
self.pattern = pattern
self.callback = pattern.callback
self.docstring_format = docstring_format
# self.name = pattern.name
self.docstring = self.__get_docstring__()
self.name_parent = simplify_regex(parent_pattern.regex.pattern).strip('/') if parent_pattern else None
Expand Down Expand Up @@ -67,7 +70,12 @@ def __get_allowed_methods__(self):
return viewset_methods + view_methods

def __get_docstring__(self):
return inspect.getdoc(self.callback)
description = inspect.getdoc(self.callback)
if (self.docstring_format == "rst"): # reStructuredText
parts = core.publish_parts(source=description, writer_name="html")
html = parts["body_pre_docinfo"] + parts["fragment"]
description = mark_safe(html)
return description

def __get_permissions_class__(self):
for perm_class in self.pattern.callback.cls.permission_classes:
Expand Down
3 changes: 2 additions & 1 deletion rest_framework_docs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class DRFSettings(object):

def __init__(self):
self.drf_settings = {
"HIDE_DOCS": self.get_setting("HIDE_DOCS") or False
"HIDE_DOCS": self.get_setting("HIDE_DOCS") or False,
"DOCSTRING_FORMAT": self.get_setting("DOCSTRING_FORMAT") or "text"
}

def get_setting(self, name):
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_docs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_context_data(self, **kwargs):
raise Http404("Django Rest Framework Docs are hidden. Check your settings.")

context = super(DRFDocsView, self).get_context_data(**kwargs)
docs = ApiDocumentation(drf_router=self.drf_router)
docs = ApiDocumentation(drf_router=self.drf_router, docstring_format=settings["DOCSTRING_FORMAT"])
endpoints = docs.get_endpoints()

query = self.request.GET.get("search", "")
Expand Down