-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocho_indexer.py
240 lines (194 loc) · 6.53 KB
/
ocho_indexer.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#################################################
'''
ocho_indexer.py creates index.html for indexing all headers in all of the lecture
and exercise pages. This makes searching for information much faster.
'''
#################################################
import os
def get_lecture_list(dir_path):
'''
Create list of lectures/exercises from local folders
'''
LECTURE_LIST = next(os.walk(dir_path))[1]
return LECTURE_LIST
def clean_headers_and_links(lecture, dir_path, headers, links):
'''
From list of lectures, open each html file and grab headers and anchors
'''
# open file
lecture_file = dir_path + '/' + lecture + '/' + lecture + '.html'
file = open(lecture_file, 'r')
for line in file:
line = line.strip('\n')
line = line.strip(' ')
if 'href="#' in line:
header = line
header = header.split('">')[1].split('</a>')[0]
anchor_and_text = line
anchor_and_text = anchor_and_text.split(
'href="')[1].split('</a>')[0]
top = dir_path.split('/')[1]
link = f'<a href="{top}/{lecture}/{lecture}.html{anchor_and_text} > {lecture}</a>'
# if the header isn't empty, check for duplicates
if len(headers) > 0:
headers_check = headers[-1].split('">')[1].split('</a>')[0]
if headers_check != header:
# not a duplicate, append to header to headers
headers.append(line)
# add header and link to links
links[header] = link
# append to headers since headers is empty
else:
headers.append(line)
# add header and link to links
links[header] = link
return links
def create_html(links, sorted_headers, links_ex, sorted_headers_ex, contents_list=[], contents_list_ex=[]):
'''
Create index.html in local directory
'''
file = open('index.html', 'w')
file.write('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Indexer</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<style>
body {
background-color: rgb(248, 248, 240);
}
a {
color: black;
}
a:hover,
a:active {
color: gray;
text-decoration: none;
}
.header {
background-color: rgb(247, 122, 123);
padding: 10px 10px;
margin: 10px 10px;
border-radius: 10px;
}
h1 {
color: white;
}
ul li{
color: rgb(247, 122, 123);
}
</style>
</head>
<body>
<div class="container">
<div class="row text-center">
<div class="col header">
<h1>Table of Contents</h1>
</div>
</div>
<div class="row text-center">
<div class="col-6 list-group">
<h3>Lectures</h3>
<ul class="text-left">''')
for contents in contents_list:
file.write(f'''
<li><a href="lectures/{contents[2]}">
<div class="row">
<div class="col text-left">
<b>{contents[1]}</b>
</div>
<div class="col text-left">
        
{convert_date(contents[0])}
</div>
</div>
</a></li>
''')
file.write('''
</ul>
</div>
<div class="col-6 list-group">
<h3>Exercises</h3>
<ul class="text-left">''')
for contents in contents_list_ex:
file.write(f'''
<li><a href="exercises/{contents[2]}">
<div class="row">
<div class="col text-left">
<b>{contents[1]}</b>
</div>
<div class="col text-left">
        
{convert_date(contents[0])}
</div>
</div>
</a></li>
''')
file.write('''
</ul>
</div>
</div>
<div class="row text-center">
<div class="col header">
<h1>Index</h1>
</div>
</div>
<div class="row text-center">
<div class="col-6 list-group">
<h3>Lectures</h3>
<ul class="text-left">''')
for header in sorted_headers:
file.write(
f'<li>{links[header]}</li>\n'
)
file.write('''
</ul>
</div>
<div class="col-6 list-group">
<h3>Exercises</h3>
<ul class="text-left">''')
for header in sorted_headers_ex:
file.write(
f'<li>{links_ex[header]}</li>\n'
)
file.write('''
</ul>
</div>
</div>
</div>
</body>
</html>
''')
file.close()
def convert_date(date_string):
'''
Recieves a string of 8 numbers and converts to a better looking
date string.
'''
months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
return f'{months[int(date_string[4:6]) - 1]} {int(date_string[6:])}, {date_string[:4]}'
def index(contents_list=[], contents_list_ex=[]):
'''
Runs the script on the lectures and exercises directories.
'''
headers = []
links = {}
headers_ex = []
links_ex = {}
LECTURE_LIST = get_lecture_list('./lectures')
EXERCISE_LIST = get_lecture_list('./exercises')
for lecture in LECTURE_LIST:
links = clean_headers_and_links(
lecture, './lectures', headers, links)
for exercise in EXERCISE_LIST:
links_ex = clean_headers_and_links(
exercise, './exercises', headers_ex, links_ex)
sorted_headers = sorted(links)
sorted_headers_ex = sorted(links_ex)
create_html(links, sorted_headers, links_ex,
sorted_headers_ex, contents_list, contents_list_ex)