Class: AtCoderFriends::Parser::SectionWrapper
- Inherits:
-
Object
- Object
- AtCoderFriends::Parser::SectionWrapper
- Defined in:
- lib/at_coder_friends/parser/section_wrapper.rb
Overview
holds a section of problrem page
Instance Attribute Summary collapse
-
#h ⇒ Object
readonly
Returns the value of attribute h.
Instance Method Summary collapse
- #code_block(mtd) ⇒ Object
- #code_block_content ⇒ Object
- #code_block_html ⇒ Object
- #content ⇒ Object
- #find_element(tags) ⇒ Object
- #html ⇒ Object
-
#initialize(h) ⇒ SectionWrapper
constructor
A new instance of SectionWrapper.
- #siblings ⇒ Object
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
#h ⇒ Object (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_content ⇒ Object
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_html ⇒ Object
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 |
#content ⇒ Object
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() elem = nil siblings.any? do |node| .any? do |tag| elem = node.name == tag ? node : node.search(tag)[0] end end elem end |
#html ⇒ Object
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 |
#siblings ⇒ Object
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 |