Class: Cocoadex::Entity

Inherits:
Element show all
Defined in:
lib/cocoadex/models/entity.rb

Overview

A top level element, roughly equivalent to one page of documentation

Direct Known Subclasses

Class, GenericRef

Instance Attribute Summary collapse

Attributes inherited from Element

#name

Instance Method Summary collapse

Methods inherited from Element

#<=>, #origin, #parse_parameters, #print, #to_s, #type

Methods included from Bri::Templates::Helpers

h3, inline_title, wrap

Constructor Details

#initialize(path) ⇒ Entity

Returns a new instance of Entity.



7
8
9
10
11
12
# File 'lib/cocoadex/models/entity.rb', line 7

def initialize path
  @path    = path
  text     = clean(IO.read(path, :mode => 'rb'))
  document = Nokogiri::HTML(text)
  parse(document)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/cocoadex/models/entity.rb', line 5

def path
  @path
end

Instance Method Details

#clean(text) ⇒ Object

Remove leading and trailing whitespace from lines, while stripping HTML tags



16
17
18
# File 'lib/cocoadex/models/entity.rb', line 16

def clean text
  text.gsub(/(\n|\t|\r)/, ' ').gsub(/>\s*</, '><').squeeze(' ')
end

#section_by_title(doc, title) ⇒ Object



24
25
26
# File 'lib/cocoadex/models/entity.rb', line 24

def section_by_title doc, title
  doc.css("section").to_a.detect {|s| s.css("h2.jump").text == title }
end

#strip(text) ⇒ Object



20
21
22
# File 'lib/cocoadex/models/entity.rb', line 20

def strip text
  text.gsub("&#xA0;&#xA0;","")
end