Skip to content

Commit 09d4c51

Browse files
committed
added image_* directive options
1 parent 7454249 commit 09d4c51

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,21 @@ Example of directive options:
314314
plt.plot(np.random.randn(100))
315315
plt.title('plotted in repl-quiet')
316316
plt.show()
317+
318+
Image Options
319+
^^^^^^^^^^^^^
320+
321+
All of the options of the ``image`` directive, except for ``target`` (since target
322+
image is generated by the REPL process). Currently, these options applies to the
323+
Matplotlib figure images.
324+
325+
================== ===========
326+
Directive Description
327+
================== ===========
328+
``:image_alt:`` Alternate text: a short description of the image
329+
``:image_height:`` The desired height of the image
330+
``:image_width:`` The width of the image
331+
``:image_scale:`` The uniform scaling factor of the image
332+
``:image_align:`` The alignment of the image
333+
``:image_class:`` Set a "classes" attribute value on the doctree element generated by the directive
334+
================== ===========

src/sphinxcontrib/repl/__init__.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ def kill_all(*_):
247247
repl_procs.clear()
248248

249249

250-
def create_image(document, line):
251-
# TODO : support image options
250+
def create_image(document, line, image_options):
252251
imgpath = line[10:]
253252
confdir = document.settings.env.app.confdir # source root
254253
rst_file = document.attributes["source"] # source file path
@@ -258,14 +257,15 @@ def create_image(document, line):
258257
)
259258
img_relpath = os.path.relpath(imgpath, rst_outdir)
260259
uri = directives.uri(img_relpath.replace("\\", "/"))
261-
return nodes.image(line, uri=uri)
260+
return nodes.image(line, uri=uri, **image_options)
262261

263262

264-
def create_mpl_container_node(document, lines):
263+
def create_mpl_container_node(document, lines, options):
264+
image_options = {k[6:]: v for k, v in options.items() if k.startswith("image_")}
265265
return nodes.container(
266266
"",
267267
*(
268-
create_image(document, line)
268+
create_image(document, line, image_options)
269269
for line in lines
270270
if line.startswith("#repl:img:")
271271
),
@@ -368,6 +368,7 @@ class REPL(Directive):
368368
"hide-input": _option_boolean,
369369
"hide-output": _option_boolean,
370370
**create_mpl_option_spec(),
371+
**create_image_option_spec(),
371372
}
372373

373374
def run(self):
@@ -403,7 +404,9 @@ def run(self):
403404
def to_node(block):
404405
if block[0].startswith("#repl:img:"):
405406
# generated new image
406-
return create_mpl_container_node(self.state_machine.document, lines)
407+
return create_mpl_container_node(
408+
self.state_machine.document, lines, self.options
409+
)
407410
else:
408411
s = "\n".join(block)
409412
return nodes.doctest_block(s, s, language="python")
@@ -415,7 +418,7 @@ class REPL_Quiet(Directive):
415418
has_content = True
416419
required_arguments = 0
417420
optional_arguments = 0
418-
option_spec = create_mpl_option_spec()
421+
option_spec = {**create_mpl_option_spec(), **create_image_option_spec()}
419422

420423
def run(self):
421424
# dump the content on REPL & ignore what's printed on the interpreter
@@ -430,7 +433,9 @@ def run(self):
430433
lines = proc.communicate(self.content, show_input=False, show_output=False)
431434

432435
# only return the image lines
433-
return [create_mpl_container_node(self.state_machine.document, lines)]
436+
return [
437+
create_mpl_container_node(self.state_machine.document, lines, self.options)
438+
]
434439

435440

436441
def mpl_init(app, config):

tests/roots/test-root/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ With ``repl-quiet`` directive, only the plotted figures are shown:
5454
:mpl_pad_inches: 0.1
5555
:mpl_transparent: False
5656
:mpl_rc_params: {"lines.marker": "x", "lines.markersize": 3}
57+
:image_width: 50%
5758

5859
plt.plot(np.random.randn(100))
5960
plt.title('plotted in repl-quiet')

0 commit comments

Comments
 (0)