feat: add python scripts

This commit is contained in:
georg.sinn-schirwitz
2024-11-03 12:45:38 +01:00
parent 4772c708f1
commit 646194555c
3 changed files with 169 additions and 0 deletions

64
main.py Normal file
View File

@@ -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()

57
src/file_handler.py Normal file
View File

@@ -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()

48
src/markdown_to_html.py Normal file
View File

@@ -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()