|
1 | | -import os |
| 1 | +# Simple README to HTML converter |
| 2 | +# Run like this: |
| 3 | +# python script.py -i README.md -o output.html -t "My Project" |
| 4 | +# Output will always go in the 'dist/' folder, so just pass a filename for -o |
| 5 | + |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | +import shutil |
| 9 | +import sys |
2 | 10 | import time |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +def main(): |
| 14 | + |
| 15 | + # Parse command line arguments |
| 16 | + parser = argparse.ArgumentParser(description="Convert a README/Markdown file to HTML") |
| 17 | + parser.add_argument("-i", "--input", required=True, help="Path to the input file, e.g., README.md") |
| 18 | + parser.add_argument("-t", "--title", default="Progress", help="Title of the HTML page") |
| 19 | + parser.add_argument("-o", "--output", required=True, help="Output filename (will be saved in dist/ folder), e.g., output.html") |
| 20 | + args = parser.parse_args() |
| 21 | + |
| 22 | + FILE_NAME = args.input |
| 23 | + TITLE = args.title |
| 24 | + OUTPUT_NAME = Path("dist") / Path(args.output).name # Only take filename, ignore any folder |
| 25 | + |
| 26 | + # Ensure output has .html extension |
| 27 | + if not OUTPUT_NAME.suffix: |
| 28 | + OUTPUT_NAME = OUTPUT_NAME.with_suffix(".html") |
| 29 | + |
| 30 | + # Make sure output folder exists |
| 31 | + OUTPUT_NAME.parent.mkdir(parents=True, exist_ok=True) |
| 32 | + |
| 33 | + # Check input file exists |
| 34 | + if not Path(FILE_NAME).is_file(): |
| 35 | + raise FileNotFoundError(f"The input file does not exist: {FILE_NAME}") |
| 36 | + |
| 37 | + |
| 38 | + # Check if npx and node are installed |
| 39 | + if not shutil.which("npx"): |
| 40 | + raise EnvironmentError("`npx` is not installed. Please install Node.js first: https://nodejs.org/") |
| 41 | + |
| 42 | + if not shutil.which("node"): |
| 43 | + raise EnvironmentError("`node` is not installed or not in PATH. Please install Node.js first: https://nodejs.org/") |
| 44 | + |
| 45 | + use_shell = sys.platform.startswith("win") # Needed for Windows |
| 46 | + |
| 47 | + |
| 48 | + # Install github-readme-to-html if missing |
| 49 | + try: |
| 50 | + subprocess.run( |
| 51 | + ["npx", "github-readme-to-html", "--version"], |
| 52 | + check=True, |
| 53 | + capture_output=True, |
| 54 | + text=True, |
| 55 | + shell=use_shell |
| 56 | + ) |
| 57 | + except subprocess.CalledProcessError: |
| 58 | + print("Installing 'github-readme-to-html' globally via npm...") |
| 59 | + try: |
| 60 | + subprocess.run( |
| 61 | + ["npm", "install", "-g", "github-readme-to-html"], |
| 62 | + check=True, |
| 63 | + capture_output=True, |
| 64 | + text=True, |
| 65 | + shell=use_shell |
| 66 | + ) |
| 67 | + except subprocess.CalledProcessError as e: |
| 68 | + print(f"Error: {e.stderr}", file=sys.stderr) |
| 69 | + raise RuntimeError(f"Failed to install 'github-readme-to-html' (exit code {e.returncode})") |
| 70 | + |
| 71 | + |
| 72 | + # Convert README/Markdown to HTML |
| 73 | + print("Converting file, please wait...") |
| 74 | + time.sleep(1) |
| 75 | + |
| 76 | + try: |
| 77 | + subprocess.run( |
| 78 | + ["npx", "github-readme-to-html", "-i", FILE_NAME, "-t", TITLE, "-o", str(OUTPUT_NAME.name)], |
| 79 | + check=True, |
| 80 | + capture_output=True, |
| 81 | + text=True, |
| 82 | + shell=use_shell |
| 83 | + ) |
| 84 | + print(f"Conversion successful! HTML saved to: {OUTPUT_NAME}") |
| 85 | + except subprocess.CalledProcessError as e: |
| 86 | + print(f"Error: {e.stderr}", file=sys.stderr) |
| 87 | + raise RuntimeError(f"Conversion failed (exit code {e.returncode}). Please check your input file and command.") from e |
3 | 88 |
|
4 | | -import argparse as parsing |
5 | | - |
6 | | - |
7 | | -parser = parsing.ArgumentParser( |
8 | | - description="ReadME.md to .html writer", |
9 | | - formatter_class=parsing.ArgumentDefaultsHelpFormatter, |
10 | | -) |
11 | | - |
12 | | -parser.add_argument( |
13 | | - "-i", |
14 | | - "--input", |
15 | | - action="store", |
16 | | - type=str, |
17 | | - required=True, |
18 | | - help="name of the input file eg. template.html", |
19 | | -) |
20 | | -parser.add_argument( |
21 | | - "-t", |
22 | | - "--title", |
23 | | - action="store", |
24 | | - type=str, |
25 | | - default="Progress", |
26 | | - help="title of the html page.", |
27 | | -) |
28 | | -parser.add_argument( |
29 | | - "-o", |
30 | | - "--output", |
31 | | - action="store", |
32 | | - type=str, |
33 | | - required=True, |
34 | | - help="the generated output file name , which is generated in dist folder. eg. template.html", |
35 | | -) |
36 | | - |
37 | | -args = parser.parse_args() |
38 | | - |
39 | | -FILE_NAME = args.input |
40 | | -TITLE = args.title |
41 | | -OUTPUT_NAME = args.output |
42 | | - |
43 | | -try: |
44 | | - os.system("npm install github-readme-to-html") |
45 | | -except: |
46 | | - print("NPM is not Installed") |
47 | | - print("Try : sudo apt install nodejs | sudo apt install npm ") |
48 | | - |
49 | | -time.sleep(5) |
50 | | -print("Wait While The File is Converting") |
51 | | -time.sleep(15) |
52 | | - |
53 | | -try: |
54 | | - os.system(f"npx github-readme-to-html -i {FILE_NAME} -t {TITLE} -o {OUTPUT_NAME}") |
55 | | -except: |
56 | | - print("Please Check If file is exit") |
| 89 | +# Entry point |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments