Skip to content

Commit

Permalink
imgmath: Move dest image file even with embed enabled
Browse files Browse the repository at this point in the history
We need the generated image files to be in one common
folder to avoid having to re-generate several times the same image
(the sha1 naming of the file acts as a cache to just read the depth)

The location should be possibly common to all workers,
that's why I choosed to just keep the original destination folder,
ie the file is just moved from tempdir to the build folder before.

This significantly drops the build times from 84min to 26min,
but obviously leaves the unused image files in the output folder.

Another solution would be to use another global temporary folder
shared across all the workers, or to add a delete pass after all workers
have joined, but there does not seem to be an api to do that.
  • Loading branch information
jschueller committed Oct 1, 2022
1 parent 7765940 commit 7283402
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions sphinx/ext/imgmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ def render_math(
self.builder._imgmath_warned_image_translator = True # type: ignore
return None, None, None, None

# Move generated image on tempdir to build dir
ensuredir(path.dirname(outfn))
shutil.move(imgpath, outfn)

return relfn, depth, imgpath, outfn


Expand Down Expand Up @@ -306,20 +310,17 @@ def html_visit_math(self: HTMLTranslator, node: nodes.math) -> None:
sm.walkabout(self)
logger.warning(__('display latex %r: %s'), node.astext(), msg)
raise nodes.SkipNode from exc
if self.builder.config.imgmath_embed:
image_format = self.builder.config.imgmath_image_format.lower()
img_src = render_maths_to_base64(image_format, imgpath)
else:
# Move generated image on tempdir to build dir
if imgpath is not None:
ensuredir(path.dirname(outfn))
shutil.move(imgpath, outfn)
img_src = fname
if img_src is None:

if fname is None:
# something failed -- use text-only as a bad substitute
self.body.append('<span class="math">%s</span>' %
self.encode(node.astext()).strip())
else:
if self.builder.config.imgmath_embed:
image_format = self.builder.config.imgmath_image_format.lower()
img_src = render_maths_to_base64(image_format, outfn)
else:
img_src = fname
c = f'<img class="math" src="{img_src}"' + get_tooltip(self, node)
if depth is not None:
c += f' style="vertical-align: {-depth:d}px"'
Expand Down Expand Up @@ -348,20 +349,17 @@ def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None
self.body.append('<span class="eqno">(%s)' % number)
self.add_permalink_ref(node, _('Permalink to this equation'))
self.body.append('</span>')
if self.builder.config.imgmath_embed:
image_format = self.builder.config.imgmath_image_format.lower()
img_src = render_maths_to_base64(image_format, imgpath)
else:
# Move generated image on tempdir to build dir
if imgpath is not None:
ensuredir(path.dirname(outfn))
shutil.move(imgpath, outfn)
img_src = fname
if img_src is None:

if fname is None:
# something failed -- use text-only as a bad substitute
self.body.append('<span class="math">%s</span></p>\n</div>' %
self.encode(node.astext()).strip())
else:
if self.builder.config.imgmath_embed:
image_format = self.builder.config.imgmath_image_format.lower()
img_src = render_maths_to_base64(image_format, outfn)
else:
img_src = fname
self.body.append(f'<img src="{img_src}"' + get_tooltip(self, node) +
'/></p>\n</div>')
raise nodes.SkipNode
Expand Down

0 comments on commit 7283402

Please sign in to comment.