2005-11-28 01:20:43 +08:00
|
|
|
"Misc. utility functions/classes for admin documentation generator."
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
import re
|
|
|
|
from email.Parser import HeaderParser
|
|
|
|
from email.Errors import HeaderParseError
|
2007-11-14 20:58:53 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2009-05-07 20:52:02 +08:00
|
|
|
from django.core.urlresolvers import reverse
|
2010-05-10 19:20:36 +08:00
|
|
|
from django.utils.encoding import smart_str
|
2005-11-28 01:20:43 +08:00
|
|
|
try:
|
|
|
|
import docutils.core
|
|
|
|
import docutils.nodes
|
|
|
|
import docutils.parsers.rst.roles
|
|
|
|
except ImportError:
|
|
|
|
docutils_is_available = False
|
|
|
|
else:
|
|
|
|
docutils_is_available = True
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def trim_docstring(docstring):
|
|
|
|
"""
|
|
|
|
Uniformly trims leading/trailing whitespace from docstrings.
|
|
|
|
|
|
|
|
Based on http://www.python.org/peps/pep-0257.html#handling-docstring-indentation
|
|
|
|
"""
|
|
|
|
if not docstring or not docstring.strip():
|
|
|
|
return ''
|
|
|
|
# Convert tabs to spaces and split into lines
|
|
|
|
lines = docstring.expandtabs().splitlines()
|
|
|
|
indent = min([len(line) - len(line.lstrip()) for line in lines if line.lstrip()])
|
|
|
|
trimmed = [lines[0].lstrip()] + [line[indent:].rstrip() for line in lines[1:]]
|
|
|
|
return "\n".join(trimmed).strip()
|
|
|
|
|
|
|
|
def parse_docstring(docstring):
|
|
|
|
"""
|
|
|
|
Parse out the parts of a docstring. Returns (title, body, metadata).
|
|
|
|
"""
|
|
|
|
docstring = trim_docstring(docstring)
|
|
|
|
parts = re.split(r'\n{2,}', docstring)
|
|
|
|
title = parts[0]
|
|
|
|
if len(parts) == 1:
|
|
|
|
body = ''
|
|
|
|
metadata = {}
|
|
|
|
else:
|
|
|
|
parser = HeaderParser()
|
|
|
|
try:
|
|
|
|
metadata = parser.parsestr(parts[-1])
|
|
|
|
except HeaderParseError:
|
|
|
|
metadata = {}
|
|
|
|
body = "\n\n".join(parts[1:])
|
|
|
|
else:
|
|
|
|
metadata = dict(metadata.items())
|
|
|
|
if metadata:
|
|
|
|
body = "\n\n".join(parts[1:-1])
|
|
|
|
else:
|
|
|
|
body = "\n\n".join(parts[1:])
|
|
|
|
return title, body, metadata
|
|
|
|
|
2009-05-07 20:52:02 +08:00
|
|
|
def parse_rst(text, default_reference_context, thing_being_parsed=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Convert the string from reST to an XHTML fragment.
|
|
|
|
"""
|
|
|
|
overrides = {
|
|
|
|
'doctitle_xform' : True,
|
|
|
|
'inital_header_level' : 3,
|
2005-08-03 03:59:51 +08:00
|
|
|
"default_reference_context" : default_reference_context,
|
2009-05-07 20:52:02 +08:00
|
|
|
"link_base" : reverse('django-admindocs-docroot').rstrip('/')
|
2005-07-13 09:25:57 +08:00
|
|
|
}
|
|
|
|
if thing_being_parsed:
|
2010-05-10 19:20:36 +08:00
|
|
|
thing_being_parsed = smart_str("<%s>" % thing_being_parsed)
|
2005-07-13 09:25:57 +08:00
|
|
|
parts = docutils.core.publish_parts(text, source_path=thing_being_parsed,
|
|
|
|
destination_path=None, writer_name='html',
|
2005-08-03 03:59:51 +08:00
|
|
|
settings_overrides=overrides)
|
2007-11-14 20:58:53 +08:00
|
|
|
return mark_safe(parts['fragment'])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-08-03 03:59:51 +08:00
|
|
|
#
|
|
|
|
# reST roles
|
|
|
|
#
|
|
|
|
ROLES = {
|
|
|
|
'model' : '%s/models/%s/',
|
|
|
|
'view' : '%s/views/%s/',
|
|
|
|
'template' : '%s/templates/%s/',
|
|
|
|
'filter' : '%s/filters/#%s',
|
|
|
|
'tag' : '%s/tags/#%s',
|
|
|
|
}
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def create_reference_role(rolename, urlbase):
|
2006-06-03 21:37:34 +08:00
|
|
|
def _role(name, rawtext, text, lineno, inliner, options=None, content=None):
|
|
|
|
if options is None: options = {}
|
|
|
|
if content is None: content = []
|
2006-05-02 09:31:56 +08:00
|
|
|
node = docutils.nodes.reference(rawtext, text, refuri=(urlbase % (inliner.document.settings.link_base, text.lower())), **options)
|
2005-07-13 09:25:57 +08:00
|
|
|
return [node], []
|
|
|
|
docutils.parsers.rst.roles.register_canonical_role(rolename, _role)
|
|
|
|
|
2006-06-03 21:37:34 +08:00
|
|
|
def default_reference_role(name, rawtext, text, lineno, inliner, options=None, content=None):
|
|
|
|
if options is None: options = {}
|
|
|
|
if content is None: content = []
|
2005-07-13 09:25:57 +08:00
|
|
|
context = inliner.document.settings.default_reference_context
|
2006-05-02 09:31:56 +08:00
|
|
|
node = docutils.nodes.reference(rawtext, text, refuri=(ROLES[context] % (inliner.document.settings.link_base, text.lower())), **options)
|
2005-07-13 09:25:57 +08:00
|
|
|
return [node], []
|
|
|
|
|
2005-12-07 13:11:19 +08:00
|
|
|
if docutils_is_available:
|
|
|
|
docutils.parsers.rst.roles.register_canonical_role('cmsreference', default_reference_role)
|
|
|
|
docutils.parsers.rst.roles.DEFAULT_INTERPRETED_ROLE = 'cmsreference'
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
for name, urlbase in ROLES.items():
|
2005-12-07 13:11:19 +08:00
|
|
|
create_reference_role(name, urlbase)
|