Skip to content

Commit

Permalink
Merge pull request #133 from alxbilger/removesuffix
Browse files Browse the repository at this point in the history
Rename files and folders to remove suffix
  • Loading branch information
hugtalbot committed Jul 5, 2024
2 parents 0e1d0ed + abde962 commit 8b34f43
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions script/generate_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
This script visits all the files of a directory and generates a table of content for mkdocs, in the configuration
file mkdocs.yml.
During the visit, it also renames the files and the folders to remove the numeric suffix. The suffixes are here
just to define the order in the table of content.
Usage:
python script.py <input_file> <input_directory>
Expand All @@ -27,13 +29,31 @@ def remove_extension(filename):
return name_without_extension


def clean_filename(s):
# Use regex to replace the starting substring that matches ^\d+_
s = re.sub(r'^\d+_', '', s)
return s


def clean_title(s):
if s.endswith('.md'):
s = remove_extension(s)
# Use regex to replace the starting substring that matches ^\d+_
title = re.sub(r'^\d+_', '', s)
title = title.replace("_", " ")
return title
s = clean_filename(s)
s = s.replace("_", " ")
return s


def clean_path(path):
# Split the path into components
components = path.split(os.sep)

# Apply clean_filename to each component
cleaned_components = [clean_filename(component) for component in components]

# Join the cleaned components back into a path
cleaned_path = os.sep.join(cleaned_components)

return cleaned_path


def contains_md_file(directory):
Expand All @@ -44,6 +64,14 @@ def contains_md_file(directory):
return False


def rename_directory(directory):
[dir_head, dir_basename] = os.path.split(directory)
new_directory_name = os.path.join(dir_head, clean_filename(dir_basename))
if not os.path.exists(new_directory_name):
print('rename', directory, new_directory_name)
os.rename(directory, new_directory_name)


def analyze_folder(yml_file, folder_path):
with open(yml_file, "a") as file:
nav_content = ''
Expand All @@ -54,20 +82,33 @@ def analyze_folder(yml_file, folder_path):
level = root.replace(folder_path, '').count(os.sep)
indent = TAB * level

root_basename = os.path.basename(root)
[root_head, root_basename] = os.path.split(root)
folder_name = clean_title(root_basename)

nav_content += f"{indent}- {folder_name}:\n"
subindent = TAB * (level + 1)
for filename in sorted(files):
if filename.endswith('.md'):
relative_file_path = os.path.relpath(os.path.join(root, filename), folder_path)
cleaned_filename = clean_filename(filename)
os.rename(os.path.join(root, filename), os.path.join(root, cleaned_filename))
relative_file_path = os.path.relpath(os.path.join(root, cleaned_filename), folder_path)
relative_file_path = clean_path(relative_file_path)
nav_content += f"{subindent}- {clean_title(filename)}: {relative_file_path}\n"

if nav_content != '':
file.write('nav:\n')
file.write(nav_content)
print(nav_content)

for root, dirs, files in os.walk(folder_path, False):
if root == folder_path:
continue # Skip the root directory
for directory in dirs:
full_directory = os.path.join(root, directory)
print('visiting', full_directory)
rename_directory(full_directory)
rename_directory(root)


if __name__ == "__main__":
if len(sys.argv) != 3:
Expand Down

0 comments on commit 8b34f43

Please sign in to comment.