-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtxtToWeb.py
executable file
·85 lines (70 loc) · 2.31 KB
/
txtToWeb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
import argparse
import os
import sys
import shutil
from include.config_parser import parse_config
from include.file_parser import process_file, process_folder
# Define the tool's version
VERSION = "txtToWeb v0.1"
def parse_arguments():
parser = argparse.ArgumentParser(
description="txtToWeb - Convert Text to Web Content"
)
parser.add_argument("input_path", help="File or folder path to process")
parser.add_argument(
"--version",
"-v",
action="version",
version=VERSION,
help="Show the tool's version",
)
parser.add_argument(
"--stylesheet",
"-s",
help="URL to a CSS stylesheet to be used in the HTML files",
)
parser.add_argument(
"--lang",
"-l",
default="en-CA", # Default language is Canadian English
help="Language code to be used in the lang attribute on the root element",
)
parser.add_argument(
"--config",
"-c",
help="URL to a TOML config file to be used as a config for the HTML files",
)
return parser.parse_args()
def handle_arguments(args):
input_path = args.input_path
stylesheet_url = args.stylesheet
lang_attribute = args.lang
if args.config:
stylesheet_url, lang_attribute = parse_config(
args.config, stylesheet_url, lang_attribute
)
return input_path, stylesheet_url, lang_attribute
def execute_conversion(input_path, stylesheet_url, lang_attribute):
if os.path.isfile(input_path) and (
input_path.endswith(".txt") or input_path.endswith(".md")
):
if os.path.exists("til"):
shutil.rmtree("til")
os.makedirs("til")
process_file(input_path, stylesheet_url, lang_attribute)
elif os.path.isdir(input_path):
process_folder(input_path, stylesheet_url, lang_attribute)
else:
print("Invalid input path. Please provide a valid file or folder path.")
raise ValueError("Invalid input path")
def main():
try:
args = parse_arguments()
input_path, stylesheet_url, lang_attribute = handle_arguments(args)
execute_conversion(input_path, stylesheet_url, lang_attribute)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()