Class: Statikaj::Article

Inherits:
Hash
  • Object
show all
Defined in:
lib/statikaj/article.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj, config) ⇒ Article

Returns a new instance of Article.



7
8
9
10
# File 'lib/statikaj/article.rb', line 7

def initialize obj, config
  @obj, @config = obj, config
  self.load
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
38
# File 'lib/statikaj/article.rb', line 35

def [] key
  self.load unless self.tainted?
  super
end

#authorObject



70
71
72
# File 'lib/statikaj/article.rb', line 70

def author
  self[:author]
end

#bodyObject



54
55
56
# File 'lib/statikaj/article.rb', line 54

def body
  markdown self[:body].sub("~~~", '')
end

#categoryObject



74
75
76
# File 'lib/statikaj/article.rb', line 74

def category
  self[:category]
end

#dateObject



66
67
68
# File 'lib/statikaj/article.rb', line 66

def date
  self[:date]
end

#loadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/statikaj/article.rb', line 20

def load
  data = if @obj.is_a? String
      meta, self[:body] = File.read(@obj).split(/\n\n/, 2)
      @obj =~ /\/(\d{4}-\d{2}-\d{2})[^\/]*$/
      ($1 ? {:date => $1} : {}).merge(YAML.load(meta))
    elsif @obj.is_a? Hash
      @obj
    end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) }

  self.taint
  self.update data
  self[:date] = Time.parse(self[:date].gsub('/', '-')) rescue Time.now
  self
end

#markdown(text) ⇒ Object



16
17
18
# File 'lib/statikaj/article.rb', line 16

def markdown(text)
  Kramdown::Document.new(text).to_html
end

#pathObject



58
59
60
# File 'lib/statikaj/article.rb', line 58

def path
  "/#{slug}"
end

#render(source) ⇒ Object



12
13
14
# File 'lib/statikaj/article.rb', line 12

def render(source)
  ERB.new(File.read(source.join("templates/pages/article.rhtml"))).result(binding)
end

#slugObject



40
41
42
# File 'lib/statikaj/article.rb', line 40

def slug
  self[:slug] || self[:title].slugize
end

#summaryObject



44
45
46
47
# File 'lib/statikaj/article.rb', line 44

def summary
  sum = self[:body].split("~~~").first
  markdown(sum.length == self[:body].length ? sum : sum.strip.sub(/\.\Z/, '…'))
end

#titleObject



62
63
64
# File 'lib/statikaj/article.rb', line 62

def title
  self[:title] || "an article"
end

#to_htmlObject Also known as: to_s



78
79
80
# File 'lib/statikaj/article.rb', line 78

def to_html
  self.load; super(:article, @config)
end

#urlObject Also known as: permalink



49
50
51
# File 'lib/statikaj/article.rb', line 49

def url
  "http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}"
end