Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Bulk Create toml menus python

Bulk Create some toml menus

You likely won’t need this. I ended up not using it, but documenting the script because it might be useful to modify later. It takes a list of categories on the left column, and then formats them to the toml with metadata that I wanted on the right side, so I could cut and paste. The theme I was trying to use was wowchemy and it wasted several hours of my life.

import tkinter as tk
from tkinter import scrolledtext
import xml.etree.ElementTree as ET
import pyperclip
from urllib.parse import urlparse

def title_case(text):
    return ' '.join(word.title() for word in text.split('-'))

def extract_and_hugofy():
    xml_content = input_text.get("1.0", "end").strip()
    root = ET.fromstring(xml_content)
    hugo_formatted = ""

    for url in root.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}url'):
        loc = url.find('{http://www.sitemaps.org/schemas/sitemap/0.9}loc').text
        parsed_url = urlparse(loc)
        path = parsed_url.path
        link_text = title_case(path.strip('/').replace('-', ' '))
        # added the | pipe so markdown would render.  Remove it.
        hugo_formatted += f'- [{link_text}]({|{< relref "{path}" >}})\n'

    output_text.delete("1.0", "end")
    output_text.insert("1.0", hugo_formatted)

def copy_to_clipboard():
    pyperclip.copy(output_text.get("1.0", "end").strip())

def clear_texts():
    input_text.delete("1.0", "end")
    output_text.delete("1.0", "end")

# Create the main window
root = tk.Tk()
root.title("XML to Hugo Format Converter")
root.geometry("1200x400")

# Create a Grid layout
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=2)  # 20% for input
root.grid_columnconfigure(1, weight=8)  # 80% for output

# Create a Text widget for input
input_text = scrolledtext.ScrolledText(root, height=15, width=30)
input_text.grid(row=0, column=0, sticky='nsew', padx=5, pady=5)

# Create a Text widget for output
output_text = scrolledtext.ScrolledText(root, height=15, width=120)
output_text.grid(row=0, column=1, sticky='nsew', padx=5, pady=5)

# Create a Frame for buttons
button_frame = tk.Frame(root)
button_frame.grid(row=1, column=0, columnspan=2, pady=5)

# Create buttons
convert_button = tk.Button(button_frame, text="Convert", command=extract_and_hugofy)
convert_button.pack(side=tk.LEFT, padx=5)

clip_button = tk.Button(button_frame, text="Clip", command=copy_to_clipboard)
clip_button.pack(side=tk.LEFT, padx=5)

clear_button = tk.Button(button_frame, text="Clear", command=clear_texts)
clear_button.pack(side=tk.LEFT, padx=5)

# Start the GUI event loop
root.mainloop()