html-to-css 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/html-to-css.rb +71 -8
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 421d1eaa5841abe3e4bcb2a9a774f9c56c42deb4
|
4
|
+
data.tar.gz: 407d169119f7c6038f8a2dacf1393dcc8f55093c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37eed1658e73c76a004ede96799288a1f3162d2792bf779fd1da2535e347458f0f4dfedc71b8b2daea8c74fc37403c7e582f0318fe1815884c484b2d33261438
|
7
|
+
data.tar.gz: 2d8fd2b7571a7179fb3124f9f26116259ef41f9c0efd1fb0341a83ccec66a69b2ee67d19040b76bd367293cedb779101fd0682bd2068ef3e1e7948aa3790a9d9
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/html-to-css.rb
CHANGED
@@ -7,11 +7,21 @@ require 'rexle'
|
|
7
7
|
|
8
8
|
class HtmlToCss
|
9
9
|
|
10
|
-
def initialize(filename)
|
10
|
+
def initialize(filename=nil)
|
11
|
+
|
12
|
+
if filename then
|
13
|
+
|
14
|
+
@doc = Rexle.new File.read(filename)
|
15
|
+
|
16
|
+
else
|
17
|
+
|
18
|
+
a = Dir.glob("*.html")
|
19
|
+
@doc = Rexle.new File.read(a.pop)
|
20
|
+
a.each {|file| merge(@doc, Rexle.new(File.read(file)).root ) }
|
21
|
+
end
|
11
22
|
|
12
|
-
@doc = Rexle.new File.read(filename)
|
13
23
|
@selectors = []
|
14
|
-
@nocss =
|
24
|
+
@nocss = ['head', 'ul li ul', 'p a', 'div div \w+']
|
15
25
|
@css = []
|
16
26
|
|
17
27
|
@elements = {
|
@@ -27,7 +37,6 @@ class HtmlToCss
|
|
27
37
|
color: #fff;
|
28
38
|
font-family: Verdana, Arial, Helvetica, sans-serif;
|
29
39
|
font-size: 1.3em;",
|
30
|
-
head: "background-color: :color;",
|
31
40
|
html: "background-color: :color;",
|
32
41
|
li: "background-color: :color;",
|
33
42
|
p: "background-color: :color;",
|
@@ -36,13 +45,48 @@ class HtmlToCss
|
|
36
45
|
end
|
37
46
|
|
38
47
|
def to_css()
|
39
|
-
|
48
|
+
scan_to_css @doc.root
|
40
49
|
@css.join "\n"
|
41
50
|
end
|
42
51
|
|
52
|
+
def to_layout()
|
53
|
+
select_layout_elements()
|
54
|
+
to_css()
|
55
|
+
end
|
56
|
+
|
43
57
|
private
|
44
58
|
|
45
|
-
def
|
59
|
+
def merge(mdoc, e, axpath=[], prev_tally=[])
|
60
|
+
|
61
|
+
i = (e.parent.parent and prev_tally.last) ?
|
62
|
+
(prev_tally.last + [e.name]).grep(e.name).length: 1
|
63
|
+
i = 1 if i == 0
|
64
|
+
|
65
|
+
index = "[%s]" % (i)
|
66
|
+
axpath << e.name + index
|
67
|
+
tally = [e.name]
|
68
|
+
|
69
|
+
# does the xpath match with the master doc?
|
70
|
+
node = mdoc.element axpath.join('/')
|
71
|
+
|
72
|
+
unless node then
|
73
|
+
|
74
|
+
xpath = axpath[0..-2].join('/')
|
75
|
+
mdoc.element(xpath).add e
|
76
|
+
|
77
|
+
else
|
78
|
+
|
79
|
+
tally << [] if e.elements.to_a.length > 0
|
80
|
+
e.elements.each do |x|
|
81
|
+
tally.last.concat merge(mdoc, x, axpath.clone, tally.clone)
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
tally
|
87
|
+
end
|
88
|
+
|
89
|
+
def scan_to_css(e, indent='', parent_selector='')
|
46
90
|
|
47
91
|
return if @nocss.include? e.name
|
48
92
|
attr = e.attributes
|
@@ -52,6 +96,8 @@ class HtmlToCss
|
|
52
96
|
else
|
53
97
|
selector = (parent_selector + ' ' + e.name).strip
|
54
98
|
end
|
99
|
+
|
100
|
+
return if @nocss.detect {|x| selector =~ /#{x}/ }
|
55
101
|
|
56
102
|
unless @selectors.include? selector then
|
57
103
|
|
@@ -72,7 +118,25 @@ class HtmlToCss
|
|
72
118
|
|
73
119
|
indent += ' '
|
74
120
|
e.elements.each do |x|
|
75
|
-
|
121
|
+
scan_to_css x, indent, parent_selector
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def select_layout_elements()
|
126
|
+
|
127
|
+
a = @doc.root.xpath '//div'
|
128
|
+
a.reverse.each do |e|
|
129
|
+
|
130
|
+
if not e.attributes[:id] then
|
131
|
+
|
132
|
+
li_inline = e.xpath('//ul/li/@style').grep(/display:\s*inline/).any?
|
133
|
+
div_float = e.xpath('//div/@style').grep(/float:\s*(?:left|right)/).any?
|
134
|
+
next if li_inline or div_float
|
135
|
+
e.delete
|
136
|
+
elsif e.attributes[:id] == 'sitemap'
|
137
|
+
e.delete
|
138
|
+
end
|
139
|
+
|
76
140
|
end
|
77
141
|
end
|
78
142
|
|
@@ -81,4 +145,3 @@ end
|
|
81
145
|
if __FILE__ == $0 then
|
82
146
|
css = HtmlToCss.new('index.html').to_css
|
83
147
|
end
|
84
|
-
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html-to-css
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -31,7 +31,7 @@ cert_chain:
|
|
31
31
|
n9X5YRMwlJkR8AjTM6rhQpKsbN0jqpzYjglj02VuBO/YASqBM1gV6sKxMtAbdYam
|
32
32
|
6yGZgJtO0psElQ==
|
33
33
|
-----END CERTIFICATE-----
|
34
|
-
date: 2013-10-
|
34
|
+
date: 2013-10-10 00:00:00.000000000 Z
|
35
35
|
dependencies:
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rexle
|
metadata.gz.sig
CHANGED
Binary file
|