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



116
117
118
119
120
121
122
123
124
125
# File 'lib/at_coder_friends/parser/sections.rb', line 116

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/at_coder_friends/parser/sections.rb', line 127

def find_key(h)
  title = normalize(h.content)
  key = nil
  SECTION_DEFS.any? do |grp|
    if (m = title.match(grp[:pattern]))
      no = (m.names.include?('no') && m['no']) || '1'
      key = format(grp[:key], no: no)
    end
  end
  key
end

.normalize(s) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/at_coder_friends/parser/sections.rb', line 139

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

.process(pbm) ⇒ Object



109
110
111
112
113
114
# File 'lib/at_coder_friends/parser/sections.rb', line 109

def process(pbm)
  sections = collect_sections(pbm.page)
  div = pbm.page.search('div#task-statement')[0]
  div && sections[Problem::SECTION_INTRO] = IntroductionWrapper.new(div)
  pbm.sections = sections
end