diff --git a/src/bulk_page.py b/src/bulk_page.py deleted file mode 100644 index 255183c..0000000 --- a/src/bulk_page.py +++ /dev/null @@ -1,78 +0,0 @@ -from __future__ import annotations - -from jinja2 import Environment, FileSystemLoader -import photodown -import os - -from path import Path - -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from bulk_page import Page - from picture import Picture - -env = Environment(loader=FileSystemLoader('src/templates')) -page_template = env.get_template('bulk_page.jinja') - -class BulkPage(): - - def __init__(self, path: Path, name: str, pictures: list[Picture] = None, prev: BulkPage|None = None, next: BulkPage|None = None): - self.name: str = name - self._path: Path = path - self._pictures: list[Picture] = pictures or [] - self._exif: Path = Path(self._path, "exif.txt") - self._readme: Path = Path(self._path, "readme.md") - self._raw: Path = Path(self._path, name + ".NEF") - self.html: Path = Path(self._path, "page.html") - self.prev: Page = prev - self.next: Page = next - if (not self._readme.exist()): - self._readme.touch() - - def add_picture(self, picture: Picture) -> None: - self._pictures.append(picture) - - def get_pictures(self) -> list[Picture]: - return self._pictures - - def _to_html(self) -> str|None: - html_rendered = page_template.render(page=self) - return html_rendered - - def create(self) -> Path: - with open(self.html.get_absolute_path(), "w") as f: - f.write(self._to_html()) - - def render_readme(self) -> str|None: - if not self._readme.exist(): - return None - with open(self._readme.get_absolute_path(), 'r') as f: - text = f.read() - if len(text) == 0: - return None - html = photodown.markdown(text) - return html - - def _gen_exif(self): - if self._raw.exist(): - if os.system(f"exiftool {self._raw.get_absolute_path()} > {self._exif.get_absolute_path()} 2>/dev/null") == 0: - return 0 - os.remove(self._exif.get_absolute_path()) - return 1 - - def get_exif(self) -> Path | None: - if not self._exif.exist(): - if (self._gen_exif()): - return None - return self._exif - - def render_exif(self): - if not self.get_exif(): - return None - - with open(self._exif.get_absolute_path()) as f: - return f.read() - - def get_raw(self): - return self._raw \ No newline at end of file diff --git a/src/bulk_category.py b/src/category.py similarity index 91% rename from src/bulk_category.py rename to src/category.py index d30acd6..1bfb73b 100644 --- a/src/bulk_category.py +++ b/src/category.py @@ -10,9 +10,9 @@ if TYPE_CHECKING: from path import Path env = Environment(loader=FileSystemLoader('src/templates')) -category_template = env.get_template('bulk_category.jinja') +category_template = env.get_template('category.jinja') -class BulkCategory(): +class Category(): def __init__(self, name: str, categorys_path: Path, pictures: list[Picture] = None, is_repertoried: bool = False): self.name: str = name diff --git a/src/collection.py b/src/collection.py new file mode 100644 index 0000000..88a5140 --- /dev/null +++ b/src/collection.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +import config +from path import Path + +if TYPE_CHECKING: + from photodown import Photodown + +from jinja2 import Environment, FileSystemLoader + +env = Environment(loader=FileSystemLoader('src/templates')) +page_template = env.get_template('collection.jinja') + +class Collection: + + def __init__(self, path: Path, markdown: Photodown): + self._path: Path = path + self._html: Path = Path(self._path.get_absolute_path()[:-len(config.COLLECTION_EXT)] + ".html") + self._markdown: Photodown = markdown + with open(path.get_absolute_path(), "r") as f: + self._raw_content: str = f.read() + + def _get_content(self): + content = self._markdown.render(self._raw_content) + return content + + def _to_html(self) -> str: + html: str = page_template.render(content=self._get_content()) + return html + + def create(self) -> Path: + with open(self._html.get_absolute_path(), "w") as f: + f.write(self._to_html()) \ No newline at end of file diff --git a/src/config.py b/src/config.py index 06f65c1..119bb22 100644 --- a/src/config.py +++ b/src/config.py @@ -3,4 +3,4 @@ CREATE_GENERAL_CATEGORY: bool = True THUMB_DIMENSION: tuple[int, int] = (200, 200) MAX_THREADS: int = 50 -PAGE_EXT: str = ".ph" \ No newline at end of file +COLLECTION_EXT: str = ".ph" \ No newline at end of file diff --git a/src/main.py b/src/main.py index 0d59c2e..dc3c34b 100644 --- a/src/main.py +++ b/src/main.py @@ -2,9 +2,9 @@ from progress.bar import Bar from path import Path from picture import Picture -from bulk_page import BulkPage -from bulk_category import BulkCategory from page import Page +from category import Category +from collection import Collection from photodown import Photodown import argparse @@ -22,14 +22,14 @@ def argument_parsing(): args = parser.parse_args() return args -def scan_bulk_pages(folders: list[Path]) -> list[BulkPage]: - pages: list[BulkPage] = [] - prev: BulkPage = None +def scan_pages(folders: list[Path]) -> list[Page]: + pages: list[Page] = [] + prev: Page = None with Bar("Scanning Pages...", max=len(folders)) as bar: for folder in folders: files: list[Path] = folder.get_files() - - page: BulkPage = BulkPage(folder, folder.get_name(), prev=prev) + + page: Page = Page(folder, folder.get_name(), prev=prev) raw: Path = Path(folder, folder.get_name() + ".NEF") id: int = 0 @@ -52,28 +52,28 @@ def scan_bulk_pages(folders: list[Path]) -> list[BulkPage]: pages[0].prev = pages[-1] return pages -def create_pages(pages: list[BulkPage]) -> None: +def create_pages(pages: list[Page]) -> None: with Bar("Generating Pages...", max=len(pages)) as bar: for page in pages: page.create() bar.next() -def scan_categories(pages: list[BulkPage], categories_path: Path) -> list[BulkCategory]: - categories: dict[str, BulkCategory] = {} +def scan_categories(pages: list[Page], categories_path: Path) -> list[Category]: + categories: dict[str, Category] = {} with Bar("Scanning pages...", max=len(pages)) as bar: for page in pages: for picture in page.get_pictures(): for category_name in picture.get_categories_name(): - category: BulkCategory | None = categories.get(category_name) + category: Category | None = categories.get(category_name) if (category is None): - category = BulkCategory(category_name, categories_path) + category = Category(category_name, categories_path) categories.update({category_name: category}) picture.categories.append(category) category.add_picture(picture) bar.next() return (categories.values()) -def create_categories(categories: list[BulkCategory]) -> None: +def create_categories(categories: list[Category]) -> None: with Bar("Generating categories...", max=len(categories)) as bar: for category in categories: category.create() @@ -82,8 +82,8 @@ def create_categories(categories: list[BulkCategory]) -> None: def gen_bulk(bulk_path: Path, markdown: Photodown): category_path: Path = Path(bulk_path, "categories") - pages: list[BulkPage] = scan_bulk_pages(bulk_path.get_dirs()) - categories: list[BulkCategory] = scan_categories(pages, category_path) + pages: list[Page] = scan_pages(bulk_path.get_dirs()) + categories: list[Category] = scan_categories(pages, category_path) bulk: list[Picture] = [] for page in pages: @@ -96,25 +96,25 @@ def gen_bulk(bulk_path: Path, markdown: Photodown): if (category.name == "general"): category.path = Path(bulk_path, "index.html") - Path("./src/templates/bulk_page.css").copy_to(Path(bulk_path, "bulk_page.css")) + Path("./src/templates/page.css").copy_to(Path(bulk_path, "page.css")) create_pages(pages) - Path("./src/templates/bulk_category.css").copy_to(Path(bulk_path, "bulk_category.css")) + Path("./src/templates/category.css").copy_to(Path(bulk_path, "category.css")) if (not category_path.exist()): category_path.create() create_categories(categories) -def scan_pages(site_path: Path, markdown: Photodown): - for path in [f for f in site_path.get_files() if f.get_name().endswith(config.PAGE_EXT)]: - page: Page = Page(path, markdown) - page.create() +def scan_collections(site_path: Path, markdown: Photodown): + for path in [f for f in site_path.get_files() if f.get_name().endswith(config.COLLECTION_EXT)]: + collection: Collection = Collection(path, markdown) + collection.create() -def gen_pages(site_path: Path, markdown: Photodown): - scan_pages(site_path, markdown) - Path("./src/templates/page.css").copy_to(Path(site_path, "page.css")) +def gen_collections(site_path: Path, markdown: Photodown): + scan_collections(site_path, markdown) + Path("./src/templates/collection.css").copy_to(Path(site_path, "collection.css")) -def regen(bulk_path: Path, is_thumb: bool, is_small: bool): - pages: list[BulkPage] = scan_bulk_pages(bulk_path.get_dirs()) +def regen(_path: Path, is_thumb: bool, is_small: bool): + pages: list[Page] = scan_pages(_path.get_dirs()) with Bar("Regenerating assets...", max=len(pages)) as bar: for page in pages: for picture in page.get_pictures(): @@ -140,7 +140,7 @@ def main(): regen(bulk_path, 't' in args.regen, 's' in args.regen) markdown = Photodown() gen_bulk(bulk_path, markdown) - gen_pages(site_path, markdown) + gen_collections(site_path, markdown) gen_home(site_path) diff --git a/src/page.py b/src/page.py index 1be425f..1594546 100644 --- a/src/page.py +++ b/src/page.py @@ -1,34 +1,77 @@ from __future__ import annotations +from jinja2 import Environment, FileSystemLoader +import photodown +import os + +from path import Path + from typing import TYPE_CHECKING -import config -from path import Path - if TYPE_CHECKING: - from photodown import Photodown - -from jinja2 import Environment, FileSystemLoader + from picture import Picture env = Environment(loader=FileSystemLoader('src/templates')) page_template = env.get_template('page.jinja') -class Page: - - def __init__(self, path: Path, markdown: Photodown): - self._path: Path = path - self._html: Path = Path(self._path.get_absolute_path()[:-len(config.PAGE_EXT)] + ".html") - self._markdown: Photodown = markdown - with open(path.get_absolute_path(), "r") as f: - self._raw_content: str = f.read() - def _get_content(self): - content = self._markdown.render(self._raw_content) - return content +class Page(): + + def __init__(self, path: Path, name: str, pictures: list[Picture] = None, prev: Page|None = None, next: Page|None = None): + self.name: str = name + self._path: Path = path + self._pictures: list[Picture] = pictures or [] + self._exif: Path = Path(self._path, "exif.txt") + self._readme: Path = Path(self._path, "readme.md") + self._raw: Path = Path(self._path, name + ".NEF") + self.html: Path = Path(self._path, "page.html") + self.prev: Page = prev + self.next: Page = next + if (not self._readme.exist()): + self._readme.touch() - def _to_html(self) -> str: - html: str = page_template.render(content=self._get_content()) + def add_picture(self, picture: Picture) -> None: + self._pictures.append(picture) + + def get_pictures(self) -> list[Picture]: + return self._pictures + + def _to_html(self) -> str|None: + html_rendered = page_template.render(page=self) + return html_rendered + + def create(self) -> Path: + with open(self.html.get_absolute_path(), "w") as f: + f.write(self._to_html()) + + def render_readme(self) -> str|None: + if not self._readme.exist(): + return None + with open(self._readme.get_absolute_path(), 'r') as f: + text = f.read() + if len(text) == 0: + return None + html = photodown.markdown(text) return html - def create(self) -> Path: - with open(self._html.get_absolute_path(), "w") as f: - f.write(self._to_html()) \ No newline at end of file + def _gen_exif(self): + if self._raw.exist(): + if os.system(f"exiftool {self._raw.get_absolute_path()} > {self._exif.get_absolute_path()} 2>/dev/null") == 0: + return 0 + os.remove(self._exif.get_absolute_path()) + return 1 + + def get_exif(self) -> Path | None: + if not self._exif.exist(): + if (self._gen_exif()): + return None + return self._exif + + def render_exif(self): + if not self.get_exif(): + return None + + with open(self._exif.get_absolute_path()) as f: + return f.read() + + def get_raw(self): + return self._raw \ No newline at end of file diff --git a/src/picture.py b/src/picture.py index 0c7692a..3bd7e88 100644 --- a/src/picture.py +++ b/src/picture.py @@ -10,11 +10,11 @@ from typing import TYPE_CHECKING import config if TYPE_CHECKING: - from bulk_page import BulkPage - from bulk_category import BulkCategory + from page import Page + from category import Category class Picture(): - def __init__(self, picture_path: Path, id: int, page: BulkPage = None, raw: Path|None = None, is_repertoried: bool = True): + def __init__(self, picture_path: Path, id: int, page: Page = None, raw: Path|None = None, is_repertoried: bool = True): self._large: Path = picture_path self._small: Path = Path(picture_path.get_absolute_path()[:-4] + "_small.jpg") self._thumb: Path = Path(picture_path.get_absolute_path()[:-4] + "_thumb.jpg") @@ -22,9 +22,9 @@ class Picture(): self._categories_file: Path = Path(picture_path.get_absolute_path()[:-4] + "_categories.txt") self._raw: Path|None = raw self.id: int = id - self._page: BulkPage = page + self._page: Page = page self._categories_name: list[str] = [] - self.categories: list[BulkCategory] = [] + self.categories: list[Category] = [] if self._categories_file.exist(): with open(self._categories_file.get_absolute_path(), "r+") as f: categories_name: list[str] = f.read() diff --git a/src/templates/bulk_page.css b/src/templates/bulk_page.css deleted file mode 100644 index d1a97bd..0000000 --- a/src/templates/bulk_page.css +++ /dev/null @@ -1,87 +0,0 @@ -:root { - --bg1: #002b36; - --bg2: #073642; - --content1: #586e75; - --content2: #657b83; - --content3: #839496; - --content4: #93a1a1; - --lbg1: #eee8d5; - --lbg2: #fdf6e3; -} - -body { - background-color: var(--bg1); - display: flex; - flex-direction: column; - align-items: center; - margin: 15% 15%; - margin-top: 5%; - height: auto; -} - -.readme { - background-color: var(--bg2); -} - -.picture-container { - background-color: var(--bg2); - padding: 10px; -} - -.picture { - padding: 5px; - background-color: var(--content1); -} - -.albums_container { - margin-right: 0; -} - -img { - max-width: 100%; - height: auto; -} - -.meta-picture, .navigation { - display: flex; - flex-direction: row; - justify-content: space-between; - width: 100%; -} - -.profile { - text-align: right; -} - -a { - color: white; - text-decoration: none; - font-size: 1.5rem; - } - -@media screen and (max-width:767px) { - body { - margin: 0% 0%; - } -} - -.exif, .readme { - width: 100%; - display: block; -} - -.download { - width: 100%; - background-color: var(--bg2); - text-align: center; -} - -.exif summary { - text-align: center; -} - -.exif details { - display: flex; - justify-content: center; - background-color: var(--bg2); -} \ No newline at end of file diff --git a/src/templates/bulk_page.jinja b/src/templates/bulk_page.jinja deleted file mode 100644 index 872af85..0000000 --- a/src/templates/bulk_page.jinja +++ /dev/null @@ -1,70 +0,0 @@ - - - -
- - - - - - {% if page.render_readme() %} -- {{ page.render_exif() }} --
+ {{ page.render_exif() }} ++