66
77from __future__ import annotations
88
9- import os .path
109import re
1110from collections import defaultdict
1211from collections .abc import Iterable
12+ from pathlib import Path
1313from typing import TYPE_CHECKING , Any , ClassVar , cast
1414
1515from docutils import nodes , writers
@@ -583,18 +583,19 @@ def generate(
583583 def render (self , template_name : str , variables : dict [str , Any ]) -> str :
584584 renderer = LaTeXRenderer (latex_engine = self .config .latex_engine )
585585 for template_dir in self .config .templates_path :
586- template = os .path .join (self .builder .confdir , template_dir , template_name )
587- if os .path .exists (template ):
588- return renderer .render (template , variables )
589- elif template .endswith ('.jinja' ):
590- legacy_template = template .removesuffix ('.jinja' ) + '_t'
591- if os .path .exists (legacy_template ):
586+ template = self .builder .confdir / template_dir / template_name
587+ if template .exists ():
588+ return renderer .render (str (template ), variables )
589+ elif template .suffix == '.jinja' :
590+ legacy_template_name = template .name .removesuffix ('.jinja' ) + '_t'
591+ legacy_template = template .with_name (legacy_template_name )
592+ if legacy_template .exists ():
592593 logger .warning (
593594 __ ('template %s not found; loading from legacy %s instead' ),
594595 template_name ,
595596 legacy_template ,
596597 )
597- return renderer .render (legacy_template , variables )
598+ return renderer .render (str ( legacy_template ) , variables )
598599
599600 return renderer .render (template_name , variables )
600601
@@ -1648,7 +1649,9 @@ def visit_image(self, node: Element) -> None:
16481649 options = ''
16491650 if include_graphics_options :
16501651 options = '[%s]' % ',' .join (include_graphics_options )
1651- base , ext = os .path .splitext (uri )
1652+ img_path = Path (uri )
1653+ base = img_path .with_suffix ('' )
1654+ ext = img_path .suffix
16521655
16531656 if self .in_title and base :
16541657 # Lowercase tokens forcely because some fncychap themes capitalize
@@ -1657,8 +1660,8 @@ def visit_image(self, node: Element) -> None:
16571660 else :
16581661 cmd = rf'\sphinxincludegraphics{ options } {{{{{ base } }}{ ext } }}'
16591662 # escape filepath for includegraphics, https://tex.stackexchange.com/a/202714/41112
1660- if '#' in base :
1661- cmd = r'{ \catcode`\#=12' + cmd + ' }'
1663+ if '#' in str ( base ) :
1664+ cmd = rf'{{ \catcode`\#=12{ cmd } } }'
16621665 self .body .append (cmd )
16631666 self .body .extend (post )
16641667
0 commit comments