Class: AtCoderFriends::Parser::SectionWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/at_coder_friends/parser/section_wrapper.rb

Overview

holds a section of problrem page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ SectionWrapper

Returns a new instance of SectionWrapper.



9
10
11
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 9

def initialize(h)
  @h = h
end

Instance Attribute Details

#hObject (readonly)

Returns the value of attribute h.



7
8
9
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 7

def h
  @h
end

Instance Method Details

#code_block(mtd) ⇒ Object



53
54
55
56
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 53

def code_block(mtd)
  elem = find_element(%w[pre blockquote])
  (elem && elem.send(mtd).lstrip.gsub("\r\n", "\n")) || ''
end

#code_block_contentObject



45
46
47
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 45

def code_block_content
  @code_block_content ||= code_block(:content)
end

#code_block_htmlObject



49
50
51
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 49

def code_block_html
  @code_block_html ||= code_block(:to_html)
end

#contentObject



25
26
27
28
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 25

def content
  @content ||=
    siblings.reduce('') { |m, node| m + node.content }.gsub("\r\n", "\n")
end

#find_element(tags) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 35

def find_element(tags)
  elem = nil
  siblings.any? do |node|
    tags.any? do |tag|
      elem = node.name == tag ? node : node.search(tag)[0]
    end
  end
  elem
end

#htmlObject



30
31
32
33
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 30

def html
  @html ||=
    siblings.reduce('') { |m, node| m + node.to_html }.gsub("\r\n", "\n")
end

#siblingsObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 13

def siblings
  @siblings ||= begin
    ret = []
    nx = h.next
    while nx && nx.name != h.name
      ret << nx
      nx = nx.next
    end
    ret
  end
end