generateIndex.py 7.44 KB
Newer Older
1
import os, re
Laurent Heirendt's avatar
Laurent Heirendt committed
2
from os import path
Laurent Heirendt's avatar
Laurent Heirendt committed
3
from natsort import natsorted
Laurent Heirendt's avatar
Laurent Heirendt committed
4

5
6
7
8
9
10
def line_prepender(filename, line):
    with open(filename, 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        f.write(line.rstrip('\r\n') + '\n' + content)

Jacek Lebioda's avatar
Jacek Lebioda committed
11
def build_link(title, href):
Laurent Heirendt's avatar
Laurent Heirendt committed
12
13

    # strip the number of the title
Laurent Heirendt's avatar
Laurent Heirendt committed
14
    if ('handbook' in href):
15
        title = re.sub('[0-9.]', '', title).strip()
Laurent Heirendt's avatar
Laurent Heirendt committed
16

17
    # remove sym link reference in href
Laurent Heirendt's avatar
Laurent Heirendt committed
18
19
20
21
    if ('handbook-additional' in href):
        href = href.replace('handbook-additional', 'handbook').strip()
    elif ('handbook-annexes' in href):
        href = href.replace('handbook-annexes', 'handbook').strip()
22

23
24
25
    # add relative url
    href = "{{ '" + href + "' | relative_url }}"

Jacek Lebioda's avatar
Jacek Lebioda committed
26
27
    return f'\t\t\t<li><a href="{href}">{title}</a></li>\n'

Laurent Heirendt's avatar
Laurent Heirendt committed
28
def build_section_start(title, shortcut):
Laurent Heirendt's avatar
Laurent Heirendt committed
29
30

    title = title.replace("Gdpr", "GDPR")
31
32
    title = title.replace("Handbook additional", "PI/Supervisor specifics")
    title = title.replace("Handbook annexes", "Annexes")
Laurent Heirendt's avatar
Laurent Heirendt committed
33
    title = title.replace("Covid 19", "COVID-19")
Laurent Heirendt's avatar
Laurent Heirendt committed
34
    title = title.replace("Ppc", "PPC")
Laurent Heirendt's avatar
Laurent Heirendt committed
35

36
    return f'\n\t<div class="index-box noborderbox" id="{shortcut}">\n\t\t<h3>{title}</h3>\n\t\t<ul>\n'
Jacek Lebioda's avatar
Jacek Lebioda committed
37
38
39
40

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

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
72
def remove_header(localroot, root, filename, n=5):
    nfirstlines = []

    #cwd = os.getcwd()

    os.chdir(localroot)
    #print("Local root folder:" + os.getcwd())

    os.chdir(root)
    #print("Local folder:" + os.getcwd())

    # count the number of lines
    count = 0
    headerCheck = False
    with open(filename, 'r') as f:
        for line in f:
            count += 1

            # check if the header is actually a header
            if (count == 0 or count == n) and line[0:3] == "---":
                headerCheck = True

    # remove the header
    if count > n and headerCheck:
        with open(filename) as f, open("tmp"+filename, "w") as out:
            for _ in range(n):
                nfirstlines.append(next(f))
            for line in f:
                out.write(line)

        os.remove(filename)
        os.rename("tmp"+filename, filename)
73
        print(" - Old header removed.")
74
75
76
77
78
79
80

    # change back to the local root
    os.chdir(localroot)
    #print("exit folder:" + os.getcwd())

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

Laurent Heirendt's avatar
Laurent Heirendt committed
82
83

# generate the index properly speaking
Laurent Heirendt's avatar
Laurent Heirendt committed
84
cardDirs = ["internal", "external", "policies"]
Laurent Heirendt's avatar
Laurent Heirendt committed
85
86
87
88
sections = []

# determine first the directories
for direct in cardDirs:
Laurent Heirendt's avatar
Laurent Heirendt committed
89
90
    if path.isdir(direct):
        dirs = os.listdir(direct)
Laurent Heirendt's avatar
Laurent Heirendt committed
91
        dirs = natsorted(dirs)
Laurent Heirendt's avatar
Laurent Heirendt committed
92

Laurent Heirendt's avatar
Laurent Heirendt committed
93
94
95
        for d in dirs:
            if d[0] != ".":
                sections.append(d)
Laurent Heirendt's avatar
Laurent Heirendt committed
96

Laurent Heirendt's avatar
Laurent Heirendt committed
97
sections = list(set(sections))
Laurent Heirendt's avatar
Laurent Heirendt committed
98
sections = natsorted(sections)
Laurent Heirendt's avatar
Laurent Heirendt committed
99

Jacek Lebioda's avatar
Jacek Lebioda committed
100
# Index contains the generated content, init it with an empty container
Jacek Lebioda's avatar
Jacek Lebioda committed
101
index = ''
Jacek Lebioda's avatar
Jacek Lebioda committed
102
103
index += '\n<div class="index-box-container">\n'

Laurent Heirendt's avatar
Laurent Heirendt committed
104
105
106
localIndexArr = [[]] * len(sections)
for folder in cardDirs:

Laurent Heirendt's avatar
Laurent Heirendt committed
107
108
109
    # check if folder exists
    if path.isdir(folder):
        dirs = os.listdir(folder)
Laurent Heirendt's avatar
Laurent Heirendt committed
110
        dirs = natsorted(dirs)
Laurent Heirendt's avatar
Laurent Heirendt committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

        for d in dirs:
            if d[0] != ".":
                # set the header of the section
                #index += "\n### " + d.replace("-", " ").capitalize() + "\n"


                # get the index of the section
                indexS = sections.index(d)

                #print(indexS)
                if len(localIndexArr[indexS]) == 0:
                    localIndexArr[indexS] = ["\n"]

                # walk through the folders with all the cards
                for root, dirs, files in os.walk(folder+"/"+d):
                    for file in files:
                        if file.endswith(".md"):
                            fileName = os.path.join(root, file)

                            # ignore subsections (.md files that start with _)
                            if file[0] != "_":
                                print(" > Generating header for: " + fileName)

                                # remove the previous header
                                remove_header(localroot, root, file, 8)

                                # generate a permalink
                                permalink = "/" + root + "/"

                                # generate the shortcut
                                shortcut = re.sub(folder, '', root)

                                # remove the first /
                                shortcut = shortcut[1:]

                                # replace the / with a :
                                shortcut = re.sub('/', ':', shortcut)

                                # define the header for each card
                                header = "---\n"
                                header += "layout: page\n"
                                header += "permalink: " + permalink + "\n"
                                header += "shortcut: " + shortcut + "\n"
                                header += "redirect_from:\n"
                                header += "  - /cards/" + shortcut + "\n"
157
                                header += "  - /" + folder + "/cards/" + shortcut + "\n"
Laurent Heirendt's avatar
Laurent Heirendt committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                                header += "---"

                                # add the header properly speaking
                                line_prepender(fileName, header)

                                # open file and get the title after the header
                                count = 0
                                title = ""
                                bp = 9
                                with open(fileName, 'r') as f:
                                    for line in f:
                                        count += 1
                                        if count == bp:
                                            if len(line) > 2:
                                                title = line
                                                break
                                            else:
                                                bp += 1

                                # remove first and last chars
                                title = title.rstrip("\n\r")
                                title = title[2:]

Jacek Lebioda's avatar
Jacek Lebioda committed
181
                                localIndexArr[indexS].append(build_link(title, root))
Laurent Heirendt's avatar
Laurent Heirendt committed
182
183
184
185
186
187

                                # output
                                print(" + New header added.")
                                print("-----------------------")

                # join all subcategories to the index
Laurent Heirendt's avatar
Laurent Heirendt committed
188
                localIndexArr[indexS] = natsorted(localIndexArr[indexS])
Laurent Heirendt's avatar
Laurent Heirendt committed
189
190
191
192
193
194

print(localIndexArr)

# determine the index
k = 0
for s in sections:
Laurent Heirendt's avatar
Laurent Heirendt committed
195
    index += build_section_start(s.replace("-", " ").capitalize(), s)
Laurent Heirendt's avatar
Laurent Heirendt committed
196
    index += ''.join(localIndexArr[k])
Jacek Lebioda's avatar
Jacek Lebioda committed
197
    index += build_section_end()
Laurent Heirendt's avatar
Laurent Heirendt committed
198
    k += 1
Laurent Heirendt's avatar
Laurent Heirendt committed
199

Jacek Lebioda's avatar
Jacek Lebioda committed
200
201
202
# Close the container
index += "\n</div>"

203
## add link to return to main index
204
205
index += """<br><center><a href="{{ '/' | relative_url }}">go back</a></center>"""
index += """<br><center><a href="{{ '/cards' | relative_url }}">Overview of all HowTo cards</a></center>"""
206

207
# output the index
Laurent Heirendt's avatar
Laurent Heirendt committed
208
209
#print(index)
# Read in the file
210
indexFile = "cards.md"
211
filedata = ""
Laurent Heirendt's avatar
Laurent Heirendt committed
212
with open(indexFile, 'r') as file :
213
214
215
216
217
218
219
    for line in file:
        filedata += line

        # stop reading once the index place holder has been reached
        if re.search("<!-- index -->", line):
            filedata += "[[ index ]]"
            break
Laurent Heirendt's avatar
Laurent Heirendt committed
220
221
222
223
224
225

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

# Write the file out again
with open(indexFile, 'w') as file:
Laurent Heirendt's avatar
Laurent Heirendt committed
226
227
  file.write(filedata)

Jacek Lebioda's avatar
Jacek Lebioda committed
228
print("\n > New index generated and saved in " + indexFile)