|
| 1 | +# ruff: noqa: F821 |
| 2 | + |
| 3 | +import pathlib |
| 4 | + |
| 5 | +import frontmatter |
| 6 | + |
| 7 | + |
| 8 | +def parse_content(content: str) -> tuple[dict, str]: |
| 9 | + """Fetching content and atttributes from a content_path""" |
| 10 | + p = frontmatter.parse(content) |
| 11 | + return p |
| 12 | + |
| 13 | + |
| 14 | +class BasePageParser: |
| 15 | + """ |
| 16 | + The default Parser for Page objects. |
| 17 | + This yields attributes and content using frontmatter. |
| 18 | + The content is not modified. |
| 19 | + """ |
| 20 | + |
| 21 | + @staticmethod |
| 22 | + def parse_content_path(content_path: str) -> tuple[dict, str]: |
| 23 | + """ |
| 24 | + Fetches content from `Page.content_path` and sets attributes. |
| 25 | +
|
| 26 | + This is a separate method so that it can be overridden by subclasses. |
| 27 | +
|
| 28 | + params: |
| 29 | + content_path: |
| 30 | + The path to the file that will be used to generate the Page's `content`. |
| 31 | + Should be a valid path to a file or a url. |
| 32 | + """ |
| 33 | + return parse_content(pathlib.Path(content_path).read_text()) |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def parse_content(content: str) -> tuple[dict, str]: |
| 37 | + """ |
| 38 | + Fetches content from `Page.content` and returns attributes and content. |
| 39 | +
|
| 40 | + This is a separate method so that it can be overridden by subclasses. |
| 41 | +
|
| 42 | + params: |
| 43 | + content: |
| 44 | + The path to the file that will be used to generate the Page's `content`. |
| 45 | + Should be a valid path to a file or a url. |
| 46 | + """ |
| 47 | + |
| 48 | + return parse_content(content) |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def parse(content: str, page: "Page" = None): |
| 52 | + """ |
| 53 | + Parses content to be rendered into HTML |
| 54 | +
|
| 55 | + In the base parser, this returns the content as is. |
| 56 | +
|
| 57 | + params: |
| 58 | + content: content to be rendered into HTML |
| 59 | + page: Page object to gain access to attributes |
| 60 | + """ |
| 61 | + return content |
0 commit comments