Compare commits

...

2 Commits

Author SHA1 Message Date
941e06e3d7 wip: implement page 2025-07-08 14:16:14 +02:00
6d6af200ab add: bozodown 2025-07-08 14:16:00 +02:00
7 changed files with 199 additions and 5 deletions

139
src/bozodown.py Normal file
View File

@ -0,0 +1,139 @@
from collections.abc import Callable
from typing import Any
def _photohub_render(id: str, text: str) -> str:
if (id == "img"):
return f"<img src='{text[2:-1]}'>"
def _bozodown_render(id: str, text: str) -> str:
if (id == "list"):
print("error: list not supported")
return ""
_specific_case_namespaces: dict[str, Callable[[str, str], str]] = {
"bozodown": _bozodown_render,
"photohub": _photohub_render,
}
_converters: list[dict[str, str]] = [
{
"from_prefix": "# ",
"from_suffix": "\n",
"to_prefix": "<h1>",
"to_suffix": "</h1>",
},
{
"from_prefix": "## ",
"from_suffix": "\n",
"to_prefix": "<h2>",
"to_suffix": "</h2>",
},
{
"from_prefix": "### ",
"from_suffix": "\n",
"to_prefix": "<h3>",
"to_suffix": "</h3>",
},
{
"from_prefix": "#### ",
"from_suffix": "\n",
"to_prefix": "<h4>",
"to_suffix": "</h4>",
},
{
"from_prefix": "`",
"from_suffix": "`",
"to_prefix": "<p><code>",
"to_suffix": "</code></p>",
},
{
"from_prefix": "**",
"from_suffix": "**",
"to_prefix": "<strong>",
"to_suffix": "</strong>",
},
{
"from_prefix": "- ",
"from_suffix": "\n",
"code": "bozodown:list",
},
{
"from_prefix": "```",
"from_suffix": "```",
"to_prefix": "<pre><code>",
"to_suffix": "</code></pre>",
},
{
"from_prefix": "![",
"from_suffix": "]",
"code": "photohub:img",
},
{
"from_prefix": "- [ ] ",
"from_suffix": "\n",
"to_prefix": "<li><input type='checkbox' disabled> ",
"to_suffix": "</li>",
},
{
"from_prefix": "- [x] ",
"from_suffix": "\n",
"to_prefix": "<li><input type='checkbox' checked disabled> ",
"to_suffix": "</li>",
},
{
"from_prefix": "<br>",
"from_suffix": "",
"to_prefix": "<br>",
},
]
_default_converter: dict[str, str] = {
"from_prefix": "",
"to_prefix": "<p>",
"to_suffix": "</p>",
}
def _render_element(text: str, converter: dict[str, str]) -> str:
code: str = converter.get("code")
if (code is not None):
namespace, id = code.split(":")
print(namespace, id)
func = _specific_case_namespaces[namespace]
return func(id, text)
print(converter)
start: int = len(converter["from_prefix"])
stop: int = len(text) - len(converter.get("from_suffix", ""))
return f"{converter['to_prefix']}{text[start:stop]}{converter['to_suffix']}"
def _get_first_converter(text: str) -> tuple[str, dict] | None:
first_converter_found: dict[str, str | Callable[[str, list[str]], Any]] | None = None
start: int | None = None
for converter in _converters:
matching_patern_pos: str = text.find(converter['from_prefix'])
if (matching_patern_pos != -1):
if (first_converter_found is None or matching_patern_pos < start):
first_converter_found = converter
start = matching_patern_pos
if (first_converter_found is None):
return text, _default_converter
if (start != 0):
return text[:start], _default_converter
suffix: int = first_converter_found.get("from_suffix", "")
prefix: int = first_converter_found['from_prefix']
stop: int = text.find(suffix, start)
if (stop == -1):
print(f"error: '{prefix}' was never finished by a '{suffix}'")
return
stop += len(suffix)
return text[start:stop], first_converter_found
def render(raw_content: str) -> str:
content: str = ""
to_parse: str = raw_content
while len(to_parse) > 0:
text, converter = _get_first_converter(to_parse)
content += _render_element(text, converter)
to_parse = to_parse[len(text):]
return content

View File

@ -2,4 +2,5 @@
CREATE_GENERAL_CATEGORY: bool = True
THUMB_DIMENSION: tuple[int, int] = (200, 200)
MAX_THREADS: int = 50
MAX_THREADS: int = 50
PAGE_EXT: str = ".ph"

View File

@ -6,6 +6,8 @@ from path import Path
from picture import Picture
from bulk_page import BulkPage
from bulk_category import BulkCategory
from page import Page
import argparse
import config
@ -21,7 +23,7 @@ def argument_parsing():
args = parser.parse_args()
return args
def scan_pages(folders: list[Path]) -> list[BulkPage]:
def scan_bulk_pages(folders: list[Path]) -> list[BulkPage]:
pages: list[BulkPage] = []
prev: BulkPage = None
with Bar("Scanning Pages...", max=len(folders)) as bar:
@ -81,7 +83,7 @@ def create_categories(categories: list[BulkCategory]) -> None:
def gen_bulk(bulk_path: Path):
category_path: Path = Path(bulk_path, "categories")
pages: list[BulkPage] = scan_pages(bulk_path.get_dirs())
pages: list[BulkPage] = scan_bulk_pages(bulk_path.get_dirs())
categories: list[BulkCategory] = scan_categories(pages, category_path)
if config.CREATE_GENERAL_CATEGORY:
@ -97,9 +99,16 @@ def gen_bulk(bulk_path: Path):
category_path.create()
create_categories(categories)
def scan_pages(site_path: Path):
for path in [f for f in site_path.get_files() if f.get_name().endswith(config.PAGE_EXT)]:
page: Page = Page(path)
page.create()
def gen_pages(site_path: Path):
scan_pages(site_path)
def regen(bulk_path: Path, is_thumb: bool, is_small: bool):
pages: list[BulkPage] = scan_pages(bulk_path.get_dirs())
pages: list[BulkPage] = scan_bulk_pages(bulk_path.get_dirs())
with Bar("Regenerating assets...", max=len(pages)) as bar:
for page in pages:
for picture in page.get_pictures():
@ -124,6 +133,7 @@ def main():
if args.regen is not None:
regen(bulk_path, 't' in args.regen, 's' in args.regen)
gen_bulk(bulk_path)
gen_pages(site_path)
gen_home(site_path)

28
src/page.py Normal file
View File

@ -0,0 +1,28 @@
from __future__ import annotations
from typing import TYPE_CHECKING
import config
import bozodown
from path import Path
class Page:
def __init__(self, path: Path):
self._path: Path = path
self._html: Path = Path(self._path.get_absolute_path()[:-len(config.PAGE_EXT)] + ".html")
with open(path.get_absolute_path(), "r") as f:
self._raw_content: str = f.read()
def _get_content(self):
content = bozodown.render(self._raw_content)
return content
def _to_html(self) -> str:
html: str = self._get_content()
return html
def create(self) -> Path:
with open(self._html.get_absolute_path(), "w") as f:
f.write(self._to_html())

View File

@ -71,4 +71,8 @@ class Path():
return self._absolute_path
def __repr__(self):
return f"Path({self._absolute_path})"
return f"Path({self._absolute_path})"
def __eq__(self, value):
if (isinstance(value, Path)):
self = Path(value.get_absolute_path())

0
src/templates/page.css Normal file
View File

12
src/templates/page.jinja Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/page.css">
</head>
<body>
<h1>{{ category.name }}</h1>
</body>
</html>