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_path, debug = false) ⇒ TriviaParser
constructor
A new instance of TriviaParser.
- #markdown_gitiles_url ⇒ Object
- #parse ⇒ Object
- #parse_answers(c) ⇒ Object
- #parse_reason(c) ⇒ Object
Constructor Details
#initialize(file_path, debug = false) ⇒ TriviaParser
Returns a new instance of TriviaParser.
5 6 7 8 9 |
# File 'lib/trivia_parser.rb', line 5 def initialize(file_path, debug = false) @file_path = file_path @file = File.read(@file_path) @debug = debug end |
Instance Method Details
#get_event(c) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/trivia_parser.rb', line 12 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 |
#markdown_gitiles_url ⇒ Object
22 23 24 |
# File 'lib/trivia_parser.rb', line 22 def markdown_gitiles_url "#{ENV.fetch('GITILES_PROJECT_URL', 'https://gitiles.com/')}#{@file_path}" end |
#parse ⇒ Object
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 86 87 88 89 90 91 |
# File 'lib/trivia_parser.rb', line 50 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/trivia_parser.rb', line 34 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
26 27 28 29 30 31 32 |
# File 'lib/trivia_parser.rb', line 26 def parse_reason(c) if c.[:lang] == "html" c.value.strip.gsub(/{GITILES_MD}/,markdown_gitiles_url) else escape_html(c.value.strip) end end |