@@ -21,6 +21,7 @@ def construct(self):
21
21
render scenes that are defined within doctests, for example::
22
22
23
23
.. manim:: DirectiveDoctestExample
24
+ :ref_classes: Dot
24
25
25
26
>>> dot = Dot(color=RED)
26
27
>>> dot.color
@@ -58,19 +59,44 @@ def construct(self):
58
59
an image representing the last frame of the scene will
59
60
be rendered and displayed, instead of a video.
60
61
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
+
61
70
"""
62
71
from docutils .parsers .rst import directives , Directive
63
- from docutils .parsers .rst .directives .images import Image
64
72
65
73
import jinja2
66
74
import os
67
75
from os .path import relpath
76
+ from typing import List
68
77
69
78
import shutil
70
79
71
80
classnamedict = {}
72
81
73
82
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
+
74
100
class ManimDirective (Directive ):
75
101
r"""The manim directive, rendering videos while building
76
102
the documentation.
@@ -87,6 +113,8 @@ class ManimDirective(Directive):
87
113
),
88
114
"save_as_gif" : bool ,
89
115
"save_last_frame" : bool ,
116
+ "ref_classes" : lambda arg : process_name_list (arg , "class" ),
117
+ "ref_functions" : lambda arg : process_name_list (arg , "func" ),
90
118
}
91
119
final_argument_whitespace = True
92
120
@@ -105,6 +133,17 @@ def run(self):
105
133
save_as_gif = "save_as_gif" in self .options
106
134
save_last_frame = "save_last_frame" in self .options
107
135
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 = ""
108
147
109
148
frame_rate = 30
110
149
pixel_height = 480
@@ -218,6 +257,7 @@ def run(self):
218
257
save_last_frame = save_last_frame ,
219
258
save_as_gif = save_as_gif ,
220
259
source_block = source_block ,
260
+ ref_block = ref_block ,
221
261
)
222
262
state_machine .insert_input (
223
263
rendered_template .split ("\n " ), source = document .attributes ["source" ]
@@ -245,6 +285,7 @@ def setup(app):
245
285
<div class="manim-example">
246
286
247
287
{{ source_block }}
288
+ {{ ref_block }}
248
289
{% endif %}
249
290
250
291
{% if not (save_as_gif or save_last_frame) %}
0 commit comments