Class: Jekyll::Commands::Post

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

Class Method Summary collapse

Class Method Details

.file_name(name, ext, date) ⇒ Object

Internal: Gets the filename of the draft to be created

Returns the filename of the draft, as a String



44
45
46
# File 'lib/jekyll/commands/post.rb', line 44

def self.file_name(name, ext, date)
  "_posts/#{date.strftime('%Y-%m-%d')}-#{name}.#{ext}"
end

.front_matter(layout, title) ⇒ Object



48
49
50
51
52
53
# File 'lib/jekyll/commands/post.rb', line 48

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
18
# File 'lib/jekyll/commands/post.rb', line 4

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

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

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

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

Raises:

  • (ArgumentError)


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

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"]

  date = options["date"].nil? ? Time.now : DateTime.parse(options["date"])

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

  post_path = file_name(name, type, date)

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

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

  puts "New post created at ./#{post_path}.\n"
end