generateIndex.py 1.84 KB
Newer Older
Laurent Heirendt's avatar
Laurent Heirendt committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os, re
from os import path

def build_link(title, href):
    return f'- <a href="{href}">{title}</a>\n'

def build_section_start(title):
    return f'\n' #<h3>{title}</h3>\n

def build_section_end():
    return "" #\t\t</ul>\n\t</div>

def camel_to_snake(name):
  name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
  return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()

# loop through the entire internal tree
localroot = os.getcwd()


# generate the index properly speaking
Laurent Heirendt's avatar
Laurent Heirendt committed
22
cardDirs = ["2021", "2020", "2019"]
Laurent Heirendt's avatar
Laurent Heirendt committed
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
69
70
71
index=''

# determine first the directories
for direct in cardDirs:
    if path.isdir(direct):
        dirs = os.listdir(direct)
        dirs.sort(reverse=True);
        index += "\n# " + direct + "\n\n"
        for d in dirs:
            if d[0] != ".":
                href = 'https://courses.lcsb.uni.lu/'+d
                linkText = camel_to_snake(d)
                linkText = linkText.replace("_", " ")
                linkText = linkText.title()

                # special rules
                linkText = linkText.replace("Dm", "Data Management")
                linkText = linkText.replace("It", "IT")
                linkText = linkText.replace("Siu", "SIU")

                index += build_link(linkText, href)

# output the index
print(index)


# Read in the file
repoName = "courses.lcsb.uni.lu"
os.chdir(repoName)
indexFile = "index.md"
filedata = ""
with open(indexFile, 'r') as file :
    for line in file:
        filedata += line

        # stop reading once the index place holder has been reached
        if re.search("<!-- index -->", line):
            filedata += "[[ index ]]"
            break

# Replace the target string
filedata = filedata.replace('[[ index ]]', index)

# Write the file out again
with open(indexFile, 'w') as file:
  file.write(filedata)

print("\n > New index generated and saved in " + indexFile)
os.chdir("..")