Class: Graphdown::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



12
13
14
15
# File 'lib/graphdown/cli.rb', line 12

def initialize
  @options = {}
  @option_parser = OptionParser.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphdown/cli.rb', line 33

def method_missing(name, *args)
  case name.to_s
  when /\.md$/
    parse(name.to_s, *args)
  when "-h", "--help"
    args << "--help"
    parse(name.to_s, *args)
  when "-v", "--version"
    puts Graphdown::VERSION
  else
    super
  end
end

Class Method Details

.start(args) ⇒ Object



7
8
9
10
# File 'lib/graphdown/cli.rb', line 7

def self.start(args)
  method_name = args.shift
  new.send(method_name, *args)
end

Instance Method Details

#parse(name, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphdown/cli.rb', line 17

def parse(name, *args)
  @option_parser.on("-o", "--output FILENAME") { |filename| @options[:output] = filename }
  @option_parser.parse!(args)

  markdown_path = Pathname.new(name).expand_path
  output_filename = @options[:output] || File.basename(name, ".md") + ".html"
  html_path = markdown_path.dirname.join(output_filename)

  renderer = Class.new(Redcarpet::Render::HTML)
  renderer.send(:include, Graphdown::Renderable)
  markdown = Redcarpet::Markdown.new(renderer)

  html = markdown.render(markdown_path.read)
  html_path.open("wb") { |file| file.write(html) }
end