Skip to content

Commit 1a6b0c2

Browse files
authored
Custom "Example reference"-blocks for examples with manim directive (#607)
* add seealso options to manim directive * illustrate use of seealso_classes * remove unneeded import * rename: seealso -> ref * custom styling * change to ref_classes in example
1 parent 780aa1a commit 1a6b0c2

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

docs/source/_static/custom.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ img {
2828
padding: 14px 0 14px 20px;
2929
margin: 20px 0;
3030
}
31+
32+
div.example-reference {
33+
background-color: #dff0d8;
34+
border: 1px solid #3c763d;
35+
}

docs/source/examples/advanced_projects.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Advanced Projects
22
=================================
33

44
.. manim:: OpeningManimExample
5-
:quality: low
5+
:ref_classes: Tex MathTex NumberPlane
66

77
class OpeningManimExample(Scene):
88
def construct(self):
@@ -245,7 +245,7 @@ Advanced Projects
245245
.. manim:: ExampleSineCurve
246246

247247
class ExampleSineCurve(Scene):
248-
# contributed by heejin_park, https://infograph.tistory.com/230
248+
# contributed by heejin_park, https://infograph.tistory.com/230
249249
def construct(self):
250250
self.show_axis()
251251
self.show_circle()

docs/source/manim_directive.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def construct(self):
2121
render scenes that are defined within doctests, for example::
2222
2323
.. manim:: DirectiveDoctestExample
24+
:ref_classes: Dot
2425
2526
>>> dot = Dot(color=RED)
2627
>>> dot.color
@@ -58,19 +59,44 @@ def construct(self):
5859
an image representing the last frame of the scene will
5960
be rendered and displayed, instead of a video.
6061
62+
ref_classes
63+
A list of classes, separated by spaces, that is
64+
rendered in a reference block after the source code.
65+
66+
ref_functions
67+
A list of functions and methods, separated by spaces,
68+
that is rendered in a reference block after the source code.
69+
6170
"""
6271
from docutils.parsers.rst import directives, Directive
63-
from docutils.parsers.rst.directives.images import Image
6472

6573
import jinja2
6674
import os
6775
from os.path import relpath
76+
from typing import List
6877

6978
import shutil
7079

7180
classnamedict = {}
7281

7382

83+
def process_name_list(option_input: str, reference_type: str) -> List[str]:
84+
r"""Reformats a string of space separated class names
85+
as a list of strings containing valid Sphinx references.
86+
87+
TESTS
88+
-----
89+
90+
::
91+
92+
>>> process_name_list("Tex TexTemplate", "class")
93+
[":class:`~.Tex`", ":class:`~.TexTemplate`"]
94+
>>> process_name_list("Scene.play Mobject.rotate", "func")
95+
[":func:`~.Scene.play`", ":func:`~.Mobject.rotate`"]
96+
"""
97+
return [f":{reference_type}:`~.{name}`" for name in option_input.split()]
98+
99+
74100
class ManimDirective(Directive):
75101
r"""The manim directive, rendering videos while building
76102
the documentation.
@@ -87,6 +113,8 @@ class ManimDirective(Directive):
87113
),
88114
"save_as_gif": bool,
89115
"save_last_frame": bool,
116+
"ref_classes": lambda arg: process_name_list(arg, "class"),
117+
"ref_functions": lambda arg: process_name_list(arg, "func"),
90118
}
91119
final_argument_whitespace = True
92120

@@ -105,6 +133,17 @@ def run(self):
105133
save_as_gif = "save_as_gif" in self.options
106134
save_last_frame = "save_last_frame" in self.options
107135
assert not (save_as_gif and save_last_frame)
136+
if "ref_classes" in self.options or "ref_functions" in self.options:
137+
ref_classes = self.options.get("ref_classes", [])
138+
ref_functions = self.options.get("ref_functions", [])
139+
ref_content = ref_classes + ref_functions
140+
ref_block = f"""
141+
.. admonition:: Example References
142+
:class: example-reference
143+
144+
{' '.join(ref_content)}"""
145+
else:
146+
ref_block = ""
108147

109148
frame_rate = 30
110149
pixel_height = 480
@@ -218,6 +257,7 @@ def run(self):
218257
save_last_frame=save_last_frame,
219258
save_as_gif=save_as_gif,
220259
source_block=source_block,
260+
ref_block=ref_block,
221261
)
222262
state_machine.insert_input(
223263
rendered_template.split("\n"), source=document.attributes["source"]
@@ -245,6 +285,7 @@ def setup(app):
245285
<div class="manim-example">
246286
247287
{{ source_block }}
288+
{{ ref_block }}
248289
{% endif %}
249290
250291
{% if not (save_as_gif or save_last_frame) %}

0 commit comments

Comments
 (0)