module AtCoderFriends::Parser::Sections

parses problem page and builds section table

Public Instance Methods

collect_sections(page) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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