homeostasis 0.0.7 → 0.0.8

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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  Description
2
2
  ===========
3
3
 
4
- Stasis plugin for asset stamping with git revisions, yaml front-matter,
5
- environment branching, and uri helpers.
4
+ Stasis plugin for asset stamping, blogs, front-matter yaml, and trailing
5
+ slashes.
6
6
 
7
7
  Installation
8
8
  ============
@@ -12,12 +12,9 @@ Installation
12
12
  In your controller:
13
13
 
14
14
  require 'rubygems'
15
- require 'homeostasis/asset' # for asset stamping
16
- require 'homeostasis/front' # for yaml front-matter
17
- require 'homeostasis/env' # for environment handler
18
- require 'homeostasis/path' # for path helpers
15
+ require 'homeostasis'
19
16
 
20
- Each component is optional.
17
+ This requires the current directory to be under `git` version control.
21
18
 
22
19
  Asset Stamping
23
20
  ==============
@@ -37,13 +34,8 @@ You'll end up with something like this:
37
34
  script.sha1.js
38
35
  styles.sha1.css
39
36
 
40
- In your views, use the `asset_path` helper:
41
-
42
- %img{:src => asset_path('background.jpg')}
43
-
44
- For CSS files, I use the extension `.erb` like `styles.css.erb`:
45
-
46
- background: url(<%= asset_path('background.jpg') %>);
37
+ Generated files in the `public` directory will go through a global search and
38
+ replace.
47
39
 
48
40
  You can set the regex for asset matching in your controller:
49
41
 
@@ -51,30 +43,35 @@ You can set the regex for asset matching in your controller:
51
43
 
52
44
  You can even concat your assets into a single file:
53
45
 
54
- # in controller.rb
55
46
  Homeostasis::Asset.concat 'all.js', %w(jquery.js mine.js)
56
47
  Homeostasis::Asset.concat 'all.css', %w(reset.css mine.css)
57
48
 
58
- # in views
59
- %link{:href => asset_path('all.css')}
60
- %script{:src => asset_path('all.js')}
49
+ Blog
50
+ ====
61
51
 
62
- Environment Handler
63
- ===================
52
+ In your controller:
64
53
 
65
- The environment handler just adds a variable:
54
+ Homeostasis::Blog.directory('blog') # directory of posts
66
55
 
67
- Homeostasis::ENV
56
+ Post files should be in the format `yyyy-mm-dd-permalink.html.{md,haml}`. Use
57
+ YAML front-matter for any metadata you want. `:date` and `:path` will be
58
+ added automatically for you.
59
+
60
+ <!--
61
+ :title: Title Goes Here
62
+ -->
68
63
 
69
- It's set to whatever `HOMEOSTASIS_ENV` or `'development'` by default. You
70
- can use this to branch in your view:
64
+ You'll have to create your own `blog/index.html`. Use the `blog_posts` helper
65
+ to construct it:
71
66
 
72
- = Homeostasis::ENV.development? ? 'local.js' : 'production.js'
67
+ - blog_posts.each do |post|
68
+ %span.date post[:date].strftime("%m/%d/%Y")
69
+ %a{:href => post[:path]}= post[:title]
73
70
 
74
- YAML Front-matter
71
+ Front-Matter YAML
75
72
  =================
76
73
 
77
- This adds YAML front-matter support:
74
+ In your views:
78
75
 
79
76
  #!
80
77
  :title: Lorem Ipsum
@@ -106,17 +103,27 @@ data will be available from the `front` method in your views and controller.
106
103
  There's also a `front_site` helper which contains the data for all pages for
107
104
  cross-page access.
108
105
 
109
- Path Helper
110
- ===========
106
+ Note that `:path` is automatically assigned if left blank. Its value will be
107
+ the public path to the page.
108
+
109
+ Trailing Slash
110
+ ==============
111
111
 
112
- The path helper uses the environment handler. It just adds the view helper
113
- `path` which returns differently depending on the environment:
112
+ This turns every page into a directory with an `index.html` file. So instead
113
+ of:
114
114
 
115
- path('/blog') # development => '/blog.html'
116
- path('/blog') # production => '/blog/'
115
+ index.html
116
+ blog.html
117
+ about.html
118
+
119
+ You'll get:
120
+
121
+ index.html
122
+ blog/index.html
123
+ about/index.html
117
124
 
118
- This goes along well with an `htaccess` file that drops the `.html` extensions
119
- from requests and adds trailing slashes.
125
+ This works well with an `htaccess` file that automatically appends trailing
126
+ slashes to URLs.
120
127
 
121
128
  License
122
129
  =======
@@ -2,9 +2,8 @@ require File.join(File.dirname(__FILE__), '..', 'homeostasis')
2
2
  require 'digest/sha1'
3
3
 
4
4
  class Homeostasis::Asset < Stasis::Plugin
5
- before_all :before_all
6
- after_all :after_all
7
- action_method :asset_path
5
+ before_all :before_all
6
+ after_all :after_all
8
7
 
9
8
  def initialize(stasis)
10
9
  @stasis = stasis
