Class: M2H::Render::Document

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_body) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
# File 'lib/m2h/render.rb', line 9

def initialize(html_body)
  @erb_template = File.open(File.dirname(__FILE__) + "/_layout.erb").read
  @html         = generate_html(html_body)
  @header       = false
end

Instance Attribute Details

#html_bodyObject

Returns the value of attribute html_body.



7
8
9
# File 'lib/m2h/render.rb', line 7

def html_body
  @html_body
end

Instance Method Details

#bind!Object



30
31
32
# File 'lib/m2h/render.rb', line 30

def bind!
  return ERB.new(@erb_template).result(binding)
end

#generate_html(html_body) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/m2h/render.rb', line 15

def generate_html(html_body)
  @title      = $1 if /h1>(.+)\<\/h1./ =~ html_body
  @pages      = html_body.split(/\/{4,}/) # page_break
  @page_count = @pages.size
  html = @pages.map.with_index { |page, i|
    unless (i + 1) == @page_count
      "<div class='page_number'>#{i+1}/#{@page_count}</div>\n" + page + "\n<div class='break'/>\n"
    else
      "<div class='page_number'>#{i+1}/#{@page_count}</div>\n" + page
    end
  }.join
  html.gsub!(/page_number'>/, "page_number'>#{@title}&nbsp;")
  return html
end

#set_coverObject



46
47
48
49
50
# File 'lib/m2h/render.rb', line 46

def set_cover
  cover = "\n<div class='cover'><h1>#{@title}</h1></div>\n"
  cover += "<div class='break'/>\n"
  @cover = cover
end

#set_headerObject



34
35
36
# File 'lib/m2h/render.rb', line 34

def set_header
  @header = true
end

#set_serifObject



42
43
44
# File 'lib/m2h/render.rb', line 42

def set_serif
  @serif = "serif, "
end

#set_tocObject



52
53
54
# File 'lib/m2h/render.rb', line 52

def set_toc
  @toc = true
end

#write(path, enc) ⇒ Object



38
39
40
# File 'lib/m2h/render.rb', line 38

def write(path, enc)
  File.open(path, enc).write(self.bind!)
end