-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeanypy-restructured-preview.py
More file actions
72 lines (62 loc) · 2.95 KB
/
geanypy-restructured-preview.py
File metadata and controls
72 lines (62 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
try:
from gi import pygtkcompat
except ImportError:
pygtkcompat = None
if pygtkcompat is not None:
pygtkcompat.enable()
pygtkcompat.enable_gtk(version='3.0')
import urlparse
from gettext import gettext as _
import gtk as Gtk
import geany
from reST import *
class ReStructuredTextPlugin(geany.Plugin):
__plugin_name__ = _("reStructuredText Preview")
__plugin_version__ = _("0.1")
__plugin_description__ = _("reStructured Text Preview Panel in message window.")
__plugin_author__ = _("Sagar Chalise <chalisesagar@gmail.com>")
file_types = ('reStructuredText',)
def __init__(self):
signals = ('document-reload', 'document-save', 'document-activate', 'document-close')
self.rest_win = RestructuredtextHtmlPanel()
self.page_num = geany.main_widgets.message_window_notebook.append_page(self.rest_win, Gtk.Label(self.name))
self.rest_win.show_all()
for signal in signals:
geany.signals.connect(signal, self.on_document_notify)
geany.signals.connect("editor-notify", self.on_editor_notify)
def cleanup(self):
##still not graceful cleanup
geany.main_widgets.message_window_notebook.remove_page(self.page_num)
self.rest_win.clean_destroy()
def check_selection_or_filetype(self, doc):
sci = doc.editor.scintilla
if doc.file_type.name in self.file_types:
content = sci.get_contents()
uri = urlparse.urljoin('file:', doc.file_name)
return (content.strip(), uri)
return ('Current document is not a reStructuredText Document', '')
def update_window(self, text, uri, doc):
msgwin = geany.msgwindow
msgwin.clear_tab(msgwin.TAB_MESSAGE)
doc.editor.indicator_clear(geany.editor.INDICATOR_ERROR)
if doc.file_type.name in self.file_types:
errors = check_for_errors(text, uri)
if errors:
msgwin.switch_tab(msgwin.TAB_MESSAGE, True)
for error in errors:
doc.editor.indicator_set_on_line(geany.editor.INDICATOR_ERROR, error.line-1)
err_msg = '{}:{}:{}'.format(error.type, error.line, error.message)
msgwin.msg_add(err_msg, msgwin.COLOR_RED, error.line, doc)
else:
geany.main_widgets.message_window_notebook.set_current_page(self.page_num)
self.rest_win.update_view(text, uri)
def on_document_notify(self, user_data, doc):
text, uri = self.check_selection_or_filetype(doc)
self.update_window(text, uri, doc)
def on_editor_notify(self, g_obj, editor, nt):
check = (nt.nmhdr.code == geany.scintilla.MODIFIED and nt.length > 0) \
and ((nt.modification_type & geany.scintilla.MOD_INSERT_TEXT) \
or (nt.modification_type & geany.scintilla.MOD_DELETE_TEXT))
if check:
text, uri = self.check_selection_or_filetype(editor.document)
self.rest_win.update_view(text, uri)