diff --git a/markdown_it/main.py b/markdown_it/main.py index dfa18189..e87a7a44 100644 --- a/markdown_it/main.py +++ b/markdown_it/main.py @@ -19,7 +19,7 @@ from .parser_block import ParserBlock # noqa F401 from .parser_inline import ParserInline # noqa F401 from .rules_core.state_core import StateCore -from .renderer import RendererHTML +from .renderer import RendererHTML, RendererProtocol from .utils import OptionsDict try: @@ -43,7 +43,7 @@ def __init__( config: Union[str, Mapping] = "commonmark", options_update: Optional[Mapping] = None, *, - renderer_cls=RendererHTML, + renderer_cls: Callable[["MarkdownIt"], RendererProtocol] = RendererHTML, ): """Main parser class diff --git a/markdown_it/renderer.py b/markdown_it/renderer.py index 9ffa94b0..9e9c7751 100644 --- a/markdown_it/renderer.py +++ b/markdown_it/renderer.py @@ -6,14 +6,34 @@ class Renderer rules if you create plugin and adds new token types. """ import inspect -from typing import MutableMapping, Optional, Sequence +from typing import ( + Any, + ClassVar, + MutableMapping, + Optional, + Sequence, +) from .common.utils import unescapeAll, escapeHtml from .token import Token from .utils import OptionsDict +try: + from typing import Protocol +except ImportError: # Python <3.8 doesn't have `Protocol` in the stdlib + from typing_extensions import Protocol # type: ignore[misc] -class RendererHTML: + +class RendererProtocol(Protocol): + __output__: ClassVar[str] + + def render( + self, tokens: Sequence[Token], options: OptionsDict, env: MutableMapping + ) -> Any: + ... + + +class RendererHTML(RendererProtocol): """Contains render rules for tokens. Can be updated and extended. Example: diff --git a/setup.cfg b/setup.cfg index 72564ba0..60929005 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ project_urls = packages = find: install_requires = attrs>=19,<21 + typing_extensions>=3.7.4;python_version<'3.8' python_requires = ~=3.6 include_package_data = True zip_safe = False