Merge pull request #1 from Meisenburger13/Meisenburger13-encoding

This commit is contained in:
Andros Fenollosa 2023-11-18 08:38:24 +01:00 committed by GitHub
commit 5c1199f315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,7 +49,7 @@ print("Renaming files and fixing links...")
path_to_files = path.join(FOLDER_EXTRACTION, "**/*." + MARKDOWN_EXTENSION) path_to_files = path.join(FOLDER_EXTRACTION, "**/*." + MARKDOWN_EXTENSION)
for filename in glob.iglob(path_to_files, recursive=True): for filename in glob.iglob(path_to_files, recursive=True):
# Get the heading of the file # Get the heading of the file
with open(filename, "r") as file: with open(filename, "r", encoding="utf-8") as file:
# Get the heading # Get the heading
first_line = file.readline() first_line = file.readline()
heading = first_line.replace("# ", "").replace("\n", "").replace("/", "-") heading = first_line.replace("# ", "").replace("\n", "").replace("/", "-")
@ -57,7 +57,7 @@ for filename in glob.iglob(path_to_files, recursive=True):
lines = file.readlines() lines = file.readlines()
lines_without_heading = lines[1:] lines_without_heading = lines[1:]
# Write the file without the heading # Write the file without the heading
with open(filename, "w") as file: with open(filename, "w", encoding="utf-8") as file:
file.write("".join(lines_without_heading)) file.write("".join(lines_without_heading))
# Rename the file # Rename the file
new_filename = path.join(path.dirname(filename), heading + "." + MARKDOWN_EXTENSION) new_filename = path.join(path.dirname(filename), heading + "." + MARKDOWN_EXTENSION)
@ -66,9 +66,9 @@ for filename in glob.iglob(path_to_files, recursive=True):
old_filename_encoded = urllib.parse.quote(ntpath.basename(filename)) old_filename_encoded = urllib.parse.quote(ntpath.basename(filename))
heading_encoded = urllib.parse.quote(heading) + "." + MARKDOWN_EXTENSION heading_encoded = urllib.parse.quote(heading) + "." + MARKDOWN_EXTENSION
for filename_to_fix in glob.iglob(path_to_files, recursive=True): for filename_to_fix in glob.iglob(path_to_files, recursive=True):
with open(filename_to_fix, "r") as file: with open(filename_to_fix, "r", encoding="utf-8") as file:
lines_to_fix = file.readlines() lines_to_fix = file.readlines()
with open(filename_to_fix, "w") as file: with open(filename_to_fix, "w", encoding="utf-8") as file:
text_to_write = "".join(lines_to_fix).replace( text_to_write = "".join(lines_to_fix).replace(
old_filename_encoded, heading_encoded old_filename_encoded, heading_encoded
) )
@ -87,9 +87,9 @@ for folder in glob.iglob(path_of_folders, recursive=True):
old_folder_name_encoded = urllib.parse.quote(current_folder_name) old_folder_name_encoded = urllib.parse.quote(current_folder_name)
new_folder_name_encoded = urllib.parse.quote(new_folder_name) new_folder_name_encoded = urllib.parse.quote(new_folder_name)
for filename_to_fix in glob.iglob(path_to_files, recursive=True): for filename_to_fix in glob.iglob(path_to_files, recursive=True):
with open(filename_to_fix, "r") as file: with open(filename_to_fix, "r", encoding="utf-8") as file:
lines_to_fix = file.readlines() lines_to_fix = file.readlines()
with open(filename_to_fix, "w") as file: with open(filename_to_fix, "w", encoding="utf-8") as file:
text_to_write = "".join(lines_to_fix).replace( text_to_write = "".join(lines_to_fix).replace(
old_folder_name_encoded, new_folder_name_encoded old_folder_name_encoded, new_folder_name_encoded
) )