Class: TotoBongo::Article

Inherits:
Hash
  • Object
show all
Includes:
Template
Defined in:
lib/toto-bongo.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.



344
345
346
347
348
# File 'lib/toto-bongo.rb', line 344

def initialize obj, config = {}
  TotoBongo::logger.debug("Article::initialize")
  @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 TotoBongo::Template

Instance Method Details

#[](key) ⇒ Object

Called by path when constructing the SEO url



372
373
374
375
376
377
# File 'lib/toto-bongo.rb', line 372

def [] key
  TotoBongo::logger.debug("Article::key: key = #{key}")

  self.load unless self.tainted?
  super
end

#authorObject



425
426
427
428
# File 'lib/toto-bongo.rb', line 425

def author() 
  TotoBongo::logger.debug("Article::path")
  self[:author] || @config[:author]  
end

#bodyObject



403
404
405
406
# File 'lib/toto-bongo.rb', line 403

def body
  TotoBongo::logger.debug("Article::body")
  markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body]
end

#dateObject



420
421
422
423
424
# File 'lib/toto-bongo.rb', line 420

def date() 
  TotoBongo::logger.debug("Article::path")
  @config[:date].call(self[:date])        
 
end

#descriptionObject



430
431
432
433
# File 'lib/toto-bongo.rb', line 430

def description()
  TotoBongo::logger.debug("Article::path")
  self[:description] || title()  
end

#keywordsObject



435
436
437
438
# File 'lib/toto-bongo.rb', line 435

def keywords()
  TotoBongo::logger.debug("Article::keywords")
  self[:keywords] || title()  
end

#loadObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/toto-bongo.rb', line 351

def load
  TotoBongo::logger.debug("Article::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

Path returns a SEO friendly URL path Eg for blog/articles/1900-05-17-the-wonderful-wizard-of-oz.txt it returns /blog/1900/05/17/the-wonderful-wizard-of-oz/



411
412
413
414
# File 'lib/toto-bongo.rb', line 411

def path
  TotoBongo::logger.debug("Article::path")
  "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/')
end

#slugObject



379
380
381
382
# File 'lib/toto-bongo.rb', line 379

def slug
  TotoBongo::logger.debug("Article::slug")
  self[:slug] || self[:title].slugize
end

#summary(length = nil) ⇒ Object

create a small summary of the body. Defaulsts 150 characters



386
387
388
389
390
391
392
393
394
395
# File 'lib/toto-bongo.rb', line 386

def summary length = nil
  TotoBongo::logger.debug("Article::summary")
  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



416
417
418
419
# File 'lib/toto-bongo.rb', line 416

def title()   
  TotoBongo::logger.debug("Article::title")
  self[:title] || "an article"
end

#to_htmlObject Also known as: to_s



441
442
443
444
# File 'lib/toto-bongo.rb', line 441

def to_html() 
  TotoBongo::logger.debug("Article::path")
  self.load; super(:article, @config) 
end

#urlObject Also known as: permalink



397
398
399
400
# File 'lib/toto-bongo.rb', line 397

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