以下は『歪界詠符』のソースコード全文です:
import tkinter as tk
from tkinter import scrolledtext
import html
class HTMLEscapeApp:
def __init__(self, root):
self.root = root
self.root.title("HTMLエスケープ変換 - 歪界詠符")
self.root.geometry("1000x500") # 初期サイズ
# ウィンドウリサイズに応じて拡大
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_columnconfigure(2, weight=1)
# 左側:入力欄
self.left_text = scrolledtext.ScrolledText(root, wrap=tk.WORD)
self.left_text.grid(row=0, column=0, padx=10, pady=10, sticky="nsew")
# 変換ボタン
self.convert_button = tk.Button(root, text="変換 ▶", command=self.convert_text)
self.convert_button.grid(row=0, column=1, padx=5, pady=10)
# 右側:出力欄
self.right_text = scrolledtext.ScrolledText(root, wrap=tk.WORD)
self.right_text.grid(row=0, column=2, padx=10, pady=10, sticky="nsew")
# ハイライトスタイル
self.right_text.tag_config("highlight", foreground="white", background="black")
def convert_text(self):
raw_text = self.left_text.get("1.0", tk.END)
escaped_text = html.escape(raw_text)
self.right_text.delete("1.0", tk.END)
self.right_text.insert("1.0", escaped_text)
# 変換された部分をハイライト
mapping = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
}
for key, val in mapping.items():
start = "1.0"
while True:
start = self.right_text.search(val, start, stopindex=tk.END)
if not start:
break
end = f"{start}+{len(val)}c"
self.right_text.tag_add("highlight", start, end)
start = end
if __name__ == "__main__":
root = tk.Tk()
app = HTMLEscapeApp(root)
root.mainloop()