Class: TriviaParser
- Inherits:
-
Object
- Object
- TriviaParser
- Includes:
- Kramdown::Utils::Html
- Defined in:
- lib/trivia_parser.rb
Instance Method Summary collapse
- #get_event(c) ⇒ Object
-
#initialize(file, debug = false) ⇒ TriviaParser
constructor
A new instance of TriviaParser.
- #parse ⇒ Object
- #parse_answers(c) ⇒ Object
- #parse_reason(c) ⇒ Object
Constructor Details
#initialize(file, debug = false) ⇒ TriviaParser
Returns a new instance of TriviaParser.
5 6 7 8 |
# File 'lib/trivia_parser.rb', line 5 def initialize(file, debug = false) @file = file @debug = debug end |
Instance Method Details
#get_event(c) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/trivia_parser.rb', line 11 def get_event(c) return { type: :event_trivia_header } if c.type == :header && c.[:level] == 2 && c.[:raw_text] == "Trivia" return { type: :event_header } if c.type == :header return { type: :event_reason, payload: { reason: parse_reason(c) } } if c.type == :codeblock return { type: :event_answers, payload: { answers: parse_answers(c) } } if c.type == :ul return { type: :event_question, payload: { question: c.children.first.children.first.value } } if c.type == :blockquote return { type: :no_event } end |
#parse ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/trivia_parser.rb', line 44 def parse @doc = Kramdown::Document.new(@file) state = :not_found questions = [] question = {} puts '-------------------------------------------' if @debug @doc.root.children.each do |c| event = get_event(c) p [state, c.type, event, c.children] if @debug case event[:type] when :event_trivia_header state = :trivia_started question = {} when :event_reason if state == :reason_parsing question[:reason] = event[:payload][:reason] state = :trivia_started end when :event_answers if state == :answer_parsing question[:answers] = event[:payload][:answers] state = :reason_parsing end when :event_question if state == :trivia_started || state == :reason_parsing questions.push(question) unless question.empty? question = { question: event[:payload][:question] } state = :answer_parsing end when :event_header state = :not_found end end questions.push(question) unless question.empty? questions end |
#parse_answers(c) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/trivia_parser.rb', line 28 def parse_answers(c) c.children.map do |li| head = li.children.first.children.first if head.type == :strong { text: head.children.first.value, isAnswer: true } else { text: head.value } end end end |
#parse_reason(c) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/trivia_parser.rb', line 20 def parse_reason(c) if c.[:lang] == "html" c.value.strip else escape_html(c.value.strip) end end |