Skip to content

Instantly share code, notes, and snippets.

@dmitriid
Created August 12, 2012 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitriid/3333593 to your computer and use it in GitHub Desktop.
Save dmitriid/3333593 to your computer and use it in GitHub Desktop.
aliases :bp, :blog_post
class BlogPost < Nanoc::CLI::Commands::CreateItem
def run
# Check arguments
if arguments.length != 1
raise Nanoc::Errors::GenericTrivial, "usage: #{command.usage}"
end
# Extract arguments and options
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 item is unique
if !self.site.items.find { |i| i.identifier == identifier }.nil?
raise Nanoc::Errors::GenericTrivial,
"An item already exists at #{identifier}. Please " +
"pick a unique name for the item you are creating."
end
# Setup notifications
Nanoc::NotificationCenter.on(:file_created) do |file_path|
Nanoc::CLI::Logger.instance.file(:high, :create, file_path)
end
# Create item
data_source = self.site.data_sources[0]
data_source.create_item(
"Hi, I'm a new item!\n",
{ :title => "A New Item" },
identifier
)
puts "An item has been created at #{identifier}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment