class Jekyll::Commands::NewProject

Public Class Methods

directory_name(name, date) click to toggle source
# File lib/dsm-portfolio-plugin.rb, line 64
def self.directory_name(name, date)
    "_posts/#{date.strftime('%Y-%m-%d')}-#{name}"
end
file_name(name, post_type, ext, date) click to toggle source
# File lib/dsm-portfolio-plugin.rb, line 68
def self.file_name(name, post_type, ext, date)
    "_posts/#{date.strftime('%Y-%m-%d')}-#{name}/#{post_type}.#{ext}"
end
init_with_program(prog) click to toggle source
# File lib/dsm-portfolio-plugin.rb, line 5
def self.init_with_program(prog)
    prog.command(:project) do |c|
        # Define documentation.
        c.syntax 'project NAME'
        c.description 'Creates a new project with the given NAME'
        
        # Define options.
        c.option 'date', '-d DATE', '--date DATE', 'Specify the post date'
        c.option 'force', '-f', '--force', 'Overwrite a post if it already exists'
        
        # Process.
        c.action do |args, options|
            Jekyll::Commands::NewProject.process(args, options)
        end
    end
end
process(args, options = {}) click to toggle source
# File lib/dsm-portfolio-plugin.rb, line 22
def self.process(args, options = {})
    # Null check required fields.
    raise ArgumentError.new('You must specify a project name.') if args.empty?
    
    # Create default options for each post type.
    ext = "md"
    date = options["date"].nil? ? Time.now : DateTime.parse(options["date"])

    title = args.shift
    name = title.gsub(' ', '-').downcase

    # Fill task from data file.
    post_types_data_file = "project-post-types"
    
    # Initialise the site object from configuration (to access data files)
    config_options = configuration_from_options({})
    site = Jekyll::Site.new(config_options)
    site.read

    post_types = site.site_data[post_types_data_file]

    if post_types.size > 0
        # Make subdirectory.
        FileUtils.mkdir_p directory_name(name, date)
    else
        raise RangeError.new("There are no post types defined in #{post_types_data_file}.")
    end
    
    # For every post-type in subfolder...
    post_types.each do |post_type|
        # Format file path.
        post_path = file_name(name, post_type['post_type'], ext, date)

        raise ArgumentError.new("A post already exists at ./#{post_path}") if File.exist?(post_path) and !options["force"]
        
        # Create file.
        IO.copy_stream("_layouts/examples/#{post_type['template_file']}", post_path)
    end

    puts "New posts created at ./_posts/#{date.strftime('%Y-%m-%d')}-#{name}.\n"
end