Sha256: 5571277f39fe30296d0bbd298b9ec3b5aa95aa1a2f14365784e1d810363df520

Contents?: true

Size: 1.89 KB

Versions: 8

Compression:

Stored size: 1.89 KB

Contents

# encoding: utf-8

usage       'create_layout [options] identifier'
aliases     'cl'
summary     'create a layout'
description <<-EOS
Create a new layout in the current site. The first data source in the site
configuration will be used.
EOS

module Nanoc::CLI::Commands

  class CreateLayout < ::Nanoc::CLI::CommandRunner

    def run
      # Check arguments
      if arguments.length != 1
        $stderr.puts "usage: #{command.usage}"
        exit 1
      end

      # Extract arguments
      identifier = arguments[0].cleaned_identifier

      # Make sure we are in a nanoc site directory
      self.require_site

      # Set VCS if possible
      self.set_vcs(options[:vcs])

      # Check whether layout is unique
      if !self.site.layouts.find { |l| l.identifier == identifier }.nil?
        $stderr.puts "A layout already exists at #{identifier}. Please " +
                     "pick a unique name for the layout you are creating."
        exit 1
      end

      # Check whether layout is not at /
      if identifier == '/'
        $stderr.puts "There cannot be a layout with the identifier '/'; " +
                     "please pick a different identifier for this layout."
        exit 1
      end

      # Setup notifications
      Nanoc::NotificationCenter.on(:file_created) do |file_path|
        Nanoc::CLI::Logger.instance.file(:high, :create, file_path)
      end

      # Create layout
      data_source = self.site.data_sources[0]
      data_source.create_layout(
        "<html>\n" +
        "  <head>\n" +
        "    <title><%= @item[:title] %></title>\n" +
        "  </head>\n" +
        "  <body>\n" +
        "    <p>Hi, I'm a new layout. Please customize me!</p>\n" +
        "<%= yield %>\n" +
        "  </body>\n" +
        "</html>\n",
        {},
        identifier
      )

      puts "A layout has been created at #{identifier}."
    end

  end

end

runner Nanoc::CLI::Commands::CreateLayout

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nanoc-3.3.7 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.6 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.5 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.4 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.3 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.2 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.1 lib/nanoc/cli/commands/create_layout.rb
nanoc-3.3.0 lib/nanoc/cli/commands/create_layout.rb