Created
December 9, 2011 14:18
-
-
Save glejeune/1451698 to your computer and use it in GitHub Desktop.
A tiny Markdown Viewer with Ruby/Gtk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'tempfile' | |
require 'rubygems' | |
require 'gtkmozembed' | |
require 'markdown' | |
class MarkdownViewer < Gtk::Window | |
def initialize(file) | |
super | |
signal_connect "destroy" do | |
Gtk.main_quit | |
end | |
set_title "Markdown viewer [#{file}]" | |
resize(780, 570) | |
set_window_position Gtk::Window::POS_CENTER | |
self << Gtk::MozEmbed.new | |
child.chrome_mask = Gtk::MozEmbed::ALLCHROME | |
child.location = "file://#{get_html(file)}" | |
show_all | |
end | |
private | |
def get_html(file) | |
data = open(file).read | |
html = Markdown.new(data).to_html | |
tmp_html = Tempfile.open("mdv") | |
tmp_html.print <<EOF | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us"> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> | |
<style> | |
body { | |
margin: 5px; | |
padding: 5px; | |
color: #000; | |
background-color: #fff; | |
font-family: sans-serif; | |
line-height: 1.5em; | |
} | |
</style> | |
<body> | |
EOF | |
tmp_html.print html | |
tmp_html.print <<EOF | |
</body> | |
</head> | |
EOF | |
tmp_html.close | |
tmp_html.path | |
end | |
end | |
if ARGV[0].nil? | |
puts "Usage : #{$0} file.md" | |
else | |
Gtk.init | |
window = MarkdownViewer.new(ARGV[0]) | |
Gtk.main | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment