hamlizer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/hamlizer/attributes_haml_converter.rb +29 -27
- data/lib/hamlizer/content_printer.rb +31 -0
- data/lib/hamlizer/content_printer_factory.rb +16 -0
- data/lib/hamlizer/element_haml_converter.rb +29 -62
- data/lib/hamlizer/erb_content_printer.rb +7 -0
- data/lib/hamlizer/html_element.rb +48 -43
- data/lib/hamlizer/non_erb_content_printer.rb +7 -0
- data/lib/hamlizer/text_content_printer.rb +7 -0
- data/lib/hamlizer/version.rb +1 -1
- data/lib/hamlizer.rb +6 -0
- data/spec/lib/hamlizer/html_element_spec.rb +58 -0
- data/spec/lib/hamlizer_spec.rb +133 -108
- metadata +10 -3
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Hamlizer
|
2
2
|
|
3
|
-
[![Code Climate](https://codeclimate.com/
|
3
|
+
[![Code Climate](https://codeclimate.com/github/lilibethdlc/hamlizer.png)](https://codeclimate.com/github/lilibethdlc/hamlizer)
|
4
4
|
|
5
5
|
HTML/ERB to HAML converter
|
6
6
|
|
@@ -1,38 +1,40 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Hamlizer
|
2
|
+
class AttributesHamlConverter
|
3
|
+
def initialize(element)
|
4
|
+
@element = element
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def haml
|
8
|
+
element.has_attributes? ? (id_haml + class_haml + regular_attributes_haml) : ''
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
+
private
|
11
12
|
|
12
|
-
|
13
|
+
attr_reader :element
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def attribute_haml(name, value)
|
16
|
+
if name.match(/^\w+$/)
|
17
|
+
attribute_name = "#{name}:"
|
18
|
+
else
|
19
|
+
attribute_name = "'#{name}' =>"
|
20
|
+
end
|
21
|
+
"#{attribute_name} '#{value}'"
|
19
22
|
end
|
20
|
-
"#{attribute_name} '#{value}'"
|
21
|
-
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def class_haml
|
25
|
+
element.html_class ? ".#{element.html_class.gsub(/\s+/, '.')}" : ''
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
def id_haml
|
29
|
+
element.id ? "##{element.id}" : ''
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
def regular_attributes_haml
|
33
|
+
attributes_haml = element.regular_attributes.map do |name, value|
|
34
|
+
attribute_haml name, value
|
35
|
+
end.compact.join(', ')
|
35
36
|
|
36
|
-
|
37
|
+
attributes_haml.empty? ? '' : "{ #{attributes_haml} }"
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Hamlizer
|
2
|
+
class ContentPrinter
|
3
|
+
def initialize(element_converter)
|
4
|
+
@element_converter = element_converter
|
5
|
+
end
|
6
|
+
|
7
|
+
def content
|
8
|
+
"\n#{level_spacing}#{ElementHamlConverter.new(element_converter.element, element_converter.level + 1).haml}"
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
attr_reader :element_converter
|
13
|
+
|
14
|
+
def element_content
|
15
|
+
element_converter.element.content.gsub("\n", ' ').strip
|
16
|
+
end
|
17
|
+
|
18
|
+
def level_spacing
|
19
|
+
' ' * element_converter.level
|
20
|
+
end
|
21
|
+
|
22
|
+
def content_haml(code = nil)
|
23
|
+
if element_converter.element.only_child?
|
24
|
+
"#{code} #{element_content}"
|
25
|
+
else
|
26
|
+
code += ' ' if code
|
27
|
+
"\n#{level_spacing}#{code}#{element_content}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Hamlizer
|
2
|
+
class ContentPrinterFactory
|
3
|
+
def self.printer_for(element_converter)
|
4
|
+
case element_converter.element.name
|
5
|
+
when 'erb_print'
|
6
|
+
ErbContentPrinter.new(element_converter)
|
7
|
+
when 'erb_non_print'
|
8
|
+
NonErbContentPrinter.new(element_converter)
|
9
|
+
when 'text'
|
10
|
+
TextContentPrinter.new(element_converter)
|
11
|
+
else
|
12
|
+
ContentPrinter.new(element_converter)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,75 +1,42 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Hamlizer
|
2
|
+
class ElementHamlConverter
|
3
|
+
def initialize(element, level = 1)
|
4
|
+
@element = element
|
5
|
+
@level = level
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def haml
|
9
|
+
element_name_haml + attributes_haml + element_content_haml
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
+
attr_reader :element, :level
|
12
13
|
|
13
|
-
|
14
|
+
private
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def attributes_haml
|
17
|
+
attributes_haml_converter.haml
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def attributes_haml_converter
|
21
|
+
AttributesHamlConverter.new(element)
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
erb_non_print_haml(child)
|
29
|
-
elsif child.name == 'text'
|
30
|
-
text_haml(child)
|
24
|
+
def element_content_haml
|
25
|
+
if element.children.any?
|
26
|
+
element.children.map do |child|
|
27
|
+
ContentPrinterFactory.printer_for(ElementHamlConverter.new(child, level)).content
|
28
|
+
end.join
|
31
29
|
else
|
32
|
-
|
30
|
+
element.content
|
33
31
|
end
|
34
|
-
end.join
|
35
|
-
|
36
|
-
content_haml
|
37
|
-
end
|
38
|
-
|
39
|
-
def element_name_haml
|
40
|
-
should_include_name_in_haml? ? "%#{element.name}" : ''
|
41
|
-
end
|
42
|
-
|
43
|
-
def child_content(child)
|
44
|
-
child.content.gsub("\n", ' ').strip
|
45
|
-
end
|
46
|
-
|
47
|
-
def child_haml(child, code = nil)
|
48
|
-
if child.only_child?
|
49
|
-
"#{code} #{child_content(child)}"
|
50
|
-
else
|
51
|
-
code += ' ' if code
|
52
|
-
"\n#{level_spacing}#{code}#{child_content(child)}"
|
53
32
|
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def erb_non_print_haml(element)
|
57
|
-
child_haml(element, '-')
|
58
|
-
end
|
59
|
-
|
60
|
-
def erb_print_haml(element)
|
61
|
-
child_haml(element, '=')
|
62
|
-
end
|
63
|
-
|
64
|
-
def level_spacing
|
65
|
-
' ' * level
|
66
|
-
end
|
67
33
|
|
68
|
-
|
69
|
-
|
70
|
-
|
34
|
+
def element_name_haml
|
35
|
+
exclude_name_from_haml? ? '' : "%#{element.name}"
|
36
|
+
end
|
71
37
|
|
72
|
-
|
73
|
-
|
38
|
+
def exclude_name_from_haml?
|
39
|
+
element.text? || (element.div? && (element.has_id? || element.has_class?))
|
40
|
+
end
|
74
41
|
end
|
75
42
|
end
|
@@ -1,60 +1,65 @@
|
|
1
1
|
require 'nokogiri'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Hamlizer
|
4
|
+
class HtmlElement
|
5
|
+
def initialize(element)
|
6
|
+
@element = element
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def children
|
10
|
+
element.children.map { |child| HtmlElement.new child }
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def content
|
14
|
+
element.content
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def div?
|
18
|
+
element.name == 'div'
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def has_attributes?
|
22
|
+
element.attributes.any?
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def has_class?
|
26
|
+
!html_class.nil?
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def has_content?
|
30
|
+
content && !content.empty?
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def has_id?
|
34
|
+
!id.nil?
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
def only_child?
|
38
|
+
element.parent.children.count == 1
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
def html_class
|
42
|
+
@class ||= element['class']
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
def id
|
46
|
+
@id ||= element['id']
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
49
|
+
def name
|
50
|
+
element.name
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
def regular_attributes
|
54
|
+
Hash[element.attributes.select { |name, _| !%w(id class).include? name }]
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
+
def text?
|
58
|
+
element.name == 'text'
|
59
|
+
end
|
57
60
|
|
58
|
-
|
59
|
-
end
|
61
|
+
private
|
60
62
|
|
63
|
+
attr_reader :element, :parent
|
64
|
+
end
|
65
|
+
end
|
data/lib/hamlizer/version.rb
CHANGED
data/lib/hamlizer.rb
CHANGED
@@ -2,10 +2,16 @@ require 'hamlizer/version'
|
|
2
2
|
require 'hamlizer/html_element'
|
3
3
|
require 'hamlizer/element_haml_converter'
|
4
4
|
require 'hamlizer/attributes_haml_converter'
|
5
|
+
require 'hamlizer/content_printer'
|
6
|
+
require 'hamlizer/content_printer_factory'
|
7
|
+
require 'hamlizer/erb_content_printer'
|
8
|
+
require 'hamlizer/non_erb_content_printer'
|
9
|
+
require 'hamlizer/text_content_printer'
|
5
10
|
require 'nokogiri'
|
6
11
|
|
7
12
|
module Hamlizer
|
8
13
|
def self.hamlize(html)
|
14
|
+
html.strip!
|
9
15
|
convert_erb_to_parseable_xml!(html)
|
10
16
|
|
11
17
|
if html.start_with?('<html>')
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Hamlizer
|
4
|
+
describe HtmlElement do
|
5
|
+
let(:element) { {} }
|
6
|
+
subject { HtmlElement.new element }
|
7
|
+
|
8
|
+
describe '#has_id?' do
|
9
|
+
context 'when the source element has an id' do
|
10
|
+
let(:element) { { 'id' => 'identificador' } }
|
11
|
+
|
12
|
+
it 'returns true' do
|
13
|
+
expect(subject).to have_id
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when the source element does not have an id' do
|
18
|
+
it 'returns false' do
|
19
|
+
expect(subject).to_not have_id
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#has_class?' do
|
25
|
+
context 'when the source element has a class' do
|
26
|
+
let(:element) { { 'class' => 'clase' } }
|
27
|
+
|
28
|
+
it 'returns true' do
|
29
|
+
expect(subject).to have_class
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when the source element does not have a class' do
|
34
|
+
it 'returns false' do
|
35
|
+
expect(subject).to_not have_class
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#text?' do
|
41
|
+
context 'when the source element\'s name is "text"' do
|
42
|
+
let(:element) { double('element', name: 'text') }
|
43
|
+
|
44
|
+
it 'returns true' do
|
45
|
+
expect(subject).to be_text
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when the source element\'s name is something other than "text"' do
|
50
|
+
let(:element) { double('element', name: 'hello') }
|
51
|
+
|
52
|
+
it 'returns true' do
|
53
|
+
expect(subject).to_not be_text
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/lib/hamlizer_spec.rb
CHANGED
@@ -1,113 +1,138 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
describe
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
3
|
+
module Hamlizer
|
4
|
+
describe Hamlizer do
|
5
|
+
describe '.hamlize' do
|
6
|
+
|
7
|
+
it 'leaves an empty string alone' do
|
8
|
+
expect(hamlize('')).to eq ''
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'leaves a non html text alone' do
|
12
|
+
expect(hamlize('hello')).to eq 'hello'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns empty string for whitespace' do
|
16
|
+
expect(hamlize(' ')).to eq ''
|
17
|
+
expect(hamlize("\n\n\n\n")).to eq ''
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'converts an empty tag correctly' do
|
21
|
+
expect(hamlize('<br/>')).to eq '%br'
|
22
|
+
expect(hamlize('<br>')).to eq '%br'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'converts html standard layout tags correctly' do
|
26
|
+
expect(hamlize('<html></html>')).to eq '%html'
|
27
|
+
expect(hamlize('<html><body></body></html>')).to eq "%html\n %body"
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'converts a tag with no content' do
|
31
|
+
expect(hamlize('<span></span>')).to eq '%span'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'converts a tag with text content' do
|
35
|
+
text = 'abc'
|
36
|
+
expect(hamlize("<span>#{text}</span>")).to eq "%span #{text}"
|
37
|
+
expect(hamlize("<span>#{text}\n#{text}</span>")).to eq "%span #{text} #{text}"
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'converts multiple tags on the same level' do
|
41
|
+
expect(hamlize('<br/><br/><br/>')).to eq "%br\n%br\n%br"
|
42
|
+
text = 'hello'
|
43
|
+
expect(hamlize("<span>#{text}</span><span/>")).to eq "%span #{text}\n%span"
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'converts nested tags' do
|
47
|
+
expect(hamlize('<div><span></span></div>')).to eq "%div\n %span"
|
48
|
+
expect(hamlize('<div><p><span></span></p></div>')).to eq "%div\n %p\n %span"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'converts nested tags with content' do
|
52
|
+
expect(hamlize('<div>abc<span>def</span></div>')).to eq "%div\n abc\n %span def"
|
53
|
+
expect(hamlize('<div>abc<p>hello<span>abc</span></p></div>')).to eq "%div\n abc\n %p\n hello\n %span abc"
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'converts a tag with attributes' do
|
57
|
+
title = 'El Titulo'
|
58
|
+
source = 'La Fuente'
|
59
|
+
|
60
|
+
expect(hamlize("<span title='#{title}'></span>")).to eq "%span{ title: '#{title}' }"
|
61
|
+
expect(hamlize("<span title='#{title}' source='#{source}'></span>")).to eq "%span{ title: '#{title}', source: '#{source}' }"
|
62
|
+
expect(hamlize("<span title='#{title}' data-source='#{source}'></span>")).to eq "%span{ title: '#{title}', 'data-source' => '#{source}' }"
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'uses the "#id" syntax for the id attribute' do
|
66
|
+
id = 'el_identificador'
|
67
|
+
title = 'El Titulo'
|
68
|
+
|
69
|
+
expect(hamlize("<span id='#{id}'></span>")).to eq "%span##{id}"
|
70
|
+
expect(hamlize("<span id='#{id}' title='#{title}'></span>")).to eq "%span##{id}{ title: '#{title}' }"
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'uses the ".class" syntax for the class attribute' do
|
74
|
+
html_class = 'la_clase'
|
75
|
+
title = 'El Titulo'
|
76
|
+
|
77
|
+
expect(hamlize("<span class='#{html_class}'></span>")).to eq "%span.#{html_class}"
|
78
|
+
expect(hamlize("<span class='#{html_class}' title='#{title}'></span>")).to eq "%span.#{html_class}{ title: '#{title}' }"
|
79
|
+
|
80
|
+
html_classes = %w(una_clase otra_clase)
|
81
|
+
expect(hamlize("<span class='#{html_classes.join(' ')}' title='#{title}'></span>")).to eq "%span.#{html_classes.join('.')}{ title: '#{title}' }"
|
82
|
+
expect(hamlize("<span class='#{html_classes.join(' ')}' title='#{title}'></span>")).to eq "%span.#{html_classes.join('.')}{ title: '#{title}' }"
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'when hamlizing the div tag' do
|
86
|
+
let(:title) { 'El Titulo' }
|
87
|
+
let(:html_class) { 'la_clase' }
|
88
|
+
let(:id) { 'el_identificador' }
|
89
|
+
|
90
|
+
it 'includes %div when the tag does not have any attributes' do
|
91
|
+
expect(hamlize('<div></div>')).to eq '%div'
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'includes %div when the attributes are not id or class' do
|
95
|
+
expect(hamlize("<div title='#{title}'></div>")).to eq "%div{ title: '#{title}' }"
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'uses "#" when the div has an id attribute' do
|
99
|
+
expect(hamlize("<div id='#{id}'></div>")).to eq "##{id}"
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'uses "." when the div has just one class in the class attribute' do
|
103
|
+
expect(hamlize("<div class='#{html_class}'></div>")).to eq ".#{html_class}"
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'uses "..." when the div has more than one class in the class attribute' do
|
107
|
+
expect(hamlize("<div class='hola tu'></div>")).to eq '.hola.tu'
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'uses "#." when the div has both id and class attributes' do
|
111
|
+
expect(hamlize("<div class='#{html_class}' id='#{id}'></div>")).to eq "##{id}.#{html_class}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'parses erb printing code' do
|
116
|
+
expect(hamlize('<strong><%= item.title %></strong>')).to eq '%strong= item.title'
|
117
|
+
expect(hamlize('<p><%= hello %><strong><%= item.title %></strong></p>')).to eq "%p\n = hello\n %strong= item.title"
|
118
|
+
expect(hamlize('<p><% hello %><strong><%= item.title %></strong></p>')).to eq "%p\n - hello\n %strong= item.title"
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'parses erb non-printing code' do
|
122
|
+
expect(hamlize('<div><% a = 2 + 2 %></div>')).to eq '%div- a = 2 + 2'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def hamlize(html)
|
127
|
+
haml = Hamlizer.hamlize(html)
|
128
|
+
|
129
|
+
begin
|
130
|
+
Haml::Engine.new(haml).to_html
|
131
|
+
rescue Exception => e
|
132
|
+
raise e.class, "#{e.message}\nIn:\n#{haml}" unless [NameError, NoMethodError].include? e.class
|
133
|
+
end
|
134
|
+
|
135
|
+
haml
|
58
136
|
end
|
59
|
-
|
60
|
-
it 'uses the "#id" syntax for the id attribute' do
|
61
|
-
id = 'el_identificador'
|
62
|
-
title = 'El Titulo'
|
63
|
-
|
64
|
-
expect(hamlize("<span id='#{id}'></span>")).to eq "%span##{id}"
|
65
|
-
expect(hamlize("<span id='#{id}' title='#{title}'></span>")).to eq "%span##{id}{ title: '#{title}' }"
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'uses the ".class" syntax for the class attribute' do
|
69
|
-
html_class = 'la_clase'
|
70
|
-
title = 'El Titulo'
|
71
|
-
|
72
|
-
expect(hamlize("<span class='#{html_class}'></span>")).to eq "%span.#{html_class}"
|
73
|
-
expect(hamlize("<span class='#{html_class}' title='#{title}'></span>")).to eq "%span.#{html_class}{ title: '#{title}' }"
|
74
|
-
|
75
|
-
html_classes = %w(una_clase otra_clase)
|
76
|
-
expect(hamlize("<span class='#{html_classes.join(' ')}' title='#{title}'></span>")).to eq "%span.#{html_classes.join('.')}{ title: '#{title}' }"
|
77
|
-
expect(hamlize("<span class='#{html_classes.join(' ')}' title='#{title}'></span>")).to eq "%span.#{html_classes.join('.')}{ title: '#{title}' }"
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'does not use %div for divs that have an id or a class' do
|
81
|
-
id = 'el_identificador'
|
82
|
-
html_class = 'la_clase'
|
83
|
-
title = 'El Titulo'
|
84
|
-
|
85
|
-
expect(hamlize('<div></div>')).to eq '%div'
|
86
|
-
expect(hamlize("<div title='#{title}'></div>")).to eq "%div{ title: '#{title}' }"
|
87
|
-
expect(hamlize("<div id='#{id}'></div>")).to eq "##{id}"
|
88
|
-
expect(hamlize("<div class='#{html_class}'></div>")).to eq ".#{html_class}"
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'parses erb printing code', :focus do
|
92
|
-
expect(hamlize('<strong><%= item.title %></strong>')).to eq '%strong= item.title'
|
93
|
-
expect(hamlize('<p><%= hello %><strong><%= item.title %></strong></p>')).to eq "%p\n = hello\n %strong= item.title"
|
94
|
-
expect(hamlize('<p><% hello %><strong><%= item.title %></strong></p>')).to eq "%p\n - hello\n %strong= item.title"
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'parses erb non-printing code' do
|
98
|
-
expect(hamlize('<div><% a = 2 + 2 %></div>')).to eq '%div- a = 2 + 2'
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def hamlize(html)
|
103
|
-
haml = Hamlizer.hamlize(html)
|
104
|
-
|
105
|
-
begin
|
106
|
-
Haml::Engine.new(haml).to_html
|
107
|
-
rescue Exception => e
|
108
|
-
raise e.class, "#{e.message}\nIn: #{haml}" unless [NameError, NoMethodError].include? e.class
|
109
|
-
end
|
110
|
-
|
111
|
-
haml
|
112
137
|
end
|
113
138
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hamlizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: nokogiri
|
@@ -77,9 +77,15 @@ files:
|
|
77
77
|
- hamlizer.gemspec
|
78
78
|
- lib/hamlizer.rb
|
79
79
|
- lib/hamlizer/attributes_haml_converter.rb
|
80
|
+
- lib/hamlizer/content_printer.rb
|
81
|
+
- lib/hamlizer/content_printer_factory.rb
|
80
82
|
- lib/hamlizer/element_haml_converter.rb
|
83
|
+
- lib/hamlizer/erb_content_printer.rb
|
81
84
|
- lib/hamlizer/html_element.rb
|
85
|
+
- lib/hamlizer/non_erb_content_printer.rb
|
86
|
+
- lib/hamlizer/text_content_printer.rb
|
82
87
|
- lib/hamlizer/version.rb
|
88
|
+
- spec/lib/hamlizer/html_element_spec.rb
|
83
89
|
- spec/lib/hamlizer_spec.rb
|
84
90
|
- spec/spec_helper.rb
|
85
91
|
homepage: ''
|
@@ -102,10 +108,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
108
|
version: '0'
|
103
109
|
requirements: []
|
104
110
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.8.
|
111
|
+
rubygems_version: 1.8.25
|
106
112
|
signing_key:
|
107
113
|
specification_version: 3
|
108
114
|
summary: HTML/ERB to HAML converter
|
109
115
|
test_files:
|
116
|
+
- spec/lib/hamlizer/html_element_spec.rb
|
110
117
|
- spec/lib/hamlizer_spec.rb
|
111
118
|
- spec/spec_helper.rb
|