Skip to content

Instantly share code, notes, and snippets.

@mvz
Created April 17, 2012 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mvz/2404415 to your computer and use it in GitHub Desktop.
Save mvz/2404415 to your computer and use it in GitHub Desktop.
Dead-simple Markdown Viewer
#!/usr/bin/env ruby
require 'ffi-gtk3'
require 'github/markup'
GirFFI.setup :WebKit, '3.0'
Gtk.init
WebKit.set_cache_model :document_viewer
class MarkDownViewer
attr_reader :file
def initialize file
@file = file
setup_gui
reload
connect_signals
@win.show_all
end
def connect_signals
@win.signal_connect 'key-press-event' do |wdg, evt, ud|
if evt.state == :control_mask
case evt.keyval
when "q".ord
@win.destroy
when "r".ord
self.reload
end
end
false
end
@win.signal_connect("destroy") { Gtk.main_quit }
end
def setup_gui
@win = Gtk::Window.new :toplevel
@win.set_default_geometry 700, 500
@scr = Gtk::ScrolledWindow.new nil, nil
@wv = WebKit::WebView.new
@win.add @scr
@scr.add @wv
end
def fullpath
@fullpath ||= File.expand_path(file, Dir.pwd)
end
def base_uri
@base_uri ||= "file://#{fullpath}"
end
def html
GitHub::Markup.render(fullpath)
end
def reload
@wv.load_string html, nil, nil, base_uri
end
end
file = ARGV[0]
raise "Need file" unless file
MarkDownViewer.new file
Gtk.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment