Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions docs/render/glue.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ A cross-pasted `any` directive:
```{glue} var_text
:doc: orphaned_nb.ipynb
```

A cross-pasted `any` directive from an image:
Copy link
Member

Choose a reason for hiding this comment

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

The colours look weird for this in the rendering, and I have no idea why:

Screenshot 2025-07-02 at 12 42 02


```{glue} var_img
:doc: orphaned_nb.ipynb
```
````

- A cross-pasted `any` role: {glue}`orphaned_nb.ipynb::var_text`
Expand All @@ -432,6 +438,12 @@ A cross-pasted `any` directive:
:doc: orphaned_nb.ipynb
```

A cross-pasted `any` directive from an image:

```{glue} var_img
:doc: orphaned_nb.ipynb
```

+++

## Advanced use-cases
Expand Down
68 changes: 65 additions & 3 deletions docs/render/orphaned_nb.ipynb

Large diffs are not rendered by default.

52 changes: 50 additions & 2 deletions myst_nb/ext/glue/crossref.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

from __future__ import annotations

from binascii import a2b_base64
from functools import lru_cache
import hashlib
import json
from mimetypes import guess_extension
from pathlib import Path
from typing import Any, Sequence
import os

from docutils import nodes
from sphinx.builders import Builder
from sphinx.environment import BuildEnvironment
from sphinx.transforms.post_transforms import SphinxPostTransform
from sphinx.util import logging as sphinx_logging

Expand Down Expand Up @@ -69,7 +75,14 @@ def apply(self, **kwargs):
if node.gtype == "text":
_nodes = generate_text_nodes(node, output)
else:
_nodes = generate_any_nodes(node, output, priority_list)
print("asdasg", self.app.builder.imagedir)
Copy link
Member

Choose a reason for hiding this comment

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

is this print a debug remnant?

_nodes = generate_any_nodes(
node,
output,
priority_list,
self.app.builder,
self.env,
)

if _nodes:
node.replace_self(_nodes)
Expand All @@ -88,7 +101,11 @@ def ref_warning(msg: str, node) -> None:


def generate_any_nodes(
node: PendingGlueReference, output: dict[str, Any], priority_list: Sequence[str]
node: PendingGlueReference,
output: dict[str, Any],
priority_list: Sequence[str],
builder: Builder,
env: BuildEnvironment,
) -> list[nodes.Element]:
"""Generate nodes for a cell, according to the highest priority mime type."""
data = output["data"]
Expand All @@ -106,6 +123,37 @@ def generate_any_nodes(
text=data[mime_type], format="html", classes=["output", "text_html"]
)
]
if mime_type in {
"image/png",
"image/jpeg",
"image/gif",
"application/pdf",
}:
# this is mostly a copy of mystnb/core/render.py::render_image

# data is b64-encoded as text
data_bytes = a2b_base64(data[mime_type])

# create filename
extension = guess_extension(mime_type) or "." + mime_type.rsplit("/")[-1]
# latex does not recognize the '.jpe' extension
extension = ".jpeg" if extension == ".jpe" else extension

data_hash = hashlib.sha256(data_bytes).hexdigest()
filename = f"{data_hash}{extension}"

# now we resolve the relative path to the image
imgdir = Path(builder.imagedir)
outdir = Path(builder.outdir)
page_dir = (outdir / env.docname).parent
filepath = outdir / imgdir / filename

uri = os.path.relpath(filepath, page_dir)
image_node = nodes.image(uri=uri)
image_node["candidates"] = {"*": uri}

return [image_node]

ref_warning(
f"No allowed mime type found in {node.key!r}: {list(output['data'])}", node
)
Expand Down