TriviaMD 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/trivia_parser.rb +52 -33
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bb0784d7cbf400d2e11cadfbcb9d8f7710b43e640009d131b3fd0edbc677725
4
- data.tar.gz: baca2775605b69b019151f544bd8efd81ba76d7388011c31d4a2e43a7d987aa1
3
+ metadata.gz: 19835c608ce43770c06cb5445f861fbcc0bff7cc8c21fc6f00f13e6f6c31b557
4
+ data.tar.gz: bd1ad6450a61bda0422c1c4dfeec351306d2744c1a902a3f6e3aee8b02d25ff1
5
5
  SHA512:
6
- metadata.gz: 39731789dbcde7931afd06c1b12c4d53ff5d1aad82e682279bdefcb1e8fb6ef6f6be13ecf8fe0bde06a9037ad6de486957f3f6807bd1f690b0d8550294cfcbb1
7
- data.tar.gz: da71ebed756d91ac6c3b396e1e7a60bad35c57082fe443e8a4c81a5c5e5f6ad5430585a5f7d7c77afc2c2cfb144f7b05dffd89d5400c7de070d1c48dbb745109
6
+ metadata.gz: 1861a36257a03a037e77c88fc7d425d799687f4039c07e77b2280c6e16d5c8a37e8b2039b5b4b5b26870f813119f7a8d1e4be7f8ec8b559309c46a33625304b4
7
+ data.tar.gz: 4173ebda5265d03df1532900ff5400c8b38c106302d4cfe053a08ba41be06e9a83f25b0edebd313ee1913a5207e8efcba352a359e011329a82d0712fdc1b5c82
data/lib/trivia_parser.rb CHANGED
@@ -6,51 +6,70 @@ class TriviaParser
6
6
  @debug = debug
7
7
  end
8
8
 
9
+ def get_event(c)
10
+ return { type: :event_trivia_header } if c.type == :header && c.options[:level] == 2 && c.options[:raw_text] == "Trivia"
11
+ return { type: :event_header } if c.type == :header
12
+ return { type: :event_reason, payload: { reason: c.children.first.value } } if c.type == :p && c.children.first.type == :codespan
13
+ return { type: :event_answers, payload: { answers: parse_answers(c) } } if c.type == :ul
14
+ return { type: :event_question, payload: { question: c.children.first.children.first.value } } if c.type == :blockquote
15
+ return { type: :no_event }
16
+ end
17
+
18
+ def parse_answers(c)
19
+ c.children.map do |li|
20
+ head = li.children.first.children.first
21
+ if head.type == :strong
22
+ {
23
+ text: head.children.first.value,
24
+ isAnswer: true
25
+ }
26
+ else
27
+ {
28
+ text: head.value
29
+ }
30
+ end
31
+ end
32
+ end
33
+
9
34
  def parse
10
35
  @doc = Kramdown::Document.new(@file)
11
36
  state = :not_found
12
37
  questions = []
13
38
  question = {}
14
- @doc.root.children.each do |c|
15
- p [state, c.type] if @debug
16
39
 
17
- if state == :trivia_started && c.type == :header && c.options[:level] == 2
18
- state = :not_found
19
- end
40
+ puts '-------------------------------------------' if @debug
41
+
42
+ @doc.root.children.each do |c|
43
+ event = get_event(c)
44
+ puts [state, c.type, event] if @debug
20
45
 
21
- if c.type == :header && c.options[:level] == 2 && c.options[:raw_text] == "Trivia"
46
+ case event[:type]
47
+ when :event_trivia_header
22
48
  state = :trivia_started
23
49
  question = {}
24
- end
25
-
26
- if state == :answer_parsing && c.type == :ul
27
- answers = []
28
- c.children.map do |li|
29
- head = li.children.first.children.first
30
- if head.type == :strong
31
- answers.push({
32
- text: head.children.first.value,
33
- isAnswer: true
34
- })
35
- else
36
- answers.push({
37
- text: head.value
38
- })
39
- end
50
+ when :event_reason
51
+ if state == :reason_parsing
52
+ question[:reason] = event[:payload][:reason]
53
+ state = :trivia_started
40
54
  end
41
-
42
- question[:answers] = answers
43
- state = :trivia_started
44
- end
45
-
46
- if state == :trivia_started && c.type == :blockquote
47
- questions.push(question) unless question.empty?
48
- question = {
49
- question: c.children.first.children.first.value
50
- }
51
- state = :answer_parsing
55
+ when :event_answers
56
+ if state == :answer_parsing
57
+ question[:answers] = event[:payload][:answers]
58
+ state = :reason_parsing
59
+ end
60
+ when :event_question
61
+ if state == :trivia_started || state == :reason_parsing
62
+ questions.push(question) unless question.empty?
63
+ question = {
64
+ question: event[:payload][:question]
65
+ }
66
+ state = :answer_parsing
67
+ end
68
+ when :event_header
69
+ state = :not_found
52
70
  end
53
71
  end
72
+
54
73
  questions.push(question) unless question.empty?
55
74
  questions
56
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: TriviaMD
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bence Pjatacsuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-28 00:00:00.000000000 Z
11
+ date: 2022-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor