Skip to content

Commit

Permalink
simplify cli app, input from file or stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
OleJoik committed Jun 11, 2024
1 parent 98f283f commit c4b71a0
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions htpy/html2htpy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
import re
import argparse
import select
from dataclasses import dataclass
from typing import Self
from html.parser import HTMLParser
Expand Down Expand Up @@ -246,50 +248,36 @@ def main():
help="Format output code (requires black installed)",
action="store_true",
)

def _convert_html(args: ConvertArgs):
convert_html_cli(args.shorthand, args.format)

parser.set_defaults(func=_convert_html)
parser.add_argument(
"input",
type=argparse.FileType("r"),
nargs="?",
default=sys.stdin,
help="input html from file or stdin",
)

args = parser.parse_args()

args.func(args)


def convert_html_cli(shorthand_id_class: bool, format: bool):
import time

print("")
print(f"HTML to HTPY converter")
print(f"selected options: ")
print(f" format: {format}")
print(f" shorthand id class: {shorthand_id_class}")
print("\n>>>>>>>>>>>>>>>>>>")
print(">>> paste html >>>")
print(">>>>>>>>>>>>>>>>>>\n")

collected_text = ""
input_starttime = None
if args.input == sys.stdin and select.select([sys.stdin], [], [], 0.1)[0]:
input = args.input.read()
elif args.input != sys.stdin:
input = args.input.read()
else:
_printerr(
"No input provided. Please supply an input file or stream.",
)
_printerr("Example usage: `cat index.html | html2htpy`")
_printerr("`html2htpy -h` for help")
sys.exit(1)

try:
while True:
user_input = input()
if not input_starttime:
input_starttime = time.time()
shorthand: bool = args.shorthand
format: bool = args.format

collected_text += user_input
print(html2htpy(input, shorthand, format))

if input_starttime + 0.1 < time.time():
break

output = html2htpy(collected_text, shorthand_id_class, format)
print("\n##############################################")
print("### serialized and formatted python (htpy) ###")
print("##############################################\n")
print(output)
except KeyboardInterrupt:
print("\nInterrupted")
def _printerr(value: str):
print(value, file=sys.stderr)


if __name__ == "__main__":
Expand Down

0 comments on commit c4b71a0

Please sign in to comment.