Attachment 'markdownpreview.py'
Download 1 # markdownpreview.py - HTML preview of Markdown formatted text in gedit
2 #
3 # Copyright (C) 2005 - Michele Campeotto
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 import gedit
20
21 import sys
22 import gtk
23 import gtkhtml2
24 import markdown
25
26 HTML_TEMPLATE = """<html><head><style type="text/css">
27 body { background-color: #fff; padding: 8px; }
28 p, div { margin: 0em; }
29 p + p, p + div, div + p, div + div { margin-top: 0.8em; }
30 blockquote { padding-left: 12px; padding-right: 12px; }
31 pre { padding: 12px; }
32 </style></head><body>%s</body></html>"""
33
34 class MarkdownPreviewPlugin(gedit.Plugin):
35
36 def __init__(self):
37 gedit.Plugin.__init__(self)
38
39 def activate(self, window):
40 action = ("Markdown Preview",
41 None,
42 "Markdown Preview",
43 "<Control><Shift>M",
44 "Update the HTML preview",
45 lambda x, y: self.update_preview(y))
46
47 # Store data in the window object
48 windowdata = dict()
49 window.set_data("MarkdownPreviewData", windowdata)
50
51 scrolled_window = gtk.ScrolledWindow()
52 scrolled_window.set_property("hscrollbar-policy",gtk.POLICY_AUTOMATIC)
53 scrolled_window.set_property("vscrollbar-policy",gtk.POLICY_AUTOMATIC)
54 scrolled_window.set_property("shadow-type",gtk.SHADOW_IN)
55
56 html_view = gtkhtml2.View()
57 html_doc = gtkhtml2.Document()
58 html_view.set_document(html_doc)
59
60 html_doc.clear()
61 html_doc.open_stream("text/html")
62 html_doc.write_stream(HTML_TEMPLATE % ("",))
63 html_doc.close_stream()
64
65 scrolled_window.set_hadjustment(html_view.get_hadjustment())
66 scrolled_window.set_vadjustment(html_view.get_vadjustment())
67 scrolled_window.add(html_view)
68 scrolled_window.show_all()
69
70 bottom = window.get_bottom_panel()
71 image = gtk.Image()
72 image.set_from_icon_name("gnome-mime-text-html", gtk.ICON_SIZE_MENU)
73 bottom.add_item(scrolled_window, "Markdown Preview", image)
74 windowdata["bottom_panel"] = scrolled_window
75 windowdata["html_doc"] = html_doc
76
77 windowdata["action_group"] = gtk.ActionGroup("MarkdownPreviewActions")
78 windowdata["action_group"].add_actions ([action], window)
79
80 manager = window.get_ui_manager()
81 manager.insert_action_group(windowdata["action_group"], -1)
82
83 windowdata["ui_id"] = manager.new_merge_id ()
84
85 manager.add_ui (windowdata["ui_id"],
86 "/MenuBar/ToolsMenu/ToolsOps_5",
87 "Markdown Preview",
88 "Markdown Preview",
89 gtk.UI_MANAGER_MENUITEM,
90 True)
91
92 def deactivate(self, window):
93 # Retreive the data of the window object
94 windowdata = window.get_data("MarkdownPreviewData")
95
96 # Remove the menu action
97 manager = window.get_ui_manager()
98 manager.remove_action_ui(windowdata["ui_id"])
99 manager.remove_action_group(windowdata["action_group"])
100
101 # Remove the bottom panel
102 bottom = window.get_bottom_panel()
103 bottom.remove_item(windowdata["bottom_panel"])
104
105 def update_preview(self, window):
106 # Retreive the data of the window object
107 windowdata = window.get_data("MarkdownPreviewData")
108
109 view = window.get_active_view()
110 if not view:
111 return
112
113 doc = view.get_buffer()
114
115 start = doc.get_start_iter()
116 end = doc.get_end_iter()
117
118 if doc.get_selection_bounds():
119 start = doc.get_iter_at_mark(doc.get_insert())
120 end = doc.get_iter_at_mark(doc.get_selection_bound())
121
122 text = doc.get_text(start, end)
123 html = HTML_TEMPLATE % (markdown.markdown(text),)
124
125 p = windowdata["bottom_panel"].get_placement()
126
127 html_doc = windowdata["html_doc"]
128 html_doc.clear()
129 html_doc.open_stream("text/html")
130 html_doc.write_stream(html)
131 html_doc.close_stream()
132
133 windowdata["bottom_panel"].set_placement(p)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.