diff --git a/README.md b/README.md index 9074370..8ee91aa 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,14 @@ ## Installing dependencies +```sh +$ python3 -m virtualenv venv +$ . venv/bin/activate +$ pip install -r requirements.txt +``` + +Or + ```sh $ python3 -m pip install --user -r requirements.txt ``` diff --git a/blog.json b/blog.json index 7cfb4aa..1b67a99 100644 --- a/blog.json +++ b/blog.json @@ -30,6 +30,7 @@ "page-title": "Ari::web -> Blog", "page-description": "My blog page", "colourscheme-type": "dark", + "short-name": "Ari's blogs", "home-keywords": [ "ari", "ari-web", @@ -40,6 +41,15 @@ "linux" ], "base-homepage": "https://ari-web.xyz/", + "meta-icons": [ + { + "src": "/favicon.ico", + "sizes": "128x128", + "type": "image/png" + } + ], + "theme-colour": "#f9f6e8", + "background-colour": "#262220", "blogs": { "new-blog-management-system-": { "title": "TmV3IGJsb2cgbWFuYWdlbWVudCBzeXN0ZW0h", diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 654694a..0000000 --- a/manifest.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "$schema": "https://json.schemastore.org/web-manifest-combined.json", - "short_name": "Ari-web blogs", - "name": "Ari::web -> Blog", - "description": "My blog page", - "icons": [{ - "src": "/favicon.ico", - "sizes": "128x128", - "type": "image/png" - }], - "start_url": ".", - "display": "standalone", - "theme_color": "#f9f6e8", - "background_color": "#262220" -} diff --git a/netlify.toml b/netlify.toml index 4fdf836..f2a713d 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,5 +1,5 @@ [build] - command = "python3 ./scripts/blog clean && python3 ./scripts/blog build" + command = "python3 ./scripts/blog static" [[redirects]] from = "/git/*" diff --git a/scripts/blog b/scripts/blog index 0946e5a..bcfab62 100755 --- a/scripts/blog +++ b/scripts/blog @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- """Manage blogs""" +import hashlib import json import os import random @@ -53,8 +54,12 @@ DEFAULT_CONFIG: Dict = { "page-title": "Ari::web -> Blog", "page-description": "My blog page", "colourscheme-type": "dark", + "short-name": "Ari's blogs", "home-keywords": ["ari", "ari-web", "blog", "ari-archer", "foss", "free", "linux"], "base-homepage": "https://ari-web.xyz/", + "meta-icons": [{"src": "/favicon.ico", "sizes": "128x128", "type": "image/png"}], + "theme-colour": "#f9f6e8", + "background-colour": "#262220", "blogs": {}, } DEFAULT_CONFIG_FILE: str = "blog.json" @@ -126,6 +131,27 @@ def log(message: str, header: str = "ERROR", code: int = EXIT_ERR) -> int: return code +def generate_file_hash(filename: str): + hash_filename = filename + ".hash" + + log(filename, "READ") + + with open(filename, "rb") as file: + file_content = file.read() + + log(hash_filename, "GENERATE") + + with open(hash_filename, "w") as hash_file: + for hashf in hashlib.algorithms_available: + try: + print( + f"{hashf} {getattr(hashlib, hashf)(file_content).hexdigest()}", + file=hash_file, + ) + except (TypeError, AttributeError): + pass + + def sanitise_title(title: str, titleset: Dict) -> str: _title: str = "" @@ -242,8 +268,6 @@ def build(config: Dict) -> Tuple[int, Dict]: if len(config["blogs"]) < 1: return log("Cannot build no blogs"), config - code_timestamp = code_timer() - if os.path.isdir(config["blog-dir"]): rmtree(config["blog-dir"]) @@ -351,10 +375,6 @@ def build(config: Dict) -> Tuple[int, Dict]: ) ) - log( - f"Successfully built blog page in {code_timer() - code_timestamp} seconds", - "TIME", - ) return EXIT_OK, config @@ -501,6 +521,8 @@ def clean(config: Dict) -> Tuple[int, Dict]: config["blog-dir"], "index.html", "content/*.min.*", + "*.hash", + "manifest.json", } def remove(file: str) -> None: @@ -520,6 +542,51 @@ def clean(config: Dict) -> Tuple[int, Dict]: return EXIT_OK, config +def generate_metadata(config: Dict) -> Tuple[int, Dict]: + """Generate metadata""" + + with open("manifest.json", "w") as manifest: + log(f"Generating {manifest.name}...", "GENERATE") + json.dump( + { + "$schema": "https://json.schemastore.org/web-manifest-combined.json", + "short_name": config["short-name"], + "name": config["page-title"], + "description": config["page-description"], + "icons": config["meta-icons"], + "start_url": ".", + "display": "standalone", + "theme_color": config["theme-colour"], + "background_color": config["background-colour"], + }, + manifest, + ) + + log(f"Hashing {manifest.name}", "HASH") + generate_file_hash(manifest.name) + + log(f"Hashing {DEFAULT_CONFIG_FILE}", "HASH") + generate_file_hash(DEFAULT_CONFIG_FILE) + + return EXIT_OK, config + + +def generate_static_full(config: Dict) -> Tuple[int, Dict]: + """Generate full static site""" + + build_cfg: Dict = { + "Cleaning up": clean, + "Building static site": build, + "Generating metatata": generate_metadata, + } + + for logger_msg, function in build_cfg.items(): + log(f"{logger_msg}...", "STATIC") + _, config = function(config) + + return EXIT_OK, config + + SUBCOMMANDS: Dict = { "help": dummy, "new": new_blog, @@ -529,6 +596,8 @@ SUBCOMMANDS: Dict = { "edit": edit, "new-config": gen_new_config, "clean": clean, + "metadata": generate_metadata, + "static": generate_static_full, } @@ -567,8 +636,12 @@ def main() -> int: return usage(EXIT_OK) with open(DEFAULT_CONFIG_FILE, "r") as lcfg: + cmd_time_init = code_timer() + code, config = SUBCOMMANDS[sys.argv[1]](config=json.load(lcfg)) + log(f"Finished in {code_timer() - cmd_time_init} seconds with code {code}", "TIME") + if config["blogs"]: log("Sorting blogs by creation time...", "CLEANUP") config["blogs"] = dict(