Class: Henshin::Post

Inherits:
Gen
  • Object
show all
Defined in:
lib/henshin/post.rb

Instance Attribute Summary collapse

Attributes inherited from Gen

#to_inject

Instance Method Summary collapse

Methods inherited from Gen

#get_generators, #get_layout, #read_file, #render, #url, #write, #write_path

Constructor Details

#initialize(path, site) ⇒ Post

Returns a new instance of Post.



7
8
9
10
11
12
13
14
15
16
# File 'lib/henshin/post.rb', line 7

def initialize( path, site )
  @path = path
  @site = site
  
  @content = ''
  @data = {}
  @generators = []
  
  @data['input'] = @path.extname[1..-1]
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



5
6
7
# File 'lib/henshin/post.rb', line 5

def content
  @content
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/henshin/post.rb', line 5

def data
  @data
end

#generatorsObject

Returns the value of attribute generators.



5
6
7
# File 'lib/henshin/post.rb', line 5

def generators
  @generators
end

#layoutObject

Returns the value of attribute layout.



5
6
7
# File 'lib/henshin/post.rb', line 5

def layout
  @layout
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/henshin/post.rb', line 5

def path
  @path
end

#siteObject

Returns the value of attribute site.



5
6
7
# File 'lib/henshin/post.rb', line 5

def site
  @site
end

Instance Method Details

#<=>(other) ⇒ Object

Sorts on date first, then permalink if dates are equal



141
142
143
144
145
146
147
148
# File 'lib/henshin/post.rb', line 141

def <=>(other)
  s = @data['date'] <=> other.data['date']
  if s == 0
    self.permalink <=> other.permalink
  else
    s
  end
end

#inspectObject



150
151
152
# File 'lib/henshin/post.rb', line 150

def inspect
  "#<Post:#{@path}>"
end

#nextPost

Gets the post after this one

Returns:

  • (Post)

    next post



105
106
107
108
109
110
111
112
113
# File 'lib/henshin/post.rb', line 105

def next
  if i = @site.posts.index(self)
    if i < @site.posts.size - 1
      @site.posts[i+1]
    else
      nil
    end
  end
end

#payloadHash

Creates the data to be sent to the layout engine

Returns:

  • (Hash)

    the payload for the layout engine



57
58
59
60
61
62
63
64
65
66
# File 'lib/henshin/post.rb', line 57

def payload
  r = { 
    'yield' => @content,
    'site'  => @site.payload['site'],
    'post'  => self.to_hash
  }
  #r['post']['next'] = self.next.to_hash if self.next
  #r['post']['prev'] = self.prev.to_hash if self.prev
  r
end

Returns the permalink of the post.

Returns:

  • (String)

    the permalink of the post



129
130
131
132
133
134
135
136
137
138
# File 'lib/henshin/post.rb', line 129

def permalink
  partials = {'year' => @data['date'].year,
              'month' => @data['date'].month,
              'date' => @data['date'].day,
              'title' => @data['title'].slugify,
              'category' => @data['category'] || ''}
              
  perm = @site.config['permalink'].gsub(/\{([a-z-]+)\}/) { partials[$1] }
  File.join(@site.base, perm)
end

#prevPost

Gets the post before this one

Returns:

  • (Post)

    previous post



118
119
120
121
122
123
124
125
126
# File 'lib/henshin/post.rb', line 118

def prev
  if i = @site.posts.index(self)
    if i > 0
      @site.posts[i-1]
    else
      nil
    end
  end
end

#readObject

Reads the file



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

def read
  self.read_name
  self.read_file if @path.exist?
  self.get_generators
  self.get_layout
  
  # now tidy up data
  @data['output'] ||= @data['input']
  @data['date'] = Time.parse(@data['date'])
  @data['tags'] = @data['tags'].flatten.uniq if @data['tags']
  self
end

#read_nameObject

Reads the filename and extracts information from it



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/henshin/post.rb', line 35

def read_name
  
  result = Parsey.parse(@path.to_s[(@site.root + 'posts').to_s.size..-1], @site.config['file_name'], Partials)

  result.each do |k, v|
    unless v.nil?
      case k
        when 'title-with-dashes'
          @data['title'] = v.gsub(/-/, ' ').titlecase
        when 'title'
          @data['title'] = v.titlecase
        else
          @data[k] = v
      end
    end
  end
  
end

#to_hashHash

Turns all of the post data into a hash

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/henshin/post.rb', line 71

def to_hash
  if @hashed
    @hashed
  else
    @hashed = @data.dup
    @hashed['content'] = @content
    @hashed['url'] = self.url
    @hashed['permalink'] = self.permalink
    
    
  
    if @data['tags']
      @hashed['tags'] = []
      @site.tags.select{|t| @data['tags'].include?(t.name)}.each do |tag|
        # can't call Tag#to_hash or it creates an infinite loop!
        @hashed['tags'] << {'name' => tag.name, 'url' => tag.url}
      end
    end
  
    if @data['category']
      @site.categories.each do |cat|
        if cat.name == @data['category']
          @hashed['category'] = {'name' => cat.name, 'url' => cat.url}
        end
      end
    end
  
    @hashed
  end
end