forked from TikaelSol/PF2e-Foundry-Data-Entry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixText.py
126 lines (89 loc) · 3.92 KB
/
fixText.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from tkinter import Tk, Frame, Canvas, Text, Button, END, BooleanVar, Menu
from regex import sub
from pyperclip import copy
# As nice as it is to have these declared separately for some reason different IDEs react poorly
# to them being * imported.
def convert_to_lower(match_obj):
if match_obj.group() is not None:
return match_obj.group().lower()
def handle_bullet_lists(string):
# Removing bullet points, should replace with the actual bullet points.
string = sub(r"•", "<ul><li>", string, count=1)
string = sub(r"•", "</li><li>", string)
return string
def reformat(text):
# Initial handling not using regex.
string = "<p>" + text
string = string.replace(" ", " ") \
.replace("-\n", "-") \
.replace("—\n", "—") \
.replace("\n", " ")
string = string.replace("<p><p>", "<p>") \
.replace(r"”", r'"') \
.replace(r"“", r'"') \
.replace(r"…", r"...")
if remove_non_ASCII:
string = string.replace("’", "'")
string = handle_bullet_lists(string)
# This is for text replacement without the wrapping <p></p> tags around the entire block.
# Easiest to just strip them.
if replacement_mode:
string = string[3:]
string = string[:-4]
# print("\n")
# print(string)
if use_clipboard:
copy(string)
# return string
outputText.delete("1.0", END)
outputText.insert(END, string)
###############################################################################
# If you want to run the console version of this instead of the GUI comment out
# the outputText lines above and uncomment `return string`. Then comment out
# everything below up to `def main()` then comment out `root.mainloop()` and
# uncomment `reformat(...)`.
#
# Build with `pyinstaller --noconsole --icon="D20_icon.ico" --onefile dataEntry.py`
###############################################################################
def clear_input():
inputText.delete("1.0", END)
Height = 700
Width = 800
root = Tk()
root.title("Modified Data Entry v 1.0")
canvas = Canvas(root, height=Height, width=Width)
canvas.pack()
frame = Frame(root, bg='#80c0ff')
frame.place(relwidth=1, relheight=1)
inputText = Text(frame, bg='white')
inputText.place(rely=0.2, relwidth=0.49, relheight=0.8)
outputText = Text(frame, bg='white')
outputText.place(relx=0.51, rely=0.2, relwidth=0.49, relheight=0.8)
# Settings
###############################################################################
replacement_mode = BooleanVar(value=False)
use_clipboard = BooleanVar(value=True)
remove_non_ASCII = BooleanVar(value=True)
###############################################################################
# Build settings menu
##############################################################################
menu = Menu(root)
settings_menu = Menu(menu)
settings_menu.add_checkbutton(label="Copy Output to Clipboard", variable=use_clipboard)
settings_menu.add_checkbutton(label="Remove non-ASCII characters", variable=remove_non_ASCII)
settings_menu.add_checkbutton(label="No Wrapping <p> tags", variable=replacement_mode)
menu.add_cascade(label="Settings", menu=settings_menu)
root.config(menu=menu)
##############################################################################
reformatButton = Button(root, text="Reformat Text",
command=lambda: reformat(inputText.get("1.0", "end-1c")))
reformatButton.place(relx=0.75, rely=0, relwidth=0.25, relheight=0.2)
resetButton = Button(root, text="Clear Input", command=lambda: clear_input())
resetButton.place(relx=0, rely=0, relwidth=0.25, relheight=0.2)
def main():
# reformat(input(), third_party = False, companion = False, eidolon = False, ancestry = False,
# use_clipboard=True, add_gm_text = False, inline_rolls = True, add_conditions = True,
# add_inline_checks = True, add_inline_templates = True, remove_non_ASCII = True)
root.mainloop()
if __name__ == "__main__":
main()