mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Use `_StrPath
in
sphinx.util
`
This commit is contained in:
parent
3015ceb546
commit
5cf3e6bf9a
@ -7,7 +7,7 @@ import re
|
||||
from collections.abc import Sequence # NoQA: TCH003
|
||||
from contextlib import contextmanager
|
||||
from copy import copy
|
||||
from os import path
|
||||
from pathlib import Path
|
||||
from typing import IO, TYPE_CHECKING, Any, cast
|
||||
|
||||
import docutils
|
||||
@ -171,25 +171,23 @@ def patched_rst_get_language() -> Iterator[None]:
|
||||
|
||||
|
||||
@contextmanager
|
||||
def using_user_docutils_conf(confdir: str | None) -> Iterator[None]:
|
||||
def using_user_docutils_conf(confdir: str | os.PathLike[str] | None) -> Iterator[None]:
|
||||
"""Let docutils know the location of ``docutils.conf`` for Sphinx."""
|
||||
try:
|
||||
docutilsconfig = os.environ.get('DOCUTILSCONFIG', None)
|
||||
docutils_config = os.environ.get('DOCUTILSCONFIG', None)
|
||||
if confdir:
|
||||
os.environ['DOCUTILSCONFIG'] = path.join(
|
||||
path.abspath(confdir), 'docutils.conf'
|
||||
)
|
||||
|
||||
docutils_conf_path = Path(confdir, 'docutils.conf').resolve()
|
||||
os.environ['DOCUTILSCONFIG'] = str(docutils_conf_path)
|
||||
yield
|
||||
finally:
|
||||
if docutilsconfig is None:
|
||||
if docutils_config is None:
|
||||
os.environ.pop('DOCUTILSCONFIG', None)
|
||||
else:
|
||||
os.environ['DOCUTILSCONFIG'] = docutilsconfig
|
||||
os.environ['DOCUTILSCONFIG'] = docutils_config
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_docutils(confdir: str | None = None) -> Iterator[None]:
|
||||
def patch_docutils(confdir: str | os.PathLike[str] | None = None) -> Iterator[None]:
|
||||
"""Patch to docutils temporarily."""
|
||||
with (
|
||||
patched_get_language(),
|
||||
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
from functools import partial
|
||||
from os import path
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from jinja2 import TemplateNotFound
|
||||
@ -21,6 +21,9 @@ if TYPE_CHECKING:
|
||||
|
||||
from jinja2.environment import Environment
|
||||
|
||||
_TEMPLATES_PATH = Path(package_dir, 'templates')
|
||||
_LATEX_TEMPLATES_PATH = _TEMPLATES_PATH / 'latex'
|
||||
|
||||
|
||||
class BaseRenderer:
|
||||
def __init__(self, loader: BaseLoader | None = None) -> None:
|
||||
@ -49,11 +52,12 @@ class FileRenderer(BaseRenderer):
|
||||
|
||||
@classmethod
|
||||
def render_from_file(
|
||||
cls: type[FileRenderer], filename: str, context: dict[str, Any]
|
||||
cls: type[FileRenderer],
|
||||
filename: str | os.PathLike[str],
|
||||
context: dict[str, Any],
|
||||
) -> str:
|
||||
dirname = os.path.dirname(filename)
|
||||
basename = os.path.basename(filename)
|
||||
return cls(dirname).render(basename, context)
|
||||
filename = Path(filename)
|
||||
return cls((filename.parent,)).render(filename.name, context)
|
||||
|
||||
|
||||
class SphinxRenderer(FileRenderer):
|
||||
@ -61,12 +65,14 @@ class SphinxRenderer(FileRenderer):
|
||||
self, template_path: Sequence[str | os.PathLike[str]] | None = None
|
||||
) -> None:
|
||||
if template_path is None:
|
||||
template_path = os.path.join(package_dir, 'templates')
|
||||
template_path = (_TEMPLATES_PATH,)
|
||||
super().__init__(template_path)
|
||||
|
||||
@classmethod
|
||||
def render_from_file(
|
||||
cls: type[FileRenderer], filename: str, context: dict[str, Any]
|
||||
cls: type[FileRenderer],
|
||||
filename: str | os.PathLike[str],
|
||||
context: dict[str, Any],
|
||||
) -> str:
|
||||
return FileRenderer.render_from_file(filename, context)
|
||||
|
||||
@ -78,7 +84,7 @@ class LaTeXRenderer(SphinxRenderer):
|
||||
latex_engine: str | None = None,
|
||||
) -> None:
|
||||
if template_path is None:
|
||||
template_path = [os.path.join(package_dir, 'templates', 'latex')]
|
||||
template_path = (_LATEX_TEMPLATES_PATH,)
|
||||
super().__init__(template_path)
|
||||
|
||||
# use texescape as escape filter
|
||||
@ -126,8 +132,9 @@ class SphinxTemplateLoader(BaseLoader):
|
||||
self.loaders = []
|
||||
self.sysloaders = []
|
||||
|
||||
conf_dir = Path(confdir)
|
||||
for templates_path in templates_paths:
|
||||
loader = SphinxFileSystemLoader(path.join(confdir, templates_path))
|
||||
loader = SphinxFileSystemLoader(conf_dir / templates_path)
|
||||
self.loaders.append(loader)
|
||||
|
||||
for templates_path in system_templates_paths:
|
||||
|
Loading…
Reference in New Issue
Block a user