Class: Statikaj::Article
Instance Method Summary collapse
- #[](key) ⇒ Object
- #author ⇒ Object
- #body ⇒ Object
- #category ⇒ Object
- #date ⇒ Object
-
#initialize(obj, config) ⇒ Article
constructor
A new instance of Article.
- #load ⇒ Object
- #markdown(text) ⇒ Object
- #path ⇒ Object
- #render(source) ⇒ Object
- #slug ⇒ Object
- #summary ⇒ Object
- #title ⇒ Object
- #to_html ⇒ Object (also: #to_s)
- #url ⇒ Object (also: #permalink)
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 |
#author ⇒ Object
70 71 72 |
# File 'lib/statikaj/article.rb', line 70 def self[:author] end |
#body ⇒ Object
54 55 56 |
# File 'lib/statikaj/article.rb', line 54 def body markdown self[:body].sub("~~~", '') end |
#category ⇒ Object
74 75 76 |
# File 'lib/statikaj/article.rb', line 74 def category self[:category] end |
#date ⇒ Object
66 67 68 |
# File 'lib/statikaj/article.rb', line 66 def date self[:date] end |
#load ⇒ Object
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 , self[:body] = File.read(@obj).split(/\n\n/, 2) @obj =~ /\/(\d{4}-\d{2}-\d{2})[^\/]*$/ ($1 ? {:date => $1} : {}).merge(YAML.load()) 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 |
#path ⇒ Object
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 |
#slug ⇒ Object
40 41 42 |
# File 'lib/statikaj/article.rb', line 40 def slug self[:slug] || self[:title].slugize end |
#summary ⇒ Object
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 |
#title ⇒ Object
62 63 64 |
# File 'lib/statikaj/article.rb', line 62 def title self[:title] || "an article" end |
#to_html ⇒ Object Also known as: to_s
78 79 80 |
# File 'lib/statikaj/article.rb', line 78 def to_html self.load; super(:article, @config) end |
#url ⇒ Object 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 |