render-engine-json 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from render_engine.parsers.base_parsers import BasePageParser
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def base_parse(body: dict[str, any]) -> tuple[dict[str, any], str]:
|
|
7
|
+
"""
|
|
8
|
+
parse content and attributes from content
|
|
9
|
+
>>> base_parse({"title": "Hello", "content": "Hello World"})
|
|
10
|
+
>>> ({'title': 'Hello'}, 'Hello World')
|
|
11
|
+
"""
|
|
12
|
+
content = body.pop("content", None)
|
|
13
|
+
return body, content
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def parse_from_slug_entry(
|
|
17
|
+
slug_entry: dict[str, dict[str, any]]
|
|
18
|
+
) -> tuple[dict[str, any], str]:
|
|
19
|
+
"""
|
|
20
|
+
Fething content and attributes from a slug entry.
|
|
21
|
+
>>> parse_from_slug_entry({"slug": {"content": "Hello World"}})
|
|
22
|
+
>>> ({'slug': 'slug'}, 'Hello World')
|
|
23
|
+
|
|
24
|
+
This function expects input to be a dictionary with a single key whose value is a dict.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
attrs, content = base_parse(list(slug_entry.values())[0])
|
|
28
|
+
attrs["slug"] = list(slug_entry.keys())[0]
|
|
29
|
+
return attrs, content
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class JSONPageParser(BasePageParser):
|
|
33
|
+
"""Parser for JSON content."""
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def parse_content_path(content_path: str) -> dict[str, Any]:
|
|
37
|
+
"""
|
|
38
|
+
Fetches content from a content_path and set attributes.
|
|
39
|
+
"""
|
|
40
|
+
with open(content_path, "rb") as json_file:
|
|
41
|
+
return json.load(json_file)
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def parse_content(content: str) -> tuple[dict[str, Any], str]:
|
|
45
|
+
"""
|
|
46
|
+
Fetching content and atttributes from content.
|
|
47
|
+
|
|
48
|
+
If the parsed_content is a dictionary, then the attributes are fetched from the dictionary
|
|
49
|
+
and the content is fetched from the "content" key.
|
|
50
|
+
If the parsed_content is a list, each item is passed as a dictionary into the `data` attribute.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
body = json.loads(content)
|
|
54
|
+
|
|
55
|
+
if isinstance(body, dict):
|
|
56
|
+
return parse_from_slug_entry(body)
|
|
57
|
+
|
|
58
|
+
if isinstance(body, list):
|
|
59
|
+
return {"data": body}, ""
|
|
60
|
+
|
|
61
|
+
raise TypeError(
|
|
62
|
+
"The content should be a stringified dictionary or list.\n \
|
|
63
|
+
Perhaps you meant to stringify the content?"
|
|
64
|
+
)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: render-engine-json
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: JSON Parser and Collection for Render Engine
|
|
5
|
+
Project-URL: homepage, https://github.com/kjaymiller/render_engine_json/
|
|
6
|
+
Project-URL: repository, https://github.com/kjaymiller/render_engine_json/
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: render-engine >=2023.12.1b3
|
|
10
|
+
Requires-Dist: python-slugify
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest ; extra == 'dev'
|
|
13
|
+
|
|
14
|
+
# Render Engine JSON
|
|
15
|
+
|
|
16
|
+
JSON Parser and Collection Module for Render Engine
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install render_engine_json
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
The `render-engine-json` extends render-engine to allow you to build pages and collections using JSON.
|
|
27
|
+
|
|
28
|
+
The `JSONPageParser` class can be used to parse a single JSON page and the `JSONCollection` class can be used to parse a single collection of JSON pages.
|
|
29
|
+
|
|
30
|
+
> **NOTE**
|
|
31
|
+
> The `JSONCollection` class is designed to work with a single JSON file that contains an array of JSON Objects or a JSON Hash where the keys are the slugs. If you want to work with multiple JSON files you can use a regular `Collection` and the `JSONPageParser` as the parser.
|
|
32
|
+
|
|
33
|
+
### Single JSON Page Entry
|
|
34
|
+
|
|
35
|
+
To create a json entry you will need the `JSONPageParser` class as the Parser for your `Page` Object.
|
|
36
|
+
|
|
37
|
+
You can pass the page a json string or a json file path.
|
|
38
|
+
|
|
39
|
+
#### As a string
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
|
|
43
|
+
from render_engine import Page, Site
|
|
44
|
+
from render_engine_json import JSONPageParser
|
|
45
|
+
|
|
46
|
+
site = Site()
|
|
47
|
+
|
|
48
|
+
json_page = """
|
|
49
|
+
{
|
|
50
|
+
"title": "My JSON Page",
|
|
51
|
+
"content": "This is my json page"
|
|
52
|
+
}
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
@site.page
|
|
56
|
+
class JSONStringPage(Page):
|
|
57
|
+
parser = JSONPageParser
|
|
58
|
+
content = json_page
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### As a file
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
|
|
66
|
+
@site.page
|
|
67
|
+
class JSONFilePage(Page):
|
|
68
|
+
parser = JSONPageParser
|
|
69
|
+
content_path = "path/to/json/file.json"
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### JSON Collection
|
|
74
|
+
|
|
75
|
+
To create a JSON collection you will need the `JSONCollection` Object.
|
|
76
|
+
|
|
77
|
+
You can pass the collection a json string or a json file path.
|
|
78
|
+
|
|
79
|
+
#### As a string
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from render_engine import Site, Collection
|
|
83
|
+
from render_engine_json import JSONCollection, JSONPageParser
|
|
84
|
+
|
|
85
|
+
site = Site()
|
|
86
|
+
|
|
87
|
+
json_collection = """
|
|
88
|
+
[
|
|
89
|
+
{
|
|
90
|
+
"title": "My JSON Page",
|
|
91
|
+
"content": "This is my json page"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"title": "My JSON Page 2",
|
|
95
|
+
"content": "This is my json page 2"
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
@site.collection
|
|
101
|
+
class JSONStringCollection(Collection):
|
|
102
|
+
parser = JSONPageParser
|
|
103
|
+
content = json_collection
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
render_engine_json/__init__.py,sha256=wimtl1Qk3sdOGgbRKAFxFRtS7WzCNhYCJczZlExBqA0,75
|
|
2
|
+
render_engine_json/parsers.py,sha256=gdPvXXBCXZCc2v307w1NTn7raIrj93dRKLtRD2YFdTY,2022
|
|
3
|
+
render_engine_json-0.0.0.dist-info/METADATA,sha256=UQDkI9DkHst8cBbKT3HY11hwGbC90s7BjVGnkngn1VU,2446
|
|
4
|
+
render_engine_json-0.0.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
5
|
+
render_engine_json-0.0.0.dist-info/top_level.txt,sha256=uy_rvUD5pKUt6k50tJdJ-h40GMtfTvzY1XDaJ3y0Jog,19
|
|
6
|
+
render_engine_json-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
render_engine_json
|