TriviaMD 0.0.4
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.
- checksums.yaml +7 -0
- data/bin/trivia-md +3 -0
- data/lib/trivia_md.rb +35 -0
- data/lib/trivia_parser.rb +57 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 01f6a69dcddc8b3180b7e01ebf2cf0514926f54d618aef0fcef1a402d1c77a78
|
4
|
+
data.tar.gz: 70fb3d6f4d38affffb37fa86436396bd89d5a8700cb3e05bdbffa4fa7ed38c1a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b03cbe0ae80a5a1cf8416385a41e291d000e75e2554f6977b2919329c288ba04d37121f489eed2d921f39470e56192ce49cb60e20523776a372cb13767349da7
|
7
|
+
data.tar.gz: 49a26d6b87143c1abfa97b032f7b596025fc0b9688cc3d2999d960a8ab1d0f7f60a4e8af32bc983b1a9d9af7e1bf4da43406ca9a2b00e390339fedc7ab47aa6d
|
data/bin/trivia-md
ADDED
data/lib/trivia_md.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "rubygems" # ruby1.9 doesn't "require" it though
|
3
|
+
require 'thor'
|
4
|
+
require 'kramdown'
|
5
|
+
require_relative './trivia_parser'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class SubCommandBase < Thor
|
9
|
+
def self.banner(command, namespace = nil, subcommand = false)
|
10
|
+
"#{basename} #{subcommand_prefix} #{command.usage}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.subcommand_prefix
|
14
|
+
self.name.gsub(%r{.*::}, '').gsub(%r{^[A-Z]}) { |match| match[0].downcase }.gsub(%r{[A-Z]}) { |match| "-#{match[0].downcase}" }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module App
|
19
|
+
class Trivia < SubCommandBase
|
20
|
+
include Thor::Actions
|
21
|
+
|
22
|
+
desc "parse", "parses trivias"
|
23
|
+
option :file_path
|
24
|
+
def parse(file_path)
|
25
|
+
puts TriviaParser.new(File.read(file_path)).parse.to_json
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class CLI < Thor
|
30
|
+
desc "trivia", "parse trivias"
|
31
|
+
subcommand "trivia", Trivia
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
App::CLI.start
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'kramdown'
|
2
|
+
|
3
|
+
class TriviaParser
|
4
|
+
def initialize(file, debug = false)
|
5
|
+
@file = file
|
6
|
+
@debug = debug
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse
|
10
|
+
@doc = Kramdown::Document.new(@file)
|
11
|
+
state = :not_found
|
12
|
+
questions = []
|
13
|
+
question = {}
|
14
|
+
@doc.root.children.each do |c|
|
15
|
+
p [state, c.type] if @debug
|
16
|
+
|
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"
|
22
|
+
state = :trivia_started
|
23
|
+
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
|
40
|
+
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
|
+
end
|
53
|
+
end
|
54
|
+
questions.push(question) unless question.empty?
|
55
|
+
questions
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: TriviaMD
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bence Pjatacsuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Parses markdown for trivia questions
|
14
|
+
email: pjatacsuk@gmail.com
|
15
|
+
executables:
|
16
|
+
- trivia-md
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/trivia-md
|
21
|
+
- lib/trivia_md.rb
|
22
|
+
- lib/trivia_parser.rb
|
23
|
+
homepage: https://rubygems.org/gems/trivia_md
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubygems_version: 3.2.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Trivia parser!
|
46
|
+
test_files: []
|