/
page.rb
47 lines (37 loc) · 851 Bytes
/
page.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# encoding: utf-8
require 'rdiscount'
class Page
attr_reader :name
def Page.dir
File.join(File.dirname(__FILE__),'content')
end
def Page.list
Dir.entries(Page.dir).reject! {|f| f.start_with?('.') }.map { |i|
i.chomp('.md')
}.compact.sort_by {|c|
File.stat(File.join(Page.dir, "#{c}.md")).mtime
}.reverse
end
def initialize(name)
@name = name
@fname = File.join(Page.dir,"#{self.title}.md")
end
def title
name.gsub(" ", "_").downcase
end
def exists?
File.exists?(@fname) and not File.basename(@fname).start_with?('.')
end
def raw
File.new(@fname).read
end
def to_html
RDiscount.new(self.raw).to_html
end
def save!(content)
File.open(@fname,"w+") { |f| f.write(content) }
end
def destroy!
File.delete(@fname) if File.exists?(@fname)
end
end