Class: Toto::Article

Inherits:
Hash show all
Includes:
Template
Defined in:
lib/toto.rb

Instance Method Summary collapse

Methods included from Template

included, #markdown, #method_missing

Constructor Details

#initialize(obj, config = {}) ⇒ Article

Returns a new instance of Article.



234
235
236
237
# File 'lib/toto.rb', line 234

def initialize obj, config = {}
  @obj, @config = obj, config
  self.load if obj.is_a? Hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Toto::Template

Instance Method Details

#[](key) ⇒ Object



256
257
258
259
# File 'lib/toto.rb', line 256

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

#authorObject



301
# File 'lib/toto.rb', line 301

def author()  self[:author] || @config[:author]   end

#bodyObject



280
281
282
# File 'lib/toto.rb', line 280

def body
  markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body]
end

#dateObject



300
# File 'lib/toto.rb', line 300

def date()    @config[:date].call(self[:date])    end

#loadObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/toto.rb', line 239

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

#pathObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/toto.rb', line 284

def path
  path = "/#{@config[:prefix]}/"
  @config[:permalink].split("/").each do |sub|
    next if sub.empty?
    case sub
    when ":year"  then path += (self[:year]  || "%Y").to_s << "/"
    when ":month" then path += (self[:month] || "%m").to_s << "/"
    when ":day"   then path += (self[:day]   || "%d").to_s << "/"
    when ":title" then path += "#{slug}/"
    when /:(\w+)/ then path += "#{self[$1.to_sym]}/"
    end
  end
  self[:date].strftime(path).squeeze("/")
end

#slugObject



261
262
263
# File 'lib/toto.rb', line 261

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

#summary(length = nil) ⇒ Object



265
266
267
268
269
270
271
272
273
# File 'lib/toto.rb', line 265

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/, '&hellip;'))
end

#titleObject



299
# File 'lib/toto.rb', line 299

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

#to_htmlObject Also known as: to_s



302
# File 'lib/toto.rb', line 302

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

#urlObject Also known as: permalink



275
276
277
# File 'lib/toto.rb', line 275

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