diff --git a/app_42.py b/app_42.py index 0c6fe812f..75f453bad 100644 --- a/app_42.py +++ b/app_42.py @@ -377,10 +377,30 @@ def projects_page(): "n/projects.md", page=deps.page, ) + @app.route("/n/") def n_any_page(page): + try: + return render_md_as_html_template( + f"n/{page}.md", + page=deps.page, + ) + except FileNotFoundError: + return render_template( + f"n/{page}.html", + page=deps.page, + ) + + @app.route("/mind/") + def mind_any_page(page): return render_md_as_html_template( - f"n/{page}.md", + f"mind/{page}.md", + page=deps.page, + ) + @app.route("/mind") + def mind_index(): + return render_md_as_html_template( + f"mind/index.md", page=deps.page, ) @@ -393,8 +413,8 @@ def n_page(): @app.route("/n/cv") def n_cv_page(): - return render_md_as_html_template( - "n/cv.md", + return render_template( + "n/cv.html", page=deps.page, ) diff --git a/potyk-io.db b/potyk-io.db index 39f16534d..7c8d05ba1 100644 Binary files a/potyk-io.db and b/potyk-io.db differ diff --git a/potyk_io_back/admin.py b/potyk_io_back/admin.py index f39009975..8dc53b054 100644 --- a/potyk_io_back/admin.py +++ b/potyk_io_back/admin.py @@ -1,7 +1,9 @@ +import os.path + import flask from flask import render_template -from potyk_io_back.blog_pages import BlogPageForm, BlogPage +from potyk_io_back.blog_pages import BlogPageForm, BlogPage, BlogPageStore from potyk_io_back.index_and_feed import ( FeedForm, FeedCard, @@ -11,6 +13,24 @@ ) +def create_page(page_form, page_store: BlogPageStore, templates_path: str): + page = BlogPage(**page_form.data) + page_store.insert(page) + + page_path = os.path.join(templates_path, page.html_path) + if not os.path.exists(page_path): + os.makedirs(os.path.dirname(page_path), exist_ok=True) + with open(page_path, 'w', encoding='utf-8') as f: + f.write('''{% extends "_layouts/base.html" %} +{% block main %} + +TODO + +{% endblock %}''') + + return page + + def add_admin_routes(app: flask.Flask, deps): @app.route("/admin", methods=["GET", "POST"]) def admin_page(): @@ -20,8 +40,7 @@ def admin_page(): if flask.request.method == "POST": if page_form.validate_on_submit(): - page = BlogPage(**page_form.data) - deps.page_store.insert(page) + page = create_page(page_form, deps.page_store, flask.current_app.template_folder) return render_template("_components/htmx_success.html", page=page) else: return render_template("_components/htmx_error.html", error=page_form.errors) diff --git a/potyk_io_back/blog_pages.py b/potyk_io_back/blog_pages.py index 2c90f1ce0..291d0431b 100644 --- a/potyk_io_back/blog_pages.py +++ b/potyk_io_back/blog_pages.py @@ -2,11 +2,17 @@ import os.path from enum import auto from pathlib import Path +from re import Match import flask import frontmatter +import markdown import mistune -from mistune import HTMLRenderer +from mistune import HTMLRenderer, InlineParser, InlineState +from mistune.plugins.footnotes import footnotes +from mistune.plugins.formatting import strikethrough +from mistune.plugins.speedup import speedup +from mistune.plugins.table import table from mistune.plugins.task_lists import task_lists from flask import render_template_string, render_template from flask_wtf import FlaskForm @@ -135,8 +141,16 @@ def __init__(self, sqlite_cursor): def insert(self, page): self.q.execute( - "insert into blog_pages " "(url, html_path, title, desc, section) " "values " "(?, ?, ?, ?, ?)", - (page.url, page.html_path, page.title, page.desc, page.section), + "insert into blog_pages (url, html_path, title, desc, section, include_in_index, breadcrumbs_title) values (?, ?, ?, ?, ?, ?, ?)", + ( + page.url, + page.html_path, + page.title, + page.desc, + page.section, + page.include_in_index, + page.breadcrumbs_title, + ), commit=True, ) @@ -201,22 +215,30 @@ class BlogPageForm(FlaskForm): class TaskListsAndNoEscapeRenderer(HTMLRenderer): def task_list_item(self, text, checked=False, **attrs): return render_template("_components/md_checkbox.html", checked=checked, text=text) + def text(self, text: str) -> str: return text def paragraph(self, text: str) -> str: - if text.strip().startswith('{%'): + if text.strip().startswith(("{%", "{{")): return text else: - return '

' + text + '

\n' - + return "

" + text + "

\n" -markdown_to_html = mistune.Markdown(TaskListsAndNoEscapeRenderer(escape=False), plugins=[task_lists],) +markdown_to_html = mistune.Markdown( + TaskListsAndNoEscapeRenderer(escape=False), + plugins=[ + strikethrough, + footnotes, + table, + task_lists, + ], +) def render_md_as_html_template(template, **kwargs): - template_path = Path(flask.current_app.template_folder).resolve() / template + template_path = BASE_DIR / flask.current_app.template_folder / template md = frontmatter.load(str(template_path)).content diff --git a/templates/_layouts/base.html b/templates/_layouts/base.html index f7c87a7ce..109d78c3a 100644 --- a/templates/_layouts/base.html +++ b/templates/_layouts/base.html @@ -11,11 +11,11 @@ - {{ title }} - + {{ page.title }} + - - + + @@ -37,7 +37,7 @@ - {% if config.is_prod and page.url != '/login' %} + {% if config.is_prod and page.url != '/login' %}