-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiorg.py
executable file
·369 lines (304 loc) · 12.9 KB
/
wikiorg.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python
import os
import re
import gtk
import gtk.glade
from gtk import gdk
import gobject
import pango
from htmlviewer import HtmlViewer
class LinkHistory:
"""
Stores information about visited pages (for back/forward buttons).
Note that many functions only work after at least one page has been added (with pushPageName).
"""
def __init__ (self, gui):
"""
"gui" is the WikiOrgGui object that displays the history buttons.
"""
self.gui = gui
self.visited = [] # list of pages (contains back, forward, and current pages)
self.currentPageIndex = None # index of current page (in self.visited)
def pushPageName (self, page):
""" Add given page to top of history """
if self.currentPageIndex != None and page == self.getCurrentPage():
print "(history: page '%s' is current page - not adding)" % page
return
# remove all "forward" pages:
if self.currentPageIndex != None:
self.visited = self.visited[:self.currentPageIndex + 1]
self.visited.append(page)
self.currentPageIndex = len(self.visited) - 1
print "(added page '%s')" % page
self.notifyGui()
def notifyGui (self):
""" Inform GUI object that it has to update its history display. """
self.gui.setHistoryButtonState(self.canGoBack(), self.canGoForward())
def canGoBack (self, count = 1):
""" Returns true if history can go back 'count' pages. """
assert(count > 0)
return self.currentPageIndex > (count - 1)
def canGoForward (self, count = 1):
""" Returns true if history can go forward 'count' pages. """
return self.currentPageIndex < (len(self.visited) - count)
def getCurrentPage (self):
""" Returns name of current page. """
assert(self.currentPageIndex != None)
return self.visited[self.currentPageIndex]
def getBackPages (self):
""" Returns a list of 'past' pages (order: newest first). """
if self.currentPageIndex == None:
return []
else:
pages = self.visited[:self.currentPageIndex]
pages.reverse()
return pages
def getForwardPages (self):
""" Returns a list of 'future' pages (order: oldest first). """
if self.currentPageIndex == None:
return []
else:
return self.visited[self.currentPageIndex + 1:]
def goBack (self, count = 1):
""" Go back 'count' pages. """
if self.canGoBack(count):
self.currentPageIndex = self.currentPageIndex - count
self.notifyGui()
self.gui.displayMarkdown( self.getCurrentPage() )
else:
print "warning: tried to go back in history but there are not enough older pages"
def goForward (self, count = 1):
""" Go forward 'count' pages. """
if self.canGoForward(count):
self.currentPageIndex = self.currentPageIndex + count
self.notifyGui()
self.gui.displayMarkdown( self.getCurrentPage() )
else:
print "warning: tried to go forward in history but there are not enough newer pages"
class FileStorageBackend:
""" Handles the actual storage of wiki pages to files. """
def __init__ (self):
self.directory = "." # the directory where files are saved
def pageExists (self, pagename):
filename = self.pageToFile(pagename)
# TODO: check for directory, and regular file, and whatnot...
return os.path.exists(filename)
def createNewPage (self, pagename):
filename = self.pageToFile(pagename)
f = open(filename, "w")
f.write("enter text for %s here..." % pagename)
f.close()
def getPageContent (self, pagename):
filename = self.pageToFile(pagename)
f = open(filename, "r")
text = f.read()
f.close()
return text
def savePageContent (self, pagename, text):
filename = self.pageToFile(pagename)
f = open(filename, "w")
f.write(text)
f.close()
def pageToFile (self, wikiword):
""" Translates a page name (wiki word) into a file name. """
return "%s/%s.markdown" % (self.directory, wikiword)
class WikiOrgGui:
""" Main class for this application (holds the GUI etc.) """
def __init__ (self):
self.currentPage = None
self.editMode = False
self.textChanged = False
self.gladeFile = "wikiorg.glade"
self.tree = gtk.glade.XML(self.gladeFile)
self.tree.signal_autoconnect(self)
# set up storage backend:
self.storage = FileStorageBackend()
# set up link history:
self.linkHistory = LinkHistory(self)
# set up menus for back/forward buttons:
btnGoBack = self.tree.get_widget("btnGoBack")
self.backMenu = gtk.Menu()
btnGoBack.set_menu(self.backMenu)
btnGoForward = self.tree.get_widget("btnGoForward")
self.forwardMenu = gtk.Menu()
btnGoForward.set_menu(self.forwardMenu)
# set up HTML viewer widget:
self.viewer = HtmlViewer()
self.viewer.setLinkHandler(self.linkHandler)
self.viewer.getWidget().show()
# remove the TextView from ScrolledWindow but keep it for later:
viewerParent = self.tree.get_widget('mainScrollWin')
self.textView = viewerParent.get_children()[0]
viewerParent.remove(self.textView)
# instead, add the HTML viewer widget:
viewerParent.add(self.viewer.getWidget())
# set up text view:
fontName = "Monospace"
pangoFont = pango.FontDescription(fontName)
self.textView.modify_font(pangoFont)
# add TextBuffer for text view:
self.textBuffer = gtk.TextBuffer(None)
self.textView.set_buffer(self.textBuffer)
# display start page:
self.displayMarkdown('index')
def on_mainWindow_delete_event (self, widget, dummy):
gtk.main_quit()
def on_miQuit_activate (self, dummy):
gtk.main_quit()
def on_btnEdit_clicked (self, widget):
if self.editMode:
if self.textChanged:
print "(unsaved changes left)"
parentWin = self.tree.get_widget("mainWindow")
saveDialog = gtk.MessageDialog(parentWin, gtk.DIALOG_MODAL,
gtk.MESSAGE_WARNING, gtk.BUTTONS_YES_NO,
"There are unsaved changes.")
saveDialog.format_secondary_text("Do you want to save?")
saveDialog.set_default_response(gtk.RESPONSE_YES)
result = saveDialog.run()
saveDialog.destroy()
if result == gtk.RESPONSE_YES:
print "(do save)"
self.on_btnSave_clicked(None)
else:
print "(don't save)"
print "(back to view)"
parent = self.tree.get_widget('mainScrollWin')
child = parent.get_children()[0]
assert(child == self.textView)
parent.remove(child)
self.textBuffer.disconnect(self.changeHandlerId)
self.changeHandlerId = None
self.tree.get_widget('btnSave').set_property('sensitive', False)
self.tree.get_widget('miSave').set_property('sensitive', False)
self.displayMarkdown(self.currentPage)
parent.add(self.viewer.getWidget())
self.tree.get_widget('btnEdit').set_property('stock_id', 'gtk-edit')
self.editMode = False
else:
print "(edit)"
parent = self.tree.get_widget('mainScrollWin')
child = parent.get_children()[0]
assert(child == self.viewer.getWidget())
parent.remove(child)
# load page content:
text = self.storage.getPageContent(self.currentPage)
self.textBuffer.set_text(text)
self.textChanged = False
self.changeHandlerId = self.textBuffer.connect('changed', self.on_textBuffer_changed)
parent.add(self.textView)
self.tree.get_widget('btnEdit').set_property('stock_id', 'gtk-ok')
self.editMode = True
def on_btnSave_clicked (self, widget):
print "(save)"
text = self.textBuffer.get_text(*self.textBuffer.get_bounds())
self.storage.savePageContent(self.currentPage, text)
self.textChanged = False
self.tree.get_widget('btnSave').set_property('sensitive', False)
self.tree.get_widget('miSave').set_property('sensitive', False)
def on_miSave_activate (self, dummy):
print "(miSave)"
if self.editMode:
self.on_btnSave_clicked(dummy)
def on_btnHome_clicked (self, widget):
print "(home)"
if not(self.editMode): # TODO: what to do if we're in edit mode?
self.displayMarkdown('index')
def on_textBuffer_changed (self, textBuffer):
self.textChanged = True
self.tree.get_widget('btnSave').set_property('sensitive', True)
self.tree.get_widget('miSave').set_property('sensitive', True)
def on_btnGoBack_clicked (self, widget):
print "(go back)"
self.linkHistory.goBack()
def on_btnGoForward_clicked (self, widget):
print "(go forward)"
self.linkHistory.goForward()
def on_backMenuItem_activate (self, item, position):
print "(back menu item at pos. %d)" % position
self.linkHistory.goBack(position)
def on_forwardMenuItem_activate (self, item, position):
print "(forward menu item at pos. %d)" % position
self.linkHistory.goForward(position)
def setHistoryButtonState (self, backEnabled, forwardEnabled):
self.tree.get_widget('btnGoBack').set_property('sensitive', backEnabled)
self.tree.get_widget('btnGoForward').set_property('sensitive', forwardEnabled)
# delete all entries from back button menu:
for mi in self.backMenu.get_children():
self.backMenu.remove(mi)
# add current entries to back button menu:
itemPos = 1
for page in self.linkHistory.getBackPages():
mi = gtk.MenuItem(page)
mi.connect('activate', self.on_backMenuItem_activate, itemPos)
mi.show()
self.backMenu.append(mi)
itemPos = itemPos + 1
# delete all entries from forward button menu:
for mi in self.forwardMenu.get_children():
self.forwardMenu.remove(mi)
# add current entries to forward button menu:
itemPos = 1
for page in self.linkHistory.getForwardPages():
mi = gtk.MenuItem(page)
mi.connect('activate', self.on_forwardMenuItem_activate, itemPos)
mi.show()
self.forwardMenu.append(mi)
itemPos = itemPos + 1
def linkHandler (self, url):
"""Is called when a link is clicked in HTML view"""
print "link clicked (%s)" % url
if url.startswith('wiki://'):
wikiword = url[7:]
print "(internal link to '%s')" % wikiword
# create new page if necessary:
if not(self.storage.pageExists(wikiword)):
self.storage.createNewPage(wikiword)
# TODO: maybe switch to edit mode here?
self.displayMarkdown(wikiword)
else:
os.system("xdg-open '%s' &" % url)
def wikiwordToHtmlLink (self, wikiword):
""" Returns an HTML link tag for the given wikiword. """
if self.storage.pageExists(wikiword):
return "<a class='wikilink' href='wiki://%s'>%s</a>" % (wikiword, wikiword)
else:
return "<a class='wikilinkbroken' href='wiki://%s'>%s</a>" % (wikiword, wikiword)
# TODO: maybe use another syntax for wiki links.
# See http://boodler.org/wiki/show/Markdown/ for an idea
def convertWikiLinks (self, html):
""" Converts Wiki links in the given HTML text to HTML tags """
newHtml = re.sub("\[\[([\w ]+)\]\]",
lambda x: self.wikiwordToHtmlLink(x.groups()[0]), html)
return newHtml
def displayMarkdown (self, wikiword):
""" Convert the Markdown+WikiLink text to HTML, and display the HTML """
filename = self.storage.pageToFile(wikiword)
cmd = "perl Markdown.pl < '%s'" % filename
html = os.popen(cmd).read()
if (html):
html = self.convertWikiLinks(html)
html = """<html>
<head>
<title>%s</title>
<style type="text/css">
a { color:#6666FF; font-style: italic; }
a:after { content:" (ext)"; }
.wikilink { color:#0000FF; font-style: normal; }
.wikilink:after { content:""; }
.wikilinkbroken { color:#FF0000; font-style: normal; }
.wikilinkbroken:after { content:""; }
</style>
</head>
<body>
%s
</body>
</html>
""" % (wikiword, html)
self.viewer.setHTML(html)
self.currentPage = wikiword
self.tree.get_widget("lblStatusBar").set_label("<i>%s</i>" % wikiword)
self.linkHistory.pushPageName(wikiword)
if __name__ == "__main__":
gui = WikiOrgGui()
gtk.main()