Skip to content

Commit 4cb857d

Browse files
committed
Use absolute paths
1 parent 1876a3d commit 4cb857d

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

sphinx/ext/imgmath.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Render math in HTML via dvipng or dvisvgm."""
22

33
import base64
4-
import posixpath
54
import re
65
import shutil
76
import subprocess
@@ -204,7 +203,7 @@ def convert_dvi_to_svg(dvipath: str, builder: Builder, out_path: str) -> Optiona
204203
def render_math(
205204
self: HTMLTranslator,
206205
math: str,
207-
) -> Tuple[Optional[str], Optional[int], Optional[str]]:
206+
) -> Tuple[Optional[str], Optional[int]]:
208207
"""Render the LaTeX math expression *math* using latex and dvipng or
209208
dvisvgm.
210209
@@ -229,27 +228,26 @@ def render_math(
229228
self.builder.confdir)
230229

231230
filename = f"{sha1(latex.encode()).hexdigest()}.{image_format}"
232-
relative_path = posixpath.join(self.builder.imgpath, 'math', filename)
233231
generated_path = path.join(self.builder.outdir, self.builder.imagedir, 'math', filename)
234232
ensuredir(path.dirname(generated_path))
235233
if path.isfile(generated_path):
236234
if image_format == 'png':
237235
depth = read_png_depth(generated_path)
238236
elif image_format == 'svg':
239237
depth = read_svg_depth(generated_path)
240-
return relative_path, depth, generated_path
238+
return generated_path, depth
241239

242240
# if latex or dvipng (dvisvgm) has failed once, don't bother to try again
243241
if hasattr(self.builder, '_imgmath_warned_latex') or \
244242
hasattr(self.builder, '_imgmath_warned_image_translator'):
245-
return None, None, None
243+
return None, None
246244

247245
# .tex -> .dvi
248246
try:
249247
dvipath = compile_math(latex, self.builder)
250248
except InvokeError:
251249
self.builder._imgmath_warned_latex = True # type: ignore
252-
return None, None, None
250+
return None, None
253251

254252
# .dvi -> .png/.svg
255253
try:
@@ -259,9 +257,9 @@ def render_math(
259257
depth = convert_dvi_to_svg(dvipath, self.builder, generated_path)
260258
except InvokeError:
261259
self.builder._imgmath_warned_image_translator = True # type: ignore
262-
return None, None, None
260+
return None, None
263261

264-
return relative_path, depth, generated_path
262+
return generated_path, depth
265263

266264

267265
def render_maths_to_base64(image_format: str, generated_path: Optional[str]) -> str:
@@ -301,7 +299,7 @@ def get_tooltip(self: HTMLTranslator, node: Element) -> str:
301299

302300
def html_visit_math(self: HTMLTranslator, node: nodes.math) -> None:
303301
try:
304-
fname, depth, generated_path = render_math(self, '$' + node.astext() + '$')
302+
rendered_path, depth = render_math(self, '$' + node.astext() + '$')
305303
except MathExtError as exc:
306304
msg = str(exc)
307305
sm = nodes.system_message(msg, type='WARNING', level=2,
@@ -310,16 +308,17 @@ def html_visit_math(self: HTMLTranslator, node: nodes.math) -> None:
310308
logger.warning(__('display latex %r: %s'), node.astext(), msg)
311309
raise nodes.SkipNode from exc
312310

313-
if fname is None:
311+
if rendered_path is None:
314312
# something failed -- use text-only as a bad substitute
315313
self.body.append('<span class="math">%s</span>' %
316314
self.encode(node.astext()).strip())
317315
else:
318316
if self.builder.config.imgmath_embed:
319317
image_format = self.builder.config.imgmath_image_format.lower()
320-
img_src = render_maths_to_base64(image_format, generated_path)
318+
img_src = render_maths_to_base64(image_format, rendered_path)
321319
else:
322-
img_src = fname
320+
relative_path = path.relpath(rendered_path, self.builder.outdir)
321+
img_src = relative_path.replace(path.sep, '/')
323322
c = f'<img class="math" src="{img_src}"' + get_tooltip(self, node)
324323
if depth is not None:
325324
c += f' style="vertical-align: {-depth:d}px"'
@@ -333,7 +332,7 @@ def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None
333332
else:
334333
latex = wrap_displaymath(node.astext(), None, False)
335334
try:
336-
fname, depth, generated_path = render_math(self, latex)
335+
rendered_path, depth = render_math(self, latex)
337336
except MathExtError as exc:
338337
msg = str(exc)
339338
sm = nodes.system_message(msg, type='WARNING', level=2,
@@ -349,16 +348,17 @@ def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None
349348
self.add_permalink_ref(node, _('Permalink to this equation'))
350349
self.body.append('</span>')
351350

352-
if fname is None:
351+
if rendered_path is None:
353352
# something failed -- use text-only as a bad substitute
354353
self.body.append('<span class="math">%s</span></p>\n</div>' %
355354
self.encode(node.astext()).strip())
356355
else:
357356
if self.builder.config.imgmath_embed:
358357
image_format = self.builder.config.imgmath_image_format.lower()
359-
img_src = render_maths_to_base64(image_format, generated_path)
358+
img_src = render_maths_to_base64(image_format, rendered_path)
360359
else:
361-
img_src = fname
360+
relative_path = path.relpath(rendered_path, self.builder.outdir)
361+
img_src = relative_path.replace(path.sep, '/')
362362
self.body.append(f'<img src="{img_src}"' + get_tooltip(self, node) +
363363
'/></p>\n</div>')
364364
raise nodes.SkipNode

0 commit comments

Comments
 (0)