-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_description_and_difficulty.py
89 lines (66 loc) · 2.72 KB
/
get_description_and_difficulty.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
import os
import warnings
descriptions = []
titles = []
pdf_links = []
difficulties = []
images = []
languages = os.listdir(f'{os.getcwd()}/worksheets')
languages = [i for i in languages if not i.startswith('.') and not i.endswith('.py')]
for language in languages:
tutorials = os.listdir(f'worksheets/{language}')
tutorials = [i for i in tutorials if not i.startswith('.')]
for tut in tutorials:
file_list = os.listdir(f'worksheets/{language}/{tut}')
text_files = [i for i in file_list if i.endswith('.txt')]
pdf_files = [i for i in file_list if i.endswith('.pdf')]
# adding to pdf_links
if len(pdf_files) > 1:
warnings.warn(f'{tut} tutorial has more than one pdf. Ignoring it')
elif len(pdf_files) < 1:
raise ValueError(f'tutorial {tut} has no pdf files')
elif len(pdf_files) == 1:
pdf_links.append(f'worksheets/{language}/{tut}/{pdf_files[0]}')
if len(text_files) > 1:
raise ValueError(f'{tut} tutorial has more than one description')
elif len(text_files) == 1:
title = text_files[0]
title = title.replace('.txt', '')
title = title.replace('_', ' ')
title = title.title()
titles.append(title)
with open(f'worksheets/{language}/{tut}/{text_files[0]}', 'r') as file:
descriptions.append(file.readline())
diff_level = file.readline()
print(diff_level)
if diff_level == '':
difficulties.append('Not set')
else:
difficulties.append(diff_level)
# image
if 'website-image.png' in file_list:
image = f'worksheets/{language}/{tut}/website-image.png'
else:
image = 'images/tech-jam-website-logo-1280x1280.png'
images.append(image)
# get list item template
with open('partials/list_item.html', 'r') as file:
template = file.read()
# fill in template
filled_templates = []
for (desc, link, title, image, difficulty_level) in zip(descriptions, pdf_links, titles, images, difficulties):
output = template.replace('{{pdf link}}', link)
output = output.replace('{{description}}', desc)
output = output.replace('{{title}}', title)
output = output.replace('{{difficulty level}}', difficulty_level)
output = output.replace('{{image}}', image)
filled_templates.append(output)
output = '\n'.join(filled_templates)
# get full list template
with open('partials/list_full.html', 'r') as file:
full_template = file.read()
# fill in template
full_html = full_template.replace('{{Content}}', output)
# save full list
with open('content/list.html', 'w') as file:
file.write(full_html)