aboutsummaryrefslogtreecommitdiff
path: root/Rakefile
diff options
context:
space:
mode:
authorFrederic Hemberger <mail@frederic-hemberger.de>2011-10-16 16:22:37 +0200
committerFrederic Hemberger <mail@frederic-hemberger.de>2011-10-16 16:22:37 +0200
commitfc138b2e35c009df5fc84c361263172b5a867dc7 (patch)
tree06e74a101b2cc8e054dd7c603fd818ba86f5d54f /Rakefile
parentb1ad2a3d8a82c6f0c4d15f2058d57cb51ec90b60 (diff)
downloadmy_new_personal_website-fc138b2e35c009df5fc84c361263172b5a867dc7.tar.xz
my_new_personal_website-fc138b2e35c009df5fc84c361263172b5a867dc7.zip
Adds more flexibility to :new_page task
Page filenames/directories are lowercased and sanitized by default to allow a greater flexibility: - new_page["/path/to/file"] # => source/path/to/file/index.markdown - new_page["path/to/file"] # => source/path/to/file/index.markdown - new_page["my path/my file"] # => source/my-path/my-file/index.markdown - new_page["lorem.ipsum.dolor"] # => source/lorem-dot-ipsum.dolor - new_page["Questions & Answers"] # => source/questions-and-answers/index.markdown
Diffstat (limited to '')
-rw-r--r--Rakefile23
1 files changed, 15 insertions, 8 deletions
diff --git a/Rakefile b/Rakefile
index 54f98d17..6f955811 100644
--- a/Rakefile
+++ b/Rakefile
@@ -115,14 +115,21 @@ 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
@@ -130,7 +137,7 @@ task :new_page, :filename do |t, args|
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"