From 2bd968cfbab8df0c4d2ffd154589972b29055131 Mon Sep 17 00:00:00 2001 From: vitrinekast Date: Sun, 13 Apr 2025 10:24:24 +0200 Subject: [PATCH] linting things --- app.py | 187 ++++++++++++++----------------------- src/assets/app.js | 6 +- src/content/thesis.md | 29 +++++- src/templates/base.jinja | 16 ++-- src/templates/thesis.jinja | 13 ++- 5 files changed, 116 insertions(+), 135 deletions(-) diff --git a/app.py b/app.py index 1dd25e0..2988cad 100644 --- a/app.py +++ b/app.py @@ -1,23 +1,19 @@ -import subprocess -import os -from pathlib import Path -import shutil import csv +import os import re -from datetime import datetime - -from jinja2 import Environment, PackageLoader, select_autoescape -import frontmatter -from slugify import slugify -import pypandoc - +import shutil +import subprocess import xml.etree.ElementTree as ET +from datetime import datetime +from pathlib import Path + +import frontmatter +import pypandoc +from jinja2 import Environment, PackageLoader, select_autoescape +from slugify import slugify # TODO make newsletter URL's absolute to klank.school -env = Environment( - loader=PackageLoader("src"), - autoescape=select_autoescape() -) +env = Environment(loader=PackageLoader("src"), autoescape=select_autoescape()) CONTENT_D = os.path.abspath("src/content") OUTPUT_D = "dist" @@ -27,13 +23,6 @@ documents = {} now = datetime.now() word_count = 0 -# Utils - - -def getParam(params, index): - return params[index] if len(params) > index else False - - # jinja filter that can list documents def listDocuments(params): param = params.split(" ") @@ -42,57 +31,47 @@ def listDocuments(params): return html -# jinja filter to make a slug out of a stirng - - -def slugify_filter(value): - return slugify(value) # Source: https://github.com/gandreadis/markdown-word-count - - def count_words_in_markdown(text): # Comments - text = re.sub(r'', '', text, flags=re.MULTILINE) + text = re.sub(r"", "", text, flags=re.MULTILINE) # Tabs to spaces - text = text.replace('\t', ' ') + text = text.replace("\t", " ") # More than 1 space to 4 spaces - text = re.sub(r'[ ]{2,}', ' ', text) + text = re.sub(r"[ ]{2,}", " ", text) # Footnotes - text = re.sub(r'^\[[^]]*\][^(].*', '', text, flags=re.MULTILINE) + text = re.sub(r"^\[[^]]*\][^(].*", "", text, flags=re.MULTILINE) # Indented blocks of code - text = re.sub(r'^( {4,}[^-*]).*', '', text, flags=re.MULTILINE) + text = re.sub(r"^( {4,}[^-*]).*", "", text, flags=re.MULTILINE) # Replace newlines with spaces for uniform handling - text = text.replace('\n', ' ') + text = text.replace("\n", " ") # Custom header IDs - text = re.sub(r'{#.*}', '', text) + text = re.sub(r"{#.*}", "", text) # Remove images - text = re.sub(r'!\[[^\]]*\]\([^)]*\)', '', text) + text = re.sub(r"!\[[^\]]*\]\([^)]*\)", "", text) # Remove HTML tags - text = re.sub(r']*>', '', text) + text = re.sub(r"]*>", "", text) # Remove special characters - text = re.sub(r'[#*`~\-–^=<>+|/:]', '', text) + text = re.sub(r"[#*`~\-–^=<>+|/:]", "", text) # Remove footnote references - text = re.sub(r'\[[0-9]*\]', '', text) + text = re.sub(r"\[[0-9]*\]", "", text) # Remove enumerations - text = re.sub(r'[0-9#]*\.', '', text) + text = re.sub(r"[0-9#]*\.", "", text) return len(text.split()) + # jinja filter for date formatting - - -def prettydate(value, format='%d/%m/%Y'): +def prettydate(value, format="%d/%m/%Y"): return datetime.fromtimestamp(int(value)).strftime(format) # jinja filter to replace shortcodes in HTML def shortcode_filter(value): - shortcode_callbacks = { - "show": listDocuments - } + shortcode_callbacks = {"show": listDocuments} def shortcode_replacer(match): @@ -109,15 +88,15 @@ def shortcode_filter(value): env.filters["shortcode"] = shortcode_filter -env.filters["slugify"] = slugify_filter +env.filters["slugify"] = slugify env.filters["prettydate"] = prettydate + # translate a single file into HTML - - -def render_single_file(page, path, dist, name=False): +def render_single_file(path, dist, name=False): name = Path(path).stem template = env.select_template([f"{name}.jinja", "post.jinja"]) + page = get_page_data(path) html = template.render(documents=documents, page=page, name=name) if not os.path.exists(dist): @@ -141,34 +120,20 @@ def get_existing_page(path, slug): if folder == "content": return False - for doc in documents[folder]: - if doc: - if doc["slug"] == slug: - return doc + return [item for item in documents[folder] if item.get("slug") == slug] return False -# build a slug including the folder - - -def get_slug(path, folder, filename): - if folder == "content": - return slugify(filename) - else: - return slugify(f"{folder}/{filename}") # compile markdown into cited HTML - - def get_page_data(path): global word_count filename = Path(path).stem folder = Path(path).parent.name - slug = get_slug(path, folder, filename) - prerendered = get_existing_page(path, slug) + slug = slugify(filename) if folder == "content" else slugify(f"{folder}/{filename}") - if prerendered: + if prerendered := get_existing_page(path, slug): return prerendered page = frontmatter.load(path) @@ -177,19 +142,18 @@ def get_page_data(path): page["folder"] = folder if "start_datetime" in page: - page["has_passed"] = datetime.fromtimestamp( - page["start_datetime"]) < now + page["has_passed"] = datetime.fromtimestamp(page["start_datetime"]) < now content = page.content - if "`include" in page.content: + if ".include" in page.content: + print("doing an include!") content = pypandoc.convert_text( page.content, - to='md', - format='md', - extra_args=[ - "--lua-filter=include-files.lua" - ]) + to="md", + format="md", + extra_args=["--lua-filter=include-files.lua"], + ) page.body = pypandoc.convert_text( content, @@ -199,13 +163,13 @@ def get_page_data(path): "--citeproc", "--bibliography=library.bib", "--csl=harvard-cite-them-right.csl", - ]) + ], + ) return page + # Do stuff to the circuit's pcb - - def save_circuit_svg(filepath, outpath, name): tree = ET.parse(filepath) @@ -232,8 +196,7 @@ def render_posts(path, output_path=OUTPUT_D): file_path = Path(path) / filename if file_path.suffix == ".md": - render_single_file(get_page_data(file_path), - file_path, f"{output_path}/{name}") + render_single_file(file_path, f"{output_path}/{name}") elif file_path.is_dir(): render_posts(file_path, f"{output_path}/{name}") elif file_path.suffix == ".svg": @@ -242,16 +205,18 @@ def render_posts(path, output_path=OUTPUT_D): os.makedirs(f"{output_path}/{name}", exist_ok=True) shutil.copyfile(file_path, f"{output_path}/{name}/{filename}") + # Pre-load before compiling - - def preload_documents(): global documents - version = subprocess.check_output( - ["git", "rev-list", "--count", "HEAD"]).decode("utf-8").strip() + version = ( + subprocess.check_output(["git", "rev-list", "--count", "HEAD"]) + .decode("utf-8") + .strip() + ) - documents["meta"] = {"now": now.strftime("%d %B %Y"), "version": version} + documents["meta"] = {"now": now.strftime("%d %B %Y - %H:%M:%S"), "version": version} for subdir in os.listdir(CONTENT_D): path = os.path.join(CONTENT_D, subdir) @@ -265,20 +230,14 @@ def preload_documents(): if filename.endswith(".md"): documents[name].append(get_page_data(cpath)) elif os.path.isdir(cpath): - documents[name].append(get_page_data( - os.path.join(cpath, "index.md"))) + documents[name].append( + get_page_data(os.path.join(cpath, "index.md")) + ) - elif Path(path).suffix == '.md': + elif Path(path).suffix == ".md": documents[Path(path).stem] = get_page_data(path) -def copy_assets(): - if os.path.exists(OUT_ASSETS): - shutil.rmtree(OUT_ASSETS) - - shutil.copytree(SRC_ASSETS, OUT_ASSETS) - - def get_inventory(): global documents @@ -291,28 +250,17 @@ def get_inventory(): def get_wordcount(): global word_count - word_count += count_words_in_markdown(documents['thesis'].body) + word_count += count_words_in_markdown(documents["thesis"].body) - for c in documents['chapters']: - if c['filename'] != 'index': - count = count_words_in_markdown(c.body) - print(f"{c['filename']}: has {count} words") - word_count += count - - for c in documents['components']: - if c['filename'] != 'index': - count = count_words_in_markdown(c.body) - print(f"{c['filename']}: has {count} words") - word_count += count - - for c in documents['recipes']: - print(c['filename']) - count = count_words_in_markdown(c.body) - print(f"{c['filename']}: has {count} words") - word_count += count + for section in ["chapters", "components", "recipes"]: + for c in documents[section]: + if section == "recipes" or c["filename"] != "index": + count = count_words_in_markdown(c.body) + print(f"{c['filename']}: has {count} words") + word_count += count print(f"word count: { word_count} ") - documents['meta']["count"] = word_count + documents["meta"]["count"] = word_count def main(): @@ -327,13 +275,16 @@ def main(): if os.path.isdir(path): print("Compile: an entire directory", Path(path).name) render_posts(path) - elif Path(path).suffix == '.md': + elif Path(path).suffix == ".md": print("Compile: single page", Path(path).name) - render_single_file(get_page_data(path), path, OUTPUT_D) + render_single_file(path, OUTPUT_D) elif Path(path).suffix in [".csv"]: print("Compile: not compiling ", Path(path).name) - copy_assets() + if os.path.exists(OUT_ASSETS): + shutil.rmtree(OUT_ASSETS) + + shutil.copytree(SRC_ASSETS, OUT_ASSETS) print(f"total words: {word_count}") diff --git a/src/assets/app.js b/src/assets/app.js index da35044..3dbdfc8 100644 --- a/src/assets/app.js +++ b/src/assets/app.js @@ -1,12 +1,12 @@ - - const dialog = document.querySelector("dialog"); + const closeDialog = (e) => { e.stopPropagation(); console.log("close") dialog.close(); document.body.removeEventListener("click", closeDialog); } + const showLightbox = (e) => { e.preventDefault(); e.stopPropagation(); @@ -18,7 +18,5 @@ const showLightbox = (e) => { document.body.addEventListener("click", closeDialog, false); } document.querySelectorAll("article img").forEach((img) => { - console.log(img); img.addEventListener("click", showLightbox); }) - diff --git a/src/content/thesis.md b/src/content/thesis.md index 0059dd3..a42d68c 100644 --- a/src/content/thesis.md +++ b/src/content/thesis.md @@ -5,4 +5,31 @@ version: 0.3 layout: index description: This is the description of the about us page --- - \ No newline at end of file + + + + diff --git a/src/templates/base.jinja b/src/templates/base.jinja index 9d8b2ad..264b95c 100644 --- a/src/templates/base.jinja +++ b/src/templates/base.jinja @@ -18,7 +18,7 @@ - + @@ -64,14 +64,14 @@ s.querySelector("audio").pause(); }) e.currentTarget.setAttribute("active", true) - audioEl.play(); + audioEl.play(); } else { audioEl.pause(); e.currentTarget.removeAttribute("active") - } + } }) - }) + }) }) } @@ -79,11 +79,11 @@ let count = 0; content.querySelectorAll(`a[role="doc-noteref"]`).forEach((ref) =>{ - + const href = ref.getAttribute("href"); let note = content.querySelector(".footnotes " + href); if(note) { - + note.setAttribute("count", ref.querySelector("sup").textContent); ref.appendChild(note); } @@ -107,7 +107,7 @@ {% block title %} {%- if page['title'] -%} - {{ page['title'] }} {{documents['meta']['version']}} + {{ page['title'] }} {%- if documents['meta'] -%}{{documents['meta']['version']}}{%- endif -%} {%- else -%} I dont have a title {%- endif -%} @@ -149,4 +149,4 @@ - \ No newline at end of file + diff --git a/src/templates/thesis.jinja b/src/templates/thesis.jinja index 8e01891..161325a 100644 --- a/src/templates/thesis.jinja +++ b/src/templates/thesis.jinja @@ -9,14 +9,19 @@ {% block content %}
- {{documents["meta"]["now"]}} + {% if documents["meta"] %} + {{documents["meta"]["now"]}} + {% endif %} {{page['title']}}

A field guide to

Salvaging Sound Devices

-

Version {{documents['meta']['version']}} | {{documents['meta']['count']}} words

-

{{documents["meta"]["now"]}}

+ {% if documents["meta"] %} +

Version {{documents['meta']['version']}} | {{documents['meta']['count']}} words

+

{{documents["meta"]["now"]}}

+ {% endif %} +
@@ -59,4 +64,4 @@ {% endblock %} -{% block aside %} {% endblock %} \ No newline at end of file +{% block aside %} {% endblock %}