From 646194555c0610d53929c442b8f6318239dc05fd Mon Sep 17 00:00:00 2001 From: "georg.sinn-schirwitz" Date: Sun, 3 Nov 2024 12:45:38 +0100 Subject: [PATCH] feat: add python scripts --- main.py | 64 +++++++++++++++++++++++++++++++++++++++++ src/file_handler.py | 57 ++++++++++++++++++++++++++++++++++++ src/markdown_to_html.py | 48 +++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 main.py create mode 100644 src/file_handler.py create mode 100644 src/markdown_to_html.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..74c6b64 --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +"""Python application main file for the repository + +In this file all python code is referenced and executed that is included in this courses repository. +The main file is used to run the code and to test the functionality of the code. + +Functionality of the code is loaded from modules found in folder /src. + +""" + +import os +import sys +from src import markdown_to_html +from src import file_handler + + +def main(): + """Main function of the courses module. + + This function is the entry point for the courses module. + The function loads the files from the /courses folder and sorts them by file type. + Markdown files are converted to html files and saved in the /html folder. + """ + # Load markdown files from /courses folder + course_files = file_handler.load_files("courses") + markdown_files = file_handler.sort_files(course_files, "md") + html_output_files = file_handler.sort_files(course_files, "html") + + # check /html folder + if os.path.exists("html"): + # load html files from /html folder + html_files = file_handler.load_files("html") + # extend converted_files with html files + html_output_files.extend(html_files) + else: + # create /html folder + os.makedirs("html") + + for file in html_output_files: + print(f"HTML file: {file}") + name = os.path.splitext(os.path.basename(file))[0] + print(f"Name: {name}") + + # move to /html folder + dst_file = f"html/{os.path.basename(file)}" + if not os.path.exists(dst_file): + os.rename(file, dst_file) + print(f"Moved file to /html folder: {dst_file}") + else: + print(f"File already exists in /html folder: {dst_file}") + + # get course markdown file name + course_file = f"courses\\{name}.md" + + # remove markdown file from list markdown_files + if course_file in markdown_files: + markdown_files.remove(course_file) + print(f"Removed markdown file from list: {course_file}") + + # Convert remaining markdown files to html files + markdown_to_html.convert_markdown_files(markdown_files) + + +if __name__ == "__main__": + main() diff --git a/src/file_handler.py b/src/file_handler.py new file mode 100644 index 0000000..64f0602 --- /dev/null +++ b/src/file_handler.py @@ -0,0 +1,57 @@ +"""File handler module.""" +import os + +def load_files(folder): + """Load files from a folder. + + This function loads files from a folder. + The function returns a list of files found in the folder. + + Args: + folder (str): Folder to load files from. + + Returns: + list: List of files found in the folder. + """ + files = [] + for file in os.listdir(folder): + files.append(os.path.join(folder, file)) + return files + +def sort_files(files, file_type): + """Sort files by file type. + + This function sorts files by file type. + The function returns a list of files that match the file type. + + Args: + files (list): List of files to sort. + file_type (str): File type to sort by. + + Returns: + list: List of files that match the file type. + """ + sorted_files = [] + for file in files: + if file.endswith(file_type): + sorted_files.append(file) + return sorted_files + +def main(): + """Main function of the file_handler module. + + This function is the entry point for the file_handler module. + The function loads files from the /courses folder and sorts them by file type. + The files are then printed to the console. + """ + # Load files from /courses folder + files = load_files("courses") + # Sort files by file type + markdown_files = sort_files(files, "md") + # Print files + print("Markdown files:") + for file in markdown_files: + print(file) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/markdown_to_html.py b/src/markdown_to_html.py new file mode 100644 index 0000000..1e5cb67 --- /dev/null +++ b/src/markdown_to_html.py @@ -0,0 +1,48 @@ +"""Markdown to HTML conversion module.""" +import os +import markdown +from src import file_handler + + +def convert_markdown_files(markdown_files): + """Convert markdown files to html files. + + This function converts markdown files to html files. + The function uses the markdown module to convert the markdown files. + The html files are saved in the /html folder. + + Args: + markdown_files (list): List of markdown files to convert. + """ + # Create /html folder if it does not exist + if not os.path.exists("html"): + os.makedirs("html") + # Convert markdown files to html files + for markdown_file in markdown_files: + # Read markdown file + with open(markdown_file, "r") as file: + markdown_content = file.read() + # Convert markdown to html + html_content = markdown.markdown(markdown_content) + # Save html file + html_file = "html/" + \ + os.path.splitext(os.path.basename(markdown_file))[0] + ".html" + with open(html_file, "w") as file: + file.write(html_content) + print(f"Converted {markdown_file} to {html_file}") + + +def main(): + """Main function of the markdown_to_html module. + + This function is the entry point for the markdown_to_html module. + The function loads the markdown files from the /courses folder and converts them to html files. + """ + # Load markdown files from /courses folder + course_files = file_handler.load_files("courses") + markdown_files = file_handler.sort_files(course_files, "md") + print(markdown_files) + + +if __name__ == "__main__": + main()