|
| 1 | +import markdown |
| 2 | + |
| 3 | +''' |
| 4 | +
|
| 5 | +Title: Markdown Parser |
| 6 | +
|
| 7 | +Description: This program turns your Markdown files into HTML files |
| 8 | +
|
| 9 | +Created By: Tyran Rice Jr. |
| 10 | +
|
| 11 | +''' |
| 12 | + |
| 13 | +''' |
| 14 | +
|
| 15 | +Function Title: read_markdown_file |
| 16 | +
|
| 17 | +Description: |
| 18 | + This function reads the user's markdown file from the |
| 19 | + given path input. |
| 20 | +
|
| 21 | +Arguments: |
| 22 | + - markdown_path: The path to the Markdown file given by the user |
| 23 | +
|
| 24 | +Returns: |
| 25 | + - markdown_content: The Markdown file that was read from the given path |
| 26 | +
|
| 27 | +''' |
| 28 | + |
| 29 | + |
| 30 | +def read_markdown_file(markdown_path): |
| 31 | + with open(markdown_path, "r", encoding="utf-8") as md_file: |
| 32 | + markdown_content = md_file.readlines() |
| 33 | + |
| 34 | + return markdown_content |
| 35 | + |
| 36 | + |
| 37 | +''' |
| 38 | +
|
| 39 | +Function Title: write_html_file |
| 40 | +
|
| 41 | +Description: |
| 42 | + This function takes the user's updated markdown file and converts it |
| 43 | + into a HTML file under the name 'convertedmarkdown.html'. |
| 44 | +
|
| 45 | +Arguments: |
| 46 | + - markdown_content: The updated Markdown file |
| 47 | +
|
| 48 | +Returns: |
| 49 | + - void: Simply writes a new HTML file |
| 50 | +
|
| 51 | +''' |
| 52 | + |
| 53 | + |
| 54 | +def write_html_file(markdown_content): |
| 55 | + # Convert Markdown file to HTML |
| 56 | + html_content = markdown.markdown(markdown_content) |
| 57 | + |
| 58 | + with open("./output/convertedmarkdown.html", "w") as f: |
| 59 | + f.write("<!DOCTYPE html>\n") |
| 60 | + f.write("<html lang='en'>\n") |
| 61 | + f.write("""<head> |
| 62 | + <meta charset = "UTF-8" > |
| 63 | + <link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown-light.min.css" > |
| 64 | + <style > |
| 65 | + body { |
| 66 | + max-width: 800px; |
| 67 | + margin: auto; |
| 68 | + padding: 2rem; |
| 69 | + } |
| 70 | + </style > |
| 71 | + </head > |
| 72 | + <body class = "markdown-body" >""") |
| 73 | + f.write(html_content + "\n") |
| 74 | + f.write("</html>") |
| 75 | + |
| 76 | + |
| 77 | +''' |
| 78 | +
|
| 79 | +Function Title: change_url_path |
| 80 | +
|
| 81 | +Description: |
| 82 | + This function changes the URL path for src references to |
| 83 | + make sure they are correctly pathed to the right directory. |
| 84 | + This is done by taking the input of the user's path to their |
| 85 | + existing mark down file and adding it to the src path. |
| 86 | +
|
| 87 | +Arguments: |
| 88 | + - markdown_content: The Markdown file given by the user |
| 89 | + - markdown_path: The path to the Markdown file given by the user |
| 90 | +
|
| 91 | +Returns: |
| 92 | + - markdown_content: The new Markdown file with updated src paths |
| 93 | +
|
| 94 | +''' |
| 95 | + |
| 96 | + |
| 97 | +def change_url_path(markdown_content, markdown_path): |
| 98 | + # Removes 'README.md' from path to ensure the path is correctly referenced in src |
| 99 | + markdown_path = markdown_path.removesuffix("README.md") |
| 100 | + for i in range(len(markdown_content)): |
| 101 | + # If "src=" is located in a line, it will be correctly referenced |
| 102 | + if (markdown_content[i].find("src=") != -1): |
| 103 | + index = markdown_content[i].find("src=") |
| 104 | + # Change src to have the correct path location |
| 105 | + markdown_content[i] = markdown_content[i][:index + 4] + \ |
| 106 | + "\"" + markdown_path + markdown_content[i][index + 5:] |
| 107 | + |
| 108 | + return markdown_content |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + try: |
| 113 | + print("Welcome to Tyran's Markdown-To-HTML Converter") |
| 114 | + markdown_path = input("Please enter the path of the mark down file: ") |
| 115 | + |
| 116 | + # Read the Markdown file and convert it into a list of strings |
| 117 | + markdown_content = read_markdown_file(markdown_path) |
| 118 | + |
| 119 | + # Update references inside Markdown file |
| 120 | + markdown_content = change_url_path(markdown_content, markdown_path) |
| 121 | + |
| 122 | + # Join the list of strings from the Markdown file into one entire string |
| 123 | + markdown_content = ''.join(markdown_content) |
| 124 | + |
| 125 | + # Turn the Markdown file into a HTML file |
| 126 | + write_html_file(markdown_content) |
| 127 | + |
| 128 | + print("Converted Markdown To HTML content") |
| 129 | + |
| 130 | + except FileNotFoundError: |
| 131 | + print("Error: The specified Markdown file was not found.") |
| 132 | + except Exception as e: |
| 133 | + print(f"An error occurred!\nError: {e}") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments