A lot of bugfix.

This commit is contained in:
2025-01-28 00:42:24 +01:00
parent 66147497a8
commit 6a6ba976b3
4 changed files with 48 additions and 85 deletions
+3 -1
View File
@@ -50,7 +50,7 @@ These are instructions for USING Dreamii, not to modify it.
python3 -m venv .venv
source .venv/bin/activate
pip install git+https://molodetz.nl/retoor/dreamii.git
echo > "# My first dreamii site" > index.md
echo "# My first dreamii site" > index.md
dreamii serve 7331
```
@@ -93,6 +93,7 @@ print(
"</pre>"
)
{% endpy3 %}
```
### Example database usage
See here an example of a visitor counter on your website. This is all you have to do. It's a persistent database. No configuration required! For more information how to use the database, visit the [dataset documentation](https://dataset.readthedocs.io/en/latest/).
@@ -108,6 +109,7 @@ def increment(counter_name="default")
print(f"<p>This page had been visited {counter()} times.</p>")
{% endpy3 %}
```
## Hidden files
Files prefixed with `_` or `.` will not be displayed or rendered by Dreamii. This is for security and it is a convenient way to mark base templates.
+42 -16
View File
@@ -28,9 +28,10 @@
from app.app import Application as BaseApplication, BaseView
import pathlib
from aiohttp import web
from dreamii.markdown import MarkdownExtension, MarkdownRenderer
from dreamii.markdown import MarkdownExtension, MarkdownRenderer, render_markdown_sync
from dreamii.python import PythonExtension
import aiohttp_jinja2
import html
class TemplateView(BaseView):
@@ -38,34 +39,59 @@ class TemplateView(BaseView):
path = pathlib.Path(self.request.app.template_path).joinpath(str(path).lstrip('/'))
if path.exists() and path.suffix in ['.html', '.md']:
return str(path)
if path.joinpath('.html').exists():
return str(path.joinpath('.html'))
if path.joinpath('.md').exists():
elif path.joinpath('.md').exists():
return str(path.joinpath('.md'))
if path.is_dir():
elif path.joinpath('.html').exists():
return str(path.joinpath('.html'))
elif path.is_dir():
if path.joinpath('index.html').exists():
return str(path.joinpath('index.html'))
if path.joinpath('index.md').exists():
elif path.joinpath('index.md').exists():
return str(path.joinpath('index.md'))
return None
async def render_template(self, path):
context = {'request': self.request, 'post': await self.request.post() or None}
context = {}
context['request'] = self.request
context['post'] = await self.request.post()
if not context['post']:
context['post'] = None
try:
context['json'] = await self.request.json()
except:
context['json'] = None
pass
response = None
if str(path.endswith('.md')):
if str(path).endswith(".md"):
renderer = MarkdownRenderer(self.request.app, path)
response = web.Response(text=renderer.render())
else:
response = await super().render_template(path, self.request, context)
response.headers['Content-Type'] = 'text/html'
return response
content = pathlib.Path(path).read_text()
if pathlib.Path(self.template_path).joinpath("_base.html").exists():
markdown_default_page = "{% extends \"_base.html\" %}<html>{% block content %}<style>{{ highlight_styles }}</style>{% markdown %}"+content+"{% endmarkdown %}{% endblock %}</body></html>"
else:
markdown_default_page = "\n".join([
"<html>",
"<head>",
"<meta charset=\"utf-8\">",
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">",
"<title>" + html.escape(path.split("/")[-1].rstrip(".md")) +"</title>",
"<style>{{ highlight_styles }}</style>",
"</head>",
"<body>{% markdown %}"+content+"{% endmarkdown %}</body>",
"</html>"
])
with open(".temp.html", "w+") as f:
f.write(markdown_default_page)
content = aiohttp_jinja2.render_string(".temp.html",self.request,context=context)
pathlib.Path(".temp.html").unlink()
response = web.Response(text=content, content_type="text/html")
return response
return await super().render_template(path)
async def get(self):
path = await self.resolve_template(self.request.match_info['tail'])