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