Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP, ENH: Add alt option to glue figure #394

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/use/glue.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,13 @@ For example, the following: ``My rounded mean: {glue:text}`boot_mean:.2f` `` wil
### The `glue:figure` directive

With `glue:figure` you can apply more formatting to figure like objects,
such as giving them a caption and referencable label:
such as giving them a caption, referencable label, and alt text:

````md
```{glue:figure} boot_fig
:figwidth: 300px
:name: "fig-boot"
:alt: "A histogram of the bootstrapped samples."

This is a **caption**, with an embedded `{glue:text}` element: {glue:text}`boot_mean:.2f`!
```
Expand All @@ -246,6 +247,7 @@ This is a **caption**, with an embedded `{glue:text}` element: {glue:text}`boot_
```{glue:figure} boot_fig
:figwidth: 300px
:name: "fig-boot"
:alt: "A histogram of the bootstrapped samples."

This is a **caption**, with an embedded `{glue:text}` element: {glue:text}`boot_mean:.2f`!
```
Expand Down
4 changes: 4 additions & 0 deletions myst_nb/nb_glue/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ def figwidth_value(argument):
option_spec["figclass"] = directives.class_option
option_spec["align"] = align
option_spec["name"] = directives.unchanged
option_spec["alt"] = directives.unchanged
has_content = True

def run(self):
figwidth = self.options.pop("figwidth", None)
figclasses = self.options.pop("figclass", None)
align = self.options.pop("align", None)
alt = self.options.pop("alt", None)
# On the Paste node we should add an attribute to specify that only image
# type mimedata is allowed, then this would be used by
# PasteNodesToDocutils -> CellOutputsToNodes to alter the render priority
Expand All @@ -192,6 +194,8 @@ def run(self):
figure_node["classes"] += figclasses
if align:
figure_node["align"] = align
if alt:
figure_node["alt"] = alt
self.add_name(figure_node)
# note: this is copied directly from sphinx.Figure
if self.content:
Expand Down
1 change: 1 addition & 0 deletions tests/notebooks/with_glue.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
"\n",
"```{glue:figure} key_plt\n",
":name: abc\n",
":alt: alt-text for glued figure\n",
"\n",
"A caption....\n",
"```",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_glue/test_parser.sphinx3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<inline classes="pasted-text">
undisplayed
inline…
<figure align="default" ids="abc" names="abc">
<figure align="default" ids="abc" names="abc" alt="alt-text for glued figure">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From looking at the output html, it seems to be that the alt should actually be on the image tag two lines below, not the figure. The trick is coming up with a consistent way to pass that information through the various containers/transforms.

<CellOutputNode classes="cell_output">
<image candidates="{'*': '_build/jupyter_execute/with_glue_5_0.png'}" uri="_build/jupyter_execute/with_glue_5_0.png">
<caption>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_glue/test_parser.sphinx4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<inline classes="pasted-text">
undisplayed
inline…
<figure ids="abc" names="abc">
<figure alt="alt-text for glued figure" ids="abc" names="abc">
<CellOutputNode classes="cell_output">
<image candidates="{'*': '_build/jupyter_execute/with_glue_5_0.png'}" uri="_build/jupyter_execute/with_glue_5_0.png">
<caption>
Expand Down