Skip to content

Commit 77fede6

Browse files
authored
Merge pull request #911 from kaliappan01/markdown_editor
markdown editor added
2 parents 0fc7018 + dc75b8b commit 77fede6

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

markdown_editor/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Markdown Editor
2+
3+
The Markdown editor helps user to create, open, write, view and save markdown file.
4+
![implementation](md_editor.png)
5+
6+
# modules used
7+
8+
Tkinter
9+
markdown2
10+
tkhtmlview
11+
Setup and activate virtual environment :
12+
13+
## For Unix based systems
14+
please execute the following command to create venv and install requirements.
15+
16+
make init
17+
source .venv/bin/activate
18+
## For Windows based system
19+
20+
pip install -r /path/to/requirements.txt
21+
# How to Use
22+
python3 markdown_viewer.py
23+

markdown_editor/makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
VENV ?= .venv
2+
REQUIREMENTS_FILE ?= requirements.txt
3+
4+
init:
5+
python3 -m venv $(VENV)
6+
$(VENV)/bin/python -m pip install --upgrade pip
7+
if [ -f $(REQUIREMENTS_FILE) ]; \
8+
then $(VENV)/bin/python -m pip install -r $(REQUIREMENTS_FILE); \
9+
fi

markdown_editor/markdown_editor.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
import tkinter as tk
4+
from tkinter import Tk
5+
from tkinter import font, filedialog, messagebox as mbox
6+
7+
from click import FileError
8+
from markdown2 import Markdown
9+
from tkhtmlview import HTMLLabel
10+
11+
12+
class Window(tk.Frame):
13+
def __init__(self, master=None):
14+
tk.Frame.__init__(self, master)
15+
self.master = master
16+
self.font = font.Font(family="Helvetica", size=14)
17+
self.init_window()
18+
19+
def onChange(self, event):
20+
self.inputeditor.edit_modified(0)
21+
md2html = Markdown()
22+
self.outputbox.set_html(md2html.convert(
23+
self.inputeditor.get("1.0", tk.END)))
24+
25+
def openfile(self):
26+
filename = filedialog.askopenfilename(
27+
filetypes=[
28+
("Markdown File", "*.md *.mdown *markdown"),
29+
("Text file", "*.txt"),
30+
("All files", "*.*"),
31+
]
32+
)
33+
if filename:
34+
try:
35+
self.inputeditor.delete("1.0", tk.END)
36+
self.inputeditor.insert(tk.END, open(filename, "r").read())
37+
except FileError:
38+
mbox.showerror(
39+
"Error opening file", "{} \
40+
cannot be opened !".format(
41+
filename
42+
),
43+
)
44+
45+
def savefile(self):
46+
filedata = self.inputeditor.get("1.0", tk.END)
47+
filename = filedialog.asksaveasfilename(
48+
filetypes=(("Markdown file", "*.md"), ("Text file", "*.txt")),
49+
title="Save Markdown File",
50+
)
51+
if filename:
52+
try:
53+
f = open(filename, "w")
54+
f.write(filedata)
55+
except FileError:
56+
mbox.showerror("Error saving file", "Unable to save the file")
57+
58+
def init_window(self):
59+
self.master.title("Mardown Viewer")
60+
self.pack(fill=tk.BOTH, expand=1)
61+
62+
self.mainmenu = tk.Menu(self)
63+
self.filemenu = tk.Menu(self.mainmenu)
64+
self.filemenu.add_command(label="Open", command=self.openfile)
65+
self.filemenu.add_command(label="Save as", command=self.savefile)
66+
self.filemenu.add_separator()
67+
self.filemenu.add_command(label="Exit", command=self.quit)
68+
self.mainmenu.add_cascade(label="File", menu=self.filemenu)
69+
self.master.config(menu=self.mainmenu)
70+
71+
self.inputeditor = tk.Text(self, width="1", font=self.font)
72+
self.inputeditor.pack(fill=tk.BOTH, expand=1, side=tk.LEFT)
73+
self.inputeditor.bind("<<Modified>>", self.onChange)
74+
75+
self.outputbox = HTMLLabel(
76+
self, width="1", background="white",
77+
html="<h1>Markdown Editor</h1>"
78+
)
79+
self.outputbox.pack(fill=tk.BOTH, expand=1, side=tk.RIGHT)
80+
81+
82+
root = Tk()
83+
root.geometry("750x600")
84+
app = Window(root)
85+
app.mainloop()

markdown_editor/md_editor.png

27.6 KB
Loading

markdown_editor/requirements.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
certifi==2022.9.24
2+
charset-normalizer==2.1.1
3+
idna==3.4
4+
markdown2==2.4.5
5+
Pillow==9.2.0
6+
requests==2.28.1
7+
tkhtmlview==0.1.1.post5
8+
urllib3==1.26.12

0 commit comments

Comments
 (0)