html2slim 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80166682417a633dce92c9ab90fbdf483031f025
4
- data.tar.gz: 6b0ba218756add50271fa2b2ce4df4510774d1b3
3
+ metadata.gz: a160f480ebf84e9e8ec97bfe7f4f2698f6789758
4
+ data.tar.gz: e33e1d4799ac272c263e9db46fc1769eb59370df
5
5
  SHA512:
6
- metadata.gz: 2090c80d405e2a9080d57783734110f2130f5e4d20e53816559b360627c08f94eba147d568328c79a03fd423ff0073fe7acb40816733c5220cd27615e0ce538f
7
- data.tar.gz: 58a092ca40b3707d689456c48835f4307deaa6c56cc86fe7e8164a6b2238cb93cb0b5df9dc36bd0081b18c03540f8ef1e0054038b4305176c4e0cec547b8456f
6
+ metadata.gz: 3e67c5c0e003f756da34b7a183d9a77ae40731e6fea884ae548a120bd44062b2abfe00749cc3eef6fdec7ec4be56579fe59e79ba007a69efb5f5c4a8b0609190
7
+ data.tar.gz: 7caa78d05f9a7dc3c955a219aa00324e4ca17a66257458c55f7380660a903f7e9cc9ea07b513601af4816e5d8dee2c28b2ce03788e1ff2f524a2fa6f3964a1fc
@@ -63,7 +63,7 @@ module HTML2Slim
63
63
  @options[:input] = file = "-" unless file
64
64
 
65
65
  if File.directory?(@options[:input])
66
- Dir["#{@options[:input]}/**/*.html"].each { |file| _process(file, destination) }
66
+ Dir["#{@options[:input]}/**/*.#{format}"].each { |file| _process(file, destination) }
67
67
  else
68
68
  _process(file, destination)
69
69
  end
@@ -71,11 +71,15 @@ module HTML2Slim
71
71
 
72
72
  private
73
73
 
74
+ def input_is_dir?
75
+ File.directory? @options[:input]
76
+ end
77
+
74
78
  def _process(file, destination = nil)
75
79
  require 'fileutils'
