-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
96 lines (77 loc) · 3.67 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
import argparse
import requests
try:
import win32clipboard
import win32con
FOUND_WIN32_CLIPBOARD = True
except ImportError:
FOUND_WIN32_CLIPBOARD = False
def main():
parser = argparse.ArgumentParser(allow_abbrev=False)
input = parser.add_mutually_exclusive_group(required=True)
output = parser.add_mutually_exclusive_group(required=True)
input.add_argument("--File", metavar="File", help="File to format")
output.add_argument("--OutputFile", metavar="OutputFile", help="HTML file to output")
parser.add_argument("--lexer", help="Lexer to be used(see lexer.txt for options, default python)",
default="python")
parser.add_argument("--style", help="Color style to use(see style.txt for options, default vs)",
default="vs")
parser.add_argument("--linenos", help="Include line number", action="store_true")
if FOUND_WIN32_CLIPBOARD:
output.add_argument("--copytoclipboard", help="Copy to clipboard(windows, pywin32 only)", action="store_true")
input.add_argument("--copyfromclipboard", help="Copy from clipboard(windows, pywin32 only)", action="store_true")
arg = parser.parse_args()
if arg.copyfromclipboard and FOUND_WIN32_CLIPBOARD:
win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT).encode()
win32clipboard.CloseClipboard()
else:
file_str = arg.File
file_obj = open(file_str, "rb")
text = file_obj.read()
file_obj.close()
data = {"code": text.decode(), "lexer": arg.lexer, "style": arg.style, "divstyles": ""}
if arg.linenos:
data["linenos"] = "true"
r = requests.post("http://hilite.me/api", data=data)
text = r.content
if arg.copytoclipboard and FOUND_WIN32_CLIPBOARD:
clipboard_data_format = r"Version:0.9\r\n" \
"StartHTML:{:09d}\r\n" \
"EndHTML:{:09d}\r\n" \
"StartFragment:{:09d}\r\n" \
"EndFragment:{:09d}\r\n" \
"<!doctype>\r\n" \
"<html>\r\n" \
"<body>\r\n" \
"<!--StartFragment -->\r\n" \
"{}\r\n" \
"<!--EndFragment -->\r\n" \
"</body>\r\n" \
"</html>\r\n"
prefix = r"Version:0.9\r\n" \
"StartHTML:{:09d}\r\n" \
"EndHTML:{:09d}\r\n" \
"StartFragment:{:09d}\r\n" \
"EndFragment:{:09d}\r\n"
text = text.decode()
data_for_prefix = prefix.format(0, 0, 0, 0)
len_prefix = len(data_for_prefix)
data_for_prefix = clipboard_data_format.format(0, 0, 0, 0, text)
data_len = len(text)
start_data = data_for_prefix.index(text)
end_data = start_data + data_len
actual_formatted_data = clipboard_data_format.format(len_prefix, len(data_for_prefix),
start_data, end_data,
text)
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.RegisterClipboardFormat("HTML Format"), actual_formatted_data.encode("UTF-8"))
win32clipboard.CloseClipboard()
else:
file_obj = open(arg.OutputFile, "wb")
file_obj.write(text)
file_obj.close()
if __name__ == "__main__":
main()