render-engine-json 0.0.0__tar.gz

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,25 @@
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
+ // README at: https://github.com/devcontainers/templates/tree/main/src/python
3
+ {
4
+ "name": "Python 3",
5
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6
+ "image": "mcr.microsoft.com/devcontainers/python:0-3.11-bullseye",
7
+ "features": {
8
+ "ghcr.io/devcontainers/features/github-cli:1": {}
9
+ }
10
+
11
+ // Features to add to the dev container. More info: https://containers.dev/features.
12
+ // "features": {},
13
+
14
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
15
+ // "forwardPorts": [],
16
+
17
+ // Use 'postCreateCommand' to run commands after the container is created.
18
+ // "postCreateCommand": "pip3 install --user -r requirements.txt",
19
+
20
+ // Configure tool-specific properties.
21
+ // "customizations": {},
22
+
23
+ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
24
+ // "remoteUser": "root"
25
+ }
@@ -0,0 +1,108 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at kjaymiller@gmail.com.
63
+ All complaints will be reviewed and investigated promptly and fairly.
64
+
65
+ All community leaders are obligated to respect the privacy and security of the
66
+ reporter of any incident.
67
+
68
+ ## Enforcement Guidelines
69
+
70
+ Community leaders will follow these Community Impact Guidelines in determining
71
+ the consequences for any action they deem in violation of this Code of Conduct:
72
+
73
+ ### 1. Correction
74
+
75
+ **Community Impact**: Use of inappropriate language or other behavior deemed
76
+ unprofessional or unwelcome in the community.
77
+
78
+ **Consequence**: A private, written warning from community leaders, providing
79
+ clarity around the nature of the violation and an explanation of why the
80
+ behavior was inappropriate. A public apology may be requested.
81
+
82
+ ### 2. Permanent Ban
83
+
84
+ **Community Impact**: Demonstrating a pattern of violation of community
85
+ standards, including sustained inappropriate behavior, harassment of an
86
+ individual, or aggression toward or disparagement of classes of individuals.
87
+
88
+ **Consequence**: A permanent ban from any sort of public interaction within the
89
+ community.
90
+
91
+ ## Attribution
92
+
93
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
94
+ version 2.1, available at
95
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
96
+
97
+ Community Impact Guidelines were inspired by
98
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
99
+
100
+ For answers to common questions about this code of conduct, see the FAQ at
101
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
102
+ [https://www.contributor-covenant.org/translations][translations].
103
+
104
+ [homepage]: https://www.contributor-covenant.org
105
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
106
+ [Mozilla CoC]: https://github.com/mozilla/diversity
107
+ [FAQ]: https://www.contributor-covenant.org/faq
108
+ [translations]: https://www.contributor-covenant.org/translations
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,30 @@
1
+ name: Build distribution
2
+
3
+ on:
4
+ release:
5
+ types: [created]
6
+
7
+ jobs:
8
+ deploy:
9
+ name: Deploy to PyPI
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - name: Checkout source
13
+ uses: actions/checkout@v4
14
+
15
+ - name: Set up Python 3.12
16
+ uses: actions/setup-python@v4
17
+ with:
18
+ python-version: 3.12
19
+
20
+ - name: Install build dependencies
21
+ run: python -m pip install build twine
22
+
23
+ - name: Build distributions
24
+ run: python -m build
25
+
26
+ - name: Publish package to PyPI
27
+ uses: pypa/gh-action-pypi-publish@release/v1
28
+ with:
29
+ user: __token__
30
+ password: ${{ secrets.TWINE_PASSWORD}}
@@ -0,0 +1,162 @@
1
+ /.vscode# Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ .vscode
@@ -0,0 +1,26 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.4.0
4
+ hooks:
5
+ - id: check-yaml
6
+ - id: end-of-file-fixer
7
+ - id: trailing-whitespace
8
+ - repo: https://github.com/psf/black
9
+ rev: 22.12.0
10
+ hooks:
11
+ - id: black
12
+ - repo: https://github.com/PyCQA/isort
13
+ rev: 5.11.4
14
+ hooks:
15
+ - id: isort
16
+ args: ["--profile", "black"]
17
+ # Add pip-compile to pre-commit hooks
18
+ - repo: https://github.com/jazzband/pip-tools
19
+ rev: 6.12.1
20
+ hooks:
21
+ - id: pip-compile
22
+ name: pip-compile
23
+ args: ["--extra=dev", "--upgrade", "pyproject.toml"]
24
+ - id: pip-compile
25
+ name: pip-compile
26
+ args: ["--extra=docs", "--upgrade", "pyproject.toml", "-o", "docs/requirements.txt"]
@@ -0,0 +1,16 @@
1
+ {
2
+ "workbench.colorCustomizations": {
3
+ "commandCenter.border": "#15202b99",
4
+ "statusBar.background": "#f9e64f",
5
+ "statusBar.foreground": "#15202b",
6
+ "statusBarItem.hoverBackground": "#f7df1e",
7
+ "statusBarItem.remoteBackground": "#f9e64f",
8
+ "statusBarItem.remoteForeground": "#15202b",
9
+ "titleBar.activeBackground": "#f9e64f",
10
+ "titleBar.activeForeground": "#15202b",
11
+ "titleBar.inactiveBackground": "#f9e64f99",
12
+ "titleBar.inactiveForeground": "#15202b99"
13
+ },
14
+ "peacock.color": "#f9e64f",
15
+ "peacock.remoteColor": "#832561"
16
+ }
@@ -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,95 @@
1
+ # Render Engine JSON
2
+
3
+ JSON Parser and Collection Module for Render Engine
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install render_engine_json
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ The `render-engine-json` extends render-engine to allow you to build pages and collections using JSON.
14
+
15
+ 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.
16
+
17
+ > **NOTE**
18
+ > 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.
19
+
20
+ ### Single JSON Page Entry
21
+
22
+ To create a json entry you will need the `JSONPageParser` class as the Parser for your `Page` Object.
23
+
24
+ You can pass the page a json string or a json file path.
25
+
26
+ #### As a string
27
+
28
+ ```python
29
+
30
+ from render_engine import Page, Site
31
+ from render_engine_json import JSONPageParser
32
+
33
+ site = Site()
34
+
35
+ json_page = """
36
+ {
37
+ "title": "My JSON Page",
38
+ "content": "This is my json page"
39
+ }
40
+ """
41
+
42
+ @site.page
43
+ class JSONStringPage(Page):
44
+ parser = JSONPageParser
45
+ content = json_page
46
+
47
+ ```
48
+
49
+ #### As a file
50
+
51
+ ```python
52
+
53
+ @site.page
54
+ class JSONFilePage(Page):
55
+ parser = JSONPageParser
56
+ content_path = "path/to/json/file.json"
57
+
58
+ ```
59
+
60
+ ### JSON Collection
61
+
62
+ To create a JSON collection you will need the `JSONCollection` Object.
63
+
64
+ You can pass the collection a json string or a json file path.
65
+
66
+ #### As a string
67
+
68
+ ```python
69
+ from render_engine import Site, Collection
70
+ from render_engine_json import JSONCollection, JSONPageParser
71
+
72
+ site = Site()
73
+
74
+ json_collection = """
75
+ [
76
+ {
77
+ "title": "My JSON Page",
78
+ "content": "This is my json page"
79
+ },
80
+ {
81
+ "title": "My JSON Page 2",
82
+ "content": "This is my json page 2"
83
+ }
84
+ ]
85
+ """
86
+
87
+ @site.collection
88
+ class JSONStringCollection(Collection):
89
+ parser = JSONPageParser
90
+ content = json_collection
91
+
92
+ ```
93
+
94
+
95
+ ```
@@ -0,0 +1,42 @@
1
+ [build-system]
2
+ requires = ["setuptools", "setuptools_scm", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "render_engine_json"
7
+ dynamic = [ "version" ]
8
+ description = "JSON Parser and Collection for Render Engine"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ dependencies = [
12
+ "render-engine >= 2023.12.1b3",
13
+ "python-slugify"
14
+ ]
15
+
16
+
17
+ [project.optional-dependencies]
18
+ dev=[
19
+ "pytest"
20
+ ]
21
+
22
+ [tool.setuptools]
23
+ package-dir = {"" = "src"}
24
+
25
+ [project.urls]
26
+ homepage = "https://github.com/kjaymiller/render_engine_json/"
27
+ repository = "https://github.com/kjaymiller/render_engine_json/"
28
+
29
+ [tool.pytest.ini_options]
30
+ pythonpath = ["src"]
31
+
32
+ [tool.semantic_release]
33
+ version_toml = "pyproject.toml:project.version"
34
+ branch = "main"
35
+
36
+ [tool.ruff]
37
+ select = ["E", "F", "I", "UP"]
38
+ target-version = "py311"
39
+ line-length = 120
40
+ src = ["src"]
41
+ ignore-init-module-imports = true
42
+
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,2 @@
1
+ from .collection import JSONCollection
2
+ from .parsers import JSONPageParser
@@ -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,17 @@
1
+ .gitignore
2
+ .pre-commit-config.yaml
3
+ README.md
4
+ pyproject.toml
5
+ test_JSONPage_parser.py
6
+ .devcontainer/devcontainer.json
7
+ .github/CODE_OF_CONDUCT.md
8
+ .github/LICENSE.md
9
+ .github/workflows/publish.yml
10
+ .vscode/settings.json
11
+ src/render_engine_json/__init__.py
12
+ src/render_engine_json/parsers.py
13
+ src/render_engine_json.egg-info/PKG-INFO
14
+ src/render_engine_json.egg-info/SOURCES.txt
15
+ src/render_engine_json.egg-info/dependency_links.txt
16
+ src/render_engine_json.egg-info/requires.txt
17
+ src/render_engine_json.egg-info/top_level.txt
@@ -0,0 +1,5 @@
1
+ render-engine>=2023.12.1b3
2
+ python-slugify
3
+
4
+ [dev]
5
+ pytest
@@ -0,0 +1 @@
1
+ render_engine_json
@@ -0,0 +1,19 @@
1
+ import pytest
2
+
3
+ from render_engine_json.parsers import JSONPageParser
4
+
5
+
6
+ def test_JSONPageParser_parses_content_no_slug():
7
+ CONTENT = '{ "title": "Hello", "content": "Hello World" }'
8
+ assert JSONPageParser().parse_content(CONTENT) == (
9
+ {"title": "Hello"},
10
+ "Hello World",
11
+ )
12
+
13
+
14
+ def test_JSONPageParser_parses_content_with_slug():
15
+ CONTENT = '{ "slug": { "title": "Hello", "content": "Hello World" } }'
16
+ assert JSONPageParser().parse_content(CONTENT) == (
17
+ {"slug": "slug", "title": "Hello"},
18
+ "Hello World",
19
+ )