Class: MarkD

Inherits:
Object
  • Object
show all
Defined in:
lib/markd/markd.rb

Constant Summary collapse

ENGINES =
{
  /md/      => MarkdownEngine,
  /wiki/    => WikiEngine,
  /textile/ => TextileEngine,
}

Instance Method Summary collapse

Instance Method Details

#create_engine(extname) ⇒ Object



14
15
16
17
18
19
# File 'lib/markd/markd.rb', line 14

def create_engine(extname)
  ENGINES.each do |regex, engine|
    return engine.new if regex =~ extname
  end
  ENGINES[nil].new
end

#publish(filename, out_dir_path, out_filename) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/markd/markd.rb', line 21

def publish(filename, out_dir_path, out_filename)
  ext = File.extname(filename)
  engine = create_engine ext
  src = File.read filename
  
  # parse
  @html = engine.to_html(src)
  
  doc = Nokogiri::HTML::Document.parse @html
  @title = doc.css("h1:first").text
  
  # render
  erb_src = File.read "#{APP_ROOT}/template/template.html.erb"
  eruby = Erubis::Eruby.new(erb_src)
  html = eruby.result(binding)
  
  # output
  FileUtils.mkdir_p out_dir_path
  File.open("#{out_dir_path}/#{out_filename}", "w") { |f| f.puts html }
  dirs = ::RESOURCES.map { |d| "#{APP_ROOT}/template/#{d}"}
  FileUtils.cp_r(dirs, out_dir_path)
end