Class: Baron::Article

Inherits:
Hash
  • Object
show all
Defined in:
lib/baron/models/article.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Article.



11
12
13
14
15
16
17
18
19
# File 'lib/baron/models/article.rb', line 11

def initialize file_parts, config = {}
  @config = config
  self[:filename_and_path] = file_parts[:filename_and_path]
  self[:filename_without_extension] = file_parts[:filename_without_extension]
  self[:slug] = file_parts[:filename]
  self[:category] = file_parts[:category].empty? ? '' : file_parts[:category]
  self[:date] = Date.parse(file_parts[:date].gsub('/', '-')) rescue Date.today
  load_article(file_parts[:filename_and_path])
end

Class Method Details

.create_filename_without_extension(title, date) ⇒ Object



5
6
7
8
9
# File 'lib/baron/models/article.rb', line 5

def self.create_filename_without_extension title, date
  slug = title.empty? ? "Untitled" : title.strip.downcase
  slug_encoded = slug.gsub(/[^a-z0-9]/, '-')
  "#{date.strftime("%Y-%m-%d-") + slug_encoded}"
end

Instance Method Details

#authorObject



61
62
63
# File 'lib/baron/models/article.rb', line 61

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

#bodyObject



31
32
33
# File 'lib/baron/models/article.rb', line 31

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

#categoryObject



65
66
67
# File 'lib/baron/models/article.rb', line 65

def category
  self[:category]
end

#dateObject



52
53
54
# File 'lib/baron/models/article.rb', line 52

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

#date_iso8601Object



56
57
58
59
# File 'lib/baron/models/article.rb', line 56

def date_iso8601
  # "2009-10-26T04:47:09Z"
  self[:date].strftime("%FT%T%:z")
end

#path(prefix = '', date_format = '') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/baron/models/article.rb', line 35

def path prefix = '', date_format = ''
  permalink_prefix = prefix.empty? ? @config[:permalink_prefix] : prefix
  permalink_date_format = date_format.empty? ? @config[:permalink_date_format] : date_format
  date_path = case permalink_date_format
    when :year_date; self[:date].strftime("/%Y")
    when :year_month_date; self[:date].strftime("/%Y/%m")
    when :year_month_day_date; self[:date].strftime("/%Y/%m/%d")
    else ''
  end
  
  "/#{permalink_prefix}/#{self[:category]}#{date_path}/#{slug}/".squeeze('/')      
end


69
70
71
# File 'lib/baron/models/article.rb', line 69

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

#slugObject



73
74
75
# File 'lib/baron/models/article.rb', line 73

def slug
  self[:slug]
end

#slug_with_dateObject



77
78
79
# File 'lib/baron/models/article.rb', line 77

def slug_with_date

end

#summary(length = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/baron/models/article.rb', line 21

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/, @config[:truncation_marker]))
end

#titleObject



48
49
50
# File 'lib/baron/models/article.rb', line 48

def title
  self[:title] || 'Untitled'
end