From feb3b9cb4fcfd2a4c1b591acb706b81456d27c0f Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 7 Dec 2025 19:04:08 +0000 Subject: [PATCH] Replace gemini-style links with html links --- blog.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ webapp_post.py | 3 +++ 2 files changed, 51 insertions(+) diff --git a/blog.py b/blog.py index 35cd56b76..edc31885e 100644 --- a/blog.py +++ b/blog.py @@ -45,6 +45,7 @@ from posts import create_blogs_timeline from newswire import rss2header from newswire import rss2footer from cache import get_person_from_cache +from flags import is_image_file def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {}, @@ -198,6 +199,52 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {}, return '' +def html_blog_post_gemini_links(content: str) -> str: + """Converts gemini-style links to html + """ + if '=> ' not in content: + return content + ctr = 0 + sections = content.split('=> ') + for section in sections: + if ctr == 0: + ctr += 1 + continue + if ' ' not in section: + ctr += 1 + continue + web_link_str = section.split(' ', 1)[0] + if '://' not in web_link_str or \ + '.' not in web_link_str: + ctr += 1 + continue + after_web_link = section.split(web_link_str, 1)[1] + after_str = '' + if '<' in after_web_link: + after_str = after_web_link.split('<', 1)[0] + elif '\n' in after_web_link: + after_str = after_web_link.split('\n', 1)[0] + else: + ctr += 1 + continue + if is_image_file(web_link_str) and \ + not web_link_str.endswith('.svg'): + link_str = '' + after_str + \
+                '\n' + else: + link_str = '' + after_str + '' + + content = content.replace('=> ' + web_link_str + ' ' + after_str, + link_str) + ctr += 1 + return content + + def html_blog_post_markdown(content: str) -> str: """Converts any markdown to html """ @@ -399,6 +446,7 @@ def _html_blog_post_content(debug: bool, session, authorized: bool, # convert any markdown blog_str = html_blog_post_markdown(blog_str) + blog_str = html_blog_post_gemini_links(blog_str) if replies == 0: blog_str += blog_separator + '\n' diff --git a/webapp_post.py b/webapp_post.py index 7ab804f5a..cf5a4f21d 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -139,6 +139,7 @@ from maps import get_event_time_span_from_post from session import get_json_valid from session import get_json from blog import html_blog_post_markdown +from blog import html_blog_post_gemini_links # maximum length for display name within html posts MAX_DISPLAY_NAME_LENGTH = 42 @@ -3141,6 +3142,8 @@ def individual_post_as_html(signing_priv_key_pem: str, # replace any markdown headers with html if post_is_blog: object_content = html_blog_post_markdown(object_content) + # replace gemini-style links with html links + object_content = html_blog_post_gemini_links(object_content) # show quote toot link quote_url = get_quote_toot_url(post_json_object) if quote_url: