html-to-css 0.1.1 → 0.1.2

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: 3b6e922852517c80339a45220997648298601c15
4
- data.tar.gz: 6953324840ce7ee68f7100feb3016efb90c205dc
3
+ metadata.gz: 421d1eaa5841abe3e4bcb2a9a774f9c56c42deb4
4
+ data.tar.gz: 407d169119f7c6038f8a2dacf1393dcc8f55093c
5
5
  SHA512:
6
- metadata.gz: f4e15a6772890c096606f803b2c9a630a103b11dd1126e2c529d5fd11480cc24c6200f07cbff46c8a13e0c2c35718ca0dabf5fa6ea967f0391956b0596567ab2
7
- data.tar.gz: 088dd101747daa174611c49db0c10bdda336c3ba868f2bb83229b9e54e0ff0beb604627d7737b9d3c03eb3aa6b7571ff8b6825b52b0a650a92a1203a1741b67d
6
+ metadata.gz: 37eed1658e73c76a004ede96799288a1f3162d2792bf779fd1da2535e347458f0f4dfedc71b8b2daea8c74fc37403c7e582f0318fe1815884c484b2d33261438
7
+ data.tar.gz: 2d8fd2b7571a7179fb3124f9f26116259ef41f9c0efd1fb0341a83ccec66a69b2ee67d19040b76bd367293cedb779101fd0682bd2068ef3e1e7948aa3790a9d9
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -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 = %w(title link)
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
- scan @doc.root
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 scan(e, indent='', parent_selector='')
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
- scan x, indent, parent_selector
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.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-09 00:00:00.000000000 Z
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