#!/bin/python # TODO : links should all be absolute, never relative import sys import os from pathlib import Path # used to escape the content import html # used to produce the time from datetime import datetime import textwrap import unicodedata import ra import racredentials global_title = "webair.xyz" global_subtitle = "Theo's web corner" global_name = "Theo" base_url = "webair.xyz/" global_pubkey = "" try: with open("public.gpg", "r") as f: global_pubkey = f.read() except FileNotFoundError: print("GPG public key not found!") global_pubkey = "" geminidir = "./public_gemini" htmldir = "./public_html" local_url = "/home/ploum/dev/gemlog/" short_limit = 25 maxwidth = 72 old_post_url = ["2005-01-25","2013-05-17","2011-02-07","2012-03-19","2012-01-18","2012-10-15"] html_page_template = "page_template.html" email_template = "email_template.html" gemini_page_template = "page_template.gmi" raindex_template = "raindex_template.gmi" raindex_destination = "rareview/retroachievements.gmi" image_extensions = [".png", ".jpg", ".jpeg"] def is_image(link): for ext in image_extensions: if link.endswith(ext): return True return False def generate_rels(links): result = "" for link in links: result += f'\n' return result def fill_globals(text): return text.replace("$AUTHOR", global_name)\ .replace("$BASE_URL", base_url)\ .replace("$GLOBAL_TITLE", global_title)\ .replace("$SUBTITLE", global_subtitle)\ .replace("$GPGPUBKEY", global_pubkey) # Add the html version to the post dictionnary # Also convert locals links that ends .gmi to .html # if index = true, a special style is applied before the first subtitle def gmi2html(raw,index=False,signature=None,relative_links=True,local=False): lines = raw.split("\n") inquote = False inpre = False inul = False inindex = index def sanitize(line): line = unicodedata.normalize('NFC', line) return html.escape(line) content = "" title = "" if inindex: content += "
\n" for line in lines: if inul and not line.startswith("=>") and not line.startswith("* "): content += "\n" inul = False if inquote and not line.startswith(">"): content += "\n" inquote = False if line.startswith("```"): if inpre: content += "\n" else: content += "
"
            inpre = not inpre
        elif inpre:
            content += sanitize(line) + "\n"
        elif line.startswith("* "):
            if not inul:
                content +="
\n" counter = 0 while line[counter] == "#": counter += 1 counter = min(6, counter) # There's no h7 in html content += f"" content += sanitize(line.lstrip("# ")) content += f"\n" elif line.startswith("# "): #We don’t add directly the first title as it is used in the template if not title: title = sanitize(line[2:]) else: content += "

" content += sanitize(line[2:]) content += "

\n" elif line.startswith("=>"): splitted = line.strip().split(maxsplit=2)[1:] link = splitted[0] #converting local links if "://" not in link and link.endswith(".gmi"): link = link[:-4] + ".html" if not relative_links and "://" not in link: link = "https://" + base_url + link.lstrip("./") elif local: link = local_url + link.lstrip("./") if len(splitted) == 1: description = "" name = link else: name = sanitize(splitted[1]) description = name if is_image(link): if inul: content += "\n" inul = False imgtag = f"{name}" content += f"{imgtag}" if description: content += f"

{description}

" else: if not inul: if inindex: content += "