Class: HTMLDocument

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

Instance Method Summary collapse

Constructor Details

#initializeHTMLDocument

Returns a new instance of HTMLDocument.



16
17
18
19
20
21
22
23
# File 'lib/rbhtml.rb', line 16

def initialize
  @open_doc = "<html><body>"
  @close_doc = "</body></html>"
  @full_doc = @open_doc + @close_doc
  @index_dict = {}
  @index_id = 0
  @count = {}
end

Instance Method Details

#count(tag) ⇒ Object



53
54
55
# File 'lib/rbhtml.rb', line 53

def count tag
  @count[tag]
end

#create_element(tag, inner, attribute = false, value = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbhtml.rb', line 24

def create_element tag, inner, attribute=false, value=false
  if attribute and value
    @open_doc += "<#{tag} #{attribute}='#{value}'>#{inner}</#{tag}>"
  else
    @open_doc += "<#{tag}>#{inner}</#{tag}>"
  end
  @index_id += 1
  if @count.keys.include? tag
    @count[tag] += 1
  else
    @count[tag] = 1
  end
  refresh_full
  index_element tag, inner
end

#remove_char(char) ⇒ Object



49
50
51
52
# File 'lib/rbhtml.rb', line 49

def remove_char char
  @open_doc.gsub!(">#{char}", ">")
  refresh_full
end

#remove_element(tag, inner) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/rbhtml.rb', line 39

def remove_element tag, inner
  if @open_doc.gsub!("<#{tag}>#{inner}</#{tag}>", "") == nil
    return
  else
    @open_doc.gsub!("<#{tag}>#{inner}</#{tag}>", "")
  end
  @close_doc.gsub!("</#{tag}>", "")
  @count[tag] -= 1
  refresh_full
end

#to_sObject



56
57
58
# File 'lib/rbhtml.rb', line 56

def to_s
  @full_doc
end