Sha256: b02eb2d069c6c241069a0cdb2afc5a240aa0155c925b3b24f9c6cb5369589b11

Contents?: true

Size: 1.91 KB

Versions: 8

Compression:

Stored size: 1.91 KB

Contents

# encoding: utf-8

usage       'create-layout [options] identifier'
aliases     :create_layout, :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
        raise Nanoc::Errors::GenericTrivial, "usage: #{command.usage}"
      end

      # Extract arguments
      identifier = arguments[0].cleaned_identifier

      # Make sure we are in a nanoc site directory
      require_site

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

      # Check whether layout is unique
      if !site.layouts.find { |l| l.identifier == identifier }.nil?
        raise Nanoc::Errors::GenericTrivial,
          "A layout already exists at #{identifier}. Please " +
          'pick a unique name for the layout you are creating.'
      end

      # Check whether layout is not at /
      if identifier == '/'
        raise Nanoc::Errors::GenericTrivial,
          "There cannot be a layout with the identifier '/'; " +
          'please pick a different identifier for this layout.'
      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 = 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.7.3 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.7.2 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.7.1 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.7.0 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.6.11 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.6.10 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.6.9 lib/nanoc/cli/commands/create-layout.rb
nanoc-3.6.8 lib/nanoc/cli/commands/create-layout.rb