Skip to content

Instantly share code, notes, and snippets.

@flying-sheep
Created June 12, 2011 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flying-sheep/1021686 to your computer and use it in GitHub Desktop.
Save flying-sheep/1021686 to your computer and use it in GitHub Desktop.
script to open markdown files. made by jezra, tweaks by me (can now open files)
#!/usr/bin/env python
'''
Hey, this is GPLv3 and copyright 2010 Jezra
'''
import sys, os
import gtk
import webkit
import markdown #http://www.freewisdom.org/projects/python-markdown/
global default_file
if len(sys.argv) > 1:
default_file = sys.argv[1]
else:
default_file = "markdown.txt"
class application:
def __init__(self):
winder = gtk.Window(gtk.WINDOW_TOPLEVEL)
winder.connect("destroy", self.quit)
winder.maximize()
#ctrl+s, ctrl+q
accel = gtk.AccelGroup()
accel.connect_group(gtk.keysyms.q, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.quit_accel)
accel.connect_group(gtk.keysyms.s, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE, self.save_accel)
accel.lock()
winder.add_accel_group(accel)
pane = gtk.HPaned()
box = gtk.VBox(False)
box.pack_start(pane)
winder.add(box)
self.tb = gtk.TextBuffer()
tv = gtk.TextView(self.tb)
tv.set_wrap_mode(gtk.WRAP_WORD)
try: #add spell checking to the textview if possible
import gtkspell
self.spell = gtkspell.Spell(tv)
self.has_spell_library=True
except Exception:
self.has_spell_library=False
print Exception.message
#add the text view to a scrollable window
input_scroll = gtk.ScrolledWindow()
input_scroll.add_with_viewport(tv)
input_scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
#add to the pane
pane.pack1(input_scroll,True)
#make the HTML viewable area
self.wv = webkit.WebView()
#disable the plugins for the webview
ws = self.wv.get_settings()
ws.set_property("enable-plugins",False)
self.wv.set_settings(ws)
out_scroll = gtk.ScrolledWindow()
out_scroll.add(self.wv)
out_scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
pane.add2(out_scroll)
#we will add buttons at the bottom
bbox = gtk.HButtonBox()
bbox.set_layout(gtk.BUTTONBOX_START)
#we need buttons!
savebutton = gtk.Button("Save")
savebutton.connect("clicked", self.save)
markdownbutton = gtk.Button("_Markdown", use_underline=True)
markdownbutton.connect("clicked", self.markdown)
#add the buttons to the bbox
bbox.pack_start(savebutton, False, False, 0)
bbox.pack_start(markdownbutton, False, False, 0)
#add the bbox to the box
box.pack_start(bbox, False, False, 0)
winder.show_all()
self.read_default_file()
def run(self):
gtk.main()
def quit(self, widget=None):
self.save()
gtk.main_quit()
def markdown(self, widget=None):
text = self.get_buffer_text()
mdtext = markdown.markdown(text)
self.wv.load_html_string(mdtext, "file:///")
def read_default_file(self):
if os.path.isfile(default_file):
try:
f = open(default_file, "r")
text = f.read()
self.tb.set_text(text)
f.close()
self.markdown()
except:
pass
def quit_accel(self, accel_group, acceleratable, keyval, modifier):
self.quit()
def save_accel(self, accel_group, acceleratable, keyval, modifier):
self.save()
def save(self, widget=None):
text = self.get_buffer_text()
f = open(default_file, "w")
f.write(text)
f.close()
def get_buffer_text(self):
start_iter = self.tb.get_start_iter()
end_iter = self.tb.get_end_iter()
text=self.tb.get_text(start_iter,end_iter)
return text
if __name__=="__main__":
a = application()
a.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment