-
Notifications
You must be signed in to change notification settings - Fork 45
feat: replace os.system with subprocess.run and add npm install check #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Mr-Sunglasses
merged 2 commits into
FOSS-Community:main
from
ApurveKaranwal:my-feature-branch
Oct 17, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.