Skip to content

Commit 92d52be

Browse files
committed
add base parsers
1 parent fce3b46 commit 92d52be

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# render-engine-parsers
1+
# Render Engine Parsers
2+
23
The page parser system used for making content for Render Engine
4+
5+
## Parsers
6+
7+
Parsers are used to parse the content of a page and convert it to HTML. The parser is specified in the page attributes as `Parser`.
8+
9+
The default parser is the `BasePageParser` which processes markdown and passes the content thru as plain text.

render-engine-parser/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Parsers
2+
3+
Parsers are used to parse the content of a page and convert it to HTML. The parser is specified in the page attributes as `Parser`.
4+
5+
The default parser is [`markdown`](markdown/README.md).

render-engine-parser/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .base_parsers import BasePageParser
2+
3+
__all__ = ["BasePageParser"]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)