class Showmd::PreviewGenerator

Attributes

input_path[RW]

Pass in a md file path, read the file, build an html file and open in the default browser

markdown[RW]

Pass in a md file path, read the file, build an html file and open in the default browser

output_path[RW]

Pass in a md file path, read the file, build an html file and open in the default browser

template_path[RW]

Pass in a md file path, read the file, build an html file and open in the default browser

Public Class Methods

new(file) click to toggle source
# File lib/showmd/preview_generator.rb, line 9
def initialize(file)
  @input_path = file
  @output_path = File.expand_path(File.join(File.dirname(__FILE__), "markdown_preview.html"))
  @template_path = File.expand_path(File.join(File.dirname(__FILE__), "template.html"))
end

Public Instance Methods

build_html_file() click to toggle source
# File lib/showmd/preview_generator.rb, line 29
def build_html_file
  template_text = File.read(template_path)
  html_string = template_text.gsub(/<% markdown %>/, markdown)
  html_string.gsub!(/<% page_title %>/, File.basename(input_path))

  File.open(output_path, "w") {|file| file.puts html_string}
end
generate() click to toggle source
# File lib/showmd/preview_generator.rb, line 15
def generate
  read_md_file
  build_html_file
  open_in_browser
end
open_in_browser() click to toggle source
# File lib/showmd/preview_generator.rb, line 37
def open_in_browser
  Launchy.open("file://localhost/#{output_path}")
end
read_md_file() click to toggle source
# File lib/showmd/preview_generator.rb, line 21
def read_md_file
  begin
    @markdown = File.read(input_path)
  rescue => exception
    raise InputError, "Sorry showmd was unable to read the file, please use a valid markdown file."
  end
end