-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbase.py
More file actions
63 lines (43 loc) · 1.86 KB
/
Copy pathbase.py
File metadata and controls
63 lines (43 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import annotations
import re
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from docs.docs_generation.content_gen_methods import ADocContentGenMethod
class DocPage:
"""A documentation page whose content is produced by an injected generation method.
Args:
content_gen_method: Strategy that produces the page content as a string.
Example::
page = DocPage(content_gen_method=Jinja2DocContentGenMethod(...))
print(page.content())
"""
def __init__(self, content_gen_method: ADocContentGenMethod) -> None:
self.content_gen_method = content_gen_method
def content(self) -> str:
return self.content_gen_method.apply()
class MDXDocPage:
"""Decorator which is a documentation page that can be written in an ``.mdx`` file.
Args:
page: The documentation page whose content will be rendered.
output_path: File path where the ``.mdx`` output will be written.
Example::
mdx = MDXDocPage(page=my_page, output_path=Path("docs/ref/client.mdx"))
mdx.to_mdx()
"""
_CONTROL_CHAR_RE = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]")
def __init__(self, page: DocPage, output_path: Path) -> None:
self.page = page
self.output_path = output_path
def to_mdx(self) -> None:
rendered = self.page.content()
rendered = self._sanitize(rendered)
self.output_path.parent.mkdir(parents=True, exist_ok=True)
self.output_path.write_text(rendered, encoding="utf-8")
print(f"Docs saved to: {self.output_path}")
@classmethod
def _sanitize(cls, text: str) -> str:
"""Normalize line endings, strip control characters and collapse blank lines."""
text = text.replace("\r\n", "\n")
text = cls._CONTROL_CHAR_RE.sub("", text)
return re.sub(r"\n{3,}", "\n\n", text)