Class: Mook::HtmlRenderer

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Includes:
Redcarpet::Render::SmartyPants
Defined in:
lib/mook/html.rb

Overview

Renders Markdown as HTML, Pygmentizing any named code blocks and generating TOC blocks.

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ HtmlRenderer

Returns a new instance of HtmlRenderer.



7
8
9
10
11
# File 'lib/mook/html.rb', line 7

def initialize(opts)
  super(opts)
  @toc_id = 0
  @toc = []
end

Instance Method Details

#block_code(code, language) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mook/html.rb', line 13

def block_code(code, language)
  if language
    Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
  else
    "<pre>#{code}</pre>"
  end
end

#header(text, header_level) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mook/html.rb', line 21

def header(text, header_level)
  tag = if text =~ / #(.+)$/ then
    text = text.gsub(/ #(.+)$/, "")
    $1
  else
    nil
  end

  @toc_id += 1

  id = if tag then tag else "toc_#{@toc_id}" end

  link = "<a href=\"##{id}\">#{text}</a>"
  
  @toc << "#{"  " * (header_level - 1)}* #{link}"

  "<a name=\"#{id}\"></a><h#{header_level}>#{text}</h#{header_level}>"
end

#tocObject



40
41
42
# File 'lib/mook/html.rb', line 40

def toc
  @toc.join("\n")
end