TriviaMD 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trivia_parser.rb +52 -33
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19835c608ce43770c06cb5445f861fbcc0bff7cc8c21fc6f00f13e6f6c31b557
|
4
|
+
data.tar.gz: bd1ad6450a61bda0422c1c4dfeec351306d2744c1a902a3f6e3aee8b02d25ff1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
46
|
+
case event[:type]
|
47
|
+
when :event_trivia_header
|
22
48
|
state = :trivia_started
|
23
49
|
question = {}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
question
|
50
|
-
|
51
|
-
|
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.
|
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-
|
11
|
+
date: 2022-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|