Skip to content

Commit 8369197

Browse files
authored
docs: add sphinx-copybutton and sphinx-codeautolink (#223)
## Summary Brings SMTB's docs config in line with RTB and MVTB, which both already have these two extensions: - `sphinx-copybutton`: adds a copy button to code blocks, stripping interactive `>>> `/`... `/`$ ` prompts so copied code actually runs. - `sphinx-codeautolink`: turns code identifiers in examples into links to their API docs. Config (extensions list, `suppress_warnings` for codeautolink's own match warnings, `copybutton_prompt_text`/etc., `codeautolink_custom_blocks`/`codeautolink_search_css_classes`) mirrors RTB's `conf.py` exactly. Also fixes a small pre-existing formatting wart in `intro.rst`: two `.. code-block:: python` blocks had `:linenos:` indented 3 spaces against 4-space code content. Plain docutils tolerated this silently, but `sphinx-codeautolink`'s own parsing pass over code blocks is stricter and surfaced it as a new "unexpected indent" warning -- fixed rather than left as a regression. ## Test plan - [x] Real `sphinx-build -b html`, isolated venv, against both master and this branch - [x] Warning count identical before/after: 14 warnings both times, same content
1 parent a77c7f0 commit 8369197

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎docs/source/conf.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# sys.path.insert(0, os.path.abspath('.'))
1616
# sys.path.insert(0, os.path.abspath('..'))
1717

18+
from sphinx_codeautolink import clean_ipython, clean_pycon
1819

1920
# -- Project information -----------------------------------------------------
2021

@@ -50,6 +51,14 @@
5051
"sphinx_pyrunblock",
5152
"sphinx.ext.intersphinx",
5253
"sphinx_favicon",
54+
"sphinx_copybutton",
55+
"sphinx_codeautolink",
56+
]
57+
58+
suppress_warnings = [
59+
"codeautolink.match_block",
60+
"codeautolink.match_name",
61+
"config.cache", # codeautolink_custom_blocks holds function refs, not picklable
5362
]
5463
#'sphinx.ext.autosummary',
5564
# typehints_use_signature_return = True
@@ -184,6 +193,24 @@
184193
np.set_printoptions(precision=4, suppress=True)
185194
"""
186195

196+
# -------- sphinx-codeautolink options --------------------------------------------#
197+
198+
codeautolink_custom_blocks = {
199+
"pycon": clean_pycon,
200+
"ipython": clean_ipython,
201+
"ipython3": clean_ipython,
202+
}
203+
# Ensure pycon (Python console) blocks are included in the autolink search.
204+
codeautolink_search_css_classes = ["highlight-python", "highlight-pycon"]
205+
206+
# -------- sphinx-copybutton options ----------------------------------------------#
207+
# Strip interactive prompts (Python and shell) when users copy code snippets.
208+
209+
copybutton_prompt_text = r">>> |\.\.\. |\$ "
210+
copybutton_prompt_is_regexp = True
211+
copybutton_only_copy_prompt_lines = False
212+
copybutton_remove_prompts = True
213+
187214
intersphinx_mapping = {
188215
"numpy": ("http://docs.scipy.org/doc/numpy/", None),
189216
"scipy": ("http://docs.scipy.org/doc/scipy/reference/", None),

‎docs/source/intro.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ Graphics
629629
If ``matplotlib`` is installed then we can add 2D coordinate frames to a figure in a variety of styles:
630630

631631
.. code-block:: python
632-
:linenos:
632+
:linenos:
633633
634634
>>> trplot2( transl2(1,2), frame='A', rviz=True, width=1)
635635
>>> trplot2( transl2(3,1), color='red', arrow=True, width=3, frame='B')
@@ -646,7 +646,7 @@ If a figure does not yet exist one is added. If a figure exists but there is no
646646
Similarly, we can plot 3D coordinate frames in a variety of styles:
647647

648648
.. code-block:: python
649-
:linenos:
649+
:linenos:
650650
651651
>>> trplot( transl(1,2,3), frame='A', rviz=True, width=1, dims=[0, 10, 0, 10, 0, 10])
652652
>>> trplot( transl(3,1, 2), color='red', width=3, frame='B')

‎pyproject.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ docs = [
6565
"sphinxcontrib-jsmath",
6666
"sphinx-favicon",
6767
"sphinx-autodoc-typehints",
68+
"sphinx-copybutton",
69+
"sphinx-codeautolink",
6870
]
6971

7072
ros-humble = [

0 commit comments

Comments
 (0)