7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/daifuku/parser.rb', line 7
def parse(markdown_str, category_name)
html_str = markdown.render(markdown_str)
html = Nokogiri::HTML(html_str).css('body')
top_level_nodes = html.children
nodes_per_event = {}
common_columns_nodes = []
current_event_name = nil
top_level_nodes.each do |node|
case node.name
when 'h2'
current_event_name = node.content
when 'p', 'ul'
if current_event_name
nodes_per_event[current_event_name] ||= []
nodes_per_event[current_event_name] << node
else
common_columns_nodes << node
end
else
next
end
end
events = nodes_per_event.map { |name_with_annotation, nodes|
event = parse_event(name_with_annotation, nodes)
[event.name, event]
}.to_h
common_event = parse_event(nil, common_columns_nodes)
descriptions = common_event&.descriptions || []
Category.new(category_name, events, descriptions, common_event&.columns || [])
end
|