-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
527 lines (430 loc) · 15.4 KB
/
main.py
File metadata and controls
527 lines (430 loc) · 15.4 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import argparse
import shutil
import os
import re
from os import walk
from typing import List, Tuple, Optional
import configparser
from html_utils.main import extract_body_content, extract_header_content
from fs_utils.main import get_absolute_path_of_where_this_script_exists
SCRIPT_DIR = get_absolute_path_of_where_this_script_exists()
def print_ini_layout():
print(
"""
Layout of fsweb.ini:
[settings]
ignore_files = file1.html, file2.html
ignore_directories = dir1, dir2
- ignore_files: A comma-separated list of file names to ignore in this directory.
- ignore_directories: A comma-separated list of directory names to ignore in this directory.
"""
)
def re_create_generated_directory(content_directory, generated_directory):
if os.path.exists(generated_directory):
shutil.rmtree(generated_directory)
shutil.copytree(
content_directory,
generated_directory,
ignore=shutil.ignore_patterns("fsweb.ini"),
)
def get_end_of_path(path):
return os.path.basename(os.path.normpath(path))
def load_fsweb_dir_ini(dir_path: str) -> Tuple[List[str], List[str]]:
"""Load the fsweb.ini file if it exists and return lists of ignored files and directories."""
config = configparser.ConfigParser()
ini_file_path = os.path.join(dir_path, "fsweb.ini")
ignored_files = []
ignored_directories = []
if os.path.exists(ini_file_path):
config.read(ini_file_path)
if "settings" in config:
ignored_files = [
f.strip()
for f in config["settings"].get("ignore_files", "").split(",")
if f.strip()
]
ignored_directories = [
d.strip()
for d in config["settings"].get("ignore_directories", "").split(",")
if d.strip()
]
return ignored_files, ignored_directories
def create_list_of_links_for_each_directory(directories: List[str]) -> str:
inner = ""
for directory in directories:
inner += f"\t\t<li><a href='{directory}/index.html'>{directory}</a></li>\n"
inner = inner[:-1] # remove ending new line
return f"""\t<ul>
{inner}
\t</ul>
"""
def create_list_of_links_for_each_html_file(files: List[str]) -> str:
inner = ""
for file in files:
inner += f"\t\t<li><a href='{file}'>{file[:-5]}</a></li>\n"
inner = inner[:-1] # remove ending new line
return f"""\t<ul>
{inner}
\t</ul>
"""
body_search_content = """
<div id="searchModal" class="modal">
<div class="modal-content">
<input type="text" id="searchInput" placeholder="Search...">
<ul id="results"></ul>
</div>
</div>
<script src="/search/fuzzysort.js"></script>
<script src="/search/search_callbacks.js"></script>
<script src="/search/search_list.js"></script>
<script src="/search/search.js"></script>
"""
def generate_links_for_header(theme: str) -> str:
return f"""
{'<link id="theme-stylesheet" rel="stylesheet" href="/search/dark_theme.css">'
if theme == 'dark' and search else ''}
{'<link id="theme-stylesheet" rel="stylesheet" href="/search/dark_theme.css">'
if theme == 'light' and search else ''}
{'<link rel="stylesheet" href="/search/search.css">' if search else ''}
"""
def generate_html_for_breadcrumb(rel_path: str) -> str:
path_parts = rel_path.split(os.sep) # Split the path by directory separator
breadcrumb = '<nav class="breadcrumb">\n<a href="/index.html">~</a>'
# Iterate through path parts and generate links
full_path = ""
for part in path_parts:
if part: # Avoid empty parts
full_path = os.path.join(full_path, part)
breadcrumb += f'/<a href="/{full_path}/index.html">{part}</a>'
breadcrumb += "\n</nav>"
return breadcrumb
def strip_output_dir(dir_path: str, output_dir: str) -> str:
"""
Strips out the 'output_dir' part of the path and returns the subpath starting from the point after 'output_dir'.
"""
if output_dir in dir_path:
# Split at 'output_dir' and take the part after it
dir_path = dir_path.split(output_dir, 1)[-1]
if dir_path.startswith(os.sep): # Remove leading slash if present
dir_path = dir_path[1:]
return dir_path
def create_list_of_links_for_each_non_html_file(files: List[str]) -> str:
"""Create HTML links for non-HTML files."""
inner = ""
for file in files:
inner += f"\t\t<li><a href='{file}'>{file}</a></li>\n"
inner = inner[:-1] # remove ending new line
return f"""\t<ul>
{inner}
\t</ul>
"""
def create_index_file(
output_dir: str,
curr_output_dir_path: str,
in_root_dir: bool,
sub_dir_names: List[str],
html_files: List[str],
non_html_files: List[str],
theme: str,
wrapper: bool,
search: bool,
breadcrumb: bool,
clobber_index_files: bool,
use_existing_index_files: bool,
merge_existing_index_files: bool,
css_file_path: Optional[str],
):
BLANK_HTML_FILE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=\"UTF-8\">
</head>
<body>
</body>
</html>
"""
# Prepare content for header and body
dir_name = get_end_of_path(strip_output_dir(curr_output_dir_path, output_dir))
breadcrumb_content = ""
if breadcrumb and not in_root_dir:
breadcrumb_content = generate_html_for_breadcrumb(
strip_output_dir(curr_output_dir_path, output_dir)
)
html_dir_content = (
f""" <h2>directories</h2>
{create_list_of_links_for_each_directory(sub_dir_names)}"""
if sub_dir_names
else ""
)
html_file_content = (
f""" <h2>files</h2>
{create_list_of_links_for_each_html_file(html_files)}"""
if html_files
else ""
)
non_html_file_content = (
f""" <h2>other files</h2>
{create_list_of_links_for_each_non_html_file(non_html_files)}"""
if non_html_files
else ""
)
header_content = f"""
<title>{dir_name}</title>
{"" if theme == 'light' else "<style>body { background-color:black; color: white; }</style>"}
{generate_links_for_header(theme) if search else ''}
{f'<link rel="stylesheet" href="{css_file_path}">' if css_file_path else ""}
"""
body_content = f"""
<article>
{"<div style='width: 70%; margin: 0 auto;'>" if wrapper else ""}
{breadcrumb_content}
<header>
<h1>{("~" if in_root_dir else dir_name) }</h1>
</header>
{html_dir_content}
{html_file_content}
{non_html_file_content}
{"</div>" if wrapper else ""}
{body_search_content if search else ''}
</article>"""
output_index_file_path = os.path.join(curr_output_dir_path, "index.html")
output_index_exists = os.path.exists(output_index_file_path)
if output_index_exists:
if clobber_index_files:
# write the blank html file as the initial content
with open(output_index_file_path, "w", encoding="utf-8") as file:
file.write(BLANK_HTML_FILE)
add_text_to_header_and_body_of_html(
output_index_file_path, header_content, body_content
)
if merge_existing_index_files:
body_content = "<hr>" + body_content
add_text_to_header_and_body_of_html(
output_index_file_path, header_content, body_content
)
if use_existing_index_files:
pass # by default if none of the above are true, then the default behavior is to leave existing index files
else:
# Write the blank HTML file as the initial content
with open(output_index_file_path, "w", encoding="utf-8") as file:
file.write(BLANK_HTML_FILE)
add_text_to_header_and_body_of_html(
output_index_file_path, header_content, body_content
)
def add_text_to_header_and_body_of_html(
html_file_path: str, head_text: str, body_text: str
) -> None:
"""
Inserts head_text before </head> and body_text before </body> or above <footer> if it exists.
"""
with open(html_file_path, "r", encoding="utf-8") as file:
html_content = file.read()
# Insert head_text before </head>
head_index = html_content.find("</head>")
if head_index != -1:
html_content = (
html_content[:head_index] + head_text + "\n" + html_content[head_index:]
)
# Determine where to insert body_text
footer_index = html_content.find("<footer>")
if footer_index != -1:
print("found footer")
insert_index = footer_index
else:
print("didn't find footer")
insert_index = html_content.find("</body>")
if insert_index != -1:
html_content = (
html_content[:insert_index] + body_text + "\n" + html_content[insert_index:]
)
with open(html_file_path, "w", encoding="utf-8") as file:
file.write(html_content)
def create_index_files(
output_dir: str,
theme: str,
wrapper: bool,
search: bool,
breadcrumb: bool,
clobber_index_files: bool,
use_existing_index_files: bool,
merge_existing_index_files: bool,
css_file_path: Optional[str],
) -> None:
if search:
shutil.copytree(SCRIPT_DIR + "/search", output_dir + "/search")
first_iteration = True
for output_dir_path, sub_dir_names, file_names in walk(output_dir):
print(f"\n==== Starting work on {output_dir_path} ====")
ignored_files, ignored_directories = load_fsweb_dir_ini(output_dir_path)
sub_dir_names[:] = [
d
for d in sub_dir_names
if not any(re.match(pattern, d) for pattern in ignored_directories)
]
html_files = [
f
for f in file_names
if f.endswith(".html")
and not any(re.match(pattern, f) for pattern in ignored_files)
]
non_html_files = [
f
for f in file_names
if not f.endswith(".html")
and not any(re.match(pattern, f) for pattern in ignored_files)
]
if search:
print("~~~> Modifying html files to include search functionality")
for html_file in html_files:
html_file_path = os.path.join(output_dir_path, html_file)
add_text_to_header_and_body_of_html(
html_file_path,
generate_links_for_header(theme),
body_search_content,
)
print(
f"~~~> Generating index file with links to dirs: {sub_dir_names}, html files: {html_files}, and other files: {non_html_files}"
)
create_index_file(
output_dir,
output_dir_path,
first_iteration,
sub_dir_names,
html_files,
non_html_files,
theme,
wrapper,
search,
breadcrumb,
clobber_index_files,
use_existing_index_files,
merge_existing_index_files,
css_file_path,
)
first_iteration = False
print(f"==== Done with {output_dir_path} ====\n")
def generate_search_list_file(generated_dir):
"""
generates list of files to be searched by the search feature
todo: need to handle dirs as well, just doing files
"""
print("generating search list")
file_list = []
for root, dirs, files in os.walk(generated_dir):
ignored_files, ignored_directories = load_fsweb_dir_ini(root)
html_files = [
f
for f in files
if f.endswith(".html")
and not any(re.match(pattern, f) for pattern in ignored_files)
]
for html_file in html_files:
relative_path = os.path.relpath(
os.path.join(root, html_file), generated_dir
)
file_list.append(
relative_path.replace("\\", "/")
) # Replace backslashes with forward slashes for JS compatibility
with open(generated_dir + "/search/search_list.js", "w") as f:
f.write("const search_list = [\n")
for file in file_list:
f.write(f' "{file}",\n')
f.write("];\n")
def create_argparser_and_get_args():
parser = argparse.ArgumentParser(
prog="fsweb",
description="Create a browsable website from a series of scattered HTML files",
epilog="Visit www.cuppajoeman.com for more information",
)
parser.add_argument(
"-s",
"--source-dir",
help="The source directory which fsweb will recursively process, path is relative to the fsweb directory",
required=True,
)
parser.add_argument(
"-o",
"--output-dir",
default="fsweb_generated_dir",
help="The directory that fsweb will output the modified files, path is relative to the fsweb directory",
)
parser.add_argument(
"-t",
"--theme",
choices=["light", "dark"],
help="Choose from 'dark' or 'light' themes",
)
parser.add_argument(
"-w",
"--wrapper",
action="store_true",
help="Add wrapper to every index file. Pushes the content towards the middle of the screen",
)
parser.add_argument(
"-ifm",
"--index-file-mode",
choices=["use", "clobber", "merge"],
default="use",
help=(
"Specify the mode for handling index files: "
"'use' to use existing index files, "
"'clobber' to overwrite them with the generated content, "
"'merge' to combine existing content with the generated content"
),
)
parser.add_argument(
"-x",
"--search",
action="store_true",
help="Add search functionality initiated by ctrl-space",
)
parser.add_argument(
"-css",
"--css-file-path",
help="Specify the absolute value to a css file relative to the generated directory root to apply to generated index files",
)
parser.add_argument(
"-b",
"--breadcrumb",
action="store_true",
help="Add breadcrumb navigation to every index file",
)
parser.add_argument(
"-il",
"--ini-layout",
action="store_true",
help="Show the layout of the fsweb.ini file and exit",
)
args = parser.parse_args()
if args.ini_layout:
print_ini_layout()
exit(0)
return args
if __name__ == "__main__":
args = create_argparser_and_get_args()
if args.source_dir and args.output_dir:
script_directory = os.path.dirname(os.path.realpath(__file__))
re_create_generated_directory(args.source_dir, args.output_dir)
theme = args.theme if args.theme else "light"
wrapper = args.wrapper
search = args.search
breadcrumb = args.breadcrumb
clobber_index_files = args.index_file_mode == "clobber"
use_existing_index_files = args.index_file_mode == "use"
merge_existing_index_files = args.index_file_mode == "merge"
css_file_path: Optional[str] = args.css_file_path
create_index_files(
args.output_dir,
theme,
wrapper,
search,
breadcrumb,
clobber_index_files,
use_existing_index_files,
merge_existing_index_files,
css_file_path,
)
if search:
generate_search_list_file(args.output_dir)
else:
print("Error: You must specify --base-dir and --gen-dir")