Skip to content

Commit

Permalink
Merge pull request imathis#225 from imathis/post_names
Browse files Browse the repository at this point in the history
Add more flexibility to :new_page task
  • Loading branch information
fhemberger committed Dec 10, 2011
2 parents e8ee521 + fc138b2 commit e5c262c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,29 @@ desc "Create a new page in #{source_dir}/(filename)/index.#{new_page_ext}"
task :new_page, :filename do |t, args|
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
args.with_defaults(:filename => 'new-page')
page_dir = source_dir
if args.filename =~ /(^.+\/)?([\w_-]+)(\.)?(.+)?/
page_dir += $4 ? "/#{$1}" : "/#{$1}#{$2}/"
name = $4 ? $2 : "index"
extension = $4 || "#{new_page_ext}"
filename = "#{name}.#{extension}"
page_dir = [source_dir]
if args.filename.downcase =~ /(^.+\/)?(.+)/
filename, dot, extension = $2.rpartition('.').reject(&:empty?) # Get filename and extension
title = filename
page_dir.concat($1.downcase.sub(/^\//, '').split('/')) unless $1.nil? # Add path to page_dir Array
if extension.nil?
page_dir << filename
filename = "index"
end
extension ||= new_page_ext
page_dir = page_dir.map! { |d| d = d.to_url }.join('/') # Sanitize path
filename = filename.downcase.to_url

mkdir_p page_dir
file = page_dir + filename
file = "#{page_dir}/#{filename}.#{extension}"
if File.exist?(file)
abort("rake aborted!") if ask("#{file} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new page: #{file}"
open(file, 'w') do |page|
page.puts "---"
page.puts "layout: page"
page.puts "title: \"#{$2.gsub(/[-_]/, ' ')}\""
page.puts "title: \"#{title}\""
page.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
page.puts "comments: true"
page.puts "sharing: true"
Expand Down

0 comments on commit e5c262c

Please sign in to comment.