2008-08-24 06:25:40 +08:00
|
|
|
"""
|
|
|
|
Sphinx plugins for Django documentation.
|
|
|
|
"""
|
2012-04-30 01:58:00 +08:00
|
|
|
import json
|
2010-07-26 04:39:40 +08:00
|
|
|
import os
|
2010-10-27 23:35:07 +08:00
|
|
|
import re
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-09-24 06:23:47 +08:00
|
|
|
from docutils import nodes
|
|
|
|
from docutils.parsers.rst import directives
|
|
|
|
|
2013-03-25 13:52:56 +08:00
|
|
|
from sphinx import addnodes, __version__ as sphinx_ver
|
2010-07-26 04:39:40 +08:00
|
|
|
from sphinx.builders.html import StandaloneHTMLBuilder
|
|
|
|
from sphinx.writers.html import SmartyPantsHTMLTranslator
|
2010-05-08 22:31:20 +08:00
|
|
|
from sphinx.util.console import bold
|
2010-08-19 00:31:15 +08:00
|
|
|
from sphinx.util.compat import Directive
|
2013-09-24 06:23:47 +08:00
|
|
|
from sphinx.util.nodes import set_source_info
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2010-10-27 23:35:07 +08:00
|
|
|
# RE for option descriptions without a '--' prefix
|
|
|
|
simple_option_desc_re = re.compile(
|
|
|
|
r'([-_a-zA-Z0-9]+)(\s*.*?)(?=,\s+(?:/|-|--)|$)')
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
def setup(app):
|
|
|
|
app.add_crossref_type(
|
2013-11-03 03:37:48 +08:00
|
|
|
directivename="setting",
|
|
|
|
rolename="setting",
|
|
|
|
indextemplate="pair: %s; setting",
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
|
|
|
app.add_crossref_type(
|
2013-11-03 03:37:48 +08:00
|
|
|
directivename="templatetag",
|
|
|
|
rolename="ttag",
|
|
|
|
indextemplate="pair: %s; template tag"
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
|
|
|
app.add_crossref_type(
|
2013-11-03 03:37:48 +08:00
|
|
|
directivename="templatefilter",
|
|
|
|
rolename="tfilter",
|
|
|
|
indextemplate="pair: %s; template filter"
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
|
|
|
app.add_crossref_type(
|
2013-11-03 03:37:48 +08:00
|
|
|
directivename="fieldlookup",
|
|
|
|
rolename="lookup",
|
|
|
|
indextemplate="pair: %s; field lookup type",
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
|
|
|
app.add_description_unit(
|
2013-11-03 03:37:48 +08:00
|
|
|
directivename="django-admin",
|
|
|
|
rolename="djadmin",
|
|
|
|
indextemplate="pair: %s; django-admin command",
|
|
|
|
parse_node=parse_django_admin_node,
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
|
|
|
app.add_description_unit(
|
2013-11-03 03:37:48 +08:00
|
|
|
directivename="django-admin-option",
|
|
|
|
rolename="djadminopt",
|
|
|
|
indextemplate="pair: %s; django-admin command-line option",
|
|
|
|
parse_node=parse_django_adminopt_node,
|
2008-08-24 06:25:40 +08:00
|
|
|
)
|
2009-03-09 09:37:17 +08:00
|
|
|
app.add_config_value('django_next_version', '0.0', True)
|
2010-08-19 00:31:15 +08:00
|
|
|
app.add_directive('versionadded', VersionDirective)
|
|
|
|
app.add_directive('versionchanged', VersionDirective)
|
2010-05-08 22:31:20 +08:00
|
|
|
app.add_builder(DjangoStandaloneHTMLBuilder)
|
|
|
|
|
2013-09-24 06:23:47 +08:00
|
|
|
# register the snippet directive
|
|
|
|
app.add_directive('snippet', SnippetWithFilename)
|
|
|
|
# register a node for snippet directive so that the xml parser
|
|
|
|
# knows how to handle the enter/exit parsing event
|
|
|
|
app.add_node(snippet_with_filename,
|
|
|
|
html=(visit_snippet, depart_snippet_literal),
|
|
|
|
latex=(visit_snippet_latex, depart_snippet_latex),
|
|
|
|
man=(visit_snippet_literal, depart_snippet_literal),
|
|
|
|
text=(visit_snippet_literal, depart_snippet_literal),
|
|
|
|
texinfo=(visit_snippet_literal, depart_snippet_literal))
|
|
|
|
|
|
|
|
|
|
|
|
class snippet_with_filename(nodes.literal_block):
|
|
|
|
"""
|
|
|
|
Subclass the literal_block to override the visit/depart event handlers
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def visit_snippet_literal(self, node):
|
|
|
|
"""
|
|
|
|
default literal block handler
|
|
|
|
"""
|
|
|
|
self.visit_literal_block(node)
|
|
|
|
|
|
|
|
|
|
|
|
def depart_snippet_literal(self, node):
|
|
|
|
"""
|
|
|
|
default literal block handler
|
|
|
|
"""
|
|
|
|
self.depart_literal_block(node)
|
|
|
|
|
|
|
|
|
|
|
|
def visit_snippet(self, node):
|
|
|
|
"""
|
|
|
|
HTML document generator visit handler
|
|
|
|
"""
|
|
|
|
lang = self.highlightlang
|
|
|
|
linenos = node.rawsource.count('\n') >= self.highlightlinenothreshold - 1
|
|
|
|
fname = node['filename']
|
|
|
|
highlight_args = node.get('highlight_args', {})
|
2013-11-27 04:31:52 +08:00
|
|
|
if 'language' in node:
|
2013-09-24 06:23:47 +08:00
|
|
|
# code-block directives
|
|
|
|
lang = node['language']
|
|
|
|
highlight_args['force'] = True
|
2013-11-27 04:31:52 +08:00
|
|
|
if 'linenos' in node:
|
2013-09-24 06:23:47 +08:00
|
|
|
linenos = node['linenos']
|
|
|
|
|
|
|
|
def warner(msg):
|
|
|
|
self.builder.warn(msg, (self.builder.current_docname, node.line))
|
|
|
|
|
|
|
|
highlighted = self.highlighter.highlight_block(node.rawsource, lang,
|
|
|
|
warn=warner,
|
|
|
|
linenos=linenos,
|
|
|
|
**highlight_args)
|
|
|
|
starttag = self.starttag(node, 'div', suffix='',
|
|
|
|
CLASS='highlight-%s' % lang)
|
|
|
|
self.body.append(starttag)
|
|
|
|
self.body.append('<div class="snippet-filename">%s</div>\n''' % (fname,))
|
|
|
|
self.body.append(highlighted)
|
|
|
|
self.body.append('</div>\n')
|
|
|
|
raise nodes.SkipNode
|
|
|
|
|
|
|
|
|
|
|
|
def visit_snippet_latex(self, node):
|
|
|
|
"""
|
|
|
|
Latex document generator visit handler
|
|
|
|
"""
|
|
|
|
self.verbatim = ''
|
|
|
|
|
|
|
|
|
|
|
|
def depart_snippet_latex(self, node):
|
|
|
|
"""
|
|
|
|
Latex document generator depart handler.
|
|
|
|
"""
|
|
|
|
code = self.verbatim.rstrip('\n')
|
|
|
|
lang = self.hlsettingstack[-1][0]
|
|
|
|
linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
|
|
|
|
fname = node['filename']
|
|
|
|
highlight_args = node.get('highlight_args', {})
|
|
|
|
if 'language' in node:
|
|
|
|
# code-block directives
|
|
|
|
lang = node['language']
|
|
|
|
highlight_args['force'] = True
|
|
|
|
if 'linenos' in node:
|
|
|
|
linenos = node['linenos']
|
|
|
|
|
|
|
|
def warner(msg):
|
|
|
|
self.builder.warn(msg, (self.curfilestack[-1], node.line))
|
|
|
|
|
|
|
|
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
|
|
|
|
linenos=linenos,
|
|
|
|
**highlight_args)
|
|
|
|
|
|
|
|
self.body.append('\n{\\colorbox[rgb]{0.9,0.9,0.9}'
|
|
|
|
'{\\makebox[\\textwidth][l]'
|
|
|
|
'{\\small\\texttt{%s}}}}\n' % (fname,))
|
|
|
|
|
|
|
|
if self.table:
|
|
|
|
hlcode = hlcode.replace('\\begin{Verbatim}',
|
|
|
|
'\\begin{OriginalVerbatim}')
|
|
|
|
self.table.has_problematic = True
|
|
|
|
self.table.has_verbatim = True
|
|
|
|
|
|
|
|
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
|
|
|
|
hlcode = hlcode.rstrip() + '\n'
|
|
|
|
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
|
|
|
|
(self.table and 'Original' or ''))
|
|
|
|
self.verbatim = None
|
|
|
|
|
|
|
|
|
|
|
|
class SnippetWithFilename(Directive):
|
|
|
|
"""
|
|
|
|
The 'snippet' directive that allows to add the filename (optional)
|
|
|
|
of a code snippet in the document. This is modeled after CodeBlock.
|
|
|
|
"""
|
|
|
|
has_content = True
|
|
|
|
optional_arguments = 1
|
|
|
|
option_spec = {'filename': directives.unchanged_required}
|
|
|
|
|
|
|
|
def run(self):
|
2013-12-07 02:06:59 +08:00
|
|
|
code = '\n'.join(self.content)
|
2013-09-24 06:23:47 +08:00
|
|
|
|
|
|
|
literal = snippet_with_filename(code, code)
|
|
|
|
if self.arguments:
|
|
|
|
literal['language'] = self.arguments[0]
|
|
|
|
literal['filename'] = self.options['filename']
|
|
|
|
set_source_info(self, literal)
|
|
|
|
return [literal]
|
|
|
|
|
2010-08-19 00:31:15 +08:00
|
|
|
|
|
|
|
class VersionDirective(Directive):
|
|
|
|
has_content = True
|
|
|
|
required_arguments = 1
|
|
|
|
optional_arguments = 1
|
|
|
|
final_argument_whitespace = True
|
|
|
|
option_spec = {}
|
|
|
|
|
|
|
|
def run(self):
|
2013-03-25 13:48:23 +08:00
|
|
|
if len(self.arguments) > 1:
|
|
|
|
msg = """Only one argument accepted for directive '{directive_name}::'.
|
|
|
|
Comments should be provided as content,
|
|
|
|
not as an extra argument.""".format(directive_name=self.name)
|
2013-03-25 13:53:48 +08:00
|
|
|
raise self.error(msg)
|
2013-03-25 13:48:23 +08:00
|
|
|
|
2010-08-19 00:31:15 +08:00
|
|
|
env = self.state.document.settings.env
|
|
|
|
ret = []
|
|
|
|
node = addnodes.versionmodified()
|
|
|
|
ret.append(node)
|
2013-03-25 13:48:23 +08:00
|
|
|
|
2013-02-09 22:16:10 +08:00
|
|
|
if self.arguments[0] == env.config.django_next_version:
|
2010-08-19 00:31:15 +08:00
|
|
|
node['version'] = "Development version"
|
2013-02-09 22:16:10 +08:00
|
|
|
else:
|
|
|
|
node['version'] = self.arguments[0]
|
2013-03-25 13:48:23 +08:00
|
|
|
|
2010-08-19 00:31:15 +08:00
|
|
|
node['type'] = self.name
|
2013-03-25 13:48:23 +08:00
|
|
|
if self.content:
|
|
|
|
self.state.nested_parse(self.content, self.content_offset, node)
|
2010-08-19 00:31:15 +08:00
|
|
|
env.note_versionchange(node['type'], node['version'], node, self.lineno)
|
|
|
|
return ret
|
2009-03-09 09:37:17 +08:00
|
|
|
|
2010-07-26 04:39:40 +08:00
|
|
|
|
|
|
|
class DjangoHTMLTranslator(SmartyPantsHTMLTranslator):
|
2008-08-24 06:25:40 +08:00
|
|
|
"""
|
|
|
|
Django-specific reST to HTML tweaks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Don't use border=1, which docutils does by default.
|
|
|
|
def visit_table(self, node):
|
2013-07-31 21:24:29 +08:00
|
|
|
self.context.append(self.compact_p)
|
|
|
|
self.compact_p = True
|
2013-11-03 03:37:48 +08:00
|
|
|
self._table_row_index = 0 # Needed by Sphinx
|
2008-08-24 06:25:40 +08:00
|
|
|
self.body.append(self.starttag(node, 'table', CLASS='docutils'))
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2013-07-31 21:24:29 +08:00
|
|
|
def depart_table(self, node):
|
|
|
|
self.compact_p = self.context.pop()
|
|
|
|
self.body.append('</table>\n')
|
|
|
|
|
2008-08-27 03:38:27 +08:00
|
|
|
def visit_desc_parameterlist(self, node):
|
2014-01-24 21:52:43 +08:00
|
|
|
self.body.append('(') # by default sphinx puts <big> around the "("
|
2008-08-27 03:38:27 +08:00
|
|
|
self.first_param = 1
|
2014-01-24 21:52:43 +08:00
|
|
|
self.optional_param_level = 0
|
2011-05-16 07:02:36 +08:00
|
|
|
self.param_separator = node.child_text_separator
|
2014-01-24 21:52:43 +08:00
|
|
|
self.required_params_left = sum([isinstance(c, addnodes.desc_parameter)
|
|
|
|
for c in node.children])
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2008-08-27 03:38:27 +08:00
|
|
|
def depart_desc_parameterlist(self, node):
|
|
|
|
self.body.append(')')
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2011-10-10 08:23:47 +08:00
|
|
|
if sphinx_ver < '1.0.8':
|
|
|
|
#
|
|
|
|
# Don't apply smartypants to literal blocks
|
|
|
|
#
|
|
|
|
def visit_literal_block(self, node):
|
|
|
|
self.no_smarty += 1
|
|
|
|
SmartyPantsHTMLTranslator.visit_literal_block(self, node)
|
|
|
|
|
|
|
|
def depart_literal_block(self, node):
|
|
|
|
SmartyPantsHTMLTranslator.depart_literal_block(self, node)
|
|
|
|
self.no_smarty -= 1
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
#
|
2010-07-26 04:39:40 +08:00
|
|
|
# Turn the "new in version" stuff (versionadded/versionchanged) into a
|
2008-09-02 11:40:42 +08:00
|
|
|
# better callout -- the Sphinx default is just a little span,
|
|
|
|
# which is a bit less obvious that I'd like.
|
|
|
|
#
|
2010-07-24 18:21:21 +08:00
|
|
|
# FIXME: these messages are all hardcoded in English. We need to change
|
2013-03-29 01:16:53 +08:00
|
|
|
# that to accommodate other language docs, but I can't work out how to make
|
2010-07-26 04:39:40 +08:00
|
|
|
# that work.
|
2008-09-02 11:40:42 +08:00
|
|
|
#
|
|
|
|
version_text = {
|
2013-11-03 03:37:48 +08:00
|
|
|
'versionchanged': 'Changed in Django %s',
|
|
|
|
'versionadded': 'New in Django %s',
|
2008-09-02 11:40:42 +08:00
|
|
|
}
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
def visit_versionmodified(self, node):
|
|
|
|
self.body.append(
|
|
|
|
self.starttag(node, 'div', CLASS=node['type'])
|
|
|
|
)
|
2014-06-07 08:31:23 +08:00
|
|
|
version_text = self.version_text.get(node['type'])
|
|
|
|
if version_text:
|
|
|
|
title = "%s%s" % (
|
|
|
|
version_text % node['version'],
|
|
|
|
":" if len(node) else "."
|
|
|
|
)
|
|
|
|
self.body.append('<span class="title">%s</span> ' % title)
|
2010-07-26 04:39:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
def depart_versionmodified(self, node):
|
|
|
|
self.body.append("</div>\n")
|
2008-12-07 12:00:08 +08:00
|
|
|
|
2010-07-26 04:39:40 +08:00
|
|
|
# Give each section a unique ID -- nice for custom CSS hooks
|
|
|
|
def visit_section(self, node):
|
|
|
|
old_ids = node.get('ids', [])
|
|
|
|
node['ids'] = ['s-' + i for i in old_ids]
|
|
|
|
node['ids'].extend(old_ids)
|
|
|
|
SmartyPantsHTMLTranslator.visit_section(self, node)
|
|
|
|
node['ids'] = old_ids
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
def parse_django_admin_node(env, sig, signode):
|
|
|
|
command = sig.split(' ')[0]
|
|
|
|
env._django_curr_admin_command = command
|
|
|
|
title = "django-admin.py %s" % sig
|
2010-07-26 04:39:40 +08:00
|
|
|
signode += addnodes.desc_name(title, title)
|
2008-08-24 06:25:40 +08:00
|
|
|
return sig
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2009-07-03 13:40:46 +08:00
|
|
|
def parse_django_adminopt_node(env, sig, signode):
|
|
|
|
"""A copy of sphinx.directives.CmdoptionDesc.parse_signature()"""
|
2010-11-20 07:19:56 +08:00
|
|
|
from sphinx.domains.std import option_desc_re
|
2009-07-03 13:40:46 +08:00
|
|
|
count = 0
|
|
|
|
firstname = ''
|
|
|
|
for m in option_desc_re.finditer(sig):
|
|
|
|
optname, args = m.groups()
|
|
|
|
if count:
|
|
|
|
signode += addnodes.desc_addname(', ', ', ')
|
|
|
|
signode += addnodes.desc_name(optname, optname)
|
|
|
|
signode += addnodes.desc_addname(args, args)
|
|
|
|
if not count:
|
|
|
|
firstname = optname
|
|
|
|
count += 1
|
2010-10-27 23:35:07 +08:00
|
|
|
if not count:
|
|
|
|
for m in simple_option_desc_re.finditer(sig):
|
|
|
|
optname, args = m.groups()
|
|
|
|
if count:
|
|
|
|
signode += addnodes.desc_addname(', ', ', ')
|
|
|
|
signode += addnodes.desc_name(optname, optname)
|
|
|
|
signode += addnodes.desc_addname(args, args)
|
|
|
|
if not count:
|
|
|
|
firstname = optname
|
|
|
|
count += 1
|
2009-07-03 13:40:46 +08:00
|
|
|
if not firstname:
|
|
|
|
raise ValueError
|
|
|
|
return firstname
|
|
|
|
|
2010-05-08 22:31:20 +08:00
|
|
|
|
2010-07-26 04:39:40 +08:00
|
|
|
class DjangoStandaloneHTMLBuilder(StandaloneHTMLBuilder):
|
2010-05-08 22:31:20 +08:00
|
|
|
"""
|
|
|
|
Subclass to add some extra things we need.
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = 'djangohtml'
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
super(DjangoStandaloneHTMLBuilder, self).finish()
|
|
|
|
self.info(bold("writing templatebuiltins.js..."))
|
2010-11-20 09:37:54 +08:00
|
|
|
xrefs = self.env.domaindata["std"]["objects"]
|
2010-11-21 05:00:41 +08:00
|
|
|
templatebuiltins = {
|
|
|
|
"ttags": [n for ((t, n), (l, a)) in xrefs.items()
|
2013-12-13 04:23:24 +08:00
|
|
|
if t == "templatetag" and l == "ref/templates/builtins"],
|
2010-11-21 05:00:41 +08:00
|
|
|
"tfilters": [n for ((t, n), (l, a)) in xrefs.items()
|
2013-12-13 04:23:24 +08:00
|
|
|
if t == "templatefilter" and l == "ref/templates/builtins"],
|
2010-11-21 05:00:41 +08:00
|
|
|
}
|
2010-05-08 22:31:20 +08:00
|
|
|
outfilename = os.path.join(self.outdir, "templatebuiltins.js")
|
2013-02-24 22:44:50 +08:00
|
|
|
with open(outfilename, 'w') as fp:
|
2012-05-05 20:01:38 +08:00
|
|
|
fp.write('var django_template_builtins = ')
|
|
|
|
json.dump(templatebuiltins, fp)
|
|
|
|
fp.write(';\n')
|