markbates-content_o_matic 0.0.1.20090720170922 → 0.0.2.20090721162019

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,15 +5,11 @@ module ContentOMatic
5
5
  attr_accessor :status
6
6
  attr_accessor :body
7
7
  attr_accessor :url
8
- attr_accessor :hpricot_doc
9
8
 
10
9
  def initialize(url, status, body = '')
11
10
  self.url = url
12
11
  self.status = status.to_i
13
12
  self.body = body
14
- if success?
15
- self.hpricot_doc = Hpricot(self.body)
16
- end
17
13
  end
18
14
 
19
15
  def success?
@@ -32,10 +28,33 @@ module ContentOMatic
32
28
  return self.body != self.html_body
33
29
  end
34
30
 
35
- def html_body
31
+ def body(normalize_assets = false)
32
+ if normalize_assets
33
+ unless @__normalized_body
34
+ @__normalized_body = @body.dup
35
+ doc = Nokogiri::HTML(@__normalized_body)
36
+ {'img' => 'src', 'script' => 'src',
37
+ 'link' => 'href', 'a' => 'href',
38
+ 'iframe' => 'src', 'form' => 'action',
39
+ 'object' => 'data'}.each do |key, attrib|
40
+ (doc/key).each do |elem|
41
+ o_link = elem[attrib]
42
+ next if o_link.blank?
43
+ elem[attrib] = normalize_asset_url(o_link)
44
+ end
45
+ end
46
+ @__normalized_body = doc.to_s
47
+ end
48
+ return @__normalized_body
49
+ end
50
+ @body
51
+ end
52
+
53
+ def html_body(normalize_assets = false)
36
54
  unless @__doc_body
37
- @__doc_body = self.hpricot_doc.at('body')
38
- @__doc_body = @__doc_body.nil? ? self.body : @__doc_body.inner_html
55
+ doc = Nokogiri::HTML(self.body(normalize_assets))
56
+ @__doc_body = doc.at('body')
57
+ @__doc_body = @__doc_body.nil? ? self.body(normalize_assets) : @__doc_body.inner_html
39
58
  end
40
59
  return @__doc_body
41
60
  end
@@ -44,6 +63,26 @@ module ContentOMatic
44
63
  ContentOMatic::InvalidResponseError.new("URL: '#{self.url}' did not return a valid response! Status: '#{self.status}' Body: '#{self.body}'") unless self.success?
45
64
  end
46
65
 
66
+ private
67
+ def normalize_asset_url(src)
68
+ unless src.match(/^([a-zA-Z]+):\/\//)
69
+ relative = !src.match(/^\//)
70
+ if relative
71
+ # find the outer most url
72
+ # http://www.example.com/foo/index.html => http://www.example.com/foo
73
+ base = File.dirname(self.url)
74
+ else
75
+ # absolute so find the inner most url
76
+ # http://www.example.com/index.html => http://www.example.com
77
+ base = self.url.scan(/([a-zA-Z]+:\/\/[^\/]+)/).first
78
+ end
79
+ # bad: <img src="http://www.example.com/full_page.html/image2.jpg" />
80
+ n_src = File.join(base, src)
81
+ return n_src
82
+ end
83
+ return src
84
+ end
85
+
47
86
  end # Response
48
87
 
49
88
  end # ContentOMatic
@@ -8,18 +8,22 @@ module ActionView
8
8
  if options.has_key?(:print_comments)
9
9
  comments = options.delete(:print_comments)
10
10
  end
11
+ html_body = true
12
+ if options.has_key?(:html_body)
13
+ html_body = options.delete(:html_body)
14
+ end
15
+ normalize_assets = true
16
+ if options.has_key?(:normalize_assets)
17
+ normalize_assets = options.delete(:normalize_assets)
18
+ end
11
19
  text = "<!-- Loading Content: '#{slug}' -->\n" if comments
12
20
  begin
13
- html_body = true
14
- if options.has_key?(:html_body)
15
- html_body = options.delete(:html_body)
16
- end
17
21
  res = ContentOMatic.get(slug, options)
18
22
  if res.success?
19
23
  if html_body
20
- text << res.html_body
24
+ text << res.html_body(normalize_assets)
21
25
  else
22
- text << res.body
26
+ text << res.body(normalize_assets)
23
27
  end
24
28
  else
25
29
  raise res.exception
@@ -1,4 +1,4 @@
1
- require 'hpricot'
1
+ require 'nokogiri'
2
2
  require 'configatron'
3
3
  require 'cachetastic'
4
4
  require 'logger'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markbates-content_o_matic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.20090720170922
4
+ version: 0.0.2.20090721162019
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-20 00:00:00 -07:00
12
+ date: 2009-07-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: hpricot
16
+ name: nokogiri
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement