diff --git a/base.html b/base.html deleted file mode 100644 index fa1e655..0000000 --- a/base.html +++ /dev/null @@ -1,16 +0,0 @@ - - - Dreamii - - - -
-
-

Dreamii

-
-
- {% block content %}{% endblock %} -
-
- - diff --git a/index.md b/index.md index bbadbd1..7dba00f 100644 --- a/index.md +++ b/index.md @@ -1,55 +1,6 @@ +# Hallo - -{% extends "base.html" %} - - -{% block content %} - - - -{% markdown %} - -Welcome to Dreamii, the fastest web framework for Python to build sites with! Dynamic and static! Use Python as template language like PHP. Normally this requires CGI but Dreamii is way faster! Your application is async by default. Also, you can use async await in the py3 block tags. - -## Syntax highligthing -### Python ```python -print("Hi, i'm syntax highlighted python") +import numpy as np + ``` -## C -```c -#include - -int main() { - printf("Hi, i'm syntax highlighted c"); - return 0; -} -``` -{% endmarkdown %} - -{% markdown %}# Python database{% endmarkdown %} - - - -{% py3 %} - -counter = dreamii.db["counter"].find_one(counter_name="main_counter") -if not counter: - counter = {"count":0,"counter_name":"main_counter"} -counter["count"] += 1 -dreamii.db["counter"].upsert(counter,["counter_name"]) -print(f"

This page had been visited {counter['count']} times.

") -{% endpy3 %} - -{% markdown %}## Python sub process execution{% endmarkdown %} -{% py3 %} -print("

List first file of directory using `ls`

") -print("
First file: ",system("ls",output=False).split("\n")[0],"
") -print("

Memory information

") -print("
",system("free -h",output=False),"
") -print("

Disk information

") -print("
",system("df -h",output=False),"
") -print("

Process information

") -print("
",system("ps aux",output=False,stderr=True),"
") -{% endpy3 %} -{% endblock content %} diff --git a/src/Dreamii.egg-info/PKG-INFO b/src/Dreamii.egg-info/PKG-INFO index 122598b..93057dd 100644 --- a/src/Dreamii.egg-info/PKG-INFO +++ b/src/Dreamii.egg-info/PKG-INFO @@ -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( "" ) {% 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"

This page had been visited {counter()} times.

") {% 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. diff --git a/src/dreamii/app.py b/src/dreamii/app.py index cc306b0..a09eb0a 100644 --- a/src/dreamii/app.py +++ b/src/dreamii/app.py @@ -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\" %}{% block content %}{% markdown %}"+content+"{% endmarkdown %}{% endblock %}" + else: + markdown_default_page = "\n".join([ + "", + "", + "", + "", + "" + html.escape(path.split("/")[-1].rstrip(".md")) +"", + "", + "", + "{% markdown %}"+content+"{% endmarkdown %}", + "" + ]) + 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'])