class Hawkins::Commands::Post

Constants

COMMAND_OPTIONS

Public Class Methods

create(args, options) click to toggle source
# File lib/hawkins/post.rb, line 28
        def create(args, options)
          options["date"] ||= Time.now.to_s
          options["editor"] ||= ENV['VISUAL'] || ENV['EDITOR'] || 'vi'
          begin
            date = Date.parse(options["date"])
          rescue
            Jekyll.logger.abort_with("Could not convert #{options['date']} into date format.")
          end

          if args.length != 1
            Jekyll.logger.abort_with(
              "Please provide an argument to use as the post title.\n
              Remember to quote multiword strings.")
          else
            title = args[0]
          end

          slug = Jekyll::Utils.slugify(title)

          site_opts = configuration_from_options(options)
          site = Jekyll::Site.new(site_opts)
          posts = site.in_source_dir('_posts')
          filename = File.join(posts, "#{date.strftime('%Y-%m-%d')}-#{slug}.md")

          # TODO incorporate Highline and allow users to elect to create the directory
          # Like Thor does
          unless File.exist?(posts)
            Jekyll.logger.abort_with("#{posts} does not exist.  Please create it.")
          end

          # TODO ask if user wishes to overwrite
          if File.exist?(filename)
            Jekyll.logger.abort_with(
              "#{filename} already exists.  Cowardly refusing to overwrite it.")
          end

          content = <<-CONTENT
            ---
            layout: post
            title: #{title}
            ---
          CONTENT

          File.open(filename, 'w') do |f|
            f.write(Jekyll::Utils.strip_heredoc(content))
          end

          Jekyll.logger.info("Wrote #{filename}")

          case options["editor"]
          when /g?vim/
            editor_args = "+"
          when /x?emacs/
            editor_args = "+#{content.lines.count}"
          else
            editor_args = nil
          end

          exec(*[options["editor"], editor_args, filename].compact)
        end
init_with_program(prog) click to toggle source
# File lib/hawkins/post.rb, line 14
def init_with_program(prog)
  prog.command(:post) do |cmd|
    cmd.syntax("post [options]")
    cmd.description("create a new post")
    COMMAND_OPTIONS.each do |key, val|
      cmd.option(key, *val)
    end

    cmd.action do |args, options|
      Hawkins::Commands::Post.create(args, options)
    end
  end
end