|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from collections.abc import Callable |
| 3 | +from collections.abc import MutableMapping as MutableMappingABC |
4 | 4 | from pathlib import Path
|
5 |
| -from typing import Any, MutableMapping |
| 5 | +from typing import Any, Callable, Iterable, MutableMapping, TypedDict |
6 | 6 |
|
7 |
| -EnvType = MutableMapping[str, Any] |
| 7 | +EnvType = MutableMapping[str, Any] # note: could use TypeAlias in python 3.10 |
8 | 8 | """Type for the environment sandbox used in parsing and rendering,
|
9 | 9 | which stores mutable variables for use by plugins and rules.
|
10 | 10 | """
|
11 | 11 |
|
12 | 12 |
|
13 |
| -class OptionsDict(dict): |
| 13 | +class OptionsType(TypedDict): |
| 14 | + """Options for parsing.""" |
| 15 | + |
| 16 | + maxNesting: int |
| 17 | + """Internal protection, recursion limit.""" |
| 18 | + html: bool |
| 19 | + """Enable HTML tags in source.""" |
| 20 | + linkify: bool |
| 21 | + """Enable autoconversion of URL-like texts to links.""" |
| 22 | + typographer: bool |
| 23 | + """Enable smartquotes and replacements.""" |
| 24 | + quotes: str |
| 25 | + """Quote characters.""" |
| 26 | + xhtmlOut: bool |
| 27 | + """Use '/' to close single tags (<br />).""" |
| 28 | + breaks: bool |
| 29 | + """Convert newlines in paragraphs into <br>.""" |
| 30 | + langPrefix: str |
| 31 | + """CSS language prefix for fenced blocks.""" |
| 32 | + highlight: Callable[[str, str, str], str] | None |
| 33 | + """Highlighter function: (content, lang, attrs) -> str.""" |
| 34 | + |
| 35 | + |
| 36 | +class PresetType(TypedDict): |
| 37 | + """Preset configuration for markdown-it.""" |
| 38 | + |
| 39 | + options: OptionsType |
| 40 | + """Options for parsing.""" |
| 41 | + components: MutableMapping[str, MutableMapping[str, list[str]]] |
| 42 | + """Components for parsing and rendering.""" |
| 43 | + |
| 44 | + |
| 45 | +class OptionsDict(MutableMappingABC): # type: ignore |
14 | 46 | """A dictionary, with attribute access to core markdownit configuration options."""
|
15 | 47 |
|
| 48 | + # Note: ideally we would probably just remove attribute access entirely, |
| 49 | + # but we keep it for backwards compatibility. |
| 50 | + |
| 51 | + def __init__(self, options: OptionsType) -> None: |
| 52 | + self._options = options |
| 53 | + |
| 54 | + def __getitem__(self, key: str) -> Any: |
| 55 | + return self._options[key] # type: ignore[literal-required] |
| 56 | + |
| 57 | + def __setitem__(self, key: str, value: Any) -> None: |
| 58 | + self._options[key] = value # type: ignore[literal-required] |
| 59 | + |
| 60 | + def __delitem__(self, key: str) -> None: |
| 61 | + del self._options[key] # type: ignore |
| 62 | + |
| 63 | + def __iter__(self) -> Iterable[str]: # type: ignore |
| 64 | + return iter(self._options) |
| 65 | + |
| 66 | + def __len__(self) -> int: |
| 67 | + return len(self._options) |
| 68 | + |
| 69 | + def __repr__(self) -> str: |
| 70 | + return repr(self._options) |
| 71 | + |
| 72 | + def __str__(self) -> str: |
| 73 | + return str(self._options) |
| 74 | + |
16 | 75 | @property
|
17 | 76 | def maxNesting(self) -> int:
|
18 | 77 | """Internal protection, recursion limit."""
|
19 |
| - return self["maxNesting"] |
| 78 | + return self._options["maxNesting"] |
20 | 79 |
|
21 | 80 | @maxNesting.setter
|
22 |
| - def maxNesting(self, value: int): |
23 |
| - self["maxNesting"] = value |
| 81 | + def maxNesting(self, value: int) -> None: |
| 82 | + self._options["maxNesting"] = value |
24 | 83 |
|
25 | 84 | @property
|
26 | 85 | def html(self) -> bool:
|
27 | 86 | """Enable HTML tags in source."""
|
28 |
| - return self["html"] |
| 87 | + return self._options["html"] |
29 | 88 |
|
30 | 89 | @html.setter
|
31 |
| - def html(self, value: bool): |
32 |
| - self["html"] = value |
| 90 | + def html(self, value: bool) -> None: |
| 91 | + self._options["html"] = value |
33 | 92 |
|
34 | 93 | @property
|
35 | 94 | def linkify(self) -> bool:
|
36 | 95 | """Enable autoconversion of URL-like texts to links."""
|
37 |
| - return self["linkify"] |
| 96 | + return self._options["linkify"] |
38 | 97 |
|
39 | 98 | @linkify.setter
|
40 |
| - def linkify(self, value: bool): |
41 |
| - self["linkify"] = value |
| 99 | + def linkify(self, value: bool) -> None: |
| 100 | + self._options["linkify"] = value |
42 | 101 |
|
43 | 102 | @property
|
44 | 103 | def typographer(self) -> bool:
|
45 | 104 | """Enable smartquotes and replacements."""
|
46 |
| - return self["typographer"] |
| 105 | + return self._options["typographer"] |
47 | 106 |
|
48 | 107 | @typographer.setter
|
49 |
| - def typographer(self, value: bool): |
50 |
| - self["typographer"] = value |
| 108 | + def typographer(self, value: bool) -> None: |
| 109 | + self._options["typographer"] = value |
51 | 110 |
|
52 | 111 | @property
|
53 | 112 | def quotes(self) -> str:
|
54 | 113 | """Quote characters."""
|
55 |
| - return self["quotes"] |
| 114 | + return self._options["quotes"] |
56 | 115 |
|
57 | 116 | @quotes.setter
|
58 |
| - def quotes(self, value: str): |
59 |
| - self["quotes"] = value |
| 117 | + def quotes(self, value: str) -> None: |
| 118 | + self._options["quotes"] = value |
60 | 119 |
|
61 | 120 | @property
|
62 | 121 | def xhtmlOut(self) -> bool:
|
63 | 122 | """Use '/' to close single tags (<br />)."""
|
64 |
| - return self["xhtmlOut"] |
| 123 | + return self._options["xhtmlOut"] |
65 | 124 |
|
66 | 125 | @xhtmlOut.setter
|
67 |
| - def xhtmlOut(self, value: bool): |
68 |
| - self["xhtmlOut"] = value |
| 126 | + def xhtmlOut(self, value: bool) -> None: |
| 127 | + self._options["xhtmlOut"] = value |
69 | 128 |
|
70 | 129 | @property
|
71 | 130 | def breaks(self) -> bool:
|
72 | 131 | """Convert newlines in paragraphs into <br>."""
|
73 |
| - return self["breaks"] |
| 132 | + return self._options["breaks"] |
74 | 133 |
|
75 | 134 | @breaks.setter
|
76 |
| - def breaks(self, value: bool): |
77 |
| - self["breaks"] = value |
| 135 | + def breaks(self, value: bool) -> None: |
| 136 | + self._options["breaks"] = value |
78 | 137 |
|
79 | 138 | @property
|
80 | 139 | def langPrefix(self) -> str:
|
81 | 140 | """CSS language prefix for fenced blocks."""
|
82 |
| - return self["langPrefix"] |
| 141 | + return self._options["langPrefix"] |
83 | 142 |
|
84 | 143 | @langPrefix.setter
|
85 |
| - def langPrefix(self, value: str): |
86 |
| - self["langPrefix"] = value |
| 144 | + def langPrefix(self, value: str) -> None: |
| 145 | + self._options["langPrefix"] = value |
87 | 146 |
|
88 | 147 | @property
|
89 | 148 | def highlight(self) -> Callable[[str, str, str], str] | None:
|
90 | 149 | """Highlighter function: (content, langName, langAttrs) -> escaped HTML."""
|
91 |
| - return self["highlight"] |
| 150 | + return self._options["highlight"] |
92 | 151 |
|
93 | 152 | @highlight.setter
|
94 |
| - def highlight(self, value: Callable[[str, str, str], str] | None): |
95 |
| - self["highlight"] = value |
| 153 | + def highlight(self, value: Callable[[str, str, str], str] | None) -> None: |
| 154 | + self._options["highlight"] = value |
96 | 155 |
|
97 | 156 |
|
98 |
| -def read_fixture_file(path: str | Path) -> list[list]: |
| 157 | +def read_fixture_file(path: str | Path) -> list[list[Any]]: |
99 | 158 | text = Path(path).read_text(encoding="utf-8")
|
100 | 159 | tests = []
|
101 | 160 | section = 0
|
|
0 commit comments