to_erb 0.0.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/AUTHORS ADDED
@@ -0,0 +1,4 @@
1
+ to_erb de tario <darios3@gmail.com> and mislav (Mislav Marohnić)
2
+ - Mislav Marohnić (ErbEngine solution, see erb_engine.rb for more info)
3
+ - Dario Seminara (Gem packaging, hack for haml attributes conversion)
4
+
data/CHANGELOG ADDED
File without changes
data/README ADDED
@@ -0,0 +1,32 @@
1
+ = to_erb: simple haml2erb conversion tool
2
+
3
+ Searching the web for a tool or method to convert haml views to erb I have found a gist on github (https://gist.github.com/17371) with the solution consiting on a hack using the haml engine itself to generate the erb code, For practicality, I have decided to use that snippet in a packaged gem with a command line tool plus a few additional improvements...
4
+ Important part of the credit must be for the creator of the gist.
5
+
6
+ == Installation
7
+
8
+ The install is as simple as execute the well-known gem install:
9
+
10
+ sudo gem install to_erb
11
+
12
+ == Known Limitations & Issues
13
+
14
+ * Does not support haml helpers (you should fix that by hand after conversion)
15
+ * Strings on tag attributes are not escaped
16
+
17
+ == Usage
18
+
19
+ Convert a haml view to erb
20
+
21
+ to_erb app/views/products/index.haml
22
+
23
+ Convert all haml views under app/views directory
24
+
25
+ to_erb app/views
26
+
27
+ NOTE: Remember to remove the haml views after conversion!!
28
+
29
+ == Copying
30
+
31
+ Copyright (c) 2012 Dario Seminara, released under the GPL License (see LICENSE)
32
+
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'to_erb'
8
+ s.version = '0.0.1'
9
+ s.author = 'Dario Seminara'
10
+ s.email = 'robertodarioseminara@gmail.com'
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = 'Haml to erb conversion tool'
13
+ s.add_dependency "haml"
14
+ s.add_dependency "ruby2ruby"
15
+ s.add_dependency "ruby_parser"
16
+ s.homepage = "http://github.com/tario/to_erb"
17
+ s.executables = ["to_erb"]
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = [ 'README' ]
20
+ s.files = Dir.glob("{lib,bin}/**/*") +[ 'AUTHORS', 'README', 'Rakefile', 'CHANGELOG' ]
21
+ end
22
+
23
+ desc 'Generate RDoc'
24
+ Rake::RDocTask.new :rdoc do |rd|
25
+ rd.rdoc_dir = 'doc'
26
+ rd.rdoc_files.add 'lib', 'README'
27
+ rd.main = 'README'
28
+ end
29
+
30
+ desc 'Build Gem'
31
+ Rake::GemPackageTask.new spec do |pkg|
32
+ pkg.need_tar = true
33
+ end
34
+
35
+ desc 'Clean up'
36
+ task :clean => [ :clobber_rdoc, :clobber_package ]
37
+
38
+ desc 'Clean up'
39
+ task :clobber => [ :clean ]
40
+
data/bin/to_erb ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ require "to_erb"
3
+
4
+ USAGE_HELP=<<END
5
+ Usage: to_erb [viewspath]
6
+
7
+ Example
8
+ Convert app/views/products/show.haml to app/views/products/show.erb
9
+
10
+ to_erb app/views/products/show.haml
11
+
12
+ Convert all views under app/views/products/ to erb
13
+
14
+ to_erb app/views/products/
15
+
16
+ END
17
+
18
+ if ARGV.size == 0
19
+
20
+ print USAGE_HELP
21
+
22
+ else
23
+
24
+ if ARGV[0].match(/\.haml$/)
25
+ file = ARGV[0]
26
+ ToErb.convert(file,file.gsub(/\.haml$/,".erb"))
27
+ else
28
+ Dir[ARGV[0]+"/**/*.haml"].each do |file|
29
+ ToErb.convert(file,file.gsub(/\.haml$/,".erb"))
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,63 @@
1
+ # Fork of https://gist.github.com/17371
2
+ require "haml"
3
+ require "ruby_parser"
4
+ require "ruby2ruby"
5
+
6
+ module Haml
7
+ class Buffer
8
+ def extended_attributes(class_id, obj_ref, *attributes_hashes)
9
+ attributes = class_id
10
+ attributes_hashes.each do |old|
11
+ self.class.merge_attrs(attributes, to_hash(old.map {|k, v| [k.to_s, v]}))
12
+ end
13
+
14
+ self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref
15
+
16
+ str_attrs = []
17
+ attributes.each do |k,v|
18
+ value = if Sexp === v
19
+ "'<%= #{Ruby2Ruby.new.process v} %>'"
20
+ else
21
+ v.gsub("'","\\'")
22
+ end
23
+
24
+ str_attrs << "#{k}=#{value}"
25
+ end
26
+
27
+ str_attrs.join(" ")
28
+ end
29
+ end
30
+ end
31
+
32
+ class ErbEngine < Haml::Engine
33
+ def initialize(*args)
34
+ @rubyparser = RubyParser.new
35
+ super(*args)
36
+ end
37
+
38
+ def push_script(text, preserve_script, in_tag = false, preserve_tag = false,
39
+ escape_html = false, nuke_inner_whitespace = false)
40
+ push_text "<%= #{text.strip} %>"
41
+ end
42
+
43
+ def push_silent(text, can_suppress = false)
44
+ push_text "<% #{text.strip} %>"
45
+ end
46
+
47
+ def push_generated_script(text)
48
+ sexp = @rubyparser.parse text
49
+ if sexp.node_type == :call and sexp[2] == :attributes
50
+ sexp[2] = :extended_attributes
51
+ arglist = sexp[3]
52
+ last_hash_arg = arglist[3]
53
+
54
+ (1..(last_hash_arg.size-1)/2).each do |i|
55
+ last_hash_arg[i*2] = @rubyparser.parse(last_hash_arg[i*2].to_s)
56
+ end
57
+ text = Ruby2Ruby.new.process sexp
58
+ end
59
+
60
+ super(text)
61
+ end
62
+ end
63
+
@@ -0,0 +1,3 @@
1
+ module ToErb
2
+ VERSION = "0.0.1"
3
+ end
data/lib/to_erb.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "to_erb/version"
2
+ require "to_erb/erb_engine"
3
+
4
+ module ToErb
5
+ def self.convert(infile,outfile)
6
+
7
+
8
+ hamlcode = File.open(infile).read
9
+ engine = ErbEngine.new(hamlcode)
10
+ File.open(outfile,"w") do |file|
11
+ file.write engine.render
12
+ end
13
+
14
+ print "converted #{infile} to #{outfile}\n"
15
+ rescue Exception => e
16
+ print "FAILED conersion of #{infile} due error #{e}"
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_erb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dario Seminara
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-02 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: haml
16
+ requirement: &71196980 !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: *71196980
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby2ruby
27
+ requirement: &71196720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *71196720
36
+ - !ruby/object:Gem::Dependency
37
+ name: ruby_parser
38
+ requirement: &71196430 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *71196430
47
+ description:
48
+ email: robertodarioseminara@gmail.com
49
+ executables:
50
+ - to_erb
51
+ extensions: []
52
+ extra_rdoc_files:
53
+ - README
54
+ files:
55
+ - lib/to_erb/erb_engine.rb
56
+ - lib/to_erb/version.rb
57
+ - lib/to_erb.rb
58
+ - bin/to_erb
59
+ - AUTHORS
60
+ - README
61
+ - Rakefile
62
+ - CHANGELOG
63
+ homepage: http://github.com/tario/to_erb
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.10
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Haml to erb conversion tool
87
+ test_files: []