|
@@ -0,0 +1,76 @@
|
|
1
|
+require 'figlet'
|
|
2
|
+require 'sinatra'
|
|
3
|
+require 'dotenv/load'
|
|
4
|
+
|
|
5
|
+def fig(input)
|
|
6
|
+ opts = parse(input)
|
|
7
|
+
|
|
8
|
+ banners = []
|
|
9
|
+
|
|
10
|
+ font = Figlet::Font.new(File.expand_path('./lib/banner.flf'))
|
|
11
|
+ figlet = Figlet::Typesetter.new(font)
|
|
12
|
+
|
|
13
|
+ chunks = opts[:message].chars.to_a.each_slice(5).to_a.map{|chunk| chunk.join}
|
|
14
|
+
|
|
15
|
+ chunks.each do |chunk|
|
|
16
|
+ banner = figlet[chunk.strip].gsub(/#/, opts[:ink]).gsub(/ /, opts[:space])
|
|
17
|
+
|
|
18
|
+ unless uses_emoji(opts)
|
|
19
|
+ banner = "```#{banner}```"
|
|
20
|
+ end
|
|
21
|
+
|
|
22
|
+ banners << banner
|
|
23
|
+ end
|
|
24
|
+
|
|
25
|
+ banners.join("\n")
|
|
26
|
+end
|
|
27
|
+
|
|
28
|
+def parse(input)
|
|
29
|
+ opts = {
|
|
30
|
+ :message => String.new,
|
|
31
|
+ :space => " ",
|
|
32
|
+ :ink => "#",
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ position = 0
|
|
36
|
+
|
|
37
|
+ encountered_options = false
|
|
38
|
+
|
|
39
|
+ while position < input.length
|
|
40
|
+ if m = input.slice(position..-1).match(/^space=(\S+)/)
|
|
41
|
+ encountered_options = true
|
|
42
|
+ opts[:space] = m[1].strip
|
|
43
|
+ position += m[0].length
|
|
44
|
+ elsif m = input.slice(position..-1).match(/^ink=(\S+)/)
|
|
45
|
+ encountered_options = true
|
|
46
|
+ opts[:ink] = m[1].strip
|
|
47
|
+ position += m[0].length
|
|
48
|
+ elsif m = input.slice(position..-1).match(/^font=(\S+)/)
|
|
49
|
+ encountered_options = true
|
|
50
|
+ opts[:font] = m[1].strip
|
|
51
|
+ position += m[0].length
|
|
52
|
+ else
|
|
53
|
+ if not encountered_options
|
|
54
|
+ opts[:message] << input[position]
|
|
55
|
+ end
|
|
56
|
+ position += 1
|
|
57
|
+ end
|
|
58
|
+
|
|
59
|
+ end
|
|
60
|
+
|
|
61
|
+ opts[:message] = opts[:message].strip
|
|
62
|
+
|
|
63
|
+ opts
|
|
64
|
+end
|
|
65
|
+
|
|
66
|
+def uses_emoji(opts)
|
|
67
|
+ (opts[:space][0] == ':' and opts[:space][-1] == ':') or (opts[:ink][0] == ':' and opts[:ink][-1] == ':')
|
|
68
|
+end
|
|
69
|
+
|
|
70
|
+post '/' do
|
|
71
|
+ content_type :json
|
|
72
|
+ {
|
|
73
|
+ response_type: 'in_channel',
|
|
74
|
+ text: fig(params[:text])
|
|
75
|
+ }.to_json
|
|
76
|
+end
|