lib/octopress/page.rb in octopress-3.0.0.rc.18 vs lib/octopress/page.rb in octopress-3.0.0.rc.19
- old
+ new
@@ -11,11 +11,11 @@
attr_accessor :site
def initialize(site, options)
@site = site
- @site.plugin_manager.conscientious_require
+ site.plugin_manager.conscientious_require
@config = DEFAULT_OPTIONS.merge(site.config)
@options = options
set_default_options
# Ensure title
@@ -27,10 +27,14 @@
@options['title'] = "\"#{@options['title']}\""
@content = options['content'] || content
end
+ def site
+ @site
+ end
+
def write
if File.exist?(path) && !@options['force']
raise "File #{relative_path(path)} already exists. Use --force to overwrite."
end
@@ -68,14 +72,10 @@
def relative_path(path)
local = Dir.pwd + '/'
path.sub(local, '')
end
- def site
- @site
- end
-
def path
return @path if @path
file = @options['path']
raise "You must specify a path." unless file
@@ -105,19 +105,20 @@
end
def convert_date(date)
date ||= 'now'
if date == 'now'
- @options['date'] = Time.now.iso8601
+ date = Time.now.iso8601
else
begin
- Time.parse(date.to_s, Time.now).iso8601
+ date = Time.parse(date.to_s, Time.now).iso8601
rescue => error
puts 'Could not parse date. Try formatting it like YYYY-MM-DD HH:MM'
abort error.message
end
end
+ date
end
# Load the user provided or default template for a new post or page.
#
def content
@@ -146,33 +147,56 @@
end
# Render Liquid vars in YAML front-matter.
def parse_template(input)
+ vars = @options.dup
+
if @config['titlecase']
- @options['title'].titlecase!
+ vars['title'].titlecase!
end
+
+ vars['slug'] = title_slug
+ date = Time.parse(vars['date'] || Time.now.iso8601)
+ vars['year'] = date.year
+ vars['month'] = date.strftime('%m')
+ vars['day'] = date.strftime('%d')
+ vars['ymd'] = date.strftime('%D')
+
# If possible only parse the YAML front matter.
# If YAML front-matter dashes aren't present parse the whole
# template and add dashes.
#
parsed = if input =~ /\A-{3}\s+(.+?)\s+-{3}(.+)?/m
input = $1
content = $2
- if @options['date'] && !(input =~ /date:/)
- input += "\ndate: #{@options['date']}"
+ if vars['date'] && !(input =~ /date:/)
+ input += "\ndate: #{vars['date']}"
end
else
content = ''
end
template = Liquid::Template.parse(input)
- "---\n#{template.render(@options).strip}\n---\n#{content}"
+ "---\n#{template.render(vars).strip}\n---\n#{content}"
end
def date_slug
@options['date'].split('T')[0]
+ end
+
+ # Returns a string which is url compatible.
+ #
+ def title_slug
+ value = (@options['slug'] || @options['title']).downcase
+ value.gsub!(/[^\x00-\x7F]/u, '')
+ value.gsub!(/(&|&)+/, 'and')
+ value.gsub!(/[']+/, '')
+ value.gsub!(/\W+/, ' ')
+ value.strip!
+ value.gsub!(' ', '-')
+ value
end
def front_matter(vars)
fm = []
vars.each do |v|