blob: 605a39d3e1841e9b70bc79713df909249f8cbc45 [file] [log] [blame]
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Union
class OptionsDict(dict):
"""A dictionary, with attribute access to core markdownit configuration options."""
def maxNesting(self) -> int:
"""Internal protection, recursion limit."""
return self["maxNesting"]
def maxNesting(self, value: int):
self["maxNesting"] = value
def html(self) -> bool:
"""Enable HTML tags in source."""
return self["html"]
def html(self, value: bool):
self["html"] = value
def linkify(self) -> bool:
"""Enable autoconversion of URL-like texts to links."""
return self["linkify"]
def linkify(self, value: bool):
self["linkify"] = value
def typographer(self) -> bool:
"""Enable smartquotes and replacements."""
return self["typographer"]
def typographer(self, value: bool):
self["typographer"] = value
def quotes(self) -> str:
"""Quote characters."""
return self["quotes"]
def quotes(self, value: str):
self["quotes"] = value
def xhtmlOut(self) -> bool:
"""Use '/' to close single tags (<br />)."""
return self["xhtmlOut"]
def xhtmlOut(self, value: bool):
self["xhtmlOut"] = value
def breaks(self) -> bool:
"""Convert newlines in paragraphs into <br>."""
return self["breaks"]
def breaks(self, value: bool):
self["breaks"] = value
def langPrefix(self) -> str:
"""CSS language prefix for fenced blocks."""
return self["langPrefix"]
def langPrefix(self, value: str):
self["langPrefix"] = value
def highlight(self) -> Optional[Callable[[str, str, str], str]]:
"""Highlighter function: (content, langName, langAttrs) -> escaped HTML."""
return self["highlight"]
def highlight(self, value: Optional[Callable[[str, str, str], str]]):
self["highlight"] = value
AttrDict = Any
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
# recursively apply to all nested dictionaries
for key, item in list(self.items()):
if isinstance(item, dict):
self[key] = AttrDict(item)
def read_fixture_file(path: Union[str, Path]) -> List[list]:
text = Path(path).read_text(encoding="utf-8")
tests = []
section = 0
last_pos = 0
lines = text.splitlines(keepends=True)
for i in range(len(lines)):
if lines[i].rstrip() == ".":
if section == 0:
tests.append([i, lines[i - 1].strip()])
section = 1
elif section == 1:
tests[-1].append("".join(lines[last_pos + 1 : i]))
section = 2
elif section == 2:
tests[-1].append("".join(lines[last_pos + 1 : i]))
section = 0
last_pos = i
return tests
def _removesuffix(string: str, suffix: str) -> str:
"""Remove a suffix from a string.
Replace this with str.removesuffix() from stdlib when minimum Python
version is 3.9.
if suffix and string.endswith(suffix):
return string[: -len(suffix)]
return string