Module: Octopress::CommandHelpers

Extended by:
CommandHelpers
Included in:
CommandHelpers
Defined in:
lib/octopress/commands/helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_common_options(c) ⇒ Object



11
12
13
# 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) ⇒ Object



5
6
7
8
9
# 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_postsObject



41
42
43
44
45
46
47
48
# 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_draftsObject



32
33
34
35
36
37
38
39
# 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_postsObject



54
55
56
# File 'lib/octopress/commands/helpers.rb', line 54

def find_exiled_posts
  find_all_posts.select { |f| f =~ /_exile\// }
end

#find_postsObject



50
51
52
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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