lib/henshin/post.rb in henshin-0.0.1 vs lib/henshin/post.rb in henshin-0.1.2
- old
+ new
@@ -8,10 +8,13 @@
@path = path
@site = site
@config = site.config
@extension = path.extension
@layout = site.layouts[ site.config[:layout] ]
+ @author = @config[:author]
+ @tags = []
+ @date = Time.now
end
##
# Processes the file
@@ -43,13 +46,13 @@
# extract data from filename
file_data = path.file_name.match( matcher ).captures
file_data.each_with_index do |data, i|
if data_order[i].include? 'title'
if data_order[i].include? 'dashes'
- override[:title] = data.gsub(/-/, ' ')
+ override[:title] = data.gsub(/-/, ' ').titlize
else
- override[:title] = data
+ override[:title] = data.titlize
end
elsif data_order[i].include? 'date'
override[:date] = data
elsif data_order[i].include? 'extension'
override[:extension] = data
@@ -73,35 +76,52 @@
# Uses the loaded data to override settings
#
# @param [Hash] override data to override settings with
def override( override )
- @title ||= override[:title]
- @layout ||= @site.layouts[ override[:layout] ]
- @date ||= Time.parse( override[:date] ) unless override[:date].nil?
- @tags ||= override[:tags]
- @category ||= override[:category]
- @author ||= override[:author]
- @extension ||= override[:extension]
+ @title = override[:title] if override[:title]
+ @layout = @site.layouts[ override[:layout] ] if override[:layout]
+ @date = Time.parse( override[:date] ) if override[:date]
+ @tags = override[:tags].split(', ') if override[:tags]
+ @category = override[:category] if override[:category]
+ @author = override[:author] if override[:author]
+ @extension = override[:extension] if override[:extension]
+ @category = override[:category] if override[:category]
+
+ if override[:tags]
+ @tags << override[:tags].split(', ')
+ @tags.flatten!
+ end
end
# Creates the data to be sent to the layout engine
#
# @return [Hash] the payload for the layout engine
def payload
{
'yield' => @content,
'site' => @site.payload['site'],
- 'post' => {'title' => @title,
- 'author' => @author,
- 'date' => @date,
- 'tags' => @tags,
- 'category' => @category}
+ 'post' => self.to_hash
}
end
+ # Turns all of the post data into a hash
+ #
+ # @return [Hash]
+ def to_hash
+ {
+ 'title' => @title,
+ 'author' => @author,
+ 'url' => self.permalink,
+ 'date' => @date,
+ 'category' => @category,
+ 'tags' => @tags,
+ 'content' => @content
+ }
+ end
+
##
# Writes the file to the correct place
def write
write_path = File.join( config[:root], config[:target], permalink )
@@ -118,9 +138,13 @@
'title' => self.title.slugify}
config[:permalink].gsub(/\{([a-z-]+)\}/) do
partials[$1]
end
+ end
+
+ def inspect
+ "#<Post:#{@path}>"
end
end
end
\ No newline at end of file