zevarito-undress 0.2.3 → 0.2.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.
Files changed (3) hide show
  1. data/lib/hpricot_ext.rb +88 -0
  2. data/undress.gemspec +2 -1
  3. metadata +2 -1
@@ -0,0 +1,88 @@
1
+ require "hpricot"
2
+
3
+ module ::Hpricot #:nodoc:
4
+ module Elem::Trav
5
+ def set_style(name, value)
6
+ styles[name.to_s] = value.fast_xs
7
+ end
8
+
9
+ def del_style(name)
10
+ styles.delete(name)
11
+ end
12
+
13
+ def has_style?(name)
14
+ styles.has_style?(name)
15
+ end
16
+
17
+ def get_style(name)
18
+ styles[name]
19
+ end
20
+ end
21
+
22
+ class Styles
23
+ def initialize e
24
+ @element = e
25
+ end
26
+
27
+ def delete(key)
28
+ p = properties.dup
29
+ if p.delete key
30
+ @element.set_attribute("style", "#{p.map {|pty,val| "#{pty}:#{val}"}.join(";")}")
31
+ end
32
+ end
33
+
34
+ def [] key
35
+ properties[key]
36
+ end
37
+
38
+ def []= k, v
39
+ s = properties.map {|pty,val| "#{pty}:#{val}"}.join(";")
40
+ @element.set_attribute("style", "#{s.chomp(";")};#{k}:#{v}".sub(/^\;/, ""))
41
+ end
42
+
43
+ def empty?
44
+ return true if properties.size == 0
45
+ end
46
+
47
+ def has_style?(key)
48
+ properties.has_key?(key)
49
+ end
50
+
51
+ def to_s
52
+ properties.to_s
53
+ end
54
+
55
+ def to_h
56
+ properties
57
+ end
58
+
59
+ def properties
60
+ return {} if not @element.has_attribute?("style")
61
+ @element.get_attribute("style").split(";").inject({}) do |hash,v|
62
+ v = v.split(":")
63
+ hash.update v.first.strip => v.last.strip
64
+ end
65
+ end
66
+ end
67
+
68
+ class Elem #:nodoc:
69
+ def ancestors
70
+ node, ancestors = parent, Elements[]
71
+ while node.respond_to?(:parent) && node.parent
72
+ ancestors << node
73
+ node = node.parent
74
+ end
75
+ ancestors
76
+ end
77
+
78
+ def change_tag!(new_tag, preserve_attr = true)
79
+ return if not etag
80
+ self.name = new_tag
81
+ attributes.each {|k,v| remove_attribute(k)} if not preserve_attr
82
+ end
83
+
84
+ def styles
85
+ Styles.new self
86
+ end
87
+ end
88
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "undress"
3
- s.version = "0.2.3"
3
+ s.version = "0.2.4"
4
4
  s.date = "2009-07-29"
5
5
 
6
6
  s.description = "Simply translate HTML to Textile, Markdown, or whatever other markup format you need"
@@ -30,6 +30,7 @@ README.rdoc
30
30
  Rakefile
31
31
  undress.gemspec
32
32
  lib/undress.rb
33
+ lib/hpricot_ext.rb
33
34
  lib/undress/grammar.rb
34
35
  lib/undress/textile.rb
35
36
  lib/undress/greencloth.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zevarito-undress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Nicol\xC3\xA1s Sanguinetti"
@@ -67,6 +67,7 @@ files:
67
67
  - Rakefile
68
68
  - undress.gemspec
69
69
  - lib/undress.rb
70
+ - lib/hpricot_ext.rb
70
71
  - lib/undress/grammar.rb
71
72
  - lib/undress/textile.rb
72
73
  - lib/undress/greencloth.rb