plate 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
- ## Plate 0.7.0 (Unreleased)
1
+ ## Plate 0.7.1
2
+
3
+ * Added DSL method `write` to create a dynamic page.
4
+ * Added DSL method `archives` to create category, monthly, and yearly post archives.
5
+
6
+ ## Plate 0.7.0
2
7
 
3
8
  * Added support for view partials
4
9
  * Reload page layout when re-rendering a page
data/README.md CHANGED
@@ -8,7 +8,7 @@ In addition to basic formatting with Markdown, Plate also supports generating mo
8
8
 
9
9
  Plate is a command line utility installed as a Ruby Gem. Installation requires Ruby 1.8.7, 1.9.2 or 1.9.3.
10
10
 
11
- Current version is **0.7.0**
11
+ Current version is **0.7.1**
12
12
 
13
13
  ## Installation
14
14
 
@@ -72,7 +72,7 @@ When creating a new post, you can choose to put it in the *drafts* folder, inste
72
72
  ```
73
73
  publish: true
74
74
  ```
75
-
75
+
76
76
  On the next site build, your post will automatically be moved to the appropriate spot in the *posts* folder.
77
77
 
78
78
  By default, the `plate post [Title]` command does not use the drafts folder. To enable draft usage by default when a new post is generated, just change the following line in the `config/plate.yml` file to true:
data/lib/plate/dsl.rb CHANGED
@@ -23,6 +23,68 @@ module Plate
23
23
  end
24
24
  end
25
25
 