@@ -36,6 +35,7 @@ class Homeostasis::Asset < Stasis::Plugin
36
35
  end
37
36
 
38
37
  def after_all
38
+ asset_paths = {}
39
39
  @@concats.each do |concatted, files|
40
40
  version = self.class.version(files)
41
41
  full_concatted = File.join(@stasis.destination, concatted)
@@ -50,25 +50,32 @@ class Homeostasis::Asset < Stasis::Plugin
50
50
  file_contents
51
51
  end.join("\n")
52
52
  File.open(full_concatted, 'w') {|f| f.print(content)}
53
+ asset_paths[concatted] = full_concatted[(@stasis.destination.length+1)..-1]
53
54
  end
54
55
  @@mapping.each do |orig, dest|
55
56
  next if dest !~ @@matcher
56
57
  full_orig = File.join(@stasis.root, orig)
57
58
  full_dest = File.join(@stasis.destination, dest)
58
- version = self.class.version(full_orig)
59
- File.rename(full_dest, self.class.stamped(full_dest, version))
60
- end
61
- end
59
+ versioned = self.class.stamped(full_dest, self.class.version(full_orig))
60
+ File.rename(full_dest, versioned)
62
61
 
63
- def asset_path(path)
64
- dest = path =~ /^\// ? path[1..-1] : path
65
- if (concats = self.class.concats[dest])
66
- self.class.stamped(path, self.class.version(concats))
67
- else
68
- orig = self.class.mapping.invert[dest]
69
- raise "Asset not found: #{dest}" if orig.nil?
70
- full_orig = File.join(@stasis.root, orig)
71
- self.class.stamped(path, self.class.version(full_orig))
62
+ relative_dest = full_dest[(@stasis.destination.length+1)..-1]
63
+ relative_versioned = versioned[(@stasis.destination.length+1)..-1]
64
+ asset_paths[relative_dest] = relative_versioned
65
+ end
66
+ (@@mapping.values + asset_paths.values).each do |dest|
67
+ filename = File.join(@stasis.destination, dest)
68
+ next if !File.exist?(filename)
69
+ contents = File.read(filename)
70
+ begin
71
+ asset_paths.each do |old, new|
72
+ contents.gsub!(/([^a-zA-Z0-9\.\-_])#{Regexp.escape(old)}/, "\\1#{new}")
73
+ contents.gsub!(/^#{Regexp.escape(old)}/, new)
74
+ end
75
+ File.open(filename, 'w') {|f| f.print(contents)}
76
+ rescue ArgumentError
77
+ next
78
+ end
72
79
  end
73
80
  end
74
81
 
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'homeostasis')
2
+ require 'date'
3
+
4
+ class Homeostasis::Blog < Stasis::Plugin
5
+ after_all :after_all
6
+ action_method :blog_posts
7
+
8
+ def initialize(stasis)
9
+ @stasis = stasis
10
+ @@directory = nil
11
+ end
12
+
13
+ def self.directory(directory)
14
+ @@directory = directory
15
+ end
16
+
17
+ def blog_posts
18
+ blog_dir = File.join(@stasis.root, @@directory)
19
+ posts = []
20
+ front_site = Homeostasis::Front._front_site
21
+ Dir.glob("#{blog_dir}/*").each do |filename|
22
+ next if File.basename(filename) !~ /^(\d{4}-\d{2}-\d{2})-/
23
+ date = $1
24
+ post = front_site[filename.sub(@stasis.root, '')[1..-1]] || {}
25
+ post[:date] = Date.parse(date)
26
+ post[:path] = post[:path].sub("/#{@@directory}/#{$1}-", "/#{@@directory}/")
27
+ posts << post
28
+ end
29
+ posts.sort_by {|post| post[:date]}.reverse
30
+ end
31
+
32
+ def after_all
33
+ raise 'Homeostasis::Blog#directory never set' if @@directory.nil?
34
+ blog_dir = File.join(@stasis.destination, @@directory)
35
+ Dir.glob("#{blog_dir}/*").each do |filename|
36
+ next if filename !~ /^\d{4}-\d{2}-\d{2}-/
37
+ newbase = File.basename(filename).sub(/^(\d{4}-\d{2}-\d{2})-/, '')
38
+ FileUtils.mv(filename, File.join(File.dirname(filename), newbase))
39
+ end
40
+ end
41
+ end
42
+
43
+ Stasis.register(Homeostasis::Blog)
@@ -8,7 +8,7 @@ class Homeostasis::Front < Stasis::Plugin
8
8
 
9
9
  def initialize(stasis)
10
10
  @stasis = stasis
11
- @front_site = {}
11
+ @@front_site = {}
12
12
  @@matchers = {
13
13
  'erb' => /<%#/,
14
14
  'haml' => /-#/,
@@ -29,22 +29,34 @@ class Homeostasis::Front < Stasis::Plugin
29
29
  data += lines[index] + "\n"
30
30
  index += 1
31
31
  end
32
+
33
+ relative = path[(@stasis.root.length+1)..-1]
34
+ ext = Tilt.mappings.keys.find { |ext| File.extname(path)[1..-1] == ext }
35
+ dest = (ext && File.extname(relative) == ".#{ext}") ?
36
+ relative[0..-1*ext.length-2] :
37
+ relative
38
+ dest = trailify(dest)
32
39
 
33
40
  begin
34
41
  yaml = YAML.load(data)
35
- @front_site[front_key(path)] = yaml if yaml.is_a?(Hash)
42
+ yaml[:path] = dest
43
+ @@front_site[front_key(path)] = yaml if yaml.is_a?(Hash)
36
44
  rescue Psych::SyntaxError => error
37
- next
45
+ @@front_site[front_key(path)] = {:path => dest}
38
46
  end
39
47
  end
40
48
  end
41
49
 
42
50
  def front
43
- @front_site[front_key(@stasis.path)] || {}
51
+ @@front_site[front_key(@stasis.path)] || {}
44
52
  end
45
53
 
46
54
  def front_site
47
- @front_site
55
+ @@front_site
56
+ end
57
+
58
+ def self._front_site # for other plugins
59
+ @@front_site
48
60
  end
49
61
 
50
62
  def self.matchers
@@ -57,7 +69,20 @@ class Homeostasis::Front < Stasis::Plugin
57
69
 
58
70
  private
59
71
  def front_key(filename)
60
- filename.sub(Dir.pwd, '')[1..-1].sub(/\.[^.]+$/, '')
72
+ filename.sub(@stasis.root, '')[1..-1]
73
+ end
74
+
75
+ def trailify(filename)
76
+ @trail_included ||= @stasis.plugins.any? {|plugin| plugin.is_a?(Homeostasis::Trail)}
77
+ if filename == 'index.html'
78
+ '/'
79
+ elsif File.basename(filename) == 'index.html'
80
+ "/#{File.dirname(filename)}/"
81
+ elsif @trail_included
82
+ "/#{filename.sub(/\.html$/, '/')}"
83
+ else
84
+ "/#{filename}"
85
+ end
61
86
  end
62
87
  end
63
88
 
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'homeostasis')
2
+
3
+ class Homeostasis::Trail < Stasis::Plugin
4
+ after_all :after_all
5
+
6
+ def initialize(stasis)
7
+ @stasis = stasis
8
+ end
9
+
10
+ def after_all
11
+ Dir.glob("#{@stasis.destination}/**/*.html").each do |filename|
12
+ next if File.basename(filename) == 'index.html'
13
+ dir = "#{filename[0..-6]}/"
14
+ if File.exists?("#{dir}index.html")
15
+ puts "Unable to trail #{filename[(@stasis.destination.length+1)..-1]}"
16
+ else
17
+ FileUtils.mkdir_p(dir)
18
+ File.rename(filename, "#{dir}index.html")
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Stasis.register(Homeostasis::Trail)
data/lib/homeostasis.rb CHANGED
@@ -1,3 +1,10 @@
1
1
  module Homeostasis
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
+ end
4
+
5
+ if !ENV['HOMEOSTASIS_BUILD']
6
+ require File.join(File.dirname(__FILE__), 'homeostasis', 'asset')
7
+ require File.join(File.dirname(__FILE__), 'homeostasis', 'front')
8
+ require File.join(File.dirname(__FILE__), 'homeostasis', 'trail')
9
+ require File.join(File.dirname(__FILE__), 'homeostasis', 'blog')
3
10
  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.7
4
+ version: 0.0.8
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-07-28 00:00:00.000000000 Z
12
+ date: 2012-08-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides asset stamping using git revisions, environments, and a few
15
15
  view helpers.
@@ -21,9 +21,9 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - LICENSE.md
23
23
  - README.md
24
- - lib/homeostasis/env.rb
24
+ - lib/homeostasis/trail.rb
25
+ - lib/homeostasis/blog.rb
25
26
  - lib/homeostasis/front.rb
26
- - lib/homeostasis/path.rb
27
27
  - lib/homeostasis/asset.rb
28
28
  - lib/homeostasis.rb
29
29
  homepage: https://github.com/hughbien/homeostasis
@@ -1,19 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'homeostasis')
2
-
3
- module Homeostasis
4
- class Environment
5
- def initialize(env)
6
- @env = env
7
- end
8
-
9
- def method_missing(message, *args, &block)
10
- if message =~ /\?$/
11
- @env == message[0..-2]
12
- else
13
- super
14
- end
15
- end
16
- end
17
-
18
- ENV = Environment.new(::ENV['HOMEOSTASIS_ENV'] || 'development')
19
- end
@@ -1,18 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'env')
2
-
3
- class Homeostasis::Path < Stasis::Plugin
4
- action_method :path
5
-
6
- def initialize(stasis)
7
- @stasis = stasis
8
- end
9
-
10
- def path(uri)
11
- uri = uri[0..-2] if uri =~ /\/$/
12
- Homeostasis::ENV.development? ?
13
- "#{uri}.html" :
14
- "#{uri}/"
15
- end
16
- end
17
-
18
- Stasis.register(Homeostasis::Path)