Class: Spiral::DisplayData

Inherits:
Object
  • Object
show all
Defined in:
lib/spiral/display_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, local, chm) ⇒ DisplayData

Returns a new instance of DisplayData.



5
6
7
8
9
# File 'lib/spiral/display_data.rb', line 5

def initialize(title, local, chm)
  @title = title
  @local = local
  @chm = chm
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/spiral/display_data.rb', line 3

def content
  @content
end

#localObject (readonly)

Returns the value of attribute local.



2
3
4
# File 'lib/spiral/display_data.rb', line 2

def local
  @local
end

#titleObject (readonly)

Returns the value of attribute title.



2
3
4
# File 'lib/spiral/display_data.rb', line 2

def title
  @title
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/spiral/display_data.rb', line 39

def available?
  !!@content
end

#content_keyObject



11
12
13
# File 'lib/spiral/display_data.rb', line 11

def content_key
  [@chm, @local].hash
end

#heightObject



43
44
45
# File 'lib/spiral/display_data.rb', line 43

def height
  @content.lines.size
end

#retrieveObject



32
33
34
35
36
37
# File 'lib/spiral/display_data.rb', line 32

def retrieve
  id = @local.split("#")[1]
  html = @chm.retrieve_object("/#{@local.gsub(/#.*/, '')}") rescue "[Error]"
  html = trim(html, id) if id
  @content = ::Spiral::Renderer::W3m.new.render(html, :cols => $stdout.winsize.last)
end

#trim(html, id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spiral/display_data.rb', line 15

def trim(html, id)
  doc = Nokogiri::XML.parse html
  e = doc.at_css("##{id}")

  p = nil
  begin
    p = e.parent rescue nil
    ep = e.previous rescue nil
    while e = ep
      ep = e.previous
      e.remove
    end
  end while e = p

  doc.to_s
end