93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
import os
|
|
import subprocess
|
|
import json
|
|
import ttkbootstrap as tb
|
|
from ttkbootstrap.constants import *
|
|
from tkinter import filedialog, messagebox, StringVar
|
|
from ..core.core import convert_markdown
|
|
|
|
CONFIG_FILE = os.path.join(os.path.expanduser("~"), ".markdown_converter_config.json")
|
|
|
|
def save_config(font_name):
|
|
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
|
|
json.dump({"font": font_name}, f)
|
|
|
|
def load_config():
|
|
if os.path.exists(CONFIG_FILE):
|
|
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
return data.get("font", "")
|
|
return ""
|
|
|
|
def open_with_default_app(filepath):
|
|
try:
|
|
os.startfile(filepath) # solo su Windows
|
|
except Exception as e:
|
|
messagebox.showerror("Errore", f"Impossibile aprire il file:\n{str(e)}")
|
|
|
|
def open_output_folder(filepath):
|
|
try:
|
|
folder = os.path.dirname(filepath)
|
|
os.startfile(folder)
|
|
except Exception as e:
|
|
messagebox.showerror("Errore", f"Impossibile aprire la cartella:\n{str(e)}")
|
|
|
|
def run_app():
|
|
app = tb.Window(themename="sandstone")
|
|
app.title("Markdown Converter")
|
|
app.geometry("680x240")
|
|
app.resizable(False, False)
|
|
|
|
selected_file = StringVar()
|
|
selected_font = StringVar(value=load_config())
|
|
selected_template = StringVar()
|
|
output_path = StringVar()
|
|
|
|
def browse_markdown():
|
|
path = filedialog.askopenfilename(filetypes=[("Markdown files", "*.md")])
|
|
if path:
|
|
selected_file.set(path)
|
|
|
|
def browse_template():
|
|
path = filedialog.askopenfilename(filetypes=[("DOCX files", "*.docx")])
|
|
if path:
|
|
selected_template.set(path)
|
|
|
|
def convert(fmt):
|
|
file_path = selected_file.get()
|
|
font = selected_font.get()
|
|
template = selected_template.get()
|
|
|
|
if not file_path:
|
|
messagebox.showerror("Errore", "Seleziona un file Markdown.")
|
|
return
|
|
|
|
try:
|
|
output = convert_markdown(file_path, fmt, font, template)
|
|
output_path.set(output)
|
|
save_config(font)
|
|
messagebox.showinfo("Successo", f"File convertito:\n{output}")
|
|
except Exception as e:
|
|
messagebox.showerror("Errore", str(e))
|
|
|
|
# Layout
|
|
tb.Label(app, text="File Markdown:").grid(row=0, column=0, padx=10, pady=10, sticky="w")
|
|
tb.Entry(app, textvariable=selected_file, width=60).grid(row=0, column=1, padx=5, sticky="w")
|
|
tb.Button(app, text="Sfoglia", command=browse_markdown, bootstyle=PRIMARY).grid(row=0, column=2, padx=5)
|
|
|
|
tb.Label(app, text="Font:").grid(row=1, column=0, padx=10, pady=10, sticky="w")
|
|
fonts = ["Arial", "Times New Roman", "Verdana", "Calibri", "Courier New"]
|
|
tb.Combobox(app, values=fonts, textvariable=selected_font, width=30, bootstyle=INFO).grid(row=1, column=1, sticky="w", padx=5)
|
|
|
|
tb.Label(app, text="Template DOCX (opzionale):").grid(row=2, column=0, padx=10, pady=10, sticky="w")
|
|
tb.Entry(app, textvariable=selected_template, width=60).grid(row=2, column=1, padx=5)
|
|
tb.Button(app, text="Sfoglia", command=browse_template, bootstyle=SECONDARY).grid(row=2, column=2, padx=5)
|
|
|
|
tb.Button(app, text="Converti in PDF", command=lambda: convert("PDF"), bootstyle=SUCCESS).grid(row=3, column=0, pady=20)
|
|
tb.Button(app, text="Converti in DOCX", command=lambda: convert("DOCX"), bootstyle=SUCCESS).grid(row=3, column=1, sticky="w", pady=20)
|
|
|
|
tb.Button(app, text="Apri file", command=lambda: open_with_default_app(output_path.get()), bootstyle=WARNING).grid(row=4, column=0, padx=10, pady=10)
|
|
tb.Button(app, text="Apri cartella", command=lambda: open_output_folder(output_path.get()), bootstyle=WARNING).grid(row=4, column=1, sticky="w", padx=5)
|
|
|
|
app.mainloop()
|