Class: MadDoc::Runner

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



17
18
19
20
21
22
23
# File 'lib/maddoc/runner.rb', line 17

def initialize argv
  parse!(argv).tap do |options|
    @title = options.title
  end

  @source = ARGF.read unless argv.empty? and $stdin.tty?
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



15
16
17
# File 'lib/maddoc/runner.rb', line 15

def title
  @title
end

Class Method Details

.run(argv) ⇒ Object



97
98
99
# File 'lib/maddoc/runner.rb', line 97

def self.run argv
  new(argv).run!
end

Instance Method Details

#asset(pathname) ⇒ Object



26
27
28
# File 'lib/maddoc/runner.rb', line 26

def asset pathname
  environment[pathname].to_s
end

#bodyObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maddoc/runner.rb', line 39

def body
  renderer = Class.new(Redcarpet::Render::SmartyHTML) do
    def block_code(code, lang)
      lang = lang && lang.split.first || "text"

      add_code_tags(
        Pygments.highlight(code, {
          :lexer => lang,
          :options => { :encoding => 'utf-8' }
        }),
        lang
      )
    end

    def add_code_tags(code, lang)
      code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
      code = code.sub(/<\/pre>/,"</code></pre>")
    end
  end.new({
    :with_toc_data      => true
  })

  Redcarpet::Markdown.new(renderer, {
    :no_intra_emphasis  => true,
    :tables             => true,
    :fenced_code_blocks => true,
    :autolink           => true,
    :strikethrough      => true,
    :lax_spacing        => true
  }).render(@source)
end

#environmentObject



31
32
33
34
35
36
# File 'lib/maddoc/runner.rb', line 31

def environment
  @environment ||= Sprockets::Environment.new(MadDoc.root.join('templates')).tap do |env|
    env.append_path 'assets/stylesheets'
    env.css_compressor = :sass
  end
end

#parse!(argv) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/maddoc/runner.rb', line 72

def parse! argv
  options = OpenStruct.new({
    :title => 'Untitled mad documentation'
  })

  OptionParser.new do |opts|
    opts.on('-t', '--title [TITLE]', 'Title') do |title|
      options.title = title.strip
    end
  end.parse! argv

  options
end

#run!Object



92
93
94
# File 'lib/maddoc/runner.rb', line 92

def run!
  puts template.render(self)
end

#templateObject



87
88
89
# File 'lib/maddoc/runner.rb', line 87

def template
  Slim::Template.new MadDoc.root.join('templates', 'layout.slim').to_s
end