lib/creator.rb in nanoc-1.3.1 vs lib/creator.rb in nanoc-1.4

- old
+ new

@@ -1,16 +1,11 @@ module Nanoc class Creator def self.create_site(a_sitename) - # Make sure we're not accidentally overwriting anything - if File.exist?(a_sitename) - $stderr.puts 'ERROR: A file or directory named ' + a_sitename + - ' already exists.' unless $quiet - exit - end + ensure_nonexistant(a_sitename) FileManager.create_dir a_sitename do FileManager.create_dir 'assets' FileManager.create_dir 'output' @@ -22,12 +17,12 @@ "# This file contains the default values for all metafiles.\n" + "# Other metafiles can override the contents of this one.\n" + "\n" + "# Built-in\n" + "layout: \"default\"\n" + - "order: 0\n" + "filters: []\n" + + "filename: \"index\"\n" + "extension: \"html\"\n" + "\n" + "# Custom\n" end @@ -100,28 +95,19 @@ end end def self.create_page(a_pagename, a_params={}) Nanoc.ensure_in_site + ensure_nonexistant(File.join(['content', a_pagename])) - # Make sure we're not accidentally overwriting anything - if File.exist?(File.join(['content', a_pagename])) - $stderr.puts 'ERROR: A file or directory named ' + a_pagename + - ' already exists.' unless $quiet - exit - end - # Sanitize page name if a_pagename =~ /^[\/\.]+/ $stderr.puts 'ERROR: page name starts with dots and/or slashes, aborting' unless $quiet == true return end # Read template - template_index = nil - template_meta = nil - template_content_filename = nil template = a_params[:template] || 'default' begin template_meta = File.read("templates/#{template}/meta.yaml") template_content_filenames = Dir["templates/#{template}/#{template}.*"] template_content_filenames += Dir["templates/#{template}/index.*"] @@ -135,11 +121,13 @@ template_index = template_index.eruby # Create index and yaml file FileManager.create_dir 'content' do FileManager.create_dir a_pagename do - FileManager.create_file "#{a_pagename.sub(/.*\/([^\/]+)/, '\1')}#{File.extname(template_content_filename)}" do + page_name = a_pagename.sub(/.*\/([^\/]+)/, '\1') + extension = File.extname(template_content_filename) + FileManager.create_file "#{page_name}#{extension}" do template_index end FileManager.create_file 'meta.yaml' do template_meta end @@ -147,18 +135,12 @@ end end def self.create_template(a_templatename) Nanoc.ensure_in_site + ensure_nonexistant(File.join(['templates', a_templatename])) - # Make sure we're not accidentally overwriting anything - if File.exist?(File.join(['templates', a_templatename])) - $stderr.puts 'ERROR: A file or directory named ' + a_templatename + - ' already exists.' unless $quiet - exit - end - FileManager.create_dir 'templates' do FileManager.create_dir a_templatename do FileManager.create_file "#{a_templatename}.txt" do "This is a new page. Please edit me!\n" end @@ -167,9 +149,18 @@ "\n" + "# Custom\n" + "title: A New Page\n" end end + end + end + + private + + def self.ensure_nonexistant(filename) + if File.exist?(filename) + $stderr.puts "ERROR: A file or directory named #{filename} already exists." unless $quiet + exit end end end end