-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make TemplateContext typed, move utils.filters to utils.templates
- Loading branch information
Showing
7 changed files
with
73 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1 @@ | ||
from __future__ import annotations | ||
|
||
try: | ||
from jinja2 import pass_context as contextfilter # type: ignore | ||
except ImportError: | ||
from jinja2 import contextfilter # type: ignore | ||
|
||
from mkdocs.utils import normalize_url | ||
|
||
|
||
@contextfilter | ||
def url_filter(context, value: str) -> str: | ||
"""A Template filter to normalize URLs.""" | ||
return normalize_url(value, page=context['page'], base=context['base_url']) | ||
from .templates import url_filter # noqa - legacy re-export |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import TYPE_CHECKING, Sequence | ||
|
||
if sys.version_info >= (3, 8): | ||
from typing import TypedDict | ||
else: | ||
from typing_extensions import TypedDict | ||
|
||
if TYPE_CHECKING: | ||
import datetime | ||
|
||
try: | ||
from jinja2 import pass_context as contextfilter # type: ignore | ||
except ImportError: | ||
from jinja2 import contextfilter # type: ignore | ||
|
||
from mkdocs.utils import normalize_url | ||
|
||
if TYPE_CHECKING: | ||
from mkdocs.config.defaults import MkDocsConfig | ||
from mkdocs.structure.files import File | ||
from mkdocs.structure.nav import Navigation | ||
from mkdocs.structure.pages import Page | ||
|
||
|
||
class TemplateContext(TypedDict): | ||
nav: Navigation | ||
pages: Sequence[File] | ||
base_url: str | ||
extra_css: Sequence[str] | ||
extra_javascript: Sequence[str] | ||
mkdocs_version: str | ||
build_date_utc: datetime.datetime | ||
config: MkDocsConfig | ||
page: Page | None | ||
|
||
|
||
@contextfilter | ||
def url_filter(context: TemplateContext, value: str) -> str: | ||
"""A Template filter to normalize URLs.""" | ||
return normalize_url(value, page=context['page'], base=context['base_url']) |