💾 Archived View for yretek.com › code › smolwebtor.py captured on 2024-06-16 at 12:59:08.

View Raw

More Information

⬅️ Previous capture (2024-02-05)

-=-=-=-=-=-=-

#!/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<h1>\n" + candidate_content + "\n</h1>"
    case "##":
      out_line = "\n<h2>\n" + candidate_content + "\n</h2>"
    case "###":
      out_line = "\n<h3>\n" + candidate_content + "\n</h3>"
    case ">":
      out_line = "\n<blockquote>\n" + candidate_content + "\n</blockquote>"
    case "*":  
      out_line = "\n<li>\n" + candidate_content + "\n</li>"
    case "=>":
      url = ""
      link_text = ""
      if len(words) > 1:
        url = words[1]
        if len(words) > 2:
          link_text = " ".join(words[2:])
        out_line = "\n<li><a href='" + url + "'>" + link_text + "</a>\n<\li>"
      else:
        out_line = "\n<p>\n => \n</p>"
    case _:
      out_line = "\n<p>\n" + out_line + "\n</p>"
  # 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<pre>\n" + text + "\n</pre>"
  output = output.replace("{{{-}}}", out_text, 1) # El ,1 final limita las sustituciones a la primera que encuentre

print(output)