Class: AtCoderFriends::Parser::SectionWrapper

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

Overview

holds section in 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_blockObject



41
42
43
44
45
46
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 41

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

#contentObject



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

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

#find_element(tags) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/at_coder_friends/parser/section_wrapper.rb', line 31

def find_element(tags)
  siblings.each do |node|
    tags.each do |tag|
      elem = node.name == tag ? node : node.search(tag)[0]
      return elem if elem
    end
  end
  nil
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