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 #if obj.is_a? Hash
end

Instance Method Details

#[](key) ⇒ Object



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

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

#authorObject



80
81
82
# File 'lib/statikaj/article.rb', line 80

def author()
  self[:author]
end

#bodyObject

alias :permalink url



60
61
62
# File 'lib/statikaj/article.rb', line 60

def body
  markdown self[:body]
end

#dateObject



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

def date()
  # TODO: costum
  # @config[:date].call(self[:date])
  self[:date].strftime("%B #{self[:date].day.ordinal} %Y")
end

#loadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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)
      # use the date from the filename, or else toto won't find the article
      @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] = Date.parse(self[:date].gsub('/', '-')) rescue Date.today
  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



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

def path
  # TODO: custom domain
  #  "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/')
  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



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

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

#summary(length = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/statikaj/article.rb', line 45

def summary length = nil
  #   config = @config[:summary]
  #   sum = if self[:body] =~ config[:delim]
  #     self[:body].split(config[:delim]).first
  #   else
  #     self[:body].match(/(.{1,#{length || config[:length] || config[:max]}}.*?)(\n|\Z)/m).to_s
  #   end
  #   markdown(sum.length == self[:body].length ? sum : sum.strip.sub(/\.\Z/, '…'))
end

#titleObject



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

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

#to_htmlObject



84
85
86
# File 'lib/statikaj/article.rb', line 84

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

#urlObject



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

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