lib/nanoc/cli/commands/create-site.rb in nanoc-4.0.0b1 vs lib/nanoc/cli/commands/create-site.rb in nanoc-4.0.0b2

- old
+ new

@@ -4,10 +4,11 @@ aliases :create_site, :cs summary 'create a site' description " Create a new site at the given path. The site will use the `filesystem_unified` data source by default, but this can be changed using the `--datasource` command-line option. " +flag nil, :force, "Force creation of new site. Disregards previous existence of site in destination" module Nanoc::CLI::Commands class CreateSite < ::Nanoc::CLI::CommandRunner class << self protected @@ -18,13 +19,13 @@ end end DEFAULT_CONFIG = <<EOS unless defined? DEFAULT_CONFIG # The syntax to use for patterns in the Rules file. Can be either `"glob"` -# (default) or `null`. The former will enable glob patterns, which behave like -# Ruby’s File.fnmatch. The latter will enable nanoc 3.x-style patterns. -pattern_syntax: glob +# (default) or `"legacy"`. The former will enable glob patterns, which behave +# like Ruby’s File.fnmatch. The latter will enable nanoc 3.x-style patterns. +string_pattern_type: glob # A list of file extensions that nanoc will consider to be textual rather than # binary. If an item with an extension not in this list is found, the file # will be considered as binary. text_extensions: #{array_to_yaml(Nanoc::Int::Site::DEFAULT_CONFIG[:text_extensions])} @@ -45,12 +46,12 @@ # before and after the last site compilation. enable_output_diff: false prune: # Whether to automatically remove files not managed by nanoc from the output - # directory. For safety reasons, this is turned off by default. - auto_prune: false + # directory. + auto_prune: true # Which files and directories you want to exclude from pruning. If you version # your output directory, you should probably exclude VCS directories such as # .git, .svn etc. exclude: [ '.git', '.hg', '.svn', 'CVS' ] @@ -86,11 +87,11 @@ # The encoding to use for input files. If your input files are not in # UTF-8 (which they should be!), change this. encoding: utf-8 - identifier_style: full + identifier_type: full # Configuration for the “check” command, which run unit tests on the site. checks: # Configuration for the “internal_links” checker, which checks whether all # internal links are valid. @@ -104,25 +105,45 @@ DEFAULT_RULES = <<EOS unless defined? DEFAULT_RULES #!/usr/bin/env ruby compile '/**/*.html' do - filter :erb layout '/default.*' end +# This is an example rule that matches Markdown (.md) files, and filters them +# using the :kramdown filter. It is commented out by default, because kramdown +# is not bundled with nanoc or Ruby. +# +#compile '/**/*.md' do +# filter :kramdown +# layout '/default.*' +#end + compile '/**/*' do end +route '/**/*.{html,md}' do + if item.identifier =~ '/index.*' + '/index.html' + else + item.identifier.without_ext + '/index.html' + end +end + route '/**/*' do item.identifier.to_s end layout '/**/*', :erb EOS DEFAULT_ITEM = <<EOS unless defined? DEFAULT_ITEM +--- +title: Home +--- + <h1>A Brand New nanoc Site</h1> <p>You’ve just created a new nanoc site. The page you are looking at right now is the home page for your site. To get started, consider replacing this default homepage with your own customized homepage. Some pointers on how to do so:</p> <ul> @@ -278,12 +299,14 @@ # Extract arguments and options path = arguments[0] data_source = options[:datasource] || 'filesystem_unified' # Check whether site exists - if File.exist?(path) && (!File.directory?(path) || !(Dir.entries(path) - %w{ . .. }).empty?) - raise Nanoc::Int::Errors::GenericTrivial, "A site at '#{path}' already exists." + if File.exist?(path) && (!File.directory?(path) || !(Dir.entries(path) - %w{ . .. }).empty?) && !options[:force] + raise Nanoc::Int::Errors::GenericTrivial, + "The site was not created because '#{path}' already exists. " + + "Re-run the command using --force to create the site anyway." end # Check whether data source exists if Nanoc::DataSource.named(data_source).nil? raise Nanoc::Int::Errors::GenericTrivial, "Unrecognised data source: #{data_source}" @@ -300,43 +323,24 @@ FileUtils.mkdir_p('content') FileUtils.mkdir_p('layouts') FileUtils.mkdir_p('lib') FileUtils.mkdir_p('output') - # Config - File.open('nanoc.yaml', 'w') { |io| io.write(DEFAULT_CONFIG) } - Nanoc::Int::NotificationCenter.post(:file_created, 'nanoc.yaml') - - # Rules - File.open('Rules', 'w') do |io| - io.write DEFAULT_RULES - end - Nanoc::Int::NotificationCenter.post(:file_created, 'Rules') - - # Home page - File.open('content/index.html', 'w') do |io| - io << '---' << "\n" - io << 'title: Home' << "\n" - io << '---' << "\n" - io << "\n" - io << DEFAULT_ITEM - end - Nanoc::Int::NotificationCenter.post(:file_created, 'content/index.html') - - # Style sheet - File.open('content/stylesheet.css', 'w') do |io| - io << DEFAULT_STYLESHEET - end - Nanoc::Int::NotificationCenter.post(:file_created, 'content/stylesheet.css') - - # Layout - File.open('layouts/default.html', 'w') do |io| - io << DEFAULT_LAYOUT - end - Nanoc::Int::NotificationCenter.post(:file_created, 'layouts/default.html') + write('nanoc.yaml', DEFAULT_CONFIG) + write('Rules', DEFAULT_RULES) + write('content/index.html', DEFAULT_ITEM) + write('content/stylesheet.css', DEFAULT_STYLESHEET) + write('layouts/default.html', DEFAULT_LAYOUT) end puts "Created a blank nanoc site at '#{path}'. Enjoy!" + end + + private + + def write(filename, content) + File.write(filename, content) + Nanoc::Int::NotificationCenter.post(:file_created, filename) end end end runner Nanoc::CLI::Commands::CreateSite