module Octopress::CommandHelpers

Public Instance Methods

add_common_options(c) click to toggle source
# File lib/octopress/commands/helpers.rb, line 11
def add_common_options(c)
  c.option 'config',    '-c', '--config <CONFIG_FILE>[,CONFIG_FILE2,...]', Array, 'Custom Jekyll configuration file'
end
add_page_options(c) click to toggle source
# File lib/octopress/commands/helpers.rb, line 5
def add_page_options(c)
  c.option 'date',     '-d', '--date DATE', "Use 'now' or a String that is parseable by Time#parse."
  c.option 'template', '-T','--template PATH', "New #{c.name.to_s} from a template."
  c.option 'force',    '-f', '--force', 'Overwrite file if it already exists'
end
find_all_posts() click to toggle source
# File lib/octopress/commands/helpers.rb, line 41
def find_all_posts
  @posts ||= begin
    dir = File.join(Octopress.site.source, '_posts')
    Find.find(dir).to_a.reject do |f| 
      File.directory?(f)
    end
  end
end
find_drafts() click to toggle source
# File lib/octopress/commands/helpers.rb, line 32
def find_drafts
  @drafts ||= begin
    dir = File.join(Octopress.site.source, '_drafts')
    Find.find(dir).to_a.reject do |f| 
      File.directory?(f)
    end
  end
end
find_exiled_posts() click to toggle source
# File lib/octopress/commands/helpers.rb, line 54
def find_exiled_posts
  find_all_posts.select { |f| f =~ /_exile\// }
end
find_posts() click to toggle source
# File lib/octopress/commands/helpers.rb, line 50
def find_posts
  find_all_posts.reject { |f| f =~ /_exile\// }
end
prompt_for_selection(posts, search, action) click to toggle source
# File lib/octopress/commands/helpers.rb, line 69
def prompt_for_selection(posts, search, action)
  abort if !STDOUT.tty?

  puts "Found #{posts.size} posts matching: '#{search}'"
  posts.each_with_index do |p, i| 
    post = p.sub(/#{Octopress.site.source}\/_(posts|drafts)\//, '')
    puts "  #{i+1}) #{post}"
  end

  print "Which do you want to #{action}? (enter a number): "
  $stdout.flush
  post = $stdin.gets.strip.to_i

  # Give a newline for further output
  puts ''

  # Handle invalid input (because "oops".to_i == 0)
  (post == 0 ? nil : post)
end
search_posts(search, posts) click to toggle source
# File lib/octopress/commands/helpers.rb, line 58
def search_posts(search, posts)
  posts = posts.select do |p|
    p =~ /#{search.gsub(/\s/, '-')}/i 
  end
  if posts.empty?
    abort (STDOUT.tty? ? "No posts found matching: #{search}".red : '')
  else
    posts
  end
end
select_posts(search, action) click to toggle source
# File lib/octopress/commands/helpers.rb, line 15
def select_posts(search, action)
  posts = (action == 'publish' ? find_drafts : find_posts)
  posts = search_posts(search, posts)

  if posts.size > 1
    post = prompt_for_selection(posts, search, action)

    if post.is_a? Integer
      posts[post - 1]
    else
      abort "#{action.capitalize} canceled: You didn't enter number."
    end
  else
    posts.first
  end
end