Class: Statikaj::Article
Instance Method Summary collapse
- #[](key) ⇒ Object
- #author ⇒ Object
-
#body ⇒ Object
alias :permalink url.
- #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(length = nil) ⇒ Object
- #title ⇒ Object
- #to_html ⇒ Object
- #url ⇒ Object
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 |
#author ⇒ Object
80 81 82 |
# File 'lib/statikaj/article.rb', line 80 def () self[:author] end |
#body ⇒ Object
alias :permalink url
60 61 62 |
# File 'lib/statikaj/article.rb', line 60 def body markdown self[:body] end |
#date ⇒ Object
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 |
#load ⇒ Object
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 , 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()) 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 |
#path ⇒ Object
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 |
#slug ⇒ Object
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 |
#title ⇒ Object
70 71 72 |
# File 'lib/statikaj/article.rb', line 70 def title() self[:title] || "an article" end |
#to_html ⇒ Object
84 85 86 |
# File 'lib/statikaj/article.rb', line 84 def to_html() self.load; super(:article, @config) end |
#url ⇒ Object
55 56 57 |
# File 'lib/statikaj/article.rb', line 55 def url # "http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}" end |