2008-08-23 22:25:40 +00:00
|
|
|
"""
|
|
|
|
Sphinx plugins for Django documentation.
|
|
|
|
"""
|
2012-04-29 17:58:00 +00:00
|
|
|
import json
|
2010-07-25 20:39:40 +00:00
|
|
|
import os
|
2010-10-27 15:35:07 +00:00
|
|
|
import re
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2013-09-23 22:23:47 +00:00
|
|
|
from docutils import nodes
|
2018-09-10 17:00:34 +00:00
|
|
|
from docutils.parsers.rst import Directive
|
2018-01-20 17:38:48 +00:00
|
|
|
from docutils.statemachine import ViewList
|
2022-02-03 19:24:19 +00:00
|
|
|
from sphinx import addnodes
|
|
|
|
from sphinx import version_info as sphinx_version
|
2010-07-25 20:39:40 +00:00
|
|
|
from sphinx.builders.html import StandaloneHTMLBuilder
|
2020-04-06 06:00:40 +00:00
|
|
|
from sphinx.directives.code import CodeBlock
|
2016-01-12 01:59:34 +00:00
|
|
|
from sphinx.domains.std import Cmdoption
|
2020-06-03 09:42:42 +00:00
|
|
|
from sphinx.errors import ExtensionError
|
2018-09-10 20:08:21 +00:00
|
|
|
from sphinx.util import logging
|
2020-06-03 09:42:42 +00:00
|
|
|
from sphinx.util.console import bold
|
2018-03-23 00:30:03 +00:00
|
|
|
from sphinx.writers.html import HTMLTranslator
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2018-09-10 20:08:21 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2010-10-27 15:35:07 +00:00
|
|
|
# RE for option descriptions without a '--' prefix
|
2022-02-03 19:24:19 +00:00
|
|
|
simple_option_desc_re = re.compile(r"([-_a-zA-Z0-9]+)(\s*.*?)(?=,\s+(?:/|-|--)|$)")
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2013-11-02 19:37:48 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
def setup(app):
|
|
|
|
app.add_crossref_type(
|
2013-11-02 19:37:48 +00:00
|
|
|
directivename="setting",
|
|
|
|
rolename="setting",
|
|
|
|
indextemplate="pair: %s; setting",
|
2008-08-23 22:25:40 +00:00
|
|
|
)
|
|
|
|
app.add_crossref_type(
|
2013-11-02 19:37:48 +00:00
|
|
|
directivename="templatetag",
|
|
|
|
rolename="ttag",
|
2022-02-03 19:24:19 +00:00
|
|
|
indextemplate="pair: %s; template tag",
|
2008-08-23 22:25:40 +00:00
|
|
|
)
|
|
|
|
app.add_crossref_type(
|
2013-11-02 19:37:48 +00:00
|
|
|
directivename="templatefilter",
|
|
|
|
rolename="tfilter",
|
2022-02-03 19:24:19 +00:00
|
|
|
indextemplate="pair: %s; template filter",
|
2008-08-23 22:25:40 +00:00
|
|
|
)
|
|
|
|
app.add_crossref_type(
|
2013-11-02 19:37:48 +00:00
|
|
|
directivename="fieldlookup",
|
|
|
|
rolename="lookup",
|
|
|
|
indextemplate="pair: %s; field lookup type",
|
2008-08-23 22:25:40 +00:00
|
|
|
)
|
2018-09-10 20:08:21 +00:00
|
|
|
app.add_object_type(
|
2013-11-02 19:37:48 +00:00
|
|
|
directivename="django-admin",
|
|
|
|
rolename="djadmin",
|
|
|
|
indextemplate="pair: %s; django-admin command",
|
|
|
|
parse_node=parse_django_admin_node,
|
2008-08-23 22:25:40 +00:00
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
app.add_directive("django-admin-option", Cmdoption)
|
|
|
|
app.add_config_value("django_next_version", "0.0", True)
|
|
|
|
app.add_directive("versionadded", VersionDirective)
|
|
|
|
app.add_directive("versionchanged", VersionDirective)
|
2010-05-08 14:31:20 +00:00
|
|
|
app.add_builder(DjangoStandaloneHTMLBuilder)
|
2022-02-03 19:24:19 +00:00
|
|
|
app.set_translator("djangohtml", DjangoHTMLTranslator)
|
|
|
|
app.set_translator("json", DjangoHTMLTranslator)
|
2018-01-20 17:38:48 +00:00
|
|
|
app.add_node(
|
|
|
|
ConsoleNode,
|
|
|
|
html=(visit_console_html, None),
|
|
|
|
latex=(visit_console_dummy, depart_console_dummy),
|
|
|
|
man=(visit_console_dummy, depart_console_dummy),
|
|
|
|
text=(visit_console_dummy, depart_console_dummy),
|
|
|
|
texinfo=(visit_console_dummy, depart_console_dummy),
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
app.add_directive("console", ConsoleDirective)
|
|
|
|
app.connect("html-page-context", html_page_context_hook)
|
|
|
|
app.add_role("default-role-error", default_role_error)
|
|
|
|
return {"parallel_read_safe": True}
|
2013-09-23 22:23:47 +00:00
|
|
|
|
|
|
|
|
2010-08-18 16:31:15 +00: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 05:48:23 +00:00
|
|
|
if len(self.arguments) > 1:
|
|
|
|
msg = """Only one argument accepted for directive '{directive_name}::'.
|
|
|
|
Comments should be provided as content,
|
2022-02-03 19:24:19 +00:00
|
|
|
not as an extra argument.""".format(
|
|
|
|
directive_name=self.name
|
|
|
|
)
|
2013-03-25 05:53:48 +00:00
|
|
|
raise self.error(msg)
|
2013-03-25 05:48:23 +00:00
|
|
|
|
2010-08-18 16:31:15 +00:00
|
|
|
env = self.state.document.settings.env
|
|
|
|
ret = []
|
|
|
|
node = addnodes.versionmodified()
|
|
|
|
ret.append(node)
|
2013-03-25 05:48:23 +00:00
|
|
|
|
2013-02-09 14:16:10 +00:00
|
|
|
if self.arguments[0] == env.config.django_next_version:
|
2022-02-03 19:24:19 +00:00
|
|
|
node["version"] = "Development version"
|
2013-02-09 14:16:10 +00:00
|
|
|
else:
|
2022-02-03 19:24:19 +00:00
|
|
|
node["version"] = self.arguments[0]
|
2013-03-25 05:48:23 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
node["type"] = self.name
|
2013-03-25 05:48:23 +00:00
|
|
|
if self.content:
|
|
|
|
self.state.nested_parse(self.content, self.content_offset, node)
|
2018-09-11 13:15:39 +00:00
|
|
|
try:
|
2022-02-03 19:24:19 +00:00
|
|
|
env.get_domain("changeset").note_changeset(node)
|
2018-09-11 13:15:39 +00:00
|
|
|
except ExtensionError:
|
|
|
|
# Sphinx < 1.8: Domain 'changeset' is not registered
|
2022-02-03 19:24:19 +00:00
|
|
|
env.note_versionchange(node["type"], node["version"], node, self.lineno)
|
2010-08-18 16:31:15 +00:00
|
|
|
return ret
|
2009-03-09 01:37:17 +00:00
|
|
|
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2017-05-17 10:09:12 +00:00
|
|
|
class DjangoHTMLTranslator(HTMLTranslator):
|
2008-08-23 22:25:40 +00: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 13:24:29 +00:00
|
|
|
self.context.append(self.compact_p)
|
|
|
|
self.compact_p = True
|
2021-11-17 11:31:25 +00:00
|
|
|
# Needed by Sphinx.
|
|
|
|
if sphinx_version >= (4, 3):
|
|
|
|
self._table_row_indices.append(0)
|
|
|
|
else:
|
|
|
|
self._table_row_index = 0
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append(self.starttag(node, "table", CLASS="docutils"))
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2013-07-31 13:24:29 +00:00
|
|
|
def depart_table(self, node):
|
|
|
|
self.compact_p = self.context.pop()
|
2021-11-17 11:31:25 +00:00
|
|
|
if sphinx_version >= (4, 3):
|
|
|
|
self._table_row_indices.pop()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append("</table>\n")
|
2013-07-31 13:24:29 +00:00
|
|
|
|
2008-08-26 19:38:27 +00:00
|
|
|
def visit_desc_parameterlist(self, node):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append("(") # by default sphinx puts <big> around the "("
|
2008-08-26 19:38:27 +00:00
|
|
|
self.first_param = 1
|
2014-01-24 13:52:43 +00:00
|
|
|
self.optional_param_level = 0
|
2011-05-15 23:02:36 +00:00
|
|
|
self.param_separator = node.child_text_separator
|
2022-02-03 19:24:19 +00:00
|
|
|
self.required_params_left = sum(
|
|
|
|
isinstance(c, addnodes.desc_parameter) for c in node.children
|
|
|
|
)
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2008-08-26 19:38:27 +00:00
|
|
|
def depart_desc_parameterlist(self, node):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append(")")
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2008-09-02 03:40:42 +00:00
|
|
|
#
|
2010-07-25 20:39:40 +00:00
|
|
|
# Turn the "new in version" stuff (versionadded/versionchanged) into a
|
2008-09-02 03:40:42 +00:00
|
|
|
# better callout -- the Sphinx default is just a little span,
|
|
|
|
# which is a bit less obvious that I'd like.
|
|
|
|
#
|
2010-07-24 10:21:21 +00:00
|
|
|
# FIXME: these messages are all hardcoded in English. We need to change
|
2013-03-28 17:16:53 +00:00
|
|
|
# that to accommodate other language docs, but I can't work out how to make
|
2010-07-25 20:39:40 +00:00
|
|
|
# that work.
|
2008-09-02 03:40:42 +00:00
|
|
|
#
|
|
|
|
version_text = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"versionchanged": "Changed in Django %s",
|
|
|
|
"versionadded": "New in Django %s",
|
2008-09-02 03:40:42 +00:00
|
|
|
}
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2008-09-02 03:40:42 +00:00
|
|
|
def visit_versionmodified(self, node):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append(self.starttag(node, "div", CLASS=node["type"]))
|
|
|
|
version_text = self.version_text.get(node["type"])
|
2014-06-07 00:31:23 +00:00
|
|
|
if version_text:
|
2022-02-03 19:24:19 +00:00
|
|
|
title = "%s%s" % (version_text % node["version"], ":" if len(node) else ".")
|
2014-06-07 00:31:23 +00:00
|
|
|
self.body.append('<span class="title">%s</span> ' % title)
|
2010-07-25 20:39:40 +00:00
|
|
|
|
2008-09-02 03:40:42 +00:00
|
|
|
def depart_versionmodified(self, node):
|
|
|
|
self.body.append("</div>\n")
|
2008-12-07 04:00:08 +00:00
|
|
|
|
2010-07-25 20:39:40 +00:00
|
|
|
# Give each section a unique ID -- nice for custom CSS hooks
|
|
|
|
def visit_section(self, node):
|
2022-02-03 19:24:19 +00:00
|
|
|
old_ids = node.get("ids", [])
|
|
|
|
node["ids"] = ["s-" + i for i in old_ids]
|
|
|
|
node["ids"].extend(old_ids)
|
2017-05-17 10:09:12 +00:00
|
|
|
super().visit_section(node)
|
2022-02-03 19:24:19 +00:00
|
|
|
node["ids"] = old_ids
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2013-11-03 00:37:15 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
def parse_django_admin_node(env, sig, signode):
|
2022-02-03 19:24:19 +00:00
|
|
|
command = sig.split(" ")[0]
|
|
|
|
env.ref_context["std:program"] = command
|
2014-07-26 11:21:52 +00:00
|
|
|
title = "django-admin %s" % sig
|
2010-07-25 20:39:40 +00:00
|
|
|
signode += addnodes.desc_name(title, title)
|
2016-01-12 01:59:34 +00:00
|
|
|
return command
|
2009-07-03 05:40:46 +00:00
|
|
|
|
2010-05-08 14:31:20 +00:00
|
|
|
|
2010-07-25 20:39:40 +00:00
|
|
|
class DjangoStandaloneHTMLBuilder(StandaloneHTMLBuilder):
|
2010-05-08 14:31:20 +00:00
|
|
|
"""
|
|
|
|
Subclass to add some extra things we need.
|
|
|
|
"""
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
name = "djangohtml"
|
2010-05-08 14:31:20 +00:00
|
|
|
|
|
|
|
def finish(self):
|
2017-01-22 06:57:14 +00:00
|
|
|
super().finish()
|
2018-09-10 20:08:21 +00:00
|
|
|
logger.info(bold("writing templatebuiltins.js..."))
|
2010-11-20 01:37:54 +00:00
|
|
|
xrefs = self.env.domaindata["std"]["objects"]
|
2010-11-20 21:00:41 +00:00
|
|
|
templatebuiltins = {
|
2016-11-14 22:40:28 +00:00
|
|
|
"ttags": [
|
2022-02-03 19:24:19 +00:00
|
|
|
n
|
|
|
|
for ((t, n), (k, a)) in xrefs.items()
|
2016-11-14 22:40:28 +00:00
|
|
|
if t == "templatetag" and k == "ref/templates/builtins"
|
|
|
|
],
|
|
|
|
"tfilters": [
|
2022-02-03 19:24:19 +00:00
|
|
|
n
|
|
|
|
for ((t, n), (k, a)) in xrefs.items()
|
2016-11-14 22:40:28 +00:00
|
|
|
if t == "templatefilter" and k == "ref/templates/builtins"
|
|
|
|
],
|
2010-11-20 21:00:41 +00:00
|
|
|
}
|
2010-05-08 14:31:20 +00:00
|
|
|
outfilename = os.path.join(self.outdir, "templatebuiltins.js")
|
2022-02-03 19:24:19 +00:00
|
|
|
with open(outfilename, "w") as fp:
|
|
|
|
fp.write("var django_template_builtins = ")
|
2012-05-05 12:01:38 +00:00
|
|
|
json.dump(templatebuiltins, fp)
|
2022-02-03 19:24:19 +00:00
|
|
|
fp.write(";\n")
|
2018-01-20 17:38:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConsoleNode(nodes.literal_block):
|
|
|
|
"""
|
|
|
|
Custom node to override the visit/depart event handlers at registration
|
|
|
|
time. Wrap a literal_block object and defer to it.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
|
|
tagname = "ConsoleNode"
|
2018-10-02 00:44:02 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
def __init__(self, litblk_obj):
|
|
|
|
self.wrapped = litblk_obj
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
2022-02-03 19:24:19 +00:00
|
|
|
if attr == "wrapped":
|
2018-01-20 17:38:48 +00:00
|
|
|
return self.__dict__.wrapped
|
|
|
|
return getattr(self.wrapped, attr)
|
|
|
|
|
|
|
|
|
|
|
|
def visit_console_dummy(self, node):
|
|
|
|
"""Defer to the corresponding parent's handler."""
|
|
|
|
self.visit_literal_block(node)
|
|
|
|
|
|
|
|
|
|
|
|
def depart_console_dummy(self, node):
|
|
|
|
"""Defer to the corresponding parent's handler."""
|
|
|
|
self.depart_literal_block(node)
|
|
|
|
|
|
|
|
|
|
|
|
def visit_console_html(self, node):
|
|
|
|
"""Generate HTML for the console directive."""
|
2022-02-03 19:24:19 +00:00
|
|
|
if self.builder.name in ("djangohtml", "json") and node["win_console_text"]:
|
2018-01-20 17:38:48 +00:00
|
|
|
# Put a mark on the document object signaling the fact the directive
|
|
|
|
# has been used on it.
|
|
|
|
self.document._console_directive_used_flag = True
|
2022-02-03 19:24:19 +00:00
|
|
|
uid = node["uid"]
|
|
|
|
self.body.append(
|
|
|
|
"""\
|
2018-01-20 17:38:48 +00:00
|
|
|
<div class="console-block" id="console-block-%(id)s">
|
2022-02-04 07:08:27 +00:00
|
|
|
<input class="c-tab-unix" id="c-tab-%(id)s-unix" type="radio" name="console-%(id)s" \
|
|
|
|
checked>
|
2018-01-20 17:38:48 +00:00
|
|
|
<label for="c-tab-%(id)s-unix" title="Linux/macOS">/</label>
|
|
|
|
<input class="c-tab-win" id="c-tab-%(id)s-win" type="radio" name="console-%(id)s">
|
|
|
|
<label for="c-tab-%(id)s-win" title="Windows"></label>
|
2022-02-03 19:24:19 +00:00
|
|
|
<section class="c-content-unix" id="c-content-%(id)s-unix">\n"""
|
|
|
|
% {"id": uid}
|
|
|
|
)
|
2018-01-20 17:38:48 +00:00
|
|
|
try:
|
|
|
|
self.visit_literal_block(node)
|
|
|
|
except nodes.SkipNode:
|
|
|
|
pass
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append("</section>\n")
|
2018-01-20 17:38:48 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append(
|
|
|
|
'<section class="c-content-win" id="c-content-%(id)s-win">\n' % {"id": uid}
|
|
|
|
)
|
|
|
|
win_text = node["win_console_text"]
|
|
|
|
highlight_args = {"force": True}
|
|
|
|
linenos = node.get("linenos", False)
|
2018-01-20 17:38:48 +00:00
|
|
|
|
|
|
|
def warner(msg):
|
|
|
|
self.builder.warn(msg, (self.builder.current_docname, node.line))
|
|
|
|
|
|
|
|
highlighted = self.highlighter.highlight_block(
|
2022-02-03 19:24:19 +00:00
|
|
|
win_text, "doscon", warn=warner, linenos=linenos, **highlight_args
|
2018-01-20 17:38:48 +00:00
|
|
|
)
|
|
|
|
self.body.append(highlighted)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.body.append("</section>\n")
|
|
|
|
self.body.append("</div>\n")
|
2018-01-20 17:38:48 +00:00
|
|
|
raise nodes.SkipNode
|
|
|
|
else:
|
|
|
|
self.visit_literal_block(node)
|
|
|
|
|
|
|
|
|
|
|
|
class ConsoleDirective(CodeBlock):
|
|
|
|
"""
|
|
|
|
A reStructuredText directive which renders a two-tab code block in which
|
|
|
|
the second tab shows a Windows command line equivalent of the usual
|
|
|
|
Unix-oriented examples.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
required_arguments = 0
|
|
|
|
# The 'doscon' Pygments formatter needs a prompt like this. '>' alone
|
|
|
|
# won't do it because then it simply paints the whole command line as a
|
2021-08-02 05:51:24 +00:00
|
|
|
# gray comment with no highlighting at all.
|
2022-02-03 19:24:19 +00:00
|
|
|
WIN_PROMPT = r"...\> "
|
2018-01-20 17:38:48 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
def args_to_win(cmdline):
|
|
|
|
changed = False
|
|
|
|
out = []
|
|
|
|
for token in cmdline.split():
|
2022-02-03 19:24:19 +00:00
|
|
|
if token[:2] == "./":
|
2018-01-20 17:38:48 +00:00
|
|
|
token = token[2:]
|
|
|
|
changed = True
|
2022-02-03 19:24:19 +00:00
|
|
|
elif token[:2] == "~/":
|
|
|
|
token = "%HOMEPATH%\\" + token[2:]
|
2018-01-20 17:38:48 +00:00
|
|
|
changed = True
|
2022-02-03 19:24:19 +00:00
|
|
|
elif token == "make":
|
|
|
|
token = "make.bat"
|
2018-03-07 21:19:22 +00:00
|
|
|
changed = True
|
2022-02-03 19:24:19 +00:00
|
|
|
if "://" not in token and "git" not in cmdline:
|
|
|
|
out.append(token.replace("/", "\\"))
|
2018-01-20 17:38:48 +00:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
out.append(token)
|
|
|
|
if changed:
|
2022-02-03 19:24:19 +00:00
|
|
|
return " ".join(out)
|
2018-01-20 17:38:48 +00:00
|
|
|
return cmdline
|
|
|
|
|
|
|
|
def cmdline_to_win(line):
|
2022-02-03 19:24:19 +00:00
|
|
|
if line.startswith("# "):
|
|
|
|
return "REM " + args_to_win(line[2:])
|
|
|
|
if line.startswith("$ # "):
|
|
|
|
return "REM " + args_to_win(line[4:])
|
|
|
|
if line.startswith("$ ./manage.py"):
|
|
|
|
return "manage.py " + args_to_win(line[13:])
|
|
|
|
if line.startswith("$ manage.py"):
|
|
|
|
return "manage.py " + args_to_win(line[11:])
|
|
|
|
if line.startswith("$ ./runtests.py"):
|
|
|
|
return "runtests.py " + args_to_win(line[15:])
|
|
|
|
if line.startswith("$ ./"):
|
2018-01-20 17:38:48 +00:00
|
|
|
return args_to_win(line[4:])
|
2022-02-03 19:24:19 +00:00
|
|
|
if line.startswith("$ python3"):
|
|
|
|
return "py " + args_to_win(line[9:])
|
|
|
|
if line.startswith("$ python"):
|
|
|
|
return "py " + args_to_win(line[8:])
|
|
|
|
if line.startswith("$ "):
|
2018-01-20 17:38:48 +00:00
|
|
|
return args_to_win(line[2:])
|
|
|
|
return None
|
|
|
|
|
|
|
|
def code_block_to_win(content):
|
|
|
|
bchanged = False
|
|
|
|
lines = []
|
|
|
|
for line in content:
|
|
|
|
modline = cmdline_to_win(line)
|
|
|
|
if modline is None:
|
|
|
|
lines.append(line)
|
|
|
|
else:
|
|
|
|
lines.append(self.WIN_PROMPT + modline)
|
|
|
|
bchanged = True
|
|
|
|
if bchanged:
|
|
|
|
return ViewList(lines)
|
|
|
|
return None
|
|
|
|
|
|
|
|
env = self.state.document.settings.env
|
2022-02-03 19:24:19 +00:00
|
|
|
self.arguments = ["console"]
|
2018-01-20 17:38:48 +00:00
|
|
|
lit_blk_obj = super().run()[0]
|
|
|
|
|
|
|
|
# Only do work when the djangohtml HTML Sphinx builder is being used,
|
|
|
|
# invoke the default behavior for the rest.
|
2022-02-03 19:24:19 +00:00
|
|
|
if env.app.builder.name not in ("djangohtml", "json"):
|
2018-01-20 17:38:48 +00:00
|
|
|
return [lit_blk_obj]
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
lit_blk_obj["uid"] = str(env.new_serialno("console"))
|
2018-01-20 17:38:48 +00:00
|
|
|
# Only add the tabbed UI if there is actually a Windows-specific
|
|
|
|
# version of the CLI example.
|
|
|
|
win_content = code_block_to_win(self.content)
|
|
|
|
if win_content is None:
|
2022-02-03 19:24:19 +00:00
|
|
|
lit_blk_obj["win_console_text"] = None
|
2018-01-20 17:38:48 +00:00
|
|
|
else:
|
|
|
|
self.content = win_content
|
2022-02-03 19:24:19 +00:00
|
|
|
lit_blk_obj["win_console_text"] = super().run()[0].rawsource
|
2018-01-20 17:38:48 +00:00
|
|
|
|
|
|
|
# Replace the literal_node object returned by Sphinx's CodeBlock with
|
|
|
|
# the ConsoleNode wrapper.
|
|
|
|
return [ConsoleNode(lit_blk_obj)]
|
|
|
|
|
|
|
|
|
|
|
|
def html_page_context_hook(app, pagename, templatename, context, doctree):
|
|
|
|
# Put a bool on the context used to render the template. It's used to
|
|
|
|
# control inclusion of console-tabs.css and activation of the JavaScript.
|
|
|
|
# This way it's include only from HTML files rendered from reST files where
|
|
|
|
# the ConsoleDirective is used.
|
2022-02-03 19:24:19 +00:00
|
|
|
context["include_console_assets"] = getattr(
|
|
|
|
doctree, "_console_directive_used_flag", False
|
|
|
|
)
|
2020-03-31 08:37:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def default_role_error(
|
|
|
|
name, rawtext, text, lineno, inliner, options=None, content=None
|
|
|
|
):
|
|
|
|
msg = (
|
2020-06-03 09:42:42 +00:00
|
|
|
"Default role used (`single backticks`): %s. Did you mean to use two "
|
2022-02-03 19:24:19 +00:00
|
|
|
"backticks for ``code``, or miss an underscore for a `link`_ ?" % rawtext
|
2020-03-31 08:37:38 +00:00
|
|
|
)
|
2020-06-03 09:42:42 +00:00
|
|
|
logger.warning(msg, location=(inliner.document.current_source, lineno))
|
|
|
|
return [nodes.Text(text)], []
|