plain.pages 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.
- plain_pages-0.0.0/LICENSE +28 -0
- plain_pages-0.0.0/PKG-INFO +15 -0
- plain_pages-0.0.0/plain/pages/README.md +1 -0
- plain_pages-0.0.0/plain/pages/__init__.py +5 -0
- plain_pages-0.0.0/plain/pages/config.py +18 -0
- plain_pages-0.0.0/plain/pages/default_settings.py +1 -0
- plain_pages-0.0.0/plain/pages/exceptions.py +6 -0
- plain_pages-0.0.0/plain/pages/markdown.py +34 -0
- plain_pages-0.0.0/plain/pages/pages.py +68 -0
- plain_pages-0.0.0/plain/pages/registry.py +45 -0
- plain_pages-0.0.0/plain/pages/templates/page.html +9 -0
- plain_pages-0.0.0/plain/pages/urls.py +44 -0
- plain_pages-0.0.0/plain/pages/views.py +48 -0
- plain_pages-0.0.0/pyproject.toml +25 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Dropseed, LLC
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: plain.pages
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: Dave Gaeddert
|
|
6
|
+
Author-email: dave.gaeddert@dropseed.dev
|
|
7
|
+
Requires-Python: >=3.8,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Dist: mistune (>=3.0.0,<4.0.0)
|
|
15
|
+
Requires-Dist: python-frontmatter (>=1.0.0,<2.0.0)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Pages
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from plain.packages import PackageConfig, packages
|
|
4
|
+
from plain.runtime import APP_PATH
|
|
5
|
+
|
|
6
|
+
from .registry import registry
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PlainPagesConfig(PackageConfig):
|
|
10
|
+
name = "plain.pages"
|
|
11
|
+
|
|
12
|
+
def ready(self):
|
|
13
|
+
for pacakge_config in packages.get_package_configs():
|
|
14
|
+
registry.discover_pages(
|
|
15
|
+
os.path.join(pacakge_config.path, "templates", "pages")
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
registry.discover_pages(os.path.join(APP_PATH, "templates", "pages"))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
PAGES_VIEW_CLASS = "plain.pages.PageView"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import mistune
|
|
2
|
+
from pygments import highlight
|
|
3
|
+
from pygments.formatters import html
|
|
4
|
+
from pygments.lexers import get_lexer_by_name
|
|
5
|
+
|
|
6
|
+
from plain.utils.text import slugify
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PagesRenderer(mistune.HTMLRenderer):
|
|
10
|
+
def heading(self, text, level, **attrs):
|
|
11
|
+
"""Automatically add an ID to headings if one is not provided."""
|
|
12
|
+
|
|
13
|
+
if "id" not in attrs:
|
|
14
|
+
attrs["id"] = slugify(text)
|
|
15
|
+
|
|
16
|
+
return super().heading(text, level, **attrs)
|
|
17
|
+
|
|
18
|
+
def block_code(self, code, info=None):
|
|
19
|
+
"""Highlight code blocks using Pygments."""
|
|
20
|
+
|
|
21
|
+
if info:
|
|
22
|
+
lexer = get_lexer_by_name(info, stripall=True)
|
|
23
|
+
formatter = html.HtmlFormatter(wrapcode=True)
|
|
24
|
+
return highlight(code, lexer, formatter)
|
|
25
|
+
|
|
26
|
+
return "<pre><code>" + mistune.escape(code) + "</code></pre>"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def render_markdown(content):
|
|
30
|
+
renderer = PagesRenderer(escape=False)
|
|
31
|
+
markdown = mistune.create_markdown(
|
|
32
|
+
renderer=renderer, plugins=["strikethrough", "table"]
|
|
33
|
+
)
|
|
34
|
+
return markdown(content)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import frontmatter
|
|
4
|
+
|
|
5
|
+
from plain.templates import Template
|
|
6
|
+
from plain.utils.functional import cached_property
|
|
7
|
+
|
|
8
|
+
from .markdown import render_markdown
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Page:
|
|
12
|
+
def __init__(self, url_path, relative_path, absolute_path):
|
|
13
|
+
self.url_path = url_path
|
|
14
|
+
self.relative_path = relative_path
|
|
15
|
+
self.absolute_path = absolute_path
|
|
16
|
+
|
|
17
|
+
@cached_property
|
|
18
|
+
def _frontmatter(self):
|
|
19
|
+
with open(self.absolute_path) as f:
|
|
20
|
+
return frontmatter.load(f)
|
|
21
|
+
|
|
22
|
+
@cached_property
|
|
23
|
+
def vars(self):
|
|
24
|
+
return self._frontmatter.metadata
|
|
25
|
+
|
|
26
|
+
@cached_property
|
|
27
|
+
def title(self):
|
|
28
|
+
default_title = os.path.splitext(os.path.basename(self.relative_path))[0]
|
|
29
|
+
return self.vars.get("title", default_title)
|
|
30
|
+
|
|
31
|
+
@cached_property
|
|
32
|
+
def content(self):
|
|
33
|
+
# Strip the frontmatter
|
|
34
|
+
content = self._frontmatter.content
|
|
35
|
+
|
|
36
|
+
if not self.vars.get("render_plain", False):
|
|
37
|
+
template = Template(os.path.join("pages", self.relative_path))
|
|
38
|
+
content = template.render({})
|
|
39
|
+
# Strip the frontmatter again, since it was in the template file itself
|
|
40
|
+
_, content = frontmatter.parse(content)
|
|
41
|
+
|
|
42
|
+
if self.content_type == "markdown":
|
|
43
|
+
content = render_markdown(content)
|
|
44
|
+
|
|
45
|
+
return content
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def content_type(self):
|
|
49
|
+
extension = os.path.splitext(self.absolute_path)[1]
|
|
50
|
+
|
|
51
|
+
# Explicitly define the known content types that we intend to handle
|
|
52
|
+
# (others will still pass through)
|
|
53
|
+
if extension == ".md":
|
|
54
|
+
return "markdown"
|
|
55
|
+
|
|
56
|
+
if extension == ".html":
|
|
57
|
+
return "html"
|
|
58
|
+
|
|
59
|
+
if extension == ".redirect":
|
|
60
|
+
return "redirect"
|
|
61
|
+
|
|
62
|
+
return extension.lstrip(".")
|
|
63
|
+
|
|
64
|
+
def get_template_name(self):
|
|
65
|
+
if template_name := self.vars.get("template_name"):
|
|
66
|
+
return template_name
|
|
67
|
+
|
|
68
|
+
return f"{self.content_type}.html"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from .exceptions import PageNotFoundError
|
|
4
|
+
from .pages import Page
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PagesRegistry:
|
|
8
|
+
"""
|
|
9
|
+
The registry loads up all the pages at once, so we only have to do a
|
|
10
|
+
dict key lookup at runtime to get a page.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def __init__(self):
|
|
14
|
+
# url path -> file path
|
|
15
|
+
self.registered_pages = {}
|
|
16
|
+
|
|
17
|
+
def register_page(self, url_path, relative_path, absolute_path):
|
|
18
|
+
self.registered_pages[url_path] = (url_path, relative_path, absolute_path)
|
|
19
|
+
|
|
20
|
+
def url_paths(self):
|
|
21
|
+
return self.registered_pages.keys()
|
|
22
|
+
|
|
23
|
+
def discover_pages(self, pages_dir):
|
|
24
|
+
for root, dirs, files in os.walk(pages_dir):
|
|
25
|
+
for file in files:
|
|
26
|
+
relative_path = os.path.relpath(os.path.join(root, file), pages_dir)
|
|
27
|
+
url_path = os.path.splitext(relative_path)[0]
|
|
28
|
+
absolute_path = os.path.join(root, file)
|
|
29
|
+
|
|
30
|
+
if os.path.basename(url_path) == "index":
|
|
31
|
+
url_path = os.path.dirname(url_path)
|
|
32
|
+
|
|
33
|
+
self.register_page(url_path, relative_path, absolute_path)
|
|
34
|
+
|
|
35
|
+
def get_page(self, url_path):
|
|
36
|
+
try:
|
|
37
|
+
url_path, relative_path, absolute_path = self.registered_pages[url_path]
|
|
38
|
+
# Instantiate the page here, so we don't store a ton of cached data over time
|
|
39
|
+
# as we render all the pages
|
|
40
|
+
return Page(url_path, relative_path, absolute_path)
|
|
41
|
+
except KeyError:
|
|
42
|
+
raise PageNotFoundError(f"Could not find a page for {url_path}")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
registry = PagesRegistry()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from plain.runtime import settings
|
|
2
|
+
from plain.urls import include, path
|
|
3
|
+
from plain.utils.module_loading import import_string
|
|
4
|
+
|
|
5
|
+
from .registry import registry
|
|
6
|
+
|
|
7
|
+
default_namespace = "pages"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_page_urls():
|
|
11
|
+
"""
|
|
12
|
+
Generate a list of real urls based on the files that exist.
|
|
13
|
+
This way, you get a concrete url reversingerror if you try
|
|
14
|
+
to refer to a page/url that isn't going to work.
|
|
15
|
+
"""
|
|
16
|
+
paths = []
|
|
17
|
+
|
|
18
|
+
view_class = import_string(settings.PAGES_VIEW_CLASS)
|
|
19
|
+
|
|
20
|
+
for url_path in registry.url_paths():
|
|
21
|
+
if url_path == "":
|
|
22
|
+
# The root index is a special case and should be
|
|
23
|
+
# referred to as pages:index
|
|
24
|
+
url = ""
|
|
25
|
+
name = "index"
|
|
26
|
+
else:
|
|
27
|
+
url = url_path + "/"
|
|
28
|
+
name = url_path
|
|
29
|
+
|
|
30
|
+
paths.append(
|
|
31
|
+
path(
|
|
32
|
+
url,
|
|
33
|
+
view_class,
|
|
34
|
+
name=name,
|
|
35
|
+
kwargs={"url_path": url_path},
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return paths
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
urlpatterns = [
|
|
43
|
+
path("", include(get_page_urls())),
|
|
44
|
+
]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from plain.http import Http404, ResponsePermanentRedirect, ResponseRedirect
|
|
2
|
+
from plain.utils.functional import cached_property
|
|
3
|
+
from plain.views import TemplateView
|
|
4
|
+
|
|
5
|
+
from .exceptions import PageNotFoundError, RedirectPageError
|
|
6
|
+
from .registry import registry
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PageView(TemplateView):
|
|
10
|
+
template_name = "page.html"
|
|
11
|
+
|
|
12
|
+
@cached_property
|
|
13
|
+
def page(self):
|
|
14
|
+
# Passed manually by the kwargs in the path definition
|
|
15
|
+
url_path = self.url_kwargs.get("url_path", "index")
|
|
16
|
+
|
|
17
|
+
try:
|
|
18
|
+
return registry.get_page(url_path)
|
|
19
|
+
except PageNotFoundError:
|
|
20
|
+
raise Http404()
|
|
21
|
+
|
|
22
|
+
def get_template_names(self) -> list[str]:
|
|
23
|
+
"""
|
|
24
|
+
Allow for more specific user templates like
|
|
25
|
+
markdown.html or html.html
|
|
26
|
+
"""
|
|
27
|
+
return [self.page.get_template_name()] + super().get_template_names()
|
|
28
|
+
|
|
29
|
+
def get_template_context(self):
|
|
30
|
+
context = super().get_template_context()
|
|
31
|
+
context["page"] = self.page
|
|
32
|
+
return context
|
|
33
|
+
|
|
34
|
+
def get(self):
|
|
35
|
+
if self.page.content_type == "redirect":
|
|
36
|
+
url = self.page.vars.get("url")
|
|
37
|
+
|
|
38
|
+
if not url:
|
|
39
|
+
raise RedirectPageError(
|
|
40
|
+
f"Redirect page {self.page.url_path} is missing a url"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
if self.page.vars.get("temporary", True):
|
|
44
|
+
return ResponseRedirect(url)
|
|
45
|
+
else:
|
|
46
|
+
return ResponsePermanentRedirect(url)
|
|
47
|
+
|
|
48
|
+
return super().get()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "plain.pages"
|
|
3
|
+
packages = [
|
|
4
|
+
{ include = "plain" },
|
|
5
|
+
]
|
|
6
|
+
|
|
7
|
+
version = "0.0.0"
|
|
8
|
+
description = ""
|
|
9
|
+
authors = ["Dave Gaeddert <dave.gaeddert@dropseed.dev>"]
|
|
10
|
+
|
|
11
|
+
[tool.poetry.dependencies]
|
|
12
|
+
python = "^3.8"
|
|
13
|
+
mistune = "^3.0.0"
|
|
14
|
+
python-frontmatter = "^1.0.0"
|
|
15
|
+
|
|
16
|
+
[tool.poetry.dev-dependencies]
|
|
17
|
+
pytest = "^7.1.2"
|
|
18
|
+
ipdb = "^0.13.9"
|
|
19
|
+
isort = "^5.10.1"
|
|
20
|
+
black = "^23.1.0"
|
|
21
|
+
pytest-django = "^4.5.2"
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["poetry-core>=1.0.0"]
|
|
25
|
+
build-backend = "poetry.core.masonry.api"
|