summaryrefslogtreecommitdiff
path: root/converters/md_to_gemini.py
blob: d7c15964073b840d69536dfd2b6e86f631053e86 (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
import os
from os import walk
import os.path
from md2gemini import md2gemini

def merge_lines(input_text):
    lines = input_text.splitlines()
    result = []
    last_line = None

    for line in lines:
        stripped = line.strip()

        if stripped == '' or stripped == '*':
            # Tentatively hold this line
            last_line = stripped
            continue

        if stripped.startswith('=>') and (last_line == '' or last_line == '*'):
            # Skip the held line
            last_line = None

        if last_line is not None:
            result.append(last_line)
            last_line = None

        result.append(line)

    # If the last line was held and never flushed
    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()
    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)"