Class: Rack::Reshow::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/reshow.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html, path, app) ⇒ Page



21
22
23
24
25
# File 'lib/rack/reshow.rb', line 21

def initialize(html, path, app)
  @html = html
  @path = path
  self.stylesheets!(app)
end

Class Method Details

.tag(type, content, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/rack/reshow.rb', line 11

def tag(type, content, options={})
  options = options.map {|key, value| "#{key}=\"#{value}\""}.join(' ')
  "  <\#{type} \#{options}\">\n    \#{content}\n  </\#{type}>\n  EOF\nend\n"

Instance Method Details

#append_to_tag(tag, string) ⇒ Object



83
84
85
# File 'lib/rack/reshow.rb', line 83

def append_to_tag(tag, string)
  @html.sub! /#{tag}/, string + "\n#{tag}"
end

#bodyObject



39
40
41
# File 'lib/rack/reshow.rb', line 39

def body
  @html.scan(/<body>(.*?)<\/body>/m).flatten.first
end

#body=(content) ⇒ Object



43
44
45
# File 'lib/rack/reshow.rb', line 43

def body=(content)
  @html.sub! /<body>.*<\/body>/m, "<body>#{content}</body>"
end

#eql?(page) ⇒ Boolean



75
76
77
# File 'lib/rack/reshow.rb', line 75

def eql?(page)
  body == page.body and stylesheets == page.stylesheets
end

#headObject



31
32
33
# File 'lib/rack/reshow.rb', line 31

def head
  @html.scan(/<head>(.*?)<\/head>/m).flatten.first
end

#head=(content) ⇒ Object



35
36
37
# File 'lib/rack/reshow.rb', line 35

def head=(content)
  @html.sub! /<head>.*<\/head>/m, "<head>#{content}</head>"
end

#lengthObject



71
72
73
# File 'lib/rack/reshow.rb', line 71

def length
  @html.length
end

#prepend_to_tag(tag, string) ⇒ Object



79
80
81
# File 'lib/rack/reshow.rb', line 79

def prepend_to_tag(tag, string)
  @html.sub! /#{tag}/, "#{tag}\n" + string
end

#stylesheetsObject



47
48
49
# File 'lib/rack/reshow.rb', line 47

def stylesheets
  @stylesheets.each_value.to_a.join
end

#stylesheets!(app) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rack/reshow.rb', line 51

def stylesheets!(app)
  @stylesheets ||= begin
    @stylesheets = {}
    links = head.scan(/<link.*?rel=['|"]+stylesheet['|"]+.*?>/)
    links.each do |link|
      href = link.scan(/href=['|"]+(.*?)['|"]+/).flatten.first
      if href[/https?:\/\//] 
        @stylesheets[href] = open(href).read
      else
        href = @path + href unless href[0] == ?/
        status, headers, body = app.call Rack::MockRequest.env_for(href)
        sheet = ''
        body.each {|part| sheet << part}
        @stylesheets[href] = sheet
      end
    end
    @stylesheets
  end
end

#to_sObject



27
28
29
# File 'lib/rack/reshow.rb', line 27

def to_s
  @html
end