Browse code

Merge pull request #225 from imathis/post_names

Add more flexibility to :new_page task

Frederic Hemberger authored on 10/12/2011 at 12:04:48
Showing 1 changed files
... ...
@@ -115,14 +115,21 @@ desc "Create a new page in #{source_dir}/(filename)/index.#{new_page_ext}"
115 115
 task :new_page, :filename do |t, args|
116 116
   raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
117 117
   args.with_defaults(:filename => 'new-page')
118
-  page_dir = source_dir
119
-  if args.filename =~ /(^.+\/)?([\w_-]+)(\.)?(.+)?/
120
-    page_dir += $4 ? "/#{$1}" : "/#{$1}#{$2}/"
121
-    name = $4 ? $2 : "index"
122
-    extension = $4 || "#{new_page_ext}"
123
-    filename = "#{name}.#{extension}"
118
+  page_dir = [source_dir]
119
+  if args.filename.downcase =~ /(^.+\/)?(.+)/ 
120
+    filename, dot, extension = $2.rpartition('.').reject(&:empty?)         # Get filename and extension
121
+    title = filename
122
+    page_dir.concat($1.downcase.sub(/^\//, '').split('/')) unless $1.nil?  # Add path to page_dir Array
123
+    if extension.nil?
124
+      page_dir << filename
125
+      filename = "index"
126
+    end
127
+    extension ||= new_page_ext
128
+    page_dir = page_dir.map! { |d| d = d.to_url }.join('/')                # Sanitize path
129
+    filename = filename.downcase.to_url
130
+
124 131
     mkdir_p page_dir
125
-    file = page_dir + filename
132
+    file = "#{page_dir}/#{filename}.#{extension}"
126 133
     if File.exist?(file)
127 134
       abort("rake aborted!") if ask("#{file} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
128 135
     end
... ...
@@ -130,7 +137,7 @@ task :new_page, :filename do |t, args|
130 130
     open(file, 'w') do |page|
131 131
       page.puts "---"
132 132
       page.puts "layout: page"
133
-      page.puts "title: \"#{$2.gsub(/[-_]/, ' ')}\""
133
+      page.puts "title: \"#{title}\""
134 134
       page.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
135 135
       page.puts "comments: true"
136 136
       page.puts "sharing: true"