76
- slim_file = file.sub(/\.html$/, '.slim')
80
+ slim_file = file.sub(/\.#{format}/, '.slim')
77
81
 
78
- if File.directory?(@options[:input]) && destination
82
+ if input_is_dir? && destination
79
83
  FileUtils.mkdir_p(File.dirname(slim_file).sub(@options[:input].chomp('/'), destination))
80
84
  slim_file.sub!(@options[:input].chomp('/'), destination)
81
85
  else
@@ -83,13 +87,12 @@ module HTML2Slim
83
87
  end
84
88
 
85
89
  in_file = if @options[:input] == "-"
86
- $stdin
87
- else
88
- File.open(file, 'r')
89
- end
90
+ $stdin
91
+ else
92
+ File.open(file, 'r')
93
+ end
90
94
 
91
95
  @options[:output] = slim_file && slim_file != '-' ? File.open(slim_file, 'w') : $stdout
92
- # raise "|||#{self.class.inspect}|||"
93
96
  @options[:output].puts HTML2Slim.convert!(in_file, format)
94
97
  @options[:output].close
95
98
 
@@ -1,7 +1,18 @@
1
1
  require 'hpricot'
2
2
 
3
3
  Hpricot::XHTMLTransitional.tagset[:ruby] = [:code]
4
- # raise Hpricot::XHTMLTransitional.tagset.inspect
4
+
5
+ module SlimText
6
+ def to_slim(lvl=0)
7
+ return nil if to_s.strip.empty?
8
+ (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
9
+ end
10
+ end
11
+
12
+ class Hpricot::CData
13
+ include SlimText
14
+ end
15
+
5
16
  class Hpricot::BogusETag
6
17
  def to_slim(lvl=0)
7
18
  nil
@@ -9,10 +20,7 @@ class Hpricot::BogusETag
9
20
  end
10
21
 
11
22
  class Hpricot::Text
12
- def to_slim(lvl=0)
13
- return nil if to_s.strip.empty?
14
- (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
15
- end
23
+ include SlimText
16
24
  end
17
25
 
18
26
  class Hpricot::Comment
@@ -23,36 +31,86 @@ end
23
31
 
24
32
  class Hpricot::DocType
25
33
  def to_slim(lvl=0)
26
- 'doctype'
34
+ if to_s.include? "xml"
35
+ to_s.include?("iso-8859-1") ? "doctype xml ISO-88591" : "doctype xml"
36
+ elsif to_s.include? "XHTML" or self.to_s.include? "HTML 4.01"
37
+ available_versions = Regexp.union ["Basic", "1.1", "strict", "Frameset", "Mobile", "Transitional"]
38
+ version = to_s.match(available_versions).to_s.downcase
39
+ "doctype #{version}"
40
+ else
41
+ "doctype html"
42
+ end
27
43
  end
28
44
  end
29
45
 
30
46
  class Hpricot::Elem
31
47
  def slim(lvl=0)
32
- r = (' ' * lvl)
33
- if self.name == "ruby"
34
- if self.attributes["code"].strip[0] == "="
35
- return r += self.attributes["code"].strip
36
- else
37
- return r += "- " + self.attributes["code"].strip
38
- end
39
- end
48
+ r = ' ' * lvl
40
49
 
41
- r += self.name unless self.name == 'div' and (self.has_attribute?('id') || self.has_attribute?('class'))
42
- r += "##{self['id']}" if self.has_attribute?('id')
43
- self.remove_attribute('id')
44
- r += ".#{self['class'].split(/\s+/).join('.')}" if self.has_attribute?('class')
45
- self.remove_attribute('class')
46
- r += "[#{attributes_as_html.to_s.strip}]" unless attributes_as_html.to_s.strip.empty?
50
+ return r + slim_ruby_code if ruby?
51
+
52
+ r += name unless skip_tag_name?
53
+ r += slim_id
54
+ r += slim_class
55
+ r += slim_attributes
47
56
  r
48
57
  end
58
+
49
59
  def to_slim(lvl=0)
50
60
  if respond_to?(:children) and children
51
- return %(#{self.slim(lvl)}\n#{children.map { |x| x.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
61
+ return %(#{slim(lvl)}\n#{children.map{|c| c.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
52
62
  else
53
- self.slim(lvl)
63
+ slim(lvl)
54
64
  end
55
65
  end
66
+
67
+ private
68
+
69
+ def slim_ruby_code
70
+ (code.strip[0] == "=" ? "" : "- ") + code.strip
71
+ end
72
+
73
+ def code
74
+ attributes["code"]
75
+ end
76
+
77
+ def skip_tag_name?
78
+ div? and (has_id? || has_class?)
79
+ end
80
+
81
+ def slim_id
82
+ has_id?? "##{self['id']}" : ""
83
+ end
84
+
85
+ def slim_class
86
+ has_class?? ".#{self['class'].split(/\s+/).join('.')}" : ""
87
+ end
88
+
89
+ def slim_attributes
90
+ remove_attribute('class')
91
+ remove_attribute('id')
92
+ has_attributes?? "[#{attributes_as_html.to_s.strip}]" : ""
93
+ end
94
+
95
+ def has_attributes?
96
+ attributes.to_hash.any?
97
+ end
98
+
99
+ def has_id?
100
+ has_attribute?('id')
101
+ end
102
+
103
+ def has_class?
104
+ has_attribute?('class')
105
+ end
106
+
107
+ def ruby?
108
+ name == "ruby"
109
+ end
110
+
111
+ def div?
112
+ name == "div"
113
+ end
56
114
  end
57
115
 
58
116
  class Hpricot::Doc
@@ -63,4 +121,4 @@ class Hpricot::Doc
63
121
  ''
64
122
  end
65
123
  end
66
- end
124
+ end
@@ -1,3 +1,3 @@
1
1
  module HTML2Slim
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html2slim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maiz Lulkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-15 00:00:00.000000000 Z
11
+ date: 2014-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hpricot
@@ -110,4 +110,3 @@ signing_key:
110
110
  specification_version: 4
111
111
  summary: HTML to Slim converter.
112
112
  test_files: []
113
- has_rdoc: