Sha256: aed83a10e928e61424ee9af54b372e95414a9acf18048f92ec5de486c6864b7b

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

#!/usr/bin/env ruby
require 'gir_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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mdv-0.1.4 bin/mdv
mdv-0.1.3 bin/mdv
mdv-0.1.2 bin/mdv
mdv-0.1.1 bin/mdv
mdv-0.1.0 bin/mdv