Skip to content

Commit b1c37d5

Browse files
authored
Add HTTP resources to TOC (#80)
* Add HTTP resources to TOC * Add option to directive and update docs * Update changelog * Update changelog
1 parent 8fcfb8e commit b1c37d5

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Major changes
2222
- Added support for Python 3.10 up to 3.14.[:pull:`85` by @stevepiercy]
2323
- Adjusted a unit test regular expression for :file:`bottle_test.py`. [:pull:`85` by @stevepiercy]
2424
- Use MDN documentation for information about HTTP status codes instead of ``w3.org``. [:pull:`78` by @jamesrobson-secondmind]
25+
- Added ``:addtoc:`` option for ``http`` directives to register HTTP API endpoints and display them in the page-level table of contents. [:pull:`80` by @sevdog]
2526

2627

2728
Internal

docs/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ Additionally, you may specify custom options to the directives:
313313
.. http:get:: /users/(int:user_id)/posts/(tag)
314314
:synopsis: Returns posts by the specified tag for the user
315315

316+
``addtoc``
317+
Adds HTTP API endpoint elements to the page-level table of contents.
318+
319+
.. sourcecode:: rst
320+
321+
.. http:get:: /users/(int:user_id)/posts/(tag)
322+
:addtoc:
323+
324+
.. versionadded:: 2.0.0
316325

317326
.. _resource-fields:
318327

src/sphinxcontrib/httpdomain/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ class HTTPResource(ObjectDescription):
305305
option_spec = {
306306
'deprecated': directives.flag,
307307
'noindex': directives.flag,
308+
'addtoc': directives.flag,
308309
'synopsis': lambda x: x,
309310
}
310311

@@ -351,6 +352,25 @@ def add_target_and_index(self, name_cls, sig, signode):
351352
def get_index_text(self, modname, name):
352353
return ''
353354

355+
def _object_hierarchy_parts(self, sig_node):
356+
if 'addtoc' in self.options:
357+
if 'fullname' not in sig_node:
358+
return ()
359+
path = sig_node.get('path')
360+
method = sig_node.get('method')
361+
if not path or not sig_node:
362+
return ()
363+
return tuple(path.split('/')) + (method, sig_node['fullname'])
364+
return ()
365+
366+
def _toc_entry_name(self, sig_node):
367+
if 'addtoc' in self.options:
368+
if not sig_node.get('_toc_parts'):
369+
return ''
370+
371+
return sig_node['_toc_parts'][-1]
372+
return ''
373+
354374

355375
class HTTPOptions(HTTPResource):
356376

test/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ Options
7979
:deprecated:
8080
:synopsis: Something special, but use PUT instead
8181

82+
.. http:options:: /baz
83+
:addtoc:
84+
8285
Roles
8386
~~~~~
8487

0 commit comments

Comments
 (0)