guard-haml2erb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "http://rubygems.org"
3
+
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2011 by Immanuel Häussermann
2
+ Haml2Erb adaptations Copyright (C) 2012 Laurence A. Lee
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ # Guard::Haml2Erb
2
+
3
+ Automatically convert HAML views to ERB via the Haml2Erb Gem.
4
+
5
+ Adapted from Guard-HAML at https://github.com/guard/guard-haml
6
+
7
+ ## Install
8
+
9
+ As the gem name suggests this is a guard extension. Make sure you get [guard](https://github.com/guard/guard) first.
10
+
11
+ You will also need a copy of Haml2Erb, which can be obtained here: [https://github.com/elia/haml2erb](https://github.com/elia/haml2erb)
12
+
13
+ Simply add it to your Gemfile:
14
+
15
+ gem 'haml2erb', :git=>'https://github.com/elia/haml2erb.git'
16
+
17
+ Now, Install the gem:
18
+
19
+ gem install guard-haml2erb
20
+
21
+ Add it to your Gemfile if you're using bundler (you should)
22
+
23
+ gem 'guard-haml2erb'
24
+
25
+ Add a basic guard setup:
26
+
27
+ guard init haml2erb
28
+
29
+ ## Options
30
+
31
+ If you want to change the output directory use the `output` option in your
32
+ Guardfile, e.g.:
33
+
34
+ guard 'haml2erb', :output => 'public' do
35
+ watch %r{^src/.+(\.html\.haml)}
36
+ end
37
+
38
+ This output is relative to the Guardfile.
39
+
40
+ If you maintain your haml files in a directory that should not be part of the output path, you can set the `input` option, e.g.:
41
+
42
+ guard 'haml2erb', :output => 'public', :input => 'src' do
43
+ watch %r{^src/.+(\.html\.haml)}
44
+ end
45
+
46
+ So when you edit a file `src/partials/_partial.html.haml`
47
+ it will be outputted in `public/partials/_partial.html` without the `src`.
48
+
49
+ ## Development
50
+
51
+ * Source is hosted on [Github: guard-haml2erb](https://github.com/rubyjedi/guard-haml2erb)
52
+ * Report issues/questions/feature requests on the [Github Issue tracker for guard-haml2erb](https://github.com/rubyjedi/guard-haml2erb/issues)
53
+
54
+ Pull requests are welcome.
55
+ Specs are very welcome, make sure you support both ruby 1.8.7 and ruby 1.9.2.
@@ -0,0 +1,68 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+ require 'guard/watcher'
4
+ require 'haml2erb'
5
+
6
+ module Guard
7
+ class Haml2Erb < Guard
8
+
9
+ def initialize(watchers = [], options = {})
10
+ super(watchers, {
11
+ :notifications => true
12
+ }.merge(options))
13
+ @watchers, @options = watchers, options
14
+ end
15
+
16
+ def convert_haml file
17
+ content = File.new(file).read
18
+ begin
19
+ ::Haml2Erb.convert(content)
20
+ rescue StandardError => error
21
+ ::Guard::UI.info "HAML2ERB Error: " + error.message
22
+ end
23
+ end
24
+
25
+ # Get the file path to output the html based on the file being
26
+ # built. The output path is relative to where guard is being run.
27
+ #
28
+ # @param file [String] path to file being built
29
+ # @return [String] path to file where output should be written
30
+ #
31
+ def get_output(file)
32
+ file_dir = File.dirname(file)
33
+ file_name = (File.basename(file).split('.')[0..-2] << 'erb').join('.')
34
+
35
+ file_dir = file_dir.gsub(Regexp.new("#{@options[:input]}(\/){0,1}"), '') if @options[:input]
36
+ file_dir = File.join(@options[:output], file_dir) if @options[:output]
37
+
38
+ if file_dir == ''
39
+ file_name
40
+ else
41
+ File.join(file_dir, file_name)
42
+ end
43
+ end
44
+
45
+ def run_all
46
+ run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.*'))))
47
+ end
48
+
49
+ def run_on_change(paths)
50
+ paths.each do |file|
51
+ output_file = get_output(file)
52
+ FileUtils.mkdir_p File.dirname(output_file)
53
+ File.open(output_file, 'w') { |f| f.write(convert_haml(file)) }
54
+ ::Guard::UI.info "# converted haml in '#{file}' to erb in '#{output_file}'"
55
+ ::Guard::Notifier.notify("# converted haml in #{file}", :title => "Guard::Haml", :image => :success) if @options[:notifications]
56
+ end
57
+ notify paths
58
+ end
59
+
60
+ def notify(changed_files)
61
+ ::Guard.guards.reject{ |guard| guard == self }.each do |guard|
62
+ paths = Watcher.match_files(guard, changed_files)
63
+ guard.run_on_change paths unless paths.empty?
64
+ end
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ guard 'haml2erb' do
2
+ watch(%r{^app/.+\.(haml)})
3
+ end
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ class Haml2ErbVersion
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Guard::Haml2Erb do
4
+ subject { Guard::Haml2Erb.new }
5
+
6
+ describe 'run all' do
7
+ it 'should rebuild all files being watched' do
8
+ Guard::Haml2Erb.stub(:run_on_change).with([]).and_return([])
9
+ Guard.stub(:guards).and_return([subject])
10
+ subject.run_all
11
+ end
12
+ end
13
+
14
+ describe '#get_output' do
15
+ context 'by default' do
16
+ it 'should return test/index.html.haml as test/index.html.erb' do
17
+ subject.get_output('test/index.html.haml').should eq('test/index.html.erb')
18
+ end
19
+
20
+ it 'should return test/index.htm.haml as test/index.htm.erb' do
21
+ subject.get_output('test/index.htm.haml').should eq('test/index.htm.erb')
22
+ end
23
+ end
24
+
25
+ context 'when the output option is set to "demo/output"' do
26
+ before do
27
+ subject.options[:output] = 'demo/output'
28
+ end
29
+
30
+ it 'should return test/index.html.haml as demo/output/test/index.html.erb' do
31
+ subject.get_output('test/index.html.haml').should eq('demo/output/test/index.html.erb')
32
+ end
33
+ end
34
+
35
+ context 'when the exclude_base_dir option is set to "test/ignore"' do
36
+ before do
37
+ subject.options[:input] = 'test/ignore'
38
+ end
39
+
40
+ it 'should return test/ignore/index.html.haml as index.html' do
41
+ subject.get_output('test/ignore/index.html.haml').should eq('index.html.erb')
42
+ end
43
+
44
+ context 'when the output option is set to "demo/output"' do
45
+ before do
46
+ subject.options[:output] = 'demo/output'
47
+ end
48
+
49
+ it 'should return test/ignore/abc/index.html.haml as demo/output/abc/index.html.erb' do
50
+ subject.get_output('test/ignore/abc/index.html.haml').should eq('demo/output/abc/index.html.erb')
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'building haml to erb' do
57
+ it 'should notify other guards upon completion' do
58
+ other_guard = mock('guard')
59
+ other_guard.should_receive(:watchers).and_return([])
60
+ Guard.stub(:guards).and_return([subject, other_guard])
61
+ subject.notify([])
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'guard/haml2erb'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ config.filter_run :focus => true
7
+ config.run_all_when_everything_filtered = true
8
+
9
+ config.before(:each) do
10
+ ENV["GUARD_ENV"] = 'test'
11
+ @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
12
+ @lib_path = Pathname.new(File.expand_path('../../lib/', __FILE__))
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-haml2erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Laurence A. Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard
16
+ requirement: &16177840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *16177840
25
+ - !ruby/object:Gem::Dependency
26
+ name: haml2erb
27
+ requirement: &16177100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.3.0.pre.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *16177100
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &16176540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *16176540
47
+ description: Converts file.html.haml into file.html.erb
48
+ email:
49
+ - rubyjedi@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/guard/haml2erb/templates/Guardfile
55
+ - lib/guard/haml2erb/version.rb
56
+ - lib/guard/haml2erb.rb
57
+ - LICENSE
58
+ - README.md
59
+ - Gemfile
60
+ - spec/guard/haml2erb_spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: ''
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project: guard-haml2erb
82
+ rubygems_version: 1.8.10
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Guard gem for Haml2Erb
86
+ test_files:
87
+ - spec/guard/haml2erb_spec.rb
88
+ - spec/spec_helper.rb