Browse Source

Page generation with child page structure

JoostSijm 6 years ago
parent
commit
f48a8a61c5
4 changed files with 56 additions and 5 deletions
  1. 8 1
      app/models.py
  2. 22 3
      app/modules/backend/app.py
  3. 1 1
      app/modules/static/app.py
  4. 25 0
      app/templates/public/site.j2

+ 8 - 1
app/models.py

@@ -59,7 +59,14 @@ class Page(db.Model):
 
     def url(self):
         """Generate URL for page"""
-        return quote(self.title.lower())
+        return quote(self.title.strip().lower().replace(" ", "_"))
+
+
+    def path(self):
+        """Generate path with parents"""
+        if self.parent_id:
+            return '%s/%s' % (self.parent.path(), self.url())
+        return self.url()
 
     user_id = db.Column(
         db.Integer,

+ 22 - 3
app/modules/backend/app.py

@@ -3,6 +3,8 @@
 Backend
 """
 
+import os
+
 from flask_login import login_required, login_user, logout_user, current_user
 from flask_menu import Menu, register_menu
 from flask import render_template, request, redirect, url_for, flash, Blueprint, abort
@@ -164,10 +166,27 @@ def view_page(page_id):
 @login_required
 def render():
     """Render pages to file"""
-    pages = Page.query.all()
+    pages = Page.query.filter(Page.parent_id == None).all()
+    path_base = 'app/modules/static/pages/'
     for page in pages:
-        with open('app/modules/static/pages/%s.html' % page.url(), 'w') as file:
-            file.write(page.content())
+        render_page(path_base, page)
 
     flash('Successfully rendered pages.', 'success')
     return redirect(url_for('backend.index'))
+
+def render_page(path, page):
+    """Function for page generation, recursive"""
+    path = path + page.url()
+    if page.children.count():
+        parent_path = path + '/'
+        if not os.path.exists(parent_path):
+            os.makedirs(parent_path)
+        for child_page in page.children:
+            render_page(parent_path, child_page)
+
+    with open('%s.html' % path, 'w') as file:
+        rendered_page = render_template(
+            'public/site.j2',
+            page=page,
+        )
+        file.write(rendered_page)

+ 1 - 1
app/modules/static/app.py

@@ -18,7 +18,7 @@ BLUEPRINT = Blueprint(
 )
 
 @BLUEPRINT.route("/", defaults={"page": "index"})
-@BLUEPRINT.route("/<page>")
+@BLUEPRINT.route("/<path:page>")
 def show(page):
     """Display static page"""
     try:

+ 25 - 0
app/templates/public/site.j2

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>{{ page.title }} - ssg</title>
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+</head>
+<body>
+    <ul>
+        {%- for child_page in page.children recursive -%}
+        <li class="nav-item" data-toggle="tooltip" data-placement="right" title="{{ child_page.title }}">
+            <a class="nav-link" href="/{{ child_page.path() }}">
+                <span class="nav-link-text">{{ child_page.title }}</span>
+            </a>
+            {% if child_page.children.count() %}
+            <ul>
+                {{ loop(child_page.children) }}
+            </ul>
+            {% endif %}
+        </li>
+        {%- endfor -%}
+    </ul>
+    <div>
+        {{ page.content() }}
+    </div>
+</body>