-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite_generator.py
More file actions
276 lines (242 loc) · 6.43 KB
/
site_generator.py
File metadata and controls
276 lines (242 loc) · 6.43 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
site_generator.py
Generates a site
Russell Lee
"""
from dataclasses import dataclass
from typing import Any, Union
@dataclass
class Site:
Title: Any
Information: Union[None, 'Content']
@dataclass
class Content:
Title: str
Paragraph: str
Images: Union[None, 'Image']
Next: Union[None, 'Content']
@dataclass
class Line:
Value: Any
Next: [None, 'Line']
@dataclass
class Image:
Location: str
Size: Union[None, str]
Next: Union[None, 'Image']
def fetch_title(title):
"""
Gets the title for the web page
:param title: title of web page
:return: the title template
"""
doctype = '<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<title>' + title + '\n\
</title>'
return doctype
def style(bg, fc, hc, fs):
"""
Builds a style template
:param bg: background color
:param fc: font color
:param hc: header color
:param fs: font style
:return: the style template
"""
css = '<style>\n\
body {background-image: linear-gradient(180deg, ' + bg + ',white);}\n\
\n\
.center {\n\
display: block;\n\
margin-left: auto;\n\
margin-right: auto;\n\
}\n\
h1 {color:' + hc + ';\n\
font-family: ' + fs + ';\n\
text-align:center;\n\
}\n\
\n\
h2 {color:' + hc + ';\n\
font-family: ' + fs + ';\n\
text-align: justify;\n\
}\n\
\n\
p {color:' + fc + ';\n\
font-family: ' + fs + ';\n\
padding: 30px;\n\
text-align: justify;\n\
background-color: white;\n\
box-shadow: 4px 0 2px -2px rgba(0,0,0,0.4);\n\
font-size: 14px;\n\
}\n\
\n\
</style>\n'
return css
def fetch_title_footer(title):
"""
Gets the "footer' of the document
:param title: title of the footer
:return: the footer template
"""
footer = '</head>\n\
<body>\n\
<h1> ' + title + '\n\
</h1>\n\
<hr/>\n'
return footer
def url_linker(names, titles):
"""
if needed, gets all of the urls
:param names: list of the file names
:param titles: list of the actual titles
:return: the framework for the urls
"""
numb = 0
name_list = []
url = ''
url += '<p align="center">'
for _ in names:
name_list.append('new_file' + str(numb) + '.html')
numb += 1
numb = 0
for i in names:
url += '<a href="' + name_list[numb] + '">' + titles[numb] + '</a>---'
numb += 1
url += '\n</p>\n'
return url
def create_body(text):
"""
Creates the body of the passage
:param text: The body of the passage
:return: the framework for the body
"""
title = text.Title
paragraph = text.Paragraph
body = '<h2>' + title + '\n\
</h2>\n\
<p>' + paragraph + '\n\
</p>\n'
"""
text img y next n
text img y next y
text img n next y
text img n next n
"""
if text.Images is not None:
if text.Next is None:
return body + str(create_img(text.Images))
else:
return body + str(create_img(text.Images)) + create_body(text.Next)
elif text.Next is not None:
return body + create_body(text.Next)
else:
return body
def create_img(content):
"""
creates all the images
:param content: the content needed to make images
:return: the framework for the image
"""
location = content.Location
size = str(content.Size)
img = '<img src="' + location + '" width= "' + size + '" class="center">\n'
if content.Next is None:
return img
else:
return img + create_img(content.Next)
"""################## File to linked list functions are below #######################"""
def linked_list(text):
"""
creates a linked list
:param text: the entire thing
:return: Site
"""
passage = text.split('\n')
# print(passage)
title_name = ''
get_title = True
while get_title is True:
if passage[0] != '!new_paragraph':
title_name += passage[0]
passage.pop(0)
else:
get_title = False
return Site(title_name, get_content(passage))
def get_content(passage):
"""
create the paragraphs for the website
:param passage: Site.Information
:return: more content or a none statement
"""
if not passage:
return None
paragraph = ''
passage.pop(0)
paragraph_title = passage[0][7:]
passage.pop(0)
get_paragraph = True
while get_paragraph is True:
if not passage:
return Content(paragraph_title, paragraph, None, None)
elif passage[0] == '!new_paragraph':
return Content(paragraph_title, paragraph, None, get_content(passage))
elif passage[0][:7] == '!image ':
image = get_image(passage)
more = get_content(passage)
return Content(paragraph_title, paragraph, image, more)
else:
paragraph += passage[0] + ' '
del passage[0]
def get_image(passage):
"""
fetches an image
:param passage: the passage
:return: an image location
"""
photo = ''
get_photo = True
while get_photo is True:
if not passage or passage[0] == '!new_paragraph':
return None
elif passage[0][:7] == '!image ':
photo += passage[0][7:]
passage.pop(0)
photo += ' 100%'
results = photo.split()
return Image(results[0], results[1], get_image(passage))
else:
photo += passage[0]
passage.pop(0)
def generator_main(parameters):
"""
The main function for this file
:param parameters: Dataclass (see the other python files)
:return:
"""
the_site = linked_list(parameters.text)
title = fetch_title(the_site.Title)
css_holder = style(parameters.settings[0],
parameters.settings[1],
parameters.settings[2],
parameters.settings[3])
footer = fetch_title_footer(the_site.Title)
if len(parameters.multi) < 2 or parameters.mode is "wizard":
url = ''
else:
url = url_linker(parameters.multi, parameters.titles)
body = create_body(the_site.Information)
closer = '</body>\n</html>'
# # debug
# print(the_site)
# print(title)
# print(css_holder)
# print(footer)
# print(body)
# print(closer)
full_file = title + css_holder + footer + url + body + closer
num = parameters.number
file_name = 'new_file' + str(num) + '.html'
with open(file_name, 'w') as file:
file.write(full_file)