|
| 1 | +import markdownify |
| 2 | +import os |
| 3 | +from tkinter import Tk, END, Frame, SUNKEN, Label |
| 4 | +from tkinter import font, Button, X, Entry, Text, BOTH |
| 5 | +from PIL import ImageTk, Image |
| 6 | + |
| 7 | +cwd = os.path.dirname(os.path.realpath(__file__)) |
| 8 | + |
| 9 | + |
| 10 | +class AlHtmlToMarkdown(): |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + root = Tk(className=" ALHTMLTOMARKDOWN ") |
| 14 | + root.geometry("400x175+1500+840") |
| 15 | + root.resizable(0, 0) |
| 16 | + root.iconbitmap(os.path.join(cwd + '\\UI\\icons', |
| 17 | + 'alhtmltomarkdown.ico')) |
| 18 | + root.config(bg="#6a199b") |
| 19 | + root.overrideredirect(1) |
| 20 | + color = '#6a199b' |
| 21 | + |
| 22 | + def callback(event): |
| 23 | + root.geometry("400x175+1500+840") |
| 24 | + |
| 25 | + def showScreen(event): |
| 26 | + root.deiconify() |
| 27 | + root.overrideredirect(1) |
| 28 | + |
| 29 | + def screenAppear(event): |
| 30 | + root.overrideredirect(1) |
| 31 | + |
| 32 | + def hideScreen(): |
| 33 | + root.overrideredirect(0) |
| 34 | + root.iconify() |
| 35 | + |
| 36 | + def markdown(): |
| 37 | + filename = fileText.get() |
| 38 | + filepath = os.path.join(cwd + '\\AlHtmlToMarkdown', filename) |
| 39 | + if os.path.exists(filepath): |
| 40 | + extension = os.path.splitext(filepath)[1] |
| 41 | + try: |
| 42 | + if extension.lower() == ".html": |
| 43 | + htmlFile = open(filepath, "r") |
| 44 | + html = htmlFile.read() |
| 45 | + htmlFile.close() |
| 46 | + markDown = markdownify.markdownify(html, |
| 47 | + heading_style="ATX") |
| 48 | + markdownFileName = filename.replace(extension, '.md') |
| 49 | + markdownFilePath = os.path.join(cwd + '\\AlHtmlToMarkd' |
| 50 | + 'own\\Markdown', |
| 51 | + markdownFileName) |
| 52 | + markdownFile = open(markdownFilePath, "w") |
| 53 | + markdownFile.writelines(markDown) |
| 54 | + markdownFile.close() |
| 55 | + text.delete(1.0, END) |
| 56 | + text.insert(1.0, markdownFileName + ' has been saved ' |
| 57 | + 'successfully in Markdown folder') |
| 58 | + except Exception as e: |
| 59 | + text.delete(1.0, END) |
| 60 | + print(str(e)) |
| 61 | + text.insert(1.0, 'Invalid document, please provide .html ' |
| 62 | + 'extension files') |
| 63 | + else: |
| 64 | + text.delete(1.0, END) |
| 65 | + text.insert(1.0, 'Invalid file path') |
| 66 | + |
| 67 | + textHighlightFont = font.Font(family='OnePlus Sans Display', size=12) |
| 68 | + appHighlightFont = font.Font(family='OnePlus Sans Display', size=12, |
| 69 | + weight='bold') |
| 70 | + |
| 71 | + titleBar = Frame(root, bg='#141414', relief=SUNKEN, bd=0) |
| 72 | + icon = Image.open(os.path.join(cwd + '\\UI\\icons', |
| 73 | + 'alhtmltomarkdown.ico')) |
| 74 | + icon = icon.resize((30, 30), Image.ANTIALIAS) |
| 75 | + icon = ImageTk.PhotoImage(icon) |
| 76 | + iconLabel = Label(titleBar, image=icon) |
| 77 | + iconLabel.photo = icon |
| 78 | + iconLabel.config(bg='#141414') |
| 79 | + iconLabel.grid(row=0, column=0, sticky="nsew") |
| 80 | + titleLabel = Label(titleBar, text='ALHTMLTOMARKDOWN', fg='#909090', |
| 81 | + bg='#141414', font=appHighlightFont) |
| 82 | + titleLabel.grid(row=0, column=1, sticky="nsew") |
| 83 | + closeButton = Button(titleBar, text="x", bg='#141414', fg="#909090", |
| 84 | + borderwidth=0, command=root.destroy, |
| 85 | + font=appHighlightFont) |
| 86 | + closeButton.grid(row=0, column=3, sticky="nsew") |
| 87 | + minimizeButton = Button(titleBar, text="-", bg='#141414', fg="#909090", |
| 88 | + borderwidth=0, command=hideScreen, |
| 89 | + font=appHighlightFont) |
| 90 | + minimizeButton.grid(row=0, column=2, sticky="nsew") |
| 91 | + titleBar.grid_columnconfigure(0, weight=1) |
| 92 | + titleBar.grid_columnconfigure(1, weight=30) |
| 93 | + titleBar.grid_columnconfigure(2, weight=1) |
| 94 | + titleBar.grid_columnconfigure(3, weight=1) |
| 95 | + titleBar.pack(fill=X) |
| 96 | + |
| 97 | + fileText = Label(root, text="HTML FILE") |
| 98 | + fileText.pack() |
| 99 | + fileText.config(bg=color, fg="white", font=appHighlightFont) |
| 100 | + fileText = Entry(root, bg="white", fg='#7a1da3', |
| 101 | + highlightbackground=color, highlightcolor=color, |
| 102 | + highlightthickness=3, bd=0, font=textHighlightFont) |
| 103 | + fileText.pack(fill=X) |
| 104 | + |
| 105 | + convertToMarkdown = Button(root, borderwidth=0, highlightthickness=3, |
| 106 | + text="MARKDOWN", command=markdown) |
| 107 | + convertToMarkdown.config(bg=color, fg="white", font=appHighlightFont) |
| 108 | + convertToMarkdown.pack(fill=X) |
| 109 | + |
| 110 | + text = Text(root, font="sans-serif", relief=SUNKEN, |
| 111 | + highlightbackground=color, highlightcolor=color, |
| 112 | + highlightthickness=5, bd=0) |
| 113 | + text.config(bg="white", fg='#7a1da3', height=2, font=textHighlightFont) |
| 114 | + text.pack(fill=BOTH, expand=True) |
| 115 | + |
| 116 | + titleBar.bind("<B1-Motion>", callback) |
| 117 | + titleBar.bind("<Button-3>", showScreen) |
| 118 | + titleBar.bind("<Map>", screenAppear) |
| 119 | + |
| 120 | + root.mainloop() |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + AlHtmlToMarkdown() |
0 commit comments