summaryrefslogtreecommitdiff
path: root/converters/md_to_gemini.py
blob: 3f751c4475e8af66188f15bdf1bbb5730b8b2089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)"