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 +4 -4
- data/lib/html2slim/command.rb +11 -8
- data/lib/html2slim/hpricot_monkeypatches.rb +81 -23
- data/lib/html2slim/version.rb +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a160f480ebf84e9e8ec97bfe7f4f2698f6789758
|
4
|
+
data.tar.gz: e33e1d4799ac272c263e9db46fc1769eb59370df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e67c5c0e003f756da34b7a183d9a77ae40731e6fea884ae548a120bd44062b2abfe00749cc3eef6fdec7ec4be56579fe59e79ba007a69efb5f5c4a8b0609190
|
7
|
+
data.tar.gz: 7caa78d05f9a7dc3c955a219aa00324e4ca17a66257458c55f7380660a903f7e9cc9ea07b513601af4816e5d8dee2c28b2ce03788e1ff2f524a2fa6f3964a1fc
|
data/lib/html2slim/command.rb
CHANGED
@@ -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]}
|
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(
|
80
|
+
slim_file = file.sub(/\.#{format}/, '.slim')
|
77
81
|
|
78
|
-
if
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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 =
|
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
|
42
|
-
|
43
|
-
|
44
|
-
r +=
|
45
|
-
|
46
|
-
r +=
|
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 %(#{
|
61
|
+
return %(#{slim(lvl)}\n#{children.map{|c| c.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
|
52
62
|
else
|
53
|
-
|
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
|
data/lib/html2slim/version.rb
CHANGED
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
|
+
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:
|
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:
|