1. """
    
  2. Sphinx plugins for Django documentation.
    
  3. """
    
  4. import json
    
  5. import os
    
  6. import re
    
  7. 
    
  8. from docutils import nodes
    
  9. from docutils.parsers.rst import Directive
    
  10. from docutils.statemachine import ViewList
    
  11. from sphinx import addnodes
    
  12. from sphinx import version_info as sphinx_version
    
  13. from sphinx.builders.html import StandaloneHTMLBuilder
    
  14. from sphinx.directives.code import CodeBlock
    
  15. from sphinx.domains.std import Cmdoption
    
  16. from sphinx.errors import ExtensionError
    
  17. from sphinx.util import logging
    
  18. from sphinx.util.console import bold
    
  19. from sphinx.writers.html import HTMLTranslator
    
  20. 
    
  21. logger = logging.getLogger(__name__)
    
  22. # RE for option descriptions without a '--' prefix
    
  23. simple_option_desc_re = re.compile(r"([-_a-zA-Z0-9]+)(\s*.*?)(?=,\s+(?:/|-|--)|$)")
    
  24. 
    
  25. 
    
  26. def setup(app):
    
  27.     app.add_crossref_type(
    
  28.         directivename="setting",
    
  29.         rolename="setting",
    
  30.         indextemplate="pair: %s; setting",
    
  31.     )
    
  32.     app.add_crossref_type(
    
  33.         directivename="templatetag",
    
  34.         rolename="ttag",
    
  35.         indextemplate="pair: %s; template tag",
    
  36.     )
    
  37.     app.add_crossref_type(
    
  38.         directivename="templatefilter",
    
  39.         rolename="tfilter",
    
  40.         indextemplate="pair: %s; template filter",
    
  41.     )
    
  42.     app.add_crossref_type(
    
  43.         directivename="fieldlookup",
    
  44.         rolename="lookup",
    
  45.         indextemplate="pair: %s; field lookup type",
    
  46.     )
    
  47.     app.add_object_type(
    
  48.         directivename="django-admin",
    
  49.         rolename="djadmin",
    
  50.         indextemplate="pair: %s; django-admin command",
    
  51.         parse_node=parse_django_admin_node,
    
  52.     )
    
  53.     app.add_directive("django-admin-option", Cmdoption)
    
  54.     app.add_config_value("django_next_version", "0.0", True)
    
  55.     app.add_directive("versionadded", VersionDirective)
    
  56.     app.add_directive("versionchanged", VersionDirective)
    
  57.     app.add_builder(DjangoStandaloneHTMLBuilder)
    
  58.     app.set_translator("djangohtml", DjangoHTMLTranslator)
    
  59.     app.set_translator("json", DjangoHTMLTranslator)
    
  60.     app.add_node(
    
  61.         ConsoleNode,
    
  62.         html=(visit_console_html, None),
    
  63.         latex=(visit_console_dummy, depart_console_dummy),
    
  64.         man=(visit_console_dummy, depart_console_dummy),
    
  65.         text=(visit_console_dummy, depart_console_dummy),
    
  66.         texinfo=(visit_console_dummy, depart_console_dummy),
    
  67.     )
    
  68.     app.add_directive("console", ConsoleDirective)
    
  69.     app.connect("html-page-context", html_page_context_hook)
    
  70.     app.add_role("default-role-error", default_role_error)
    
  71.     return {"parallel_read_safe": True}
    
  72. 
    
  73. 
    
  74. class VersionDirective(Directive):
    
  75.     has_content = True
    
  76.     required_arguments = 1
    
  77.     optional_arguments = 1
    
  78.     final_argument_whitespace = True
    
  79.     option_spec = {}
    
  80. 
    
  81.     def run(self):
    
  82.         if len(self.arguments) > 1:
    
  83.             msg = """Only one argument accepted for directive '{directive_name}::'.
    
  84.             Comments should be provided as content,
    
  85.             not as an extra argument.""".format(
    
  86.                 directive_name=self.name
    
  87.             )
    
  88.             raise self.error(msg)
    
  89. 
    
  90.         env = self.state.document.settings.env
    
  91.         ret = []
    
  92.         node = addnodes.versionmodified()
    
  93.         ret.append(node)
    
  94. 
    
  95.         if self.arguments[0] == env.config.django_next_version:
    
  96.             node["version"] = "Development version"
    
  97.         else:
    
  98.             node["version"] = self.arguments[0]
    
  99. 
    
  100.         node["type"] = self.name
    
  101.         if self.content:
    
  102.             self.state.nested_parse(self.content, self.content_offset, node)
    
  103.         try:
    
  104.             env.get_domain("changeset").note_changeset(node)
    
  105.         except ExtensionError:
    
  106.             # Sphinx < 1.8: Domain 'changeset' is not registered
    
  107.             env.note_versionchange(node["type"], node["version"], node, self.lineno)
    
  108.         return ret
    
  109. 
    
  110. 
    
  111. class DjangoHTMLTranslator(HTMLTranslator):
    
  112.     """
    
  113.     Django-specific reST to HTML tweaks.
    
  114.     """
    
  115. 
    
  116.     # Don't use border=1, which docutils does by default.
    
  117.     def visit_table(self, node):
    
  118.         self.context.append(self.compact_p)
    
  119.         self.compact_p = True
    
  120.         # Needed by Sphinx.
    
  121.         if sphinx_version >= (4, 3):
    
  122.             self._table_row_indices.append(0)
    
  123.         else:
    
  124.             self._table_row_index = 0
    
  125.         self.body.append(self.starttag(node, "table", CLASS="docutils"))
    
  126. 
    
  127.     def depart_table(self, node):
    
  128.         self.compact_p = self.context.pop()
    
  129.         if sphinx_version >= (4, 3):
    
  130.             self._table_row_indices.pop()
    
  131.         self.body.append("</table>\n")
    
  132. 
    
  133.     def visit_desc_parameterlist(self, node):
    
  134.         self.body.append("(")  # by default sphinx puts <big> around the "("
    
  135.         self.optional_param_level = 0
    
  136.         self.param_separator = node.child_text_separator
    
  137.         # Counts 'parameter groups' being either a required parameter, or a set
    
  138.         # of contiguous optional ones.
    
  139.         required_params = [
    
  140.             isinstance(c, addnodes.desc_parameter) for c in node.children
    
  141.         ]
    
  142.         # How many required parameters are left.
    
  143.         self.required_params_left = sum(required_params)
    
  144.         if sphinx_version < (7, 1):
    
  145.             self.first_param = 1
    
  146.         else:
    
  147.             self.is_first_param = True
    
  148.             self.params_left_at_level = 0
    
  149.             self.param_group_index = 0
    
  150.             self.list_is_required_param = required_params
    
  151.             self.multi_line_parameter_list = False
    
  152. 
    
  153.     def depart_desc_parameterlist(self, node):
    
  154.         self.body.append(")")
    
  155. 
    
  156.     #
    
  157.     # Turn the "new in version" stuff (versionadded/versionchanged) into a
    
  158.     # better callout -- the Sphinx default is just a little span,
    
  159.     # which is a bit less obvious that I'd like.
    
  160.     #
    
  161.     # FIXME: these messages are all hardcoded in English. We need to change
    
  162.     # that to accommodate other language docs, but I can't work out how to make
    
  163.     # that work.
    
  164.     #
    
  165.     version_text = {
    
  166.         "versionchanged": "Changed in Django %s",
    
  167.         "versionadded": "New in Django %s",
    
  168.     }
    
  169. 
    
  170.     def visit_versionmodified(self, node):
    
  171.         self.body.append(self.starttag(node, "div", CLASS=node["type"]))
    
  172.         version_text = self.version_text.get(node["type"])
    
  173.         if version_text:
    
  174.             title = "%s%s" % (version_text % node["version"], ":" if len(node) else ".")
    
  175.             self.body.append('<span class="title">%s</span> ' % title)
    
  176. 
    
  177.     def depart_versionmodified(self, node):
    
  178.         self.body.append("</div>\n")
    
  179. 
    
  180.     # Give each section a unique ID -- nice for custom CSS hooks
    
  181.     def visit_section(self, node):
    
  182.         old_ids = node.get("ids", [])
    
  183.         node["ids"] = ["s-" + i for i in old_ids]
    
  184.         node["ids"].extend(old_ids)
    
  185.         super().visit_section(node)
    
  186.         node["ids"] = old_ids
    
  187. 
    
  188. 
    
  189. def parse_django_admin_node(env, sig, signode):
    
  190.     command = sig.split(" ")[0]
    
  191.     env.ref_context["std:program"] = command
    
  192.     title = "django-admin %s" % sig
    
  193.     signode += addnodes.desc_name(title, title)
    
  194.     return command
    
  195. 
    
  196. 
    
  197. class DjangoStandaloneHTMLBuilder(StandaloneHTMLBuilder):
    
  198.     """
    
  199.     Subclass to add some extra things we need.
    
  200.     """
    
  201. 
    
  202.     name = "djangohtml"
    
  203. 
    
  204.     def finish(self):
    
  205.         super().finish()
    
  206.         logger.info(bold("writing templatebuiltins.js..."))
    
  207.         xrefs = self.env.domaindata["std"]["objects"]
    
  208.         templatebuiltins = {
    
  209.             "ttags": [
    
  210.                 n
    
  211.                 for ((t, n), (k, a)) in xrefs.items()
    
  212.                 if t == "templatetag" and k == "ref/templates/builtins"
    
  213.             ],
    
  214.             "tfilters": [
    
  215.                 n
    
  216.                 for ((t, n), (k, a)) in xrefs.items()
    
  217.                 if t == "templatefilter" and k == "ref/templates/builtins"
    
  218.             ],
    
  219.         }
    
  220.         outfilename = os.path.join(self.outdir, "templatebuiltins.js")
    
  221.         with open(outfilename, "w") as fp:
    
  222.             fp.write("var django_template_builtins = ")
    
  223.             json.dump(templatebuiltins, fp)
    
  224.             fp.write(";\n")
    
  225. 
    
  226. 
    
  227. class ConsoleNode(nodes.literal_block):
    
  228.     """
    
  229.     Custom node to override the visit/depart event handlers at registration
    
  230.     time. Wrap a literal_block object and defer to it.
    
  231.     """
    
  232. 
    
  233.     tagname = "ConsoleNode"
    
  234. 
    
  235.     def __init__(self, litblk_obj):
    
  236.         self.wrapped = litblk_obj
    
  237. 
    
  238.     def __getattr__(self, attr):
    
  239.         if attr == "wrapped":
    
  240.             return self.__dict__.wrapped
    
  241.         return getattr(self.wrapped, attr)
    
  242. 
    
  243. 
    
  244. def visit_console_dummy(self, node):
    
  245.     """Defer to the corresponding parent's handler."""
    
  246.     self.visit_literal_block(node)
    
  247. 
    
  248. 
    
  249. def depart_console_dummy(self, node):
    
  250.     """Defer to the corresponding parent's handler."""
    
  251.     self.depart_literal_block(node)
    
  252. 
    
  253. 
    
  254. def visit_console_html(self, node):
    
  255.     """Generate HTML for the console directive."""
    
  256.     if self.builder.name in ("djangohtml", "json") and node["win_console_text"]:
    
  257.         # Put a mark on the document object signaling the fact the directive
    
  258.         # has been used on it.
    
  259.         self.document._console_directive_used_flag = True
    
  260.         uid = node["uid"]
    
  261.         self.body.append(
    
  262.             """\
    
  263. <div class="console-block" id="console-block-%(id)s">
    
  264. <input class="c-tab-unix" id="c-tab-%(id)s-unix" type="radio" name="console-%(id)s" \
    
  265. checked>
    
  266. <label for="c-tab-%(id)s-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
    
  267. <input class="c-tab-win" id="c-tab-%(id)s-win" type="radio" name="console-%(id)s">
    
  268. <label for="c-tab-%(id)s-win" title="Windows">&#xf17a</label>
    
  269. <section class="c-content-unix" id="c-content-%(id)s-unix">\n"""
    
  270.             % {"id": uid}
    
  271.         )
    
  272.         try:
    
  273.             self.visit_literal_block(node)
    
  274.         except nodes.SkipNode:
    
  275.             pass
    
  276.         self.body.append("</section>\n")
    
  277. 
    
  278.         self.body.append(
    
  279.             '<section class="c-content-win" id="c-content-%(id)s-win">\n' % {"id": uid}
    
  280.         )
    
  281.         win_text = node["win_console_text"]
    
  282.         highlight_args = {"force": True}
    
  283.         linenos = node.get("linenos", False)
    
  284. 
    
  285.         def warner(msg):
    
  286.             self.builder.warn(msg, (self.builder.current_docname, node.line))
    
  287. 
    
  288.         highlighted = self.highlighter.highlight_block(
    
  289.             win_text, "doscon", warn=warner, linenos=linenos, **highlight_args
    
  290.         )
    
  291.         self.body.append(highlighted)
    
  292.         self.body.append("</section>\n")
    
  293.         self.body.append("</div>\n")
    
  294.         raise nodes.SkipNode
    
  295.     else:
    
  296.         self.visit_literal_block(node)
    
  297. 
    
  298. 
    
  299. class ConsoleDirective(CodeBlock):
    
  300.     """
    
  301.     A reStructuredText directive which renders a two-tab code block in which
    
  302.     the second tab shows a Windows command line equivalent of the usual
    
  303.     Unix-oriented examples.
    
  304.     """
    
  305. 
    
  306.     required_arguments = 0
    
  307.     # The 'doscon' Pygments formatter needs a prompt like this. '>' alone
    
  308.     # won't do it because then it simply paints the whole command line as a
    
  309.     # gray comment with no highlighting at all.
    
  310.     WIN_PROMPT = r"...\> "
    
  311. 
    
  312.     def run(self):
    
  313.         def args_to_win(cmdline):
    
  314.             changed = False
    
  315.             out = []
    
  316.             for token in cmdline.split():
    
  317.                 if token[:2] == "./":
    
  318.                     token = token[2:]
    
  319.                     changed = True
    
  320.                 elif token[:2] == "~/":
    
  321.                     token = "%HOMEPATH%\\" + token[2:]
    
  322.                     changed = True
    
  323.                 elif token == "make":
    
  324.                     token = "make.bat"
    
  325.                     changed = True
    
  326.                 if "://" not in token and "git" not in cmdline:
    
  327.                     out.append(token.replace("/", "\\"))
    
  328.                     changed = True
    
  329.                 else:
    
  330.                     out.append(token)
    
  331.             if changed:
    
  332.                 return " ".join(out)
    
  333.             return cmdline
    
  334. 
    
  335.         def cmdline_to_win(line):
    
  336.             if line.startswith("# "):
    
  337.                 return "REM " + args_to_win(line[2:])
    
  338.             if line.startswith("$ # "):
    
  339.                 return "REM " + args_to_win(line[4:])
    
  340.             if line.startswith("$ ./manage.py"):
    
  341.                 return "manage.py " + args_to_win(line[13:])
    
  342.             if line.startswith("$ manage.py"):
    
  343.                 return "manage.py " + args_to_win(line[11:])
    
  344.             if line.startswith("$ ./runtests.py"):
    
  345.                 return "runtests.py " + args_to_win(line[15:])
    
  346.             if line.startswith("$ ./"):
    
  347.                 return args_to_win(line[4:])
    
  348.             if line.startswith("$ python3"):
    
  349.                 return "py " + args_to_win(line[9:])
    
  350.             if line.startswith("$ python"):
    
  351.                 return "py " + args_to_win(line[8:])
    
  352.             if line.startswith("$ "):
    
  353.                 return args_to_win(line[2:])
    
  354.             return None
    
  355. 
    
  356.         def code_block_to_win(content):
    
  357.             bchanged = False
    
  358.             lines = []
    
  359.             for line in content:
    
  360.                 modline = cmdline_to_win(line)
    
  361.                 if modline is None:
    
  362.                     lines.append(line)
    
  363.                 else:
    
  364.                     lines.append(self.WIN_PROMPT + modline)
    
  365.                     bchanged = True
    
  366.             if bchanged:
    
  367.                 return ViewList(lines)
    
  368.             return None
    
  369. 
    
  370.         env = self.state.document.settings.env
    
  371.         self.arguments = ["console"]
    
  372.         lit_blk_obj = super().run()[0]
    
  373. 
    
  374.         # Only do work when the djangohtml HTML Sphinx builder is being used,
    
  375.         # invoke the default behavior for the rest.
    
  376.         if env.app.builder.name not in ("djangohtml", "json"):
    
  377.             return [lit_blk_obj]
    
  378. 
    
  379.         lit_blk_obj["uid"] = str(env.new_serialno("console"))
    
  380.         # Only add the tabbed UI if there is actually a Windows-specific
    
  381.         # version of the CLI example.
    
  382.         win_content = code_block_to_win(self.content)
    
  383.         if win_content is None:
    
  384.             lit_blk_obj["win_console_text"] = None
    
  385.         else:
    
  386.             self.content = win_content
    
  387.             lit_blk_obj["win_console_text"] = super().run()[0].rawsource
    
  388. 
    
  389.         # Replace the literal_node object returned by Sphinx's CodeBlock with
    
  390.         # the ConsoleNode wrapper.
    
  391.         return [ConsoleNode(lit_blk_obj)]
    
  392. 
    
  393. 
    
  394. def html_page_context_hook(app, pagename, templatename, context, doctree):
    
  395.     # Put a bool on the context used to render the template. It's used to
    
  396.     # control inclusion of console-tabs.css and activation of the JavaScript.
    
  397.     # This way it's include only from HTML files rendered from reST files where
    
  398.     # the ConsoleDirective is used.
    
  399.     context["include_console_assets"] = getattr(
    
  400.         doctree, "_console_directive_used_flag", False
    
  401.     )
    
  402. 
    
  403. 
    
  404. def default_role_error(
    
  405.     name, rawtext, text, lineno, inliner, options=None, content=None
    
  406. ):
    
  407.     msg = (
    
  408.         "Default role used (`single backticks`): %s. Did you mean to use two "
    
  409.         "backticks for ``code``, or miss an underscore for a `link`_ ?" % rawtext
    
  410.     )
    
  411.     logger.warning(msg, location=(inliner.document.current_source, lineno))
    
  412.     return [nodes.Text(text)], []