homeostasis 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +16 -10
  2. data/lib/homeostasis.rb +126 -34
  3. metadata +1 -1
data/README.md CHANGED
@@ -56,7 +56,7 @@ In your controller:
56
56
 
57
57
  Homeostasis::Blog.directory('blog') # directory of posts
58
58
 
59
- Post files should be in the format `yyyy-mm-dd-permalink.html.{md,haml}`. Use
59
+ Post files should be in the format `yyyy-mm-dd-permalink.html.md`. Use
60
60
  YAML front-matter for any metadata you want. `:date` and `:path` will be
61
61
  added automatically for you.
62
62
 
@@ -115,7 +115,21 @@ Sitemap
115
115
  A sitemap will automatically be generated in `public/sitemap.xml`. You'll need
116
116
  to set the root URL for this to happen:
117
117
 
118
- Homeostasis.root 'http://example.com'
118
+ Homeostasis::Sitemap.url 'http://example.com'
119
+
120
+ `loc` and `lastmod` will be generated for each page. Use front-yaml to set the
121
+ `changefreq` or `priority`:
122
+
123
+ <!--
124
+ :changefreq: monthly
125
+ :priority: 0.9
126
+ -->
127
+
128
+ Use the key `private` to avoid generating an entry:
129
+
130
+ <!--
131
+ :private: true
132
+ -->
119
133
 
120
134
  Trailing Slash
121
135
  ==============
@@ -136,14 +150,6 @@ You'll get:
136
150
  This works well with an `htaccess` file that automatically appends trailing
137
151
  slashes to URLs.
138
152
 
139
- TODO
140
- ====
141
-
142
- * use Dir.glob with relative paths
143
- * add tests/coverage
144
- * RSS generation
145
- * sitemap generation
146
-
147
153
  License
148
154
  =======
149
155
 
data/lib/homeostasis.rb CHANGED
@@ -1,12 +1,36 @@
1
1
  require 'rubygems'
2
2
  require 'stasis'
3
3
  require 'digest/sha1'
4
+ require 'bluecloth'
4
5
  require 'yaml'
6
+ require 'cgi'
5
7
 
6
8
  module Homeostasis
7
- VERSION = '0.0.10'
9
+ VERSION = '0.0.11'
10
+
11
+ module Helpers
12
+ private
13
+ def ignore?(path)
14
+ @ignore_paths ||= @stasis.plugins.
15
+ find { |plugin| plugin.class == Stasis::Ignore }.
16
+ instance_variable_get(:@ignore)
17
+
18
+ matches = _match_key?(@ignore_paths, path)
19
+ matches.each do |group|
20
+ group.each do |group_path|
21
+ return true if _within?(group_path)
22
+ end
23
+ end
24
+ false
25
+ end
26
+
27
+ def h(html)
28
+ CGI::escapeHTML(html)
29
+ end
30
+ end
8
31
 
9
32
  class Asset < Stasis::Plugin
33
+ include Helpers
10
34
  before_all :before_all
11
35
  after_all :after_all
12
36
 
@@ -117,22 +141,10 @@ module Homeostasis
117
141
  end
118
142
 
119
143
  private
120
- def ignore?(path)
121
- @ignore_paths ||= @stasis.plugins.
122
- find { |plugin| plugin.class == Stasis::Ignore }.
123
- instance_variable_get(:@ignore)
124
-
125
- matches = _match_key?(@ignore_paths, path)
126
- matches.each do |group|
127
- group.each do |group_path|
128
- return true if _within?(group_path)
129
- end
130
- end
131
- false
132
- end
133
144
  end
134
145
 
135
146
  class Front < Stasis::Plugin
147
+ include Helpers
136
148
  before_all :before_all
137
149
  action_method :front
138
150
  action_method :front_site
@@ -151,28 +163,32 @@ module Homeostasis
151
163
  def before_all
152
164
  @stasis.paths.each do |path|
