Skip to content

Commit d576137

Browse files
Refactor: safer subprocess calls, install check, and fix output path
1 parent 253468b commit d576137

File tree

1 file changed

+68
-45
lines changed

1 file changed

+68
-45
lines changed

script.py

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,79 @@
1-
import os
2-
import time
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
35

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-
)
6+
import argparse
7+
import subprocess
8+
import shutil
9+
import sys
10+
import time
11+
from pathlib import Path
3612

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")
3720
args = parser.parse_args()
3821

3922
FILE_NAME = args.input
4023
TITLE = args.title
41-
OUTPUT_NAME = args.output
24+
OUTPUT_NAME = Path("dist") / Path(args.output).name # Only take filename, ignore any folder
25+
26+
# Make sure output folder exists
27+
OUTPUT_NAME.parent.mkdir(parents=True, exist_ok=True)
28+
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)
35+
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)
42+
43+
use_shell = sys.platform.startswith("win") # Needed for Windows
4244

45+
# ------------------------------
46+
# Install github-readme-to-html if missing
47+
# ------------------------------
4348
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 ")
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...")
58+
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)
4863

49-
time.sleep(5)
50-
print("Wait While The File is Converting")
51-
time.sleep(15)
64+
# ------------------------------
65+
# Convert README/Markdown to HTML
66+
# ------------------------------
67+
print("Converting file, please wait...")
68+
time.sleep(1)
5269

5370
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")
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)

0 commit comments

Comments
 (0)