TriviaMD 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/trivia_parser.rb +68 -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: aaa9f99ee0dbed631214a87e322f9e5a1e060e80468c2aa9bab0fb42c56aaa58
4
+ data.tar.gz: 7a46023d6944327e917686fc40e421ea80d9bd021dc51fd4dd2e77c6647e1558
5
5
  SHA512:
6
- metadata.gz: 39731789dbcde7931afd06c1b12c4d53ff5d1aad82e682279bdefcb1e8fb6ef6f6be13ecf8fe0bde06a9037ad6de486957f3f6807bd1f690b0d8550294cfcbb1
7
- data.tar.gz: da71ebed756d91ac6c3b396e1e7a60bad35c57082fe443e8a4c81a5c5e5f6ad5430585a5f7d7c77afc2c2cfb144f7b05dffd89d5400c7de070d1c48dbb745109
6
+ metadata.gz: 07e955e9bd7929ab1adc1934a7a74e1dff752ac199d5d859aae16057c8a99446a5c7e2bd32280ccaf3d63d60dd73f32e698642716b4b143e16c434985f4c0f4e
7
+ data.tar.gz: b979eb1de13ef3b65bb79ab04b4e5959b294ecb7171c503268d181e724f36d3867fd89ce8e2c9e60580d906ac2e457f81f7ec18d7681f8b56cb21e77683aba42
data/lib/trivia_parser.rb CHANGED
@@ -6,49 +6,84 @@ class TriviaParser
6
6
  @debug = debug
7
7
  end
8
8
 
9
+
10
+ def event_trivia_header(c)
11
+ c.type == :header && c.options[:level] == 2 && c.options[:raw_text] == "Trivia"
12
+ end
13
+
14
+ def event_header(c)
15
+ c.type == :header
16
+ end
17
+
18
+ def event_reason(c)
19
+ c.type == :p && c.children.first.type == :codespan
20
+ end
21
+
22
+ def event_answers(c)
23
+ c.type == :ul
24
+ end
25
+
26
+ def event_question(c)
27
+ c.type == :blockquote
28
+ end
29
+
30
+ def get_event(c)
31
+ return :event_trivia_header if event_trivia_header(c)
32
+ return :event_header if event_header(c)
33
+ return :event_reason if event_reason(c)
34
+ return :event_answers if event_answers(c)
35
+ return :event_question if event_question(c)
36
+ return :no_event
37
+ end
38
+
39
+
40
+ def parse_answers(c)
41
+ c.children.map do |li|
42
+ head = li.children.first.children.first
43
+ if head.type == :strong
44
+ {
45
+ text: head.children.first.value,
46
+ isAnswer: true
47
+ }
48
+ else
49
+ {
50
+ text: head.value
51
+ }
52
+ end
53
+ end
54
+ end
9
55
  def parse
10
56
  @doc = Kramdown::Document.new(@file)
11
57
  state = :not_found
12
58
  questions = []
13
59
  question = {}
60
+ puts '-------------------------------------------'
14
61
  @doc.root.children.each do |c|
15
- p [state, c.type] if @debug
62
+ p [state, c.type, get_event(c)]
63
+ event = get_event(c)
16
64
 
17
- if state == :trivia_started && c.type == :header && c.options[:level] == 2
18
- state = :not_found
19
- end
20
-
21
- if c.type == :header && c.options[:level] == 2 && c.options[:raw_text] == "Trivia"
65
+ case event
66
+ when :event_trivia_header
22
67
  state = :trivia_started
23
68
  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
69
+ when :event_reason
70
+ if state == :reason_parsing
71
+ question[:reason] = c.children.first.value
72
+ state = :trivia_started
73
+ end
74
+ when :event_answers
75
+ if state == :answer_parsing
76
+ question[:answers] = parse_answers(c)
77
+ state = :reason_parsing
78
+ end
79
+ when :event_question
80
+ if state == :trivia_started || state == :reason_parsing
81
+ questions.push(question) unless question.empty?
82
+ question = {
83
+ question: c.children.first.children.first.value
84
+ }
85
+ state = :answer_parsing
40
86
  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
52
87
  end
53
88
  end
54
89
  questions.push(question) unless question.empty?
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.2
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-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor