html-to-css 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bb567fab6e41c0222d3c482d3830711ab82ed62
4
- data.tar.gz: 99b4ea9ceb1975d692de150a010607863fa87d9d
3
+ metadata.gz: 7ce023ea55a86ff16fef105a2ae4c1a7338aa76a
4
+ data.tar.gz: 6befdb1a8b3c493fb40b51d3e94e272e6aa82fb6
5
5
  SHA512:
6
- metadata.gz: a5d9df03b2f703e504398f0729b758363eb649d8899b3a44489eb2c528aac925b03495223094ad94579132b310de229f11d274c9fb4e6cceefb15c3e62622d63
7
- data.tar.gz: 5d0879fe7a4f3fe5d7ca40563e411fc83987c8bcbb1cfb1dc37576b9820840f8d169aba07a24a3f4ccbba045dd6bf596f2d0f95c31e2cab0e5fe14b463b13abe
6
+ metadata.gz: df21b9e5cba643f151dbc83cf54b553a3e323439cc4fae9a84c029c62a3acf2fcbd3fb57278dbdb18b7bec04a0506096bc2f6d8f178b6676781e7320d3705f41
7
+ data.tar.gz: 8853e230d5609db634711ccadf56e5387379eccf014bd2c433acd2a2287da283ab67c07cf6f184797a0116e8d10fc4407e7f96ef72af3aa7d18bfcff3bc59039
@@ -1,5 +1,3 @@
1
- tl-�7!)߈��wDsҠ�[�Ю#2q�mi�Ü�m�q
2
- xa�\o� ��r
3
- G�;!��/��S�f#'��(0�M�Az�BA��m�?Qm��@��I���ڷ�;�3F����lٷ&A?L6K��,}K�5��r�Q�Qڶ���X�IT�
4
- �b��=�p>��F'S�aG����<�g�|���
5
- . �ه��0�x(� ى�C#naN#٤�;W��Fu�֔��5�XQ$ `�8�3��ze1��dw
1
+ P27�|��! ���\_6���8��XN_+��]��>�e6Z�$�����Q)���7�7ك�̺�{v&sq ���2.����}@�At��cCO��ûě���16%���0p��U�����S�����4�����D��.�m��|+]Sh� � jz��&�#�8}\����f�q������(�^lL�q{R��$�z��"�E<�*�����)���G���9q�i�
2
+ ғ}��/��MP
3
+ ��Aj��
data.tar.gz.sig CHANGED
Binary file
@@ -13,6 +13,8 @@ end
13
13
 
14
14
  class HtmlToCss
15
15
 
16
+ attr_accessor :elements
17
+
16
18
  def initialize(filename=nil)
17
19
 
18
20
  if filename then
@@ -27,7 +29,8 @@ class HtmlToCss
27
29
  end
28
30
 
29
31
  @selectors = []
30
- @nocss = ['head', 'ul li ul', 'p a', 'div div \w+']
32
+ @nocss = ['head']
33
+ @nolayoutcss = ['ul li ul', 'p a', 'div div \w+']
31
34
  @css = []
32
35
 
33
36
  @elements = {
@@ -51,17 +54,34 @@ class HtmlToCss
51
54
  end
52
55
 
53
56
  def to_css()
54
- scan_to_css @doc.root
55
- @css.join "\n"
57
+ apply_css
56
58
  end
57
59
 
58
60
  def to_layout()
59
- select_layout_elements()
60
- to_css()
61
+
62
+ css = apply_css(:layout) {|doc| select_layout_elements(doc) }
63
+ @layout_selectors = @selectors.clone
64
+ css
65
+ end
66
+
67
+ def to_style()
68
+ apply_css :style
61
69
  end
62
70
 
63
71
  private
64
72
 
73
+ def apply_css(type=:default)
74
+
75
+ @css, @selectors = [], []
76
+ doc = @doc.root.deep_clone
77
+
78
+ yield(doc) if block_given?
79
+
80
+ scan_to_css(type, doc.root)
81
+ @layout_selectors = @selectors.clone
82
+ @css.join "\n"
83
+ end
84
+
65
85
  def merge(mdoc, e, axpath=[], prev_tally=[])
66
86
 
67
87
  i = (e.parent.parent and prev_tally.last) ?
@@ -92,7 +112,7 @@ class HtmlToCss
92
112
  tally
93
113
  end
94
114
 
95
- def scan_to_css(e, indent='', parent_selector='')
115
+ def scan_to_css(type, e, indent='', parent_selector='', &blk)
96
116
 
97
117
  return if @nocss.include? e.name
98
118
  h = e.attributes
@@ -102,21 +122,33 @@ class HtmlToCss
102
122
  else
103
123
  selector = (parent_selector + ' ' + e.name).strip
104
124
  end
105
-
106
- return if @nocss.detect {|x| selector =~ /#{x}/ }
125
+
126
+ return if @nolayoutcss.detect {|x| selector =~ /#{x}/ } and type == :layout
107
127
 
108
128
  unless @selectors.include? selector then
109
129
 
110
130
  @selectors << selector
111
131
 
112
132
  if @elements.has_key?(e.name.to_sym) then
113
- attributes = @elements[e.name.to_sym].strip.sub(':color','#a4f').to_h
133
+ attributes = @elements[e.name.to_sym].strip.sub(':color','#a5f').to_h
114
134
  else
115
135
  attributes = {}
116
136
  end
117
-
118
- attr = attributes.merge!(h[:style].to_h)
119
- .map{|x| x.join(': ')}.join(";\n" + indent + ' ')
137
+
138
+ h_attributes = attributes.merge! h[:style].to_h
139
+
140
+ if type == :layout then
141
+
142
+ %w(font color font-family font-size background-image border line-height
143
+ list-style-type font-weight font-style)\
144
+ .each {|x| h_attributes.delete x if h_attributes.has_key? x}
145
+ elsif type == :style and @layout_selectors.include? selector
146
+
147
+ %w(float margin padding clear align width height display overflow)\
148
+ .each {|x| h_attributes.delete x if h_attributes.has_key? x}
149
+ end
150
+
151
+ attr = h_attributes.map{|x| x.join(': ')}.join(";\n" + indent + ' ')
120
152
 
121
153
  @css << indent + selector + " {\n#{indent} #{attr}\n#{indent}}"
122
154
 
@@ -126,13 +158,13 @@ class HtmlToCss
126
158
 
127
159
  indent += ' '
128
160
  e.elements.each do |x|
129
- scan_to_css x, indent, parent_selector
161
+ scan_to_css type, x, indent, parent_selector
130
162
  end
131
163
  end
132
164
 
133
- def select_layout_elements()
165
+ def select_layout_elements(doc)
134
166
 
135
- a = @doc.root.xpath '//div'
167
+ a = doc.root.xpath '//div'
136
168
  a.reverse.each do |e|
137
169
 
138
170
  if not e.attributes[:id] then
@@ -152,4 +184,4 @@ end
152
184
 
153
185
  if __FILE__ == $0 then
154
186
  css = HtmlToCss.new('index.html').to_css
155
- end
187
+ end
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file