Skip to content

imgmath: Allow embedding images in HTML as base64#10816

Merged
AA-Turner merged 16 commits intosphinx-doc:5.xfrom
jschueller:math_embed
Sep 23, 2022
Merged

imgmath: Allow embedding images in HTML as base64#10816
AA-Turner merged 16 commits intosphinx-doc:5.xfrom
jschueller:math_embed

Conversation

@jschueller
Copy link
Copy Markdown
Contributor

@jschueller jschueller commented Sep 12, 2022

A new flag imgmath_embed allows to include LaTeX output images as inline base64 into the HTML instead of separate png/svg files, using:
<img class="math" src="data:image/svg+xml;base64,ocmVmPScjZzEtNzMnL...>
This allows to avoid uploading many small files for continuous integration artifacts.
The latex output file is removed after encoding.

inspired from https://github.com/sphinx-contrib/imageembed

@jschueller jschueller force-pushed the math_embed branch 2 times, most recently from be0c5b2 to c06244a Compare September 12, 2022 12:08
Comment thread doc/usage/extensions/math.rst
Comment thread sphinx/ext/imgmath.py Outdated
@AA-Turner
Copy link
Copy Markdown
Member

@jschueller please don't force-push, I'll squash on merge.

A

@AA-Turner AA-Turner added type:enhancement enhance or introduce a new feature builder labels Sep 12, 2022
@AA-Turner AA-Turner added this to the 5.2.0 milestone Sep 12, 2022
@jschueller
Copy link
Copy Markdown
Contributor Author

oh, ok

@AA-Turner
Copy link
Copy Markdown
Member

Please may you add some tests for the new behaviour, and an entry in CHANGES?

A

@AA-Turner AA-Turner changed the title imgmath: Allow to embed images as base64 HTML imgmath: Allow embedding images in HTML as base64 Sep 12, 2022
@AA-Turner
Copy link
Copy Markdown
Member

As a design point, I'd suggest splitting render_math() into two functions, at the # Move generated image on tempdir to build dir line.

We would always call the function that creates the temporary image, but the flag would control if we called the move_rendered_image_from_temp_dir_to_build_dir() function or the read_rendered_image_and_return_base_64_encoded_string() function (names to be improved!).

This then means we always know the mime type and don't have to use the guess functions, and don't need to remove any images, as they're in temp directories anyway.

A

@jschueller
Copy link
Copy Markdown
Contributor Author

@AA-Turner the move is now in a separate function get_img_src, but I dont have as many functions as suggested,
could you give more details on how to split the functions if its not ok for you ?

# Conflicts:
#	CHANGES
@AA-Turner AA-Turner merged commit ef01c5b into sphinx-doc:5.x Sep 23, 2022
@AA-Turner
Copy link
Copy Markdown
Member

Thanks Julien!

A

@jschueller jschueller deleted the math_embed branch September 23, 2022 19:52
@jschueller
Copy link
Copy Markdown
Contributor Author

thanks!

@jschueller
Copy link
Copy Markdown
Contributor Author

jschueller commented Sep 26, 2022

@AA-Turner it seems it does not work in parallel (using -j4 flag), any idea why ?

File "/usr/lib/python3.10/site-packages/sphinx/util/parallel.py", line 125, in _join_one
    raise SphinxParallelError(*result)
sphinx.errors.SphinxParallelError: FileNotFoundError: [Errno 2] No such file or directory: '/home/devel/project/build/python/src/sphinx_build/html/_images/math/dd25924f1cd0f37c1985116c759fb5c0dbfae2bb.svg'

@AA-Turner
Copy link
Copy Markdown
Member

Could you open a new issue please, or a PR adding a failing test for parallel building?

A

@jschueller
Copy link
Copy Markdown
Contributor Author

ok, see #10869

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 28, 2022
@AA-Turner AA-Turner added the extensions:mathematics The `sphinx.ext.imgmath`, `sphinx.ext.jsmath`, or `sphinx.ext.mathjax` extensions label Jan 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

builder extensions:mathematics The `sphinx.ext.imgmath`, `sphinx.ext.jsmath`, or `sphinx.ext.mathjax` extensions type:enhancement enhance or introduce a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants