Module: AtCoderFriends::Parser::Sections

Includes:
SectionsConstants
Defined in:
lib/at_coder_friends/parser/sections.rb

Overview

parses problem page and builds section table

Constant Summary

Constants included from SectionsConstants

AtCoderFriends::Parser::SectionsConstants::SECTION_DEFS

Class Method Summary collapse

Class Method Details

.collect_sections(page) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/at_coder_friends/parser/sections.rb', line 81

def collect_sections(page)
  %w[h2 h3].each_with_object({}) do |tag, sections|
    page
      .search(tag)
      .each do |h|
        key = find_key(h)
        key && sections[key] ||= SectionWrapper.new(h)
      end
  end
end

.find_key(h) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/at_coder_friends/parser/sections.rb', line 92

def find_key(h)
  title = normalize(h.content)
  SECTION_DEFS.each do |grp|
    grp[:patterns].each do |pat|
      next unless (m = title.match(/#{pat}/i))

      no = m.names.include?('no') && m['no'] || '1'
      return format(grp[:key], no: no)
    end
  end
  nil
end

.normalize(s) ⇒ Object



105
106
107
108
109
110
# File 'lib/at_coder_friends/parser/sections.rb', line 105

def normalize(s)
  s
    .tr(' 0-9A-Za-z', ' 0-9A-Za-z')
    .gsub(/[^一-龠_ぁ-ん_ァ-ヶーa-zA-Z0-9 ]/, '')
    .strip
end

.process(pbm) ⇒ Object



76
77
78
79
# File 'lib/at_coder_friends/parser/sections.rb', line 76

def process(pbm)
  sections = collect_sections(pbm.page)
  pbm.sections = sections
end