page navigation

This commit is contained in:
Minecon724 2024-05-26 14:53:09 +02:00
parent c075e4ac86
commit f49d4ddfed
4 changed files with 28 additions and 10 deletions

View file

@ -1 +1,3 @@
- navigating pages
- README & LICENSE
- basic standalone template
- git as part of software

View file

@ -57,3 +57,9 @@ class Site:
return Site(custom=custom, **kwargs)
@dataclass
class Page:
index: int
last: int
url_previous: str
url_next: str

View file

@ -1,10 +1,11 @@
from json import loads
from math import ceil
from os import walk, mkdir
from os.path import isdir, join, exists
from shutil import copytree
from typing import Dict
from article import Article, Site
from article import Article, Page, Site
from template import TemplateEnvironment
def compile(work_directory: str, template_directory: str=None, target_directory: str=None, force: bool=False):
@ -53,19 +54,26 @@ def compile(work_directory: str, template_directory: str=None, target_directory:
articles += [article]
page = 1
page_index = 1
pages = ceil(len(articles) / articles_per_page)
while len(articles) > 0:
if page == 1:
page = Page(
page_index, pages,
f'/index/page{page_index - 1}.html' if page > 1 else None,
f'/index/page{page_index + 1}.html' if page < pages else None
)
if page_index == 1:
fn = join(target_directory, 'index.html')
else:
fn = join(target_directory, 'index', f'page{page}.html')
fn = join(target_directory, 'index', f'page{page_index}.html')
articles_on_page = articles[:articles_per_page] # TODO make this customizable
content = template.process_index(*articles_on_page)
content = template.process_index(page, *articles_on_page)
file = open(fn, 'w')
file.write(content)
file.close()
articles = articles[articles_per_page:]
page += 1
page_index += 1

View file

@ -1,10 +1,11 @@
from article import Article, Site
from dataclasses import dataclass
from io import TextIOWrapper
from jinja2 import Environment, Template, FileSystemLoader, select_autoescape
from json import loads
from os.path import join
from article import Article, Page, Page
TEMPLATE_VERSIONS = [1]
@dataclass
@ -55,8 +56,9 @@ class TemplateEnvironment:
article = article
)
def process_index(self, *articles: Article) -> str:
def process_index(self, page: Page, *articles: Article) -> str:
return self.index_template.render(
site = self.site,
page = page,
articles = articles
)