hamlify 1.0.0

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.
Files changed (8) hide show
  1. data/.document +5 -0
  2. data/.gitignore +7 -0
  3. data/LICENSE +20 -0
  4. data/README.md +21 -0
  5. data/Rakefile +56 -0
  6. data/VERSION +1 -0
  7. data/bin/hamlify +171 -0
  8. metadata +71 -0
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ .rake_tasks~
7
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Maxim Chernyak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ hamlify
2
+ ========
3
+
4
+ This tool will convert your html/erb to haml better than html2haml alone.
5
+
6
+ Problems fixed:
7
+
8
+ - IE comments parse correctly.
9
+ - Single-line indentations are moved on the same line as parent.
10
+ - Non-output HAML blocks are fixed. (ie `- end` is removed and block indentation fixed)
11
+
12
+ install
13
+ ---------
14
+
15
+ sudo gem sources -a http://gems.github.com
16
+ sudo gem install maxim-hamlify
17
+
18
+ usage
19
+ ------
20
+
21
+ hamlify path/to/file.html.erb
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hamlify"
8
+ gem.summary = %Q{Wrapper over html2haml to fix some conversion issues.}
9
+ gem.description = %Q{HTML to HAML conversion tool which wraps around standard html2haml and fixes issues with its output.}
10
+ gem.email = "max@bitsonnet.com"
11
+ gem.homepage = "http://github.com/maxim/hamlify"
12
+ gem.authors = ["Maxim Chernyak"]
13
+ gem.add_dependency "haml"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "hamlify #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env ruby
2
+ require 'cgi'
3
+
4
+ DEBUG_MODE = false
5
+
6
+ # REPLACABLE1 START
7
+ def get_indent(line)
8
+ line = line.to_s
9
+ space_areas = line.scan(/^\s+/)
10
+ space_areas.empty? ? 0 : (space_areas.first.size / 2)
11
+ end
12
+
13
+ def block_start?(line)
14
+ block_starters = [/\s+do/, /^-\s+while/, /^-\s+module/, /^-\s+begin/,
15
+ /^-\s+case/, /^-\s+class/, /^-\s+unless/, /^-\s+for/,
16
+ /^-\s+until/, /^-\s*if/]
17
+
18
+ line = line.to_s
19
+ line.strip =~ /^-/ && block_starters.any?{|bs| line.strip =~ bs}
20
+ end
21
+
22
+ def block_end?(line)
23
+ line = line.to_s
24
+ line.strip =~ /^-\send$/
25
+ end
26
+
27
+ def ie_block_start?(line)
28
+ line = line.to_s
29
+ line =~ /\[if/i && line =~ /IE/ && line.strip =~ /\]>$/
30
+ end
31
+
32
+ def ie_block_end?(line)
33
+ line = line.to_s
34
+ line =~ /<!\[endif\]/i
35
+ end
36
+
37
+ def comment_line?(line)
38
+ line = line.to_s
39
+ line.strip =~ /^\//
40
+ end
41
+
42
+ def indent(line, steps = 0)
43
+ line = line.to_s
44
+ exceptions = [/\s+else\W/, /^-\s+elsif/, /^-\s+when/, /^-\s+ensure/, /^-\s+rescue/]
45
+ return if exceptions.any?{|ex| line.strip =~ ex}
46
+
47
+ steps ||= 0
48
+ line = line.to_s
49
+ (" " * steps) + line
50
+ end
51
+
52
+ def alter_lines(lines, altered_lines)
53
+ altered_lines.each do |pair|
54
+ line_number, text = pair
55
+ lines[line_number] = text
56
+ end
57
+ lines
58
+ end
59
+
60
+ def indent_lines(lines, indented_lines)
61
+ indented_lines.each do |pair|
62
+ line_number, indent_by = pair
63
+ lines[line_number] = indent(lines[line_number], indent_by)
64
+ end
65
+ lines
66
+ end
67
+
68
+ def remove_lines(lines, goner_lines)
69
+ goner_lines.each do |i|
70
+ lines[i] = nil
71
+ end
72
+ lines.compact
73
+ end
74
+
75
+ # REPLACABLE1 END
76
+
77
+ origin = ARGV[0]
78
+
79
+ destination = origin.gsub(/\.erb$/, '.haml')
80
+ system "html2haml -rx #{origin} #{destination}"
81
+
82
+ # REPLACABLE2 START
83
+ stack = []
84
+ lines = File.readlines(destination)
85
+ line_number = lines.size
86
+ goner_lines = []
87
+ indented_lines = []
88
+ altered_lines = []
89
+ inside_ie_block = false
90
+ just_passed_ie_block = false
91
+
92
+ lines.reverse_each do |line|
93
+ line_number -= 1
94
+
95
+ debug = Proc.new { |msg|
96
+ puts "line #{line_number + 1} -> \"#{line.strip}\" #{stack.inspect}: #{msg}" if DEBUG_MODE
97
+ }
98
+
99
+ if just_passed_ie_block
100
+ altered_lines << [line_number, line.sub('/', "/" + just_passed_ie_block)]
101
+ debug.call("just passed ie block, altering to #{line.sub('/', "/" + just_passed_ie_block)}")
102
+ just_passed_ie_block = false
103
+ elsif ie_block_start?(line)
104
+ goner_lines << line_number
105
+ inside_ie_block = false
106
+ stack.pop
107
+ just_passed_ie_block = line.strip.chop
108
+ debug.call("ie block start detected, removing it, popping stack")
109
+ elsif ie_block_end?(line)
110
+ goner_lines << line_number
111
+ inside_ie_block = true
112
+ stack << get_indent(line)
113
+ debug.call("ie block end detected, removing it, adding #{get_indent(line)} to stack")
114
+ elsif inside_ie_block
115
+ match = line.match(/<haml[^>]*>([^<]+)<\/haml/)
116
+ string = match && match[1]
117
+ string = string ? "= #{CGI::unescapeHTML(CGI::unescapeHTML(string.strip))}\n" : "#{line.strip}\n"
118
+ altered_lines << [line_number, string]
119
+ indented_lines << [line_number, stack.last]
120
+ debug.call("inside ie block, altering to #{string} and identing by #{stack.last}")
121
+ elsif block_end?(line)
122
+ stack << 1
123
+ goner_lines << line_number
124
+ debug.call("haml block end detected, removing it")
125
+ elsif block_start?(line)
126
+ stack.pop
127
+ debug.call("haml block start detected, popping stack")
128
+ else
129
+ indented_lines << [line_number, stack.last]
130
+ debug.call("indenting by #{stack.last}") if stack.last
131
+ end
132
+ end
133
+
134
+ lines = alter_lines(lines, altered_lines)
135
+ lines = indent_lines(lines, indented_lines)
136
+ lines = remove_lines(lines, goner_lines)
137
+
138
+ altered_lines = []
139
+ indented_lines = []
140
+ goner_lines = []
141
+
142
+ line_number = -1
143
+ lines.each_cons(3) do |three_lines|
144
+ line_number += 1
145
+ line2_number = line_number + 1
146
+ middle_indented_by_one = (get_indent(three_lines[1]) - get_indent(three_lines[0]) == 1)
147
+ top_indented_more = (get_indent(three_lines[0]) >= get_indent(three_lines[2]))
148
+ commented = (three_lines[0].to_s.strip =~ /^\//)
149
+
150
+ if(top_indented_more && middle_indented_by_one && !commented)
151
+ if (three_lines[1].strip =~ /^=/)
152
+ altered_lines << [line_number, (three_lines[0].rstrip + three_lines[1].lstrip)]
153
+ else
154
+ altered_lines << [line_number, (three_lines[0].rstrip + " " + three_lines[1].lstrip)]
155
+ end
156
+ goner_lines << line2_number
157
+ end
158
+ end
159
+
160
+ lines = alter_lines(lines, altered_lines)
161
+ lines = remove_lines(lines, goner_lines)
162
+
163
+ # REPLACABLE2 END
164
+
165
+ if DEBUG_MODE
166
+ destination = destination.gsub(/\.haml$/, '.processed.haml')
167
+ end
168
+
169
+ File.open(destination, "w") do |f|
170
+ f.write(lines.join)
171
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hamlify
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Chernyak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-08 00:00:00 -04:00
13
+ default_executable: hamlify
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: haml
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: HTML to HAML conversion tool which wraps around standard html2haml and fixes issues with its output.
26
+ email: max@bitsonnet.com
27
+ executables:
28
+ - hamlify
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.md
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - VERSION
41
+ - bin/hamlify
42
+ has_rdoc: true
43
+ homepage: http://github.com/maxim/hamlify
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Wrapper over html2haml to fix some conversion issues.
70
+ test_files: []
71
+