Class: Jekyll::Commands::Draft

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll/commands/draft.rb

Class Method Summary collapse

Class Method Details

.draft_name(name, ext = 'markdown') ⇒ Object

Internal: Gets the filename of the draft to be created

Returns the filename of the draft, as a String



41
42
43
# File 'lib/jekyll/commands/draft.rb', line 41

def self.draft_name(name, ext='markdown')
  "_drafts/#{name}.#{ext}"
end

.front_matter(layout, title) ⇒ Object



45
46
47
48
49
50
# File 'lib/jekyll/commands/draft.rb', line 45

def self.front_matter(layout, title)
  "---
layout: #{layout}
title: #{title}
---"
end

.init_with_program(prog) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jekyll/commands/draft.rb', line 4

def self.init_with_program(prog)
  prog.command(:draft) do |c|
    c.syntax 'draft NAME'
    c.description 'Creates a new draft post with the given NAME'

    c.option 'type', '-t TYPE', '--type TYPE', 'Specify the content type'
    c.option 'layout', '-l LAYOUT', '--layout LAYOUT', 'Specify the post layout'
    c.option 'force', '-f', '--force', 'Overwrite a draft if it already exists'

    c.action do |args, options|
      Jekyll::Commands::Draft.process(args, options)
    end
  end
end

.process(args, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll/commands/draft.rb', line 19

def self.process(args, options = {})
  raise ArgumentError.new('You must specify a name.') if args.empty?
  
  type = options["type"].nil? ? "markdown" : options["type"]
  layout = options["layout"].nil? ? "post" : options["layout"]

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

  draft_path = draft_name(name, type)

  raise ArgumentError.new("A draft already exists at ./#{draft_path}") if File.exist?(draft_path) and !options["force"]

  File.open(draft_path, "w") do |f|
    f.puts(front_matter(layout, title))
  end

  puts "New draft created at ./#{draft_path}.\n"
end