-
Notifications
You must be signed in to change notification settings - Fork 91
ENH: Add handling of image based PendingGlueReference in generate_any_nodes
#675
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea2fa73
handling of pending image glue
StFroese 6cada8f
an orphaned image
StFroese 5c9852e
display cross-ref orphaned img
StFroese e95d40d
Merge branch 'master' into glue_fig_pending
bsipocz 44abaec
Merge branch 'master' into glue_fig_pending
bsipocz fb5a06d
MAINT: remove debug line
bsipocz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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) | ||
|
||
| _nodes = generate_any_nodes( | ||
| node, | ||
| output, | ||
| priority_list, | ||
| self.app.builder, | ||
| self.env, | ||
| ) | ||
|
|
||
| if _nodes: | ||
| node.replace_self(_nodes) | ||
|
|
@@ -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"] | ||
|
|
@@ -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 | ||
| ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: