Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 68 additions & 45 deletions script.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,79 @@
import os
import time
# Simple README to HTML converter
# Run like this:
# python script.py -i README.md -o output.html -t "My Project"
# Output will always go in the 'dist/' folder, so just pass a filename for -o

import argparse as parsing


parser = parsing.ArgumentParser(
description="ReadME.md to .html writer",
formatter_class=parsing.ArgumentDefaultsHelpFormatter,
)

parser.add_argument(
"-i",
"--input",
action="store",
type=str,
required=True,
help="name of the input file eg. template.html",
)
parser.add_argument(
"-t",
"--title",
action="store",
type=str,
default="Progress",
help="title of the html page.",
)
parser.add_argument(
"-o",
"--output",
action="store",
type=str,
required=True,
help="the generated output file name , which is generated in dist folder. eg. template.html",
)
import argparse
import subprocess
import shutil
import sys
import time
from pathlib import Path

# ------------------------------
# Parse command line arguments
# ------------------------------
parser = argparse.ArgumentParser(description="Convert a README/Markdown file to HTML")
parser.add_argument("-i", "--input", required=True, help="Path to the input file, e.g., README.md")
parser.add_argument("-t", "--title", default="Progress", help="Title of the HTML page")
parser.add_argument("-o", "--output", required=True, help="Output filename (will be saved in dist/ folder), e.g., output.html")
args = parser.parse_args()

FILE_NAME = args.input
TITLE = args.title
OUTPUT_NAME = args.output
OUTPUT_NAME = Path("dist") / Path(args.output).name # Only take filename, ignore any folder

# Make sure output folder exists
OUTPUT_NAME.parent.mkdir(parents=True, exist_ok=True)

# ------------------------------
# Check input file exists
# ------------------------------
if not Path(FILE_NAME).is_file():
print(f"Error: The input file does not exist: {FILE_NAME}", file=sys.stderr)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to raise an exception here, instead of sys.exit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay sir, i will fix both sys.exit to raise an exception.

sys.exit(1)

# ------------------------------
# Check if npx is installed
# ------------------------------
if not shutil.which("npx"):
print("Error: 'npx' is not installed. Please install Node.js first: https://nodejs.org/", file=sys.stderr)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here also, use exception instead of sys.exit

sys.exit(1)

use_shell = sys.platform.startswith("win") # Needed for Windows

# ------------------------------
# Install github-readme-to-html if missing
# ------------------------------
try:
os.system("npm install github-readme-to-html")
except:
print("NPM is not Installed")
print("Try : sudo apt install nodejs | sudo apt install npm ")
subprocess.run(
["npx", "github-readme-to-html", "--version"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=use_shell
)
except subprocess.CalledProcessError:
print("Installing 'github-readme-to-html' globally via npm...")
try:
subprocess.run(["npm", "install", "-g", "github-readme-to-html"], check=True, shell=use_shell)
except subprocess.CalledProcessError as e:
print(f"Error: Failed to install 'github-readme-to-html' (exit code {e.returncode})", file=sys.stderr)
sys.exit(e.returncode)

time.sleep(5)
print("Wait While The File is Converting")
time.sleep(15)
# ------------------------------
# Convert README/Markdown to HTML
# ------------------------------
print("Converting file, please wait...")
time.sleep(1)

try:
os.system(f"npx github-readme-to-html -i {FILE_NAME} -t {TITLE} -o {OUTPUT_NAME}")
except:
print("Please Check If file is exit")
subprocess.run(
["npx", "github-readme-to-html", "-i", FILE_NAME, "-t", TITLE, "-o", str(OUTPUT_NAME.name)],
check=True,
shell=use_shell
)
print(f"Conversion successful! HTML saved to: {OUTPUT_NAME}")
except subprocess.CalledProcessError as e:
print(f"Error: Conversion failed (exit code {e.returncode}). Please check your input file and command.", file=sys.stderr)
sys.exit(e.returncode)