#!/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 global_title = "webair.xyz" global_subtitle = "Alice's web corner" global_name = "Alice" htmldir = "./public_html" geminidir = "./public_gemini" base_url = "webair.xyz/" 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" def fill_globals(text): return text.replace("$AUTHOR", global_name)\ .replace("$BASE_URL", base_url)\ .replace("$GLOBAL_TITLE", global_title)\ .replace("$SUBTITLE", global_subtitle) # 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[2:].strip().split(maxsplit=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 link[-4:] in [".jpg",".png",".gif"] or link[-5:] == ".jpeg": if inul: content += "\n" inul = False #content += "
" imgtag = "\"%s\""%(name,link) content += ""%link + imgtag + "" #content += "
" if description: content += "

" + description + "

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