26
+ # Used for writing dynamic pages. Allows for calling:
27
+ #
28
+ # title "Test"
29
+ # layout "default"
30
+ #
31
+ # Instead of:
32
+ #
33
+ # self.title = "Test"
34
+ # self.layout = "default"
35
+ #
36
+ class PageProxy
37
+ def initialize(page, site)
38
+ @page = page
39
+ @site = site
40
+ end
41
+
42
+ %w( category month year month_name ).each do |m|
43
+ class_eval <<-META
44
+ def _#{m}=(val) # def _category=(val)
45
+ @_#{m} = val # @_category = val
46
+ end # end
47
+
48
+ def #{m} # def category
49
+ defined?(@_#{m}) ? @_#{m} : nil # defined?(@_category) ? @_category : nil
50
+ end # end
51
+ META
52
+ end
53
+
54
+ def site
55
+ @site
56
+ end
57
+
58
+ def method_missing(method, *args)
59
+ if @page.respond_to?("#{method}=")
60
+ @page.send("#{method}=", *args)
61
+ else
62
+ @page.send(method, *args)
63
+ end
64
+ end
65
+
66
+ def to_url(*args)
67
+ @site.to_url(*args)
68
+ end
69
+ end
70
+
71
+ # Loop through the given item name to generate archive pages.
72
+ #
73
+ # Available item types are:
74
+ #
75
+ # * :category
76
+ # * :tag
77
+ # * :year
78
+ # * :month
79
+ # * :day
80
+ #
81
+ def archives(item_type, options = {}, &block)
82
+ # If there is a method to return the list of items
83
+ if self.respond_to?("archive_#{item_type}_items")
84
+ self.send("archive_#{item_type}_items", options, &block)
85
+ end
86
+ end
87
+
26
88
  # Register a new callback for the given object and event.
27
89
  #
28
90
  # @example Run block after rendering a site
@@ -64,5 +126,113 @@ module Plate
64
126
  def register_template_engine(extension, klass)
65
127
  Plate.register_template_engine(extension, klass)
66
128
  end
129
+
130
+ # Create and write a dynamic page. Pages are written during the Site.after_render
131
+ # callback.
132
+ def write_page(path, site = nil, &block)
133
+ delayed_write = lambda { |site|
134
+ page = DynamicPage.new(site, path)
135
+ proxy = PageProxy.new(page, site)
136
+ proxy.instance_eval(&block)
137
+ page.write!
138
+ proxy = nil
139
+ page = nil
140
+ }
141
+
142
+ # If a site was passed to the write method, use it.
143
+ if site
144
+ delayed_write.call(site)
145
+ # If no site was sent to the write method, run it after site render
146
+ else
147
+ callback :site, :after_render do |rendered_site|
148
+ delayed_write.call(rendered_site)
149
+ end
150
+ end
151
+ end
152
+ alias :write :write_page
153
+
154
+ protected
155
+ # Category archives
156
+ def archive_category_items(options, &block)
157
+ callback :site, :after_render do |site|
158
+ site.categories.each do |category|
159
+ page = DynamicPage.new(site, "/categories/#{category}")
160
+ proxy = PageProxy.new(page, site)
161
+ proxy._category = category
162
+ page.meta[:category] = category
163
+ proxy.instance_eval(&block)
164
+ page.write!
165
+ proxy = nil
166
+ page = nil
167
+ end
168
+ end
169
+ end
170
+
171
+ # Monthly archive rendering
172
+ def archive_month_items(options, &block)
173
+ filter_year = options[:year] || nil
174
+ filter_category = options[:category] || nil
175
+
176
+ callback :site, :after_render do |site|
177
+ site.posts.archives.keys.each do |year|
178
+ if filter_year == nil or filter_year == year
179
+ site.posts.archives[year].keys.each do |month|
180
+ posts = []
181
+
182
+ site.posts.archives[year][month].each_value do |day|
183
+ posts << day.select { |post| filter_category == nil or post.category == filter_category }
184
+ end
185
+
186
+ posts.flatten!
187
+
188
+ if posts.size > 0
189
+ page = DynamicPage.new(site, "/archives/#{year}/#{month}")
190
+ proxy = PageProxy.new(page, site)
191
+ proxy._year = year
192
+ proxy._month = month
193
+ proxy._month_name = Date.new(year.to_i, month.to_i, 1).strftime('%B %Y')
194
+ proxy._category = filter_category if filter_category
195
+ proxy.locals = { :monthly_posts => posts, :month => month, :year => year }
196
+ proxy.instance_eval(&block)
197
+ page.write!
198
+ proxy = nil
199
+ page = nil
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ # Yearly archive rendering
208
+ def archive_year_items(options, &block)
209
+ filter_category = options[:category] || nil
210
+
211
+ callback :site, :after_render do |site|
212
+ site.posts.archives.keys.each do |year|
213
+ posts = []
214
+
215
+ site.posts.archives[year].keys.each do |month|
216
+ site.posts.archives[year][month].each_value do |day|
217
+ posts << day.select { |post| filter_category == nil or post.category == filter_category }
218
+ end
219
+ end
220
+
221
+ posts.flatten!
222
+
223
+ if posts.size > 0
224
+ page = DynamicPage.new(site, "/archives/#{year}")
225
+ proxy = PageProxy.new(page, site)
226
+ proxy._year = year
227
+ proxy._category = filter_category if filter_category
228
+ proxy.locals = { :yearly_posts => posts, :year => year }
229
+ proxy.instance_eval(&block)
230
+ page.write!
231
+ proxy = nil
232
+ page = nil
233
+ end
234
+ end
235
+ end
236
+ end
67
237
  end
68
238
  end
@@ -29,18 +29,38 @@ module Plate
29
29
  # Set up a new instance of Dynamic Page
30
30
  def initialize(site, destination_path, meta = {})
31
31
  self.site = site
32
- self.file_path = destination_path
32
+ self.path = destination_path
33
33
  self.meta = meta
34
34
  self.content = ""
35
35
  self.locals = {}
36
36
  self.partials = []
37
37
  end
38
-
38
+
39
+ # name for dynamic pages is the path
40
+ def name
41
+ self.file_path
42
+ end
43
+
39
44
  # Set the full hash to be provided to the view. Hash keys are symbolized before
40
45
  # sending to the view.
41
46
  def locals=(hash)
42
47
  @locals = hash.symbolize_keys!
43
48
  end
49
+
50
+ # Set the destination path for this page.
51
+ def path=(destination_path)
52
+ # Remove leading and trailing slashes
53
+ destination_path = destination_path.to_s.gsub(/^\//, '').gsub(/\/$/, '')
54
+
55
+ # Unless file ends in an extension, add /index.html
56
+ unless destination_path =~ /(.*?)\.[a-z0-0]{2,4}/i
57
+ destination_path = "#{destination_path}/index.html"
58
+ end
59
+
60
+ destination_path = "/#{destination_path}"
61
+
62
+ self.file_path = destination_path
63
+ end
44
64
 
45
65
  # Alias for the content that is provided
46
66
  def rendered_body
@@ -6,7 +6,7 @@ module Plate
6
6
  # Returns a customized hash of post archives for the given category
7
7
  def category_archives(category)
8
8
  result = {}
9
-
9
+
10
10
  posts.archives.keys.each do |year|
11
11
  posts.archives[year].keys.each do |month|
12
12
  posts.archives[year][month].keys.each do |day|
@@ -21,16 +21,20 @@ module Plate
21
21
  end
22
22
  end
23
23
  end
24
-
24
+
25
25
  result
26
26
  end
27
-
27
+
28
28
  # Escape markup for use in XML or HTML
29
29
  def html_escape(text)
30
30
  ::CGI.escapeHTML(text)
31
31
  end
32
32
  alias_method :xml_escape, :html_escape
33
-
33
+
34
+ def month_name(year, month)
35
+ Date.new(year.to_i, month.to_i, 1).strftime('%B %Y')
36
+ end
37
+
34
38
  # Grab the next blog post.
35
39
  #
36
40
  # Returns nil if this is the last post.
@@ -38,34 +42,34 @@ module Plate
38
42
  return nil if post_index < 0 or post_index >= post_count
39
43
  @next_post ||= self.posts[post_index + 1]
40
44
  end
41
-
42
- # Grab the post previous to this one.
45
+
46
+ # Grab the post previous to this one.
43
47
  #
44
48
  # Returns nil if this is the first post.
45
49
  def previous_post
46
- return nil if post_index < 1
50
+ return nil if post_index < 1
47
51
  @previous_post ||= self.posts[post_index - 1]
48
52
  end
49
-
53
+
50
54
  # The total number blog posts
51
55
  def post_count
52
56
  @post_count ||= self.posts.length
53
57
  end
54
-
58
+
55
59
  # Returns a number of where this post is in all posts
56
60
  def post_index
57
61
  @post_index ||= self.posts.index(self.post)
58
62
  end
59
-
63
+
60
64
  # Find all posts for the given category
61
65
  def posts_for_category(category)
62
66
  self.posts.select { |p| p.category == category }
63
67
  end
64
-
68
+
65
69
  # Find all posts for the given year, month and optional category.
66
70
  def posts_for_month(year, month, category = nil)
67
71
  result = []
68
-
72
+
69
73
  if months = self.posts.archives[year.to_s]
70
74
  if month = months[month.to_s]
71
75
  month.each_value do |day|
@@ -73,14 +77,14 @@ module Plate
73
77
  end
74
78
  end
75
79
  end
76
-
80
+
77
81
  result.flatten
78
82
  end
79
-
83
+
80
84
  # Find all posts for the given year and optional category.
81
85
  def posts_for_year(year, category = nil)
82
86
  result = []
83
-
87
+
84
88
  if months = self.posts.archives[year.to_s]
85
89
  months.each_value do |month|
86
90
  month.each_value do |day|
@@ -88,19 +92,19 @@ module Plate
88
92
  end
89
93
  end
90
94
  end
91
-
95
+
92
96
  result.flatten
93
97
  end
94
-
98
+
95
99
  def years_for_category(category)
96
100
  result = []
97
-
101
+
98
102
  self.posts.archives.keys.each do |year|
99
103
  if posts_for_year(year, category).size > 0
100
- result << year
104
+ result << year
101
105
  end
102
106
  end
103
-
107
+
104
108
  result.sort
105
109
  end
106
110
  end
data/lib/plate/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Plate
2
- VERSION = "0.7.0" unless defined?(::Plate::VERSION)
2
+ VERSION = "0.7.1" unless defined?(::Plate::VERSION)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-31 00:00:00.000000000 Z
12
+ date: 2012-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport