Skip to content

👌 IMPROVE: Add RendererProtocol typing #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions markdown_it/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
23 changes: 21 additions & 2 deletions markdown_it/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@ class Renderer
rules if you create plugin and adds new token types.
"""
import inspect
from typing import MutableMapping, Optional, Sequence
from typing import (
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
) -> str:
...


class RendererHTML(RendererProtocol):
"""Contains render rules for tokens. Can be updated and extended.

Example:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down