153
165
  next if path !~ /\.(#{@@matchers.keys.join('|')})$/
154
- next if (contents = File.read(path)) !~ @@matchers[File.extname(path)[1..-1]]
155
-
156
- lines, data, index = contents.split("\n"), "", 1
157
- while index < lines.size
158
- break if lines[index] !~ /^ /
159
- data += lines[index] + "\n"
160
- index += 1
166
+ if (contents = File.read(path)) !~ @@matchers[File.extname(path)[1..-1]]
167
+ yaml = {}
168
+ else
169
+ lines, data, index = contents.split("\n"), "", 1
170
+ while index < lines.size
171
+ break if lines[index] !~ /^ /
172
+ data += lines[index] + "\n"
173
+ index += 1
174
+ end
175
+ begin
176
+ yaml = YAML.load(data)
177
+ rescue Psych::SyntaxError
178
+ yaml = {}
179
+ end
161
180
  end
162
181
 
163
- relative = path[(@stasis.root.length+1)..-1]
164
- ext = Tilt.mappings.keys.find { |ext| File.extname(path)[1..-1] == ext }
165
- dest = trailify((ext && File.extname(relative) == ".#{ext}") ?
166
- relative[0..-1*ext.length-2] :
167
- relative)
168
-
169
- begin
170
- yaml = YAML.load(data)
182
+ # add special :path key for generated files
183
+ if !ignore?(path)
184
+ relative = path[(@stasis.root.length+1)..-1]
185
+ ext = Tilt.mappings.keys.find { |ext| File.extname(path)[1..-1] == ext }
186
+ dest = trailify((ext && File.extname(relative) == ".#{ext}") ?
187
+ relative[0..-1*ext.length-2] :
188
+ relative)
171
189
  yaml[:path] = dest
172
- @@front_site[front_key(path)] = yaml if yaml.is_a?(Hash)
173
- rescue Psych::SyntaxError => error
174
- @@front_site[front_key(path)] = {:path => dest}
175
190
  end
191
+ @@front_site[front_key(path)] = yaml
176
192
  end
177
193
  end
178
194
 
@@ -229,7 +245,54 @@ module Homeostasis
229
245
  end
230
246
  end
231
247
 
248
+ class Sitemap < Stasis::Plugin
249
+ include Helpers
250
+ after_all :after_all
251
+
252
+ def initialize(stasis)
253
+ @stasis = stasis
254
+ @@url = nil
255
+ end
256
+
257
+ def self.url(url)
258
+ @@url = url
259
+ end
260
+
261
+ def after_all
262
+ return if @@url.nil?
263
+ xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
264
+ xml += "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"
265
+ front_site = Homeostasis::Front._front_site
266
+ pages = front_site.keys.sort_by do |page|
267
+ depth = page.scan('/').length
268
+ depth -= 1 if page =~ /index\.html/
269
+ "#{depth}-#{page}"
270
+ end
271
+ pages.each do |page|
272
+ front = front_site[page]
273
+ filename = File.join(@stasis.root, page)
274
+ next if page !~ /\.html/ || front[:private] || front[:path].nil?
275
+
276
+ log = `git log -n1 #{filename} 2> /dev/null | grep "Date:"`
277
+ lastmod = log.length > 0 ?
278
+ Date.parse(log.split("\n")[0].split(":",2)[1].strip).strftime('%Y-%m-%d') :
279
+ nil
280
+ xml += " <url>\n"
281
+ xml += " <loc>#{h(@@url + front[:path])}</loc>\n" if front[:path]
282
+ xml += " <lastmod>#{h lastmod}</lastmod>\n" if lastmod
283
+ xml += " <changefreq>#{h front[:changefreq]}</changefreq>\n" if front[:changefreq]
284
+ xml += " <priority>#{h front[:priority]}</priority>\n" if front[:priority]
285
+ xml += " </url>\n"
286
+ end
287
+ xml += '</urlset>'
288
+ File.open(File.join(@stasis.destination, 'sitemap.xml'), 'w') do |f|
289
+ f.puts(xml)
290
+ end
291
+ end
292
+ end
293
+
232
294
  class Blog < Stasis::Plugin
295
+ include Helpers
233
296
  DATE_REGEX = /^(\d{4}-\d{2}-\d{2})-/
234
297
  before_all :before_all
235
298
  after_all :after_all
@@ -238,11 +301,17 @@ module Homeostasis
238
301
  def initialize(stasis)
239
302
  @stasis = stasis
240
303
  @@directory = nil
304
+ @@link = nil
305
+ @@title = nil
306
+ @@desc = nil
241
307
  @@posts = []
242
308
  end
243
309
 
244
- def self.directory(directory)
310
+ def self.config(directory, link, title, desc)
245
311
  @@directory = directory
312
+ @@link = link
313
+ @@title = title
314
+ @@desc = desc
246
315
  end
247
316
 
248
317
  def before_all
@@ -254,6 +323,7 @@ module Homeostasis
254
323
  post = front_site[filename.sub(@stasis.root, '')[1..-1]] || {}
255
324
  post[:date] = Date.parse(date)
256
325
  post[:path] = post[:path].sub("/#{@@directory}/#{date}-", "/#{@@directory}/")
326
+ post[:body] = BlueCloth.new(File.read(filename)).to_html if filename =~ /\.md$/
257
327
  @@posts << post
258
328
  end
259
329
  @@posts = @@posts.sort_by { |post| post[:date] }.reverse
@@ -267,10 +337,31 @@ module Homeostasis
267
337
  filename,
268
338
  File.join(File.dirname(filename), base.sub(DATE_REGEX, '')))
269
339
  end
340
+ url = h("#{@@link}/#{@@directory}/")
341
+ rss = "<?xml version=\"1.0\"?>\n"
342
+ rss += "<rss version=\"2.0\">\n"
343
+ rss += " <channel>\n"
344
+ rss += " <title>#{h @@title}</title>\n" if @@title
345
+ rss += " <link>#{h @@link}/</link>\n" if @@link
346
+ rss += " <description>#{h @@desc}</description>\n" if @@desc
347
+ rss += " <atom:link href=\"#{url}/\" rel=\"self\" type=\"application/rss+xml\" />\n"
348
+ blog_posts[0..5].each do |post|
349
+ rss += " <item>\n"
350
+ rss += " <title>#{h post[:title]}</title>\n"
351
+ rss += " <link>#{h(@@link + post[:path])}</link>\n"
352
+ rss += " <pubDate>#{post[:date].strftime('%m-%d-%Y %H:%M')}</pubDate>\n"
353
+ rss += " <description>#{h post[:body]}</description>\n"
354
+ rss += " </item>\n"
355
+ end
356
+ rss += " </channel>\n"
357
+ rss += "</rss>"
358
+ File.open(File.join(@stasis.destination, @@directory, 'rss.xml'), 'w') do |f|
359
+ f.puts(rss)
360
+ end
270
361
  end
271
362
 
272
363
  def blog_posts
273
- raise 'Homeostasis::Blog#directory never set' if @@directory.nil?
364
+ raise 'Homeostasis::Blog#config never called' if @@directory.nil?
274
365
  @@posts
275
366
  end
276
367
  end
@@ -280,5 +371,6 @@ if !ENV['HOMEOSTASIS_UNREGISTER']
280
371
  Stasis.register(Homeostasis::Asset)
281
372
  Stasis.register(Homeostasis::Front)
282
373
  Stasis.register(Homeostasis::Trail)
374
+ Stasis.register(Homeostasis::Sitemap)
283
375
  Stasis.register(Homeostasis::Blog)
284
376
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: homeostasis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: