homeostasis 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +50 -29
  2. data/lib/homeostasis.rb +150 -28
  3. data/lib/version.rb +1 -1
  4. metadata +83 -3
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  Description
2
2
  ===========
3
3
 
4
- Stasis plugin for asset stamping, blogs, front-matter yaml, sitemaps, and
5
- trailing slashes.
4
+ Stasis plugin for asset stamping, before/after_all, blogs, front-matter yaml,
5
+ multi templates, sitemaps, and trailing slashes.
6
6
 
7
7
  Installation
8
8
  ============
@@ -48,6 +48,19 @@ You can concatenate multiple assets into a single file:
48
48
  Homeostasis::Asset.concat 'all.js', %w(jquery.js mine.js)
49
49
  Homeostasis::Asset.concat 'all.css', %w(reset.css mine.css)
50
50
 
51
+ Before and After All Events
52
+ ===========================
53
+
54
+ In your controller:
55
+
56
+ before_all do
57
+ # called exactly once before all files are rendered
58
+ end
59
+
60
+ after_all do
61
+ # called exactly once after all files are rendered
62
+ end
63
+
51
64
  Blog
52
65
  ====
53
66
 
@@ -60,13 +73,13 @@ In your controller:
60
73
  :title => 'Blog Title',
61
74
  :desc => 'Blog Description for RSS feed')
62
75
 
63
- Post files should be in the format `yyyy-mm-dd-permalink.html.md`. Use
64
- YAML front-matter for any metadata you want. `:date` and `:path` will be
65
- added automatically for you.
76
+ Post files should be in the format `yyyy-mm-dd-permalink.*`. Use YAML
77
+ front-matter for any metadata you want. `:date` and `:path` will be added
78
+ automatically for you.
66
79
 
67
- <!--
68
- :title: Title Goes Here
69
- -->
80
+ ---
81
+ :title: Title Goes Here
82
+ ---
70
83
 
71
84
  You'll have to create your own `blog/index.html`. Use the `blog_posts` helper
72
85
  to construct it:
@@ -80,9 +93,10 @@ Front-Matter YAML
80
93
 
81
94
  In your views:
82
95
 
83
- #!
84
- :title: Lorem Ipsum
85
- :desc: Quick fox over lazy dog.
96
+ ---
97
+ :title: Lorem Ipsum
98
+ :desc: Quick fox over lazy dog.
99
+ ---
86
100
  %div
87
101
  Page continues as normal here
88
102
  %h1= front[:title]
@@ -91,20 +105,14 @@ In your views:
91
105
  Note the 2-space indentation is required. This works for HTML, Markdown, and
92
106
  ERB comments as well:
93
107
 
94
- <!--
95
- :title: Lorem Ipsum
96
- -->
108
+ ---
109
+ :title: Lorem Ipsum
110
+ ---
97
111
  Continue as normal
98
112
 
99
113
  You can configure which files to check in `controller.rb`. Here's the default:
100
114
 
101
- Homeostasis::Front.config(
102
- :matchers => {
103
- 'erb' => /<%#/,
104
- 'haml' => /-#/,
105
- 'html' => /<!--/,
106
- 'md' => /<!--/
107
- })
115
+ Homeostasis::Front.config(:matcher => /\.erb|\.haml|\.html|\.md$/)
108
116
 
109
117
  Just start the file with YAML inside a comment with 2-space indentation. The
110
118
  data will be available from the `front` method in your views and controller.
@@ -114,6 +122,19 @@ cross-page access.
114
122
  Note that `:path` is automatically assigned if left blank. Its value will be
115
123
  the public path to the page.
116
124
 
125
+ Multi Templates
126
+ ===============
127
+
128
+ Use multiple file extensions and have Tilt automatically process them. The
129
+ filename `example.html.md.erb` will first run through an ERB processor, then
130
+ a Markdown processor:
131
+
132
+ # Sample Markdown <%= 1 + 2 %>
133
+
134
+ Will become:
135
+
136
+ <h1>Sample Markdown 3</h1>
137
+
117
138
  Sitemap
118
139
  =======
119
140
 
@@ -128,16 +149,16 @@ to set the root URL for this to happen:
128
149
  `loc` and `lastmod` will be generated for each page. Use front-yaml to set the
129
150
  `changefreq` or `priority`:
130
151
 
131
- <!--
132
- :changefreq: monthly
133
- :priority: 0.9
134
- -->
152
+ ---
153
+ :changefreq: monthly
154
+ :priority: 0.9
155
+ ---
135
156
 
136
157
  Use the key `private` to avoid generating an entry:
137
158
 
138
- <!--
139
- :private: true
140
- -->
159
+ ---
160
+ :private: true
161
+ ---
141
162
 
142
163
  Trailing Slash
143
164
  ==============
@@ -161,7 +182,7 @@ slashes to URLs.
161
182
  TODO
162
183
  ====
163
184
 
164
- * make each plugin optional
185
+ * setup homeostasis website
165
186
 
166
187
  License
167
188
  =======
data/lib/homeostasis.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require 'rubygems'
2
2
  require 'stasis'
3
3
  require 'digest/sha1'
4
- require 'bluecloth'
5
4
  require 'yaml'
6
5
  require 'cgi'
7
6
  require 'uri'
7
+ require 'tilt'
8
+ require 'tempfile'
9
+ require 'preamble'
8
10
 
9
11
  module Homeostasis
10
12
  module Helpers
@@ -26,6 +28,27 @@ module Homeostasis
26
28
  def h(html)
27
29
  CGI::escapeHTML(html)
28
30
  end
31
+
32
+ def render_multi(path, body = nil, context = nil, locals = {})
33
+ body ||= File.read(path)
34
+ render_templates_for(path).each do |template|
35
+ blk = proc { body }
36
+ body = template.new(path, &blk).render(context, locals)
37
+ end
38
+ body
39
+ end
40
+
41
+ def render_templates_for(path)
42
+ File.basename(path).split('.')[1..-1].reverse.map { |ext| Tilt[ext] }.compact
43
+ end
44
+
45
+ def self.stasis_path
46
+ @@stasis_path
47
+ end
48
+
49
+ def self.set_stasis_path(stasis, path)
50
+ @@stasis_path = path if path =~ /^#{stasis.root}/ || path.nil?
51
+ end
29
52
  end
30
53
 
31
54
  class Asset < Stasis::Plugin
@@ -145,41 +168,59 @@ module Homeostasis
145
168
  private
146
169
  end
147
170
 
171
+ class Event < Stasis::Plugin
172
+ before_all :before_all_send
173
+ after_all :after_all_send
174
+ controller_method :before_all
175
+ controller_method :after_all
176
+ reset :reset
177
+
178
+ def initialize(stasis)
179
+ @stasis = stasis
180
+ reset
181
+ end
182
+
183
+ def before_all(&block)
184
+ @befores << block
185
+ end
186
+
187
+ def after_all(&block)
188
+ @afters << block
189
+ end
190
+
191
+ def before_all_send
192
+ @befores.each { |b| @stasis.action.instance_eval(&b) }
193
+ end
194
+
195
+ def after_all_send
196
+ @afters.each { |b| @stasis.action.instance_eval(&b) }
197
+ end
198
+
199
+ def reset
200
+ @befores = []
201
+ @afters = []
202
+ end
203
+ end
204
+
148
205
  class Front < Stasis::Plugin
149
206
  include Helpers
150
207
  before_all :before_all
208
+ before_render :before_render
209
+ after_render :after_render
151
210
  action_method :front
152
211
  action_method :front_site
212
+ priority 2
153
213
 
154
214
  def initialize(stasis)
155
215
  @stasis = stasis
156
216
  @@front_site = {}
157
- @@matchers = {
158
- 'erb' => /<%#/,
159
- 'haml' => /-#/,
160
- 'html' => /<!--/,
161
- 'md' => /<!--/
162
- }
217
+ @@matcher = /\.erb|\.haml|\.html|\.md$/
163
218
  end
164
219
 
165
220
  def before_all
166
221
  @stasis.paths.each do |path|
167
- next if path !~ /\.(#{@@matchers.keys.join('|')})$/
168
- if (contents = File.read(path)) !~ @@matchers[File.extname(path)[1..-1]]
169
- yaml = {}
170
- else
171
- lines, data, index = contents.split("\n"), "", 1
172
- while index < lines.size
173
- break if lines[index] !~ /^ /
174
- data += lines[index] + "\n"
175
- index += 1
176
- end
177
- begin
178
- yaml = YAML.load(data)
179
- rescue Psych::SyntaxError
180
- yaml = {}
181
- end
182
- end
222
+ yaml, body = Front.preamble_load(path)
223
+ next if yaml.nil?
183
224
 
184
225
  # add special :path key for generated files
185
226
  if !ignore?(path)
@@ -194,8 +235,29 @@ module Homeostasis
194
235
  end
195
236
  end
196
237
 
238
+ def before_render
239
+ if @stasis.path && @stasis.path =~ @@matcher && !ignore?(@stasis.path)
240
+ ext = File.basename(@stasis.path).split('.', 2).last
241
+ yaml, body = Front.preamble_load(@stasis.path)
242
+ @tmpfile = Tempfile.new(['temp', ".#{ext}"])
243
+ @tmpfile.puts(body)
244
+ @tmpfile.close
245
+ Helpers.set_stasis_path(@stasis, @stasis.path)
246
+ @stasis.path = @tmpfile.path
247
+ end
248
+ end
249
+
250
+ def after_render
251
+ if @tmpfile
252
+ @stasis.path = Helpers.stasis_path if @stasis.path !~ /^#{@stasis.root}/
253
+ Helpers.set_stasis_path(@stasis, nil)
254
+ @tmpfile.unlink
255
+ @tmpfile = nil
256
+ end
257
+ end
258
+
197
259
  def front
198
- @@front_site[front_key(@stasis.path)] || {}
260
+ @@front_site[front_key(Helpers.stasis_path || @stasis.path)] || {}
199
261
  end
200
262
 
201
263
  def front_site
@@ -207,7 +269,14 @@ module Homeostasis
207
269
  end
208
270
 
209
271
  def self.config(options)
210
- @@matchers = options[:matchers] if options[:matchers]
272
+ @@matcher = options[:matcher] if options[:matcher]
273
+ end
274
+
275
+ def self.preamble_load(path)
276
+ return nil if path.nil? || path !~ @@matcher
277
+ Preamble.load(path)
278
+ rescue
279
+ [{}, File.read(path)]
211
280
  end
212
281
 
213
282
  private
@@ -229,6 +298,56 @@ module Homeostasis
229
298
  end
230
299
  end
231
300
 
301
+ class Multi < Stasis::Plugin
302
+ include Helpers
303
+ before_render :before_render
304
+ after_render :after_render
305
+ after_write :after_write
306
+ priority 1
307
+
308
+ def initialize(stasis)
309
+ @stasis = stasis
310
+ end
311
+
312
+ def before_render
313
+ if @stasis.path && !ignore?(@stasis.path)
314
+ exts = File.basename(@stasis.path).split('.')[2..-1]
315
+ return if exts.nil? || exts.length < 2
316
+
317
+ yaml, body = Front.preamble_load(@stasis.path)
318
+ body ||= File.read(@stasis.path)
319
+
320
+ @tmpfile = Tempfile.new(["temp", ".txt"])
321
+ @tmpfile.puts(render_multi(@stasis.path, body))
322
+ @tmpfile.close
323
+ Helpers.set_stasis_path(@stasis, @stasis.path)
324
+ @stasis.path = @tmpfile.path
325
+ end
326
+ end
327
+
328
+ def after_render
329
+ if @tmpfile
330
+ @stasis.path = Helpers.stasis_path if @stasis.path !~ /^#{@stasis.root}/
331
+ Helpers.set_stasis_path(@stasis, nil)
332
+ @tmpfile.unlink if @tmpfile
333
+ @tmpfile = nil
334
+ end
335
+ end
336
+
337
+ def after_write
338
+ return if @stasis.path.nil? || ignore?(@stasis.path)
339
+ dirname = File.dirname(@stasis.dest)
340
+ basename = File.basename(@stasis.dest)
341
+ exts = basename.split('.')[2..-1]
342
+
343
+ return if exts.nil? || exts.length < 1
344
+ exts.each do |ext|
345
+ basename = basename.sub(/\.#{ext}/, '')
346
+ end
347
+ File.rename(@stasis.dest, File.join(dirname, basename))
348
+ end
349
+ end
350
+
232
351
  class Trail < Stasis::Plugin
233
352
  after_all :after_all
234
353
 
@@ -300,6 +419,7 @@ module Homeostasis
300
419
  before_all :before_all
301
420
  after_all :after_all
302
421
  action_method :blog_posts
422
+ priority 3
303
423
 
304
424
  def initialize(stasis)
305
425
  @stasis = stasis
@@ -331,7 +451,7 @@ module Homeostasis
331
451
  post[:path] = post[:path].sub(
332
452
  "/#{@@directory}/#{date}-",
333
453
  File.join('/', @@path, '/'))
334
- post[:body] = BlueCloth.new(File.read(filename)).to_html if filename =~ /\.md$/
454
+ post[:body] = render_multi(filename, File.read(filename))
335
455
  @@posts << post
336
456
  end
337
457
  @@posts = @@posts.sort_by { |post| post[:date] }.reverse
@@ -353,7 +473,7 @@ module Homeostasis
353
473
  rss += " <title>#{h @@title}</title>\n" if @@title
354
474
  rss += " <link>#{h @@url}/</link>\n" if @@url
355
475
  rss += " <description>#{h @@desc}</description>\n" if @@desc
356
- blog_posts[0..5].each do |post|
476
+ blog_posts.reject {|p| p.has_key?(:norss) }[0..5].each do |post|
357
477
  body = post[:body]
358
478
  body.gsub!(/(href|src)=('|")\//, "\\1=\\2#{@@url}/")
359
479
  rss += " <item>\n"
@@ -365,7 +485,7 @@ module Homeostasis
365
485
  end
366
486
  rss += " </channel>\n"
367
487
  rss += "</rss>"
368
- File.open(File.join(blog_dest, @@path, 'rss.xml'), 'w') do |f|
488
+ File.open(File.join(blog_dest, 'rss.xml'), 'w') do |f|
369
489
  f.puts(rss)
370
490
  end
371
491
  end
@@ -379,7 +499,9 @@ end
379
499
 
380
500
  if !ENV['HOMEOSTASIS_UNREGISTER']
381
501
  Stasis.register(Homeostasis::Asset)
502
+ Stasis.register(Homeostasis::Event)
382
503
  Stasis.register(Homeostasis::Front)
504
+ Stasis.register(Homeostasis::Multi)
383
505
  Stasis.register(Homeostasis::Trail)
384
506
  Stasis.register(Homeostasis::Sitemap)
385
507
  Stasis.register(Homeostasis::Blog)
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Homeostasis
2
- VERSION = '0.0.15'
2
+ VERSION = '0.0.16'
3
3
  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.15
4
+ version: 0.0.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,88 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-27 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: stasis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: preamble
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: tilt
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bluecloth
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: haml
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
14
94
  description: Provides asset stamping using git revisions, environments, and a few
15
95
  view helpers.
16
96
  email: