import os from os import walk import os.path from md2gemini import md2gemini import re def merge_lines(text): pattern = r'(^=>.*)(?:\n[ \t\*]*)+(=>)' pattern = r'(^=>.*)(?:\r?\n[ \t\*]*)+\r?\n(?=^=>)' while True: new_text = re.sub(pattern, r'\1\n', text, flags=re.MULTILINE) if new_text == text: break text = new_text text = re.sub(r'^\* *\r?$', r'', text, flags=re.MULTILINE) return text # Match any blank line (or whitespace-only line) that’s followed by a line starting with => pattern = r'(^=>.*)(?:[\n \t\*]*)(?=^=>)' return re.sub(pattern, r'\1\n', input_text, flags=re.MULTILINE) lines = input_text.splitlines() result = [] last_line = None for line in lines: stripped = line.rstrip() if last_line is not None: if (last_line == '' or last_line == '*') and stripped.startswith('=>'): last_line = None # Skip the previous line if last_line is not None: result.append(last_line) last_line = stripped if last_line is not None: result.append(last_line) return '\n'.join(result) def convert_md_to_gemini(): for file in os.listdir("output/markdown"): with open("output/markdown/" + str(file), "r") as f: gemini = md2gemini(f.read()) f.close() f = open("output/gemini/" + str(file).replace(".md", ".gmi"), "w") gemini = merge_lines(gemini) f.write(gemini) f.close() return "markdown was converted to gemini (2/3)" for file in os.listdir("output/gemini"): processed = "" f = open("output/gemini/" + file, "r") for line in f.readlines(): if line == "html\n": pass else: processed += line f.close() f = open("output/gemini/" + file, "w") f.write(processed) f.close() return "markdown was converted to gemini (2/3)"