Class: Matryoshka::Document::Html
- Inherits:
-
Object
- Object
- Matryoshka::Document::Html
- Defined in:
- lib/matryoshka/document/html.rb
Defined Under Namespace
Classes: Merge
Constant Summary collapse
- EXTERNAL_SEARCH_PATTERN =
'[@rel="external"]'
- PROXY_SEARCH_PATTERN =
'[@rel="proxy"]'
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#content ⇒ Object
Returns the value of attribute content.
-
#headers ⇒ Object
this needs to update headers like ‘Content-Length’ and remove Matryoshka-specific ones Might need to make this cleaned_headers or something.
-
#path ⇒ Object
Returns the value of attribute path.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_link(elem, attribute, base_dir) ⇒ Object
- #call(env) ⇒ Object
- #closest_directory(path) ⇒ Object
- #convert_proxy_links(proxy_base) ⇒ Object
- #externals_loaded? ⇒ Boolean
-
#initialize(rack_array) ⇒ Html
constructor
A new instance of Html.
- #is_absolute?(uri) ⇒ Boolean
- #load_externals ⇒ Object
- #make_links_absolute(base_url, replace_pairs = {'a'=>'href', 'img'=>'src'}) ⇒ Object
- #prepare_proxy(full_request_path = '') ⇒ Object
- #proxy? ⇒ Boolean
- #proxy_loaded? ⇒ Boolean
Constructor Details
#initialize(rack_array) ⇒ Html
Returns a new instance of Html.
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/matryoshka/document/html.rb', line 124 def initialize(rack_array) self.status = rack_array[0] self.headers = rack_array[1] html = rack_array[2] if html.respond_to?(:to_html) self.content = html else self.body = html end load_externals unless externals_loaded? end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
6 7 8 |
# File 'lib/matryoshka/document/html.rb', line 6 def body @body end |
#content ⇒ Object
Returns the value of attribute content.
6 7 8 |
# File 'lib/matryoshka/document/html.rb', line 6 def content @content end |
#headers ⇒ Object
this needs to update headers like ‘Content-Length’ and remove Matryoshka-specific ones Might need to make this cleaned_headers or something
159 160 161 |
# File 'lib/matryoshka/document/html.rb', line 159 def headers @headers end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/matryoshka/document/html.rb', line 6 def path @path end |
#status ⇒ Object
Returns the value of attribute status.
6 7 8 |
# File 'lib/matryoshka/document/html.rb', line 6 def status @status end |
Class Method Details
.accepts ⇒ Object
149 150 151 |
# File 'lib/matryoshka/document/html.rb', line 149 def self.accepts ['text/html'] end |
.extentions ⇒ Object
145 146 147 |
# File 'lib/matryoshka/document/html.rb', line 145 def self.extentions ['.html','.htm'] end |
Instance Method Details
#absolute_link(elem, attribute, base_dir) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/matryoshka/document/html.rb', line 117 def absolute_link(elem,attribute,base_dir) relative_uri = elem.attributes[attribute] unless (relative_uri.include?(base_dir) or is_absolute?(relative_uri)) elem.set_attribute(attribute, (base_dir + relative_uri)) end end |
#call(env) ⇒ Object
166 167 168 169 |
# File 'lib/matryoshka/document/html.rb', line 166 def call(env) # content = IO.read Matryoshka::ROOT_DIR + '/public/index.html' [status, headers, body] end |
#closest_directory(path) ⇒ Object
53 54 55 |
# File 'lib/matryoshka/document/html.rb', line 53 def closest_directory(path) path[/\A.+\//] end |
#convert_proxy_links(proxy_base) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/matryoshka/document/html.rb', line 57 def convert_proxy_links(proxy_base) (content/'a').each do |link| href = link.attributes['href'] # Remove base of urls that we are proxying. href.sub!(/\A#{proxy_base}/,'') # Except for absolute links, add template path to complete relative urls unless true #is_absolute?(href) if href[/\A\//] # link begins with / href = '?' else href = closest_directory(@headers[:template_path]) + href end link.set_attribute('href', href) end case href when /\A#{proxy_base}/ href.sub!(/#{proxy_base}/,'').sub!(/\A\//,'') when /\A#{URI.parse(proxy_base).path}/ href.sub!(/#{URI.parse(proxy_base).path}/,'').sub!(/\A\//,'') end unless is_absolute?(href) href = closest_directory(@headers[:template_path]) + href end link.set_attribute('href', href) end end |
#externals_loaded? ⇒ Boolean
8 9 10 |
# File 'lib/matryoshka/document/html.rb', line 8 def externals_loaded? @externals_loaded end |
#is_absolute?(uri) ⇒ Boolean
113 114 115 |
# File 'lib/matryoshka/document/html.rb', line 113 def is_absolute?(uri) uri[/\Ahttp:\/\//] end |
#load_externals ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/matryoshka/document/html.rb', line 90 def load_externals (content/EXTERNAL_SEARCH_PATTERN).each do |element| external = self.class.new(Matryoshka::External.find(element[:href])) external.make_links_absolute(element[:href]) if external.status == 200 Merge.new(element,external.content,Merge::EXTERNAL_MERGE).run # Cleanup external code element.remove_attribute(:href) element.remove_attribute(:rel) end end @externals_loaded = true end |
#make_links_absolute(base_url, replace_pairs = {'a'=>'href', 'img'=>'src'}) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/matryoshka/document/html.rb', line 104 def make_links_absolute(base_url, replace_pairs = {'a'=>'href', 'img'=>'src'}) base_dir = base_url[/\A.+\//] replace_pairs.each_pair do |search_pattern, attribute| (content/search_pattern).each do |link| absolute_link link, attribute, base_dir end end end |
#prepare_proxy(full_request_path = '') ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/matryoshka/document/html.rb', line 23 def prepare_proxy(full_request_path = '') element = content.at PROXY_SEARCH_PATTERN proxy_link = if full_request_path == @headers[:template_path] element[:href] else closest_directory(element[:href]) + full_request_path.sub(/\A#{closest_directory(@headers[:template_path])}/,'') end # Need to prime external with template_path external = self.class.new(Matryoshka::External.find(proxy_link, {:template_path => @headers[:template_path]})) # external.make_links_absolute(element[:href]) base_dir = closest_directory(element[:href]) # if doc is not a full (<html><head> ...) don't bother with transform? # merge the external with a copy of the element # convoluted copy element (.dup is weird with Hpricot) element_copy = Hpricot(element.to_html).at '*' external.content = Merge.new(element_copy,external.content,Merge::EXTERNAL_MERGE).run external.convert_proxy_links(base_dir) external.make_links_absolute(base_dir,{'img'=>'src'}) # Tag the element and the proxy doc so that they cleanly merge. # This is to keep the pattern established in answer.rb element.set_attribute('id', 'matryoshka_proxy_target') external.content.set_attribute('id','matryoshka_proxy_target') external end |
#proxy? ⇒ Boolean
19 20 21 |
# File 'lib/matryoshka/document/html.rb', line 19 def proxy? content.at PROXY_SEARCH_PATTERN end |
#proxy_loaded? ⇒ Boolean
12 13 14 |
# File 'lib/matryoshka/document/html.rb', line 12 def proxy_loaded? @proxy_loaded end |