From 8eb7deb46412795d9ccccbe64d7416846810ddd5 Mon Sep 17 00:00:00 2001 From: nebula Date: Thu, 3 Apr 2025 05:51:37 +0000 Subject: [PATCH] add support for local urls in gemini when text posts are present --- generate.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/generate.py b/generate.py index 23c40e0..7b8a32c 100644 --- a/generate.py +++ b/generate.py @@ -9,6 +9,7 @@ from os import path import re numbers = re.compile("[0-9]+") +name_from_url = re.compile(r"https:\/\/helixnebula.space/(.*)/") out_dir = "/var/www/html/" gemini_out_dir = "/var/gemini/" base_url = "https://helixnebula.space/" @@ -123,6 +124,16 @@ def render_places(): "photos_json": photos_json })) +def sub_http_local_urls(match): + name = match.group(1) + post_names = [ + path.basename(post).split('/')[-1][:-3] for post in glob(md_dir + "*") + ] + if name in post_names: + return name + ".gmi" + else: + return match.group(0) + def render_gemini_index(): template = template_environment.get_template("gemini_main") post_names = [ @@ -143,7 +154,10 @@ def render_gemini_places(): content = f.read() process = Popen(["gemgen"], stdout=PIPE, stdin=PIPE, stderr=PIPE, text=True) with open(gemini_out_dir + post_name + ".gmi", "w") as f: - f.write(process.communicate(input=content)[0]) + header = f"=> https://helixnebula.space/{post_name}/ Link to photography.\n\n" + gemtext = process.communicate(input=content)[0] + gemtext_with_local_urls = name_from_url.sub(sub_http_local_urls, gemtext) + f.write(header + gemtext_with_local_urls) def copy_files(): call("cp js/* html/js/", shell=True)