|
10 | 10 | import time |
11 | 11 | from pathlib import Path |
12 | 12 |
|
13 | | -# ------------------------------ |
14 | | -# Parse command line arguments |
15 | | -# ------------------------------ |
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() |
| 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 | 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 |
| 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 | 25 |
|
26 | | -# Make sure output folder exists |
27 | | -OUTPUT_NAME.parent.mkdir(parents=True, exist_ok=True) |
| 26 | + # Ensure output has .html extension |
| 27 | + if not OUTPUT_NAME.suffix: |
| 28 | + OUTPUT_NAME = OUTPUT_NAME.with_suffix(".html") |
28 | 29 |
|
29 | | -# ------------------------------ |
30 | | -# Check input file exists |
31 | | -# ------------------------------ |
32 | | -if not Path(FILE_NAME).is_file(): |
33 | | - print(f"Error: The input file does not exist: {FILE_NAME}", file=sys.stderr) |
34 | | - sys.exit(1) |
| 30 | + # Make sure output folder exists |
| 31 | + OUTPUT_NAME.parent.mkdir(parents=True, exist_ok=True) |
35 | 32 |
|
36 | | -# ------------------------------ |
37 | | -# Check if npx is installed |
38 | | -# ------------------------------ |
39 | | -if not shutil.which("npx"): |
40 | | - print("Error: 'npx' is not installed. Please install Node.js first: https://nodejs.org/", file=sys.stderr) |
41 | | - sys.exit(1) |
| 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}") |
42 | 36 |
|
43 | | -use_shell = sys.platform.startswith("win") # Needed for Windows |
44 | 37 |
|
45 | | -# ------------------------------ |
46 | | -# Install github-readme-to-html if missing |
47 | | -# ------------------------------ |
48 | | -try: |
49 | | - subprocess.run( |
50 | | - ["npx", "github-readme-to-html", "--version"], |
51 | | - check=True, |
52 | | - stdout=subprocess.DEVNULL, |
53 | | - stderr=subprocess.DEVNULL, |
54 | | - shell=use_shell |
55 | | - ) |
56 | | -except subprocess.CalledProcessError: |
57 | | - print("Installing 'github-readme-to-html' globally via npm...") |
| 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 |
58 | 49 | try: |
59 | | - subprocess.run(["npm", "install", "-g", "github-readme-to-html"], check=True, shell=use_shell) |
60 | | - except subprocess.CalledProcessError as e: |
61 | | - print(f"Error: Failed to install 'github-readme-to-html' (exit code {e.returncode})", file=sys.stderr) |
62 | | - sys.exit(e.returncode) |
| 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) |
63 | 75 |
|
64 | | -# ------------------------------ |
65 | | -# Convert README/Markdown to HTML |
66 | | -# ------------------------------ |
67 | | -print("Converting file, please wait...") |
68 | | -time.sleep(1) |
| 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 |
69 | 88 |
|
70 | | -try: |
71 | | - subprocess.run( |
72 | | - ["npx", "github-readme-to-html", "-i", FILE_NAME, "-t", TITLE, "-o", str(OUTPUT_NAME.name)], |
73 | | - check=True, |
74 | | - shell=use_shell |
75 | | - ) |
76 | | - print(f"Conversion successful! HTML saved to: {OUTPUT_NAME}") |
77 | | -except subprocess.CalledProcessError as e: |
78 | | - print(f"Error: Conversion failed (exit code {e.returncode}). Please check your input file and command.", file=sys.stderr) |
79 | | - sys.exit(e.returncode) |
| 89 | +# Entry point |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments