include feedback
This commit is contained in:
102
app.py
102
app.py
@ -1,3 +1,4 @@
|
|||||||
|
import subprocess
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
@ -12,7 +13,6 @@ import pypandoc
|
|||||||
|
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
|
||||||
# TODO make newsletter URL's absolute to klank.school
|
# TODO make newsletter URL's absolute to klank.school
|
||||||
env = Environment(
|
env = Environment(
|
||||||
loader=PackageLoader("src"),
|
loader=PackageLoader("src"),
|
||||||
@ -28,10 +28,7 @@ now = datetime.now()
|
|||||||
|
|
||||||
# Utils
|
# Utils
|
||||||
def getParam(params, index):
|
def getParam(params, index):
|
||||||
if len(params) > index:
|
return params[index] if len(params) > index else False
|
||||||
return params[index]
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
# jinja filter that can list documents
|
# jinja filter that can list documents
|
||||||
@ -42,31 +39,11 @@ def listDocuments(params):
|
|||||||
|
|
||||||
return html
|
return html
|
||||||
|
|
||||||
# jinja filter that can list events
|
|
||||||
def listEvents(params):
|
|
||||||
param = params.split(" ")
|
|
||||||
tag = getParam(param, 1)
|
|
||||||
|
|
||||||
if "events" not in documents:
|
|
||||||
return ""
|
|
||||||
|
|
||||||
events = []
|
|
||||||
if tag:
|
|
||||||
for event in documents["events"]:
|
|
||||||
if tag in event["tags"]:
|
|
||||||
events.append(event)
|
|
||||||
else:
|
|
||||||
events = documents["events"]
|
|
||||||
|
|
||||||
template = env.select_template(["snippets/list-events.jinja"])
|
|
||||||
html = template.render(events=events, filter=param[0], tag=getParam(param, 1))
|
|
||||||
|
|
||||||
return html
|
|
||||||
|
|
||||||
# jinja filter to make a slug out of a stirng
|
# jinja filter to make a slug out of a stirng
|
||||||
def slugify_filter(value):
|
def slugify_filter(value):
|
||||||
return slugify(value)
|
return slugify(value)
|
||||||
|
|
||||||
|
|
||||||
# jinja filter for date formatting
|
# 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)
|
return datetime.fromtimestamp(int(value)).strftime(format)
|
||||||
@ -76,8 +53,7 @@ def prettydate(value, format='%d/%m/%Y'):
|
|||||||
def shortcode_filter(value):
|
def shortcode_filter(value):
|
||||||
|
|
||||||
shortcode_callbacks = {
|
shortcode_callbacks = {
|
||||||
"show": listDocuments,
|
"show": listDocuments
|
||||||
"events": listEvents
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def shortcode_replacer(match):
|
def shortcode_replacer(match):
|
||||||
@ -113,7 +89,7 @@ def render_single_file(page, path, dist, name = False):
|
|||||||
|
|
||||||
# find a pre-rendered page
|
# find a pre-rendered page
|
||||||
def get_existing_page(path, slug):
|
def get_existing_page(path, slug):
|
||||||
stem = Path(path).stem;
|
stem = Path(path).stem
|
||||||
folder = os.path.basename(os.path.dirname(path))
|
folder = os.path.basename(os.path.dirname(path))
|
||||||
|
|
||||||
if stem == "index" and folder != "content":
|
if stem == "index" and folder != "content":
|
||||||
@ -140,10 +116,10 @@ def get_slug(path, folder, filename):
|
|||||||
return slugify(f"{folder}/{filename}")
|
return slugify(f"{folder}/{filename}")
|
||||||
|
|
||||||
# compile markdown into cited HTML
|
# compile markdown into cited HTML
|
||||||
def get_page_data(path, isPreload=False):
|
def get_page_data(path):
|
||||||
|
|
||||||
filename = Path(path).stem
|
filename = Path(path).stem
|
||||||
folder = os.path.basename(os.path.dirname(path))
|
folder = Path(path).parent.name
|
||||||
slug = get_slug(path, folder, filename)
|
slug = get_slug(path, folder, filename)
|
||||||
|
|
||||||
prerendered = get_existing_page(path, slug)
|
prerendered = get_existing_page(path, slug)
|
||||||
@ -152,18 +128,17 @@ def get_page_data(path, isPreload=False):
|
|||||||
return prerendered
|
return prerendered
|
||||||
|
|
||||||
page = frontmatter.load(path)
|
page = frontmatter.load(path)
|
||||||
page['slug'] = slug
|
page["slug"] = slug
|
||||||
page.filename = filename
|
page["filename"] = filename
|
||||||
page.folder = folder
|
page["folder"] = folder
|
||||||
|
|
||||||
latex = page.content
|
|
||||||
|
|
||||||
if "start_datetime" in page:
|
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:
|
||||||
latex = pypandoc.convert_text(
|
content = pypandoc.convert_text(
|
||||||
page.content,
|
page.content,
|
||||||
to='md',
|
to='md',
|
||||||
format='md',
|
format='md',
|
||||||
@ -172,7 +147,7 @@ def get_page_data(path, isPreload=False):
|
|||||||
])
|
])
|
||||||
|
|
||||||
page.body = pypandoc.convert_text(
|
page.body = pypandoc.convert_text(
|
||||||
latex,
|
content,
|
||||||
to="html",
|
to="html",
|
||||||
format="md",
|
format="md",
|
||||||
extra_args=[
|
extra_args=[
|
||||||
@ -193,19 +168,12 @@ def save_circuit_svg(filepath, outpath, name):
|
|||||||
width_px = float(root.get("width", 0))
|
width_px = float(root.get("width", 0))
|
||||||
height_px = float(root.get("height", 0))
|
height_px = float(root.get("height", 0))
|
||||||
|
|
||||||
DPI = 300
|
|
||||||
|
|
||||||
# Convert px to mm
|
|
||||||
width_mm = (width_px * 25.4) / DPI > 15
|
|
||||||
height_mm = (height_px * 25.4) / DPI
|
|
||||||
|
|
||||||
# Set new width/height in mm
|
# Set new width/height in mm
|
||||||
root.set("width", f"{width_px}mm")
|
root.set("width", f"{width_px}mm")
|
||||||
root.set("height", f"{height_px}mm")
|
root.set("height", f"{height_px}mm")
|
||||||
|
|
||||||
os.makedirs(outpath, exist_ok = True)
|
os.makedirs(outpath, exist_ok = True)
|
||||||
|
|
||||||
|
|
||||||
tree.write(f"{outpath}/{name}")
|
tree.write(f"{outpath}/{name}")
|
||||||
|
|
||||||
|
|
||||||
@ -214,17 +182,15 @@ def render_posts(path, output_path=OUTPUT_D):
|
|||||||
name = Path(path).stem
|
name = Path(path).stem
|
||||||
|
|
||||||
for filename in sorted(os.listdir(path)):
|
for filename in sorted(os.listdir(path)):
|
||||||
file_path = os.path.join(path, filename)
|
file_path = Path(path) / filename
|
||||||
|
|
||||||
if filename.endswith(".md"):
|
if file_path.suffix == ".md":
|
||||||
print("is md", filename)
|
render_single_file(get_page_data(file_path), file_path, f"{output_path}/{name}")
|
||||||
page = get_page_data(file_path)
|
elif file_path.is_dir():
|
||||||
render_single_file(page, file_path, f"{output_path}/{name}", name)
|
|
||||||
elif os.path.isdir(file_path):
|
|
||||||
render_posts(file_path, f"{output_path}/{name}")
|
render_posts(file_path, f"{output_path}/{name}")
|
||||||
elif filename.endswith(".svg"):
|
elif file_path.suffix == ".svg":
|
||||||
save_circuit_svg(file_path, f"{output_path}/{name}", filename)
|
save_circuit_svg(file_path, f"{output_path}/{name}", filename)
|
||||||
elif Path(filename).suffix in [".jpeg", ".mp3", ".jpg", ".png"]:
|
elif file_path.suffix in {".jpeg", ".mp3", ".jpg", ".png"}:
|
||||||
os.makedirs(f"{output_path}/{name}", exist_ok = True)
|
os.makedirs(f"{output_path}/{name}", exist_ok = True)
|
||||||
shutil.copyfile(file_path, f"{output_path}/{name}/{filename}")
|
shutil.copyfile(file_path, f"{output_path}/{name}/{filename}")
|
||||||
else:
|
else:
|
||||||
@ -232,8 +198,12 @@ def render_posts(path, output_path=OUTPUT_D):
|
|||||||
|
|
||||||
# Pre-load before compiling
|
# Pre-load before compiling
|
||||||
def preload_documents():
|
def preload_documents():
|
||||||
print("preload any needed data")
|
global documents
|
||||||
documents["meta"] = {"now": now.strftime("%d %B %Y")}
|
|
||||||
|
version = subprocess.check_output(["git", "rev-list", "--count", "HEAD"]).decode("utf-8").strip()
|
||||||
|
|
||||||
|
|
||||||
|
documents["meta"] = {"now": now.strftime("%d %B %Y"), "version": version}
|
||||||
|
|
||||||
for subdir in os.listdir(CONTENT_D):
|
for subdir in os.listdir(CONTENT_D):
|
||||||
path = os.path.join(CONTENT_D, subdir)
|
path = os.path.join(CONTENT_D, subdir)
|
||||||
@ -243,15 +213,14 @@ def preload_documents():
|
|||||||
documents.setdefault(name, [])
|
documents.setdefault(name, [])
|
||||||
|
|
||||||
for filename in sorted(os.listdir(path)):
|
for filename in sorted(os.listdir(path)):
|
||||||
print(filename)
|
|
||||||
cpath = os.path.join(path, filename)
|
cpath = os.path.join(path, filename)
|
||||||
if filename.endswith(".md"):
|
if filename.endswith(".md"):
|
||||||
documents[name].append(get_page_data(cpath, isPreload=True))
|
documents[name].append(get_page_data(cpath))
|
||||||
elif os.path.isdir(cpath):
|
elif os.path.isdir(cpath):
|
||||||
documents[name].append(get_page_data(os.path.join(cpath, "index.md"), isPreload=True))
|
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, isPreload=True)
|
documents[Path(path).stem] = get_page_data(path)
|
||||||
|
|
||||||
|
|
||||||
def copy_assets():
|
def copy_assets():
|
||||||
@ -262,19 +231,12 @@ def copy_assets():
|
|||||||
|
|
||||||
|
|
||||||
def get_inventory():
|
def get_inventory():
|
||||||
|
global documents
|
||||||
|
|
||||||
with open("src/content/component-inventory.csv") as f:
|
with open("src/content/component-inventory.csv") as f:
|
||||||
documents['inventory'] = []
|
documents['inventory'] = []
|
||||||
for line in csv.DictReader(
|
|
||||||
f,
|
for line in csv.DictReader(f, fieldnames=('ID', 'Amount', 'Name', 'Value', 'type', 'Date', 'Where','Mounting type')):
|
||||||
fieldnames=(
|
|
||||||
'ID',
|
|
||||||
'Amount',
|
|
||||||
'Name',
|
|
||||||
'Value',
|
|
||||||
'type',
|
|
||||||
'Date',
|
|
||||||
'Where',
|
|
||||||
'Mounting type')):
|
|
||||||
documents['inventory'].append(line)
|
documents['inventory'].append(line)
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@
|
|||||||
</script>
|
</script>
|
||||||
{% block title %}
|
{% block title %}
|
||||||
{%- if page['title'] -%}
|
{%- if page['title'] -%}
|
||||||
<title>{{ page['title'] }}</title>
|
<title>{{ page['title'] }} {{documents['meta']['version']}}</title>
|
||||||
{%- else -%}
|
{%- else -%}
|
||||||
<title>I dont have a title</title>
|
<title>I dont have a title</title>
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
<header>
|
<header>
|
||||||
<h2>A field guide to</h2>
|
<h2>A field guide to</h2>
|
||||||
<h1>Salvaging Sound Devices</h1>
|
<h1>Salvaging Sound Devices</h1>
|
||||||
<p>Version {{page['version']}}</p>
|
<p>Version {{documents['meta']['version']}}</p>
|
||||||
<p>{{documents["meta"]["now"]}}</p>
|
<p>{{documents["meta"]["now"]}}</p>
|
||||||
</header>
|
</header>
|
||||||
</section>
|
</section>
|
||||||
|
Reference in New Issue
Block a user