#!/usr/bin/python3 # ***************************************************************** # smolwebtor BY MIGUEL DE LUIS ESPINOSA # YRETEK # -------------------------------------------------------------- # (c) 2023 Miguel de Luis # This is Free Software licesed under the GPL 3 license terms # See https://www.gnu.org/licenses/gpl-3.0.html # ***************************************************************** # usage python3 smolwebtor.py in.gmi > out.html # chmod +x smolwebtor.py will make it executable so you could just do # gmi2html.py in.gmi > out.html import sys FILENAME = sys.argv[1] def extract_preformated(text_lines): ordinary_lines = [] preformated_texts = [] started_preformated_text = False this_preformated_text = "" for line in text_lines: stripped_line = line.strip() if stripped_line == "": continue if stripped_line == "```": if started_preformated_text: ordinary_lines.append("{{{-}}}") preformated_texts.append(this_preformated_text) started_preformated_text = False else: started_preformated_text = True elif started_preformated_text: this_preformated_text += line #must not be stripped but be verbatim else: ordinary_lines.append(stripped_line) return (ordinary_lines, preformated_texts) with open (FILENAME, 'r') as reader: text_lines = reader.readlines() ordinary_lines, preformated_texts = extract_preformated(text_lines) output = "" for line in ordinary_lines: out_line = line.strip() if out_line == "\n{{{-}}}": output += out_line continue candidate_content = "" candidate_gemini_tag = "" words = line.split() if len(words) > 1: candidate_gemini_tag = words[0] candidate_content = " ".join(words[1:]) print(candidate_gemini_tag) match candidate_gemini_tag.strip(): case "#": out_line = "\n

\n" + candidate_content + "\n

" case "##": out_line = "\n

\n" + candidate_content + "\n

" case "###": out_line = "\n

\n" + candidate_content + "\n

" case ">": out_line = "\n
\n" + candidate_content + "\n
" case "*": out_line = "\n
  • \n" + candidate_content + "\n
  • " case "=>": url = "" link_text = "" if len(words) > 1: url = words[1] if len(words) > 2: link_text = " ".join(words[2:]) out_line = "\n
  • " + link_text + "\n<\li>" else: out_line = "\n

    \n => \n

    " case _: out_line = "\n

    \n" + out_line + "\n

    " # Todos estos "\n" son innecesarios, pero a mí me gusta añadir # espaciado y líneas en blanco para que se vea mejor. Preferencia # personal output += out_line for text in preformated_texts: out_text = "\n
    \n" + text + "\n
    " output = output.replace("{{{-}}}", out_text, 1) # El ,1 final limita las sustituciones a la primera que encuentre print(output)