Class: Octopress::Post

Inherits:
Page
  • Object
show all
Defined in:
lib/octopress/post.rb

Direct Known Subclasses

Draft

Constant Summary

Constants inherited from Page

Octopress::Page::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Page

#site

Instance Method Summary collapse

Methods inherited from Page

#content, #convert_date, #date_slug, #default_front_matter, #extension, #initialize, #parse_template, #print_collection_tip, #relative_path, #title_slug, #write

Constructor Details

This class inherits a constructor from Octopress::Page

Instance Method Details

#default_templateObject



27
28
29
# File 'lib/octopress/post.rb', line 27

def default_template
  'post'
end

#pathObject



20
21
22
23
24
25
# File 'lib/octopress/post.rb', line 20

def path
  name = "#{date_slug}-#{title_slug}.#{extension}"
  dir = File.join(site.source, '_posts', @options['dir'])
  FileUtils.mkdir_p dir
  File.join(dir, name)
end

#readObject

Reads the file from options



54
55
56
57
58
59
60
# File 'lib/octopress/post.rb', line 54

def read
  @post_content ||= begin
    file = @options['path']
    raise "File #{file} not found." if !File.exist? file
    File.read(file)
  end
end

#read_post_contentObject

Get content from draft post file also update the draft’s date configuration



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/octopress/post.rb', line 72

def read_post_content
  if @options['date']
    # remove date if it exists
    content = read.sub(/date:.*$\n/, "")
    
    # Insert date after title
    content.sub(/(title:.+$)/i, '\1'+"\ndate: #{@options['date']}")
  else
    read
  end
end

#read_post_yaml(key) ⇒ Object

Get title from post file



64
65
66
67
# File 'lib/octopress/post.rb', line 64

def read_post_yaml(key)
  match = read.match(/#{key}:\s*(.+)?$/)
  match[1] if match
end

#set_default_optionsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/octopress/post.rb', line 5

def set_default_options
  @options['type']      ||= 'post'
  @options['write_message'] ||= 'New post:'

  if @options['title'].nil? && @options[:type] == 'post'
    raise "Post not created: Please choose a title.\n".red + "  For example: " + "octopress new post \"Friendship with a tomato?\"".yellow
  end

  @options['layout']      = @config['post_layout']
  @options['date']        = convert_date @options['date']
  @options['extension'] ||= @config['post_ext']
  @options['template']  ||= @config['post_template']
  @options['dir']       ||= ''
end

#unpublishObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/octopress/post.rb', line 31

def unpublish
  @options['date'] = read_post_yaml('date')
  @options['title'] = read_post_yaml('title')

  post_options = {
    'title'   => @options['title'],
    'date'    => @options['date'],
    'slug'    => title_slug,
    'content' => read_post_content,
    'dir'     => @options['dir'],
    'type'    => "draft from post",
    'write_message' => "Unpublished: #{relative_path(path)}"
  }

  Draft.new(site, post_options).write     
  
  # Remove the old post file
  #
  FileUtils.rm @options['path']
end