1
0
mirror of https://github.com/django/django.git synced 2025-01-03 06:55:47 +00:00

Fixed #27409 -- Made admindocs support custom link text in docstrings.

This commit is contained in:
sai-ganesh-03 2024-11-03 11:03:48 +05:30 committed by Sarah Boyce
parent 78c9a27031
commit c2c544cf01
5 changed files with 60 additions and 3 deletions

View File

@ -99,6 +99,21 @@ ROLES = {
"tag": "%s/tags/#%s", "tag": "%s/tags/#%s",
} }
explicit_title_re = re.compile(r"^(.+?)\s*(?<!\x00)<([^<]*?)>$", re.DOTALL)
def split_explicit_title(text):
"""
Split role content into title and target, if given.
From sphinx.util.nodes.split_explicit_title
See https://github.com/sphinx-doc/sphinx/blob/230ccf2/sphinx/util/nodes.py#L389
"""
match = explicit_title_re.match(text)
if match:
return True, match.group(1), match.group(2)
return False, text, text
def create_reference_role(rolename, urlbase): def create_reference_role(rolename, urlbase):
# Views and template names are case-sensitive. # Views and template names are case-sensitive.
@ -107,14 +122,15 @@ def create_reference_role(rolename, urlbase):
def _role(name, rawtext, text, lineno, inliner, options=None, content=None): def _role(name, rawtext, text, lineno, inliner, options=None, content=None):
if options is None: if options is None:
options = {} options = {}
_, title, target = split_explicit_title(text)
node = docutils.nodes.reference( node = docutils.nodes.reference(
rawtext, rawtext,
text, title,
refuri=( refuri=(
urlbase urlbase
% ( % (
inliner.document.settings.link_base, inliner.document.settings.link_base,
text if is_case_sensitive else text.lower(), target if is_case_sensitive else target.lower(),
) )
), ),
**options, **options,

View File

@ -31,6 +31,8 @@ Once those steps are complete, you can start browsing the documentation by
going to your admin interface and clicking the "Documentation" link in the going to your admin interface and clicking the "Documentation" link in the
upper right of the page. upper right of the page.
.. _admindocs-helpers:
Documentation helpers Documentation helpers
===================== =====================
@ -47,6 +49,13 @@ Template filters ``:filter:`filtername```
Templates ``:template:`path/to/template.html``` Templates ``:template:`path/to/template.html```
================= ======================= ================= =======================
Each of these support custom link text with the format
``:role:`link text <link>```. For example, ``:tag:`block <built_in-block>```.
.. versionchanged:: 5.2
Support for custom link text was added.
Model reference Model reference
=============== ===============

View File

@ -44,7 +44,10 @@ Minor features
:mod:`django.contrib.admindocs` :mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ... * Links to components in docstrings now supports custom link text, using the
format ``:role:`link text <link>```. See :ref:`documentation helpers
<admindocs-helpers>` for more details.
:mod:`django.contrib.auth` :mod:`django.contrib.auth`
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -15,6 +15,16 @@ class Group(models.Model):
class Family(models.Model): class Family(models.Model):
"""
Links with different link text.
This is a line with tag :tag:`extends <built_in-extends>`
This is a line with model :model:`Family <myapp.Family>`
This is a line with view :view:`Index <myapp.views.Index>`
This is a line with template :template:`index template <Index.html>`
This is a line with filter :filter:`example filter <filtername>`
"""
last_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200)

View File

@ -441,6 +441,25 @@ class TestModelDetailView(TestDataMixin, AdminDocsTestCase):
self.assertContains(self.response, body, html=True) self.assertContains(self.response, body, html=True)
self.assertContains(self.response, model_body, html=True) self.assertContains(self.response, model_body, html=True)
def test_model_docstring_built_in_tag_links(self):
summary = "Links with different link text."
body = (
'<p>This is a line with tag <a class="reference external" '
'href="/admindocs/tags/#built_in-extends">extends</a>\n'
'This is a line with model <a class="reference external" '
'href="/admindocs/models/myapp.family/">Family</a>\n'
'This is a line with view <a class="reference external" '
'href="/admindocs/views/myapp.views.Index/">Index</a>\n'
'This is a line with template <a class="reference external" '
'href="/admindocs/templates/Index.html/">index template</a>\n'
'This is a line with filter <a class="reference external" '
'href="/admindocs/filters/#filtername">example filter</a></p>'
)
url = reverse("django-admindocs-models-detail", args=["admin_docs", "family"])
response = self.client.get(url)
self.assertContains(response, summary, html=True)
self.assertContains(response, body, html=True)
def test_model_detail_title(self): def test_model_detail_title(self):
self.assertContains(self.response, "<h1>admin_docs.Person</h1>", html=True) self.assertContains(self.response, "<h1>admin_docs.Person</h1>", html=True)