Class: MdPage::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Page

Returns a new instance of Page.



5
6
7
8
9
10
11
12
13
# File 'lib/mdpage/page.rb', line 5

def initialize(argv)
  @argv = argv
  check_results = @argv.map {|av| File.exist? av}
  @path_check = check_results.include?(false) ? false : true
  tpl_path = File.dirname(__FILE__) + "/default.html.erb"
  @template = File.open(tpl_path, "r:utf-8") {|f| f.read}
  @page = Hash.new
  @result = nil
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



3
4
5
# File 'lib/mdpage/page.rb', line 3

def argv
  @argv
end

#docObject (readonly)

Returns the value of attribute doc.



3
4
5
# File 'lib/mdpage/page.rb', line 3

def doc
  @doc
end

#path_checkObject (readonly)

Returns the value of attribute path_check.



3
4
5
# File 'lib/mdpage/page.rb', line 3

def path_check
  @path_check
end

#resultObject (readonly)

Returns the value of attribute result.



3
4
5
# File 'lib/mdpage/page.rb', line 3

def result
  @result
end

Instance Method Details

#md2html(file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mdpage/page.rb', line 15

def md2html(file)
  begin
    raw_contents = File.open(file, "r:utf-8") {|f| f.read}
  rescue
    raise "#{file} encoding should be UTF-8."
  end
  raw_contents =~ /^.*#\s?(.+)\n.+/
  @page[:title] = $1 || ''
  contents = RDiscount.new(raw_contents).to_html
  return contents
end

#set_template(template_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/mdpage/page.rb', line 47

def set_template(template_name)
  begin
    home = ENV["HOME"]
    template_path = "#{home}/.mdpage/#{template_name}.html.erb"
    @template = File.open(template_path, "r:utf-8") {|f| f.read}
    return self
  rescue 
    raise "Missing template: #{template_path}"
  end
end

#to_html_filesObject



27
28
29
30
31
32
33
# File 'lib/mdpage/page.rb', line 27

def to_html_files
  path_data = to_html_objects
  path_data.each do |k, v|
    File.open(k, "w:utf-8") {|f| f.write v}
  end
  @result = true
end

#to_html_objectsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mdpage/page.rb', line 35

def to_html_objects
  erb = ERB.new(@template)
  html_objects = Hash.new
  @argv.each do |file|
    @page[:contents] = md2html(file)
    html = erb.result(binding)
    out_path = File.expand_path(file) + ".html"
    html_objects.store(out_path, html)
  end
  return html_objects
end