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, #path_slug, #print_collection_tip, #relative_path, #title_slug, #write

Constructor Details

This class inherits a constructor from Octopress::Page

Instance Method Details

#default_templateObject



33
34
35
# File 'lib/octopress/post.rb', line 33

def default_template
  'post'
end

#pathObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/octopress/post.rb', line 20

def path
  name = if @options['path']
    "#{date_slug}-#{path_slug(@options['path'])}.#{File.extname(@options['path']).sub(/^\./, '')}"
  else
    "#{date_slug}-#{title_slug}.#{extension}"
  end
  File.join(site.source, '_drafts', name)
  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



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

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/octopress/post.rb', line 80

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



72
73
74
75
# File 'lib/octopress/post.rb', line 72

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/octopress/post.rb', line 37

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

  post_options = {
    'title'     => @options['title'],
    'date'      => @options['date'],
    'slug'      => path_slug(@options['path']),
    'extension' => File.extname(@options['path']).sub(/^\./, ''),
    'content'   => read_post_content,
    'dir'       => @options['dir'],
    'type'      => "draft from post",
    'write_message' => "Unpublished: #{relative_path(path)}",
    'force'     => @options['force']
  }

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