jekyll-hackclub 1.3.0 → 1.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0495d9b427da78fb0c05e06159bebbd4c8b345398450d2962274fc0acd8e1bc4'
4
- data.tar.gz: 81c1e2d3ac7e91326cd00c26a96eb353af2c1b141239aa449005e89d2801545c
3
+ metadata.gz: e4721706e534b10dd13f091c8b30cd6999e19348227e189f4a8a8169b4820b73
4
+ data.tar.gz: b0e2f43bf635c97f7ed581cc71a3f5e1d4b02865baff200721dab04ecb1b6eba
5
5
  SHA512:
6
- metadata.gz: 766b2fdcd3e220a8d75bc500f8232f4dad39bcb3f800c10006c5e2735a1a2acb40ab8f4169b5e1933ae4240d6ffec87d711bbbc4568e2499774cbe8f9c22e049
7
- data.tar.gz: 6cff13dae2e12a3a47aad22d598d53dd6a6f88181621c9a385f3793b2d25253861688b41810ad31498d9513fd90f9460e681c8824d42f27fbcc67701e5e7d34f
6
+ metadata.gz: 7537c156d3097764a069795b97b4ddd5788718ec3e6bcc07579f7e229614c1b640eb19ec95bca9cb84561953b7b0bae837a93d09c715a9a6dfdc7a29de55765c
7
+ data.tar.gz: d1f87e8f43d9e48a68ff789a3d0d165701026e45c9b8423b3221ebb02e629bf887688fd89fb310c11bcee137205268ff4ebb2a0daa9185b8ce358e5755153cba
data/lib/args-parser.rb CHANGED
@@ -1,20 +1,6 @@
1
- module StringManipulation
2
- def self.uppercase(content)
3
- content.upcase
4
- end
5
-
6
- def self.downcase(content)
7
- content.downcase
8
- end
9
-
10
- def self.capitalize(content)
11
- content.capitalize
12
- end
13
-
14
- def self.size(content)
15
- content.size
16
- end
17
- end
1
+ require_relative "./pipes/strings"
2
+ require_relative "./pipes/numbers"
3
+ require_relative "./pipes/arrays"
18
4
 
19
5
  module ArgsParser
20
6
  def self.split_args(content)
@@ -23,13 +9,19 @@ module ArgsParser
23
9
 
24
10
  def self.eval_pipes_with_val(value, pipes)
25
11
  pipes.each do |pipe|
12
+ args = pipe.split(" ") || []
13
+ func = args.shift || pipe
26
14
  if (match = pipe.match(/\[(-?\d+):(-?\d+)\]/))
27
15
  start_idx, end_idx = match[1].to_i, match[2].to_i
28
16
  value = value[start_idx..end_idx]
29
- elsif StringManipulation.respond_to?(pipe)
30
- value = StringManipulation.public_send(pipe, value)
17
+ elsif StringManipulation.respond_to?(func)
18
+ value = StringManipulation.public_send(func, value, args)
19
+ elsif NumbersManipulation.respond_to?(func)
20
+ value = NumbersManipulation.public_send(func, value, args)
21
+ elsif ArraysManipulation.respond_to?(func)
22
+ value = ArraysManipulation.public_send(func, value, args)
31
23
  else
32
- raise ArgumentError, "Unkown pipe '#{pipe}'"
24
+ raise ArgumentError, "Unknown function '#{func}'"
33
25
  end
34
26
  end
35
27
  value
@@ -2,6 +2,9 @@ require_relative "./tags/emoji"
2
2
  require_relative "./tags/mentions"
3
3
  require_relative "./tags/user-tag"
4
4
  require_relative "./tags/channel-tag"
5
- require_relative "./tags/parser"
5
+ require_relative "./tags/usergroup-tag"
6
+ require_relative "./tags/profile-pic"
7
+ require_relative "./tags/file"
6
8
 
9
+ require_relative "./tags/parser"
7
10
  require_relative "./server-bridge"
@@ -0,0 +1,17 @@
1
+ module ArraysManipulation
2
+ def self.first(content, args)
3
+ content = content.first
4
+ end
5
+
6
+ def self.last(content, args)
7
+ content = content.last
8
+ end
9
+
10
+ def self.get(content, args)
11
+ content = content[args[0]]
12
+ end
13
+
14
+ def self.cut(content, args)
15
+ content = content[args[0]..args[1]]
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module NumbersManipulation
2
+ def self.to_int(content, args)
3
+ content.to_i
4
+ end
5
+
6
+ def self.multiply(content, args)
7
+ content * args[0].to_i
8
+ end
9
+
10
+ def self.add(content, args)
11
+ content + args[0].to_i
12
+ end
13
+
14
+ def self.substract(content, args)
15
+ content - args[0].to_i
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module StringManipulation
2
+ def self.uppercase(content, args)
3
+ content.upcase
4
+ end
5
+
6
+ def self.downcase(content, args)
7
+ content.downcase
8
+ end
9
+
10
+ def self.capitalize(content, args)
11
+ content.capitalize
12
+ end
13
+
14
+ def self.size(content, args)
15
+ content.size
16
+ end
17
+ end
data/lib/server-bridge.rb CHANGED
@@ -10,70 +10,64 @@ module HackclubRequest
10
10
  attr_accessor :host
11
11
  end
12
12
 
13
- def self.resolve_emoji(id)
14
- uri = URI("#{host}/emoji/#{id.strip}")
15
- res = Net::HTTP.get_response(uri)
13
+ def self.make_request(path)
14
+ uri = URI("#{host}#{path}")
15
+ req = Net::HTTP::Get.new(uri)
16
+ req['Referer'] = "jekyll-hackclub"
16
17
 
17
- if res.is_a?(Net::HTTPSuccess)
18
- res.body
19
- else
20
- DEFAULT_EMOJI
18
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
19
+ http.request(req)
21
20
  end
21
+
22
+ return JSON.parse(res.body), res
23
+ rescue JSON::ParserError
24
+ return {}, res
25
+ rescue => e
26
+ warn "Request to #{uri} failed: #{e}"
27
+ return {}, nil
28
+ end
29
+
30
+ def self.resolve_emoji(id)
31
+ _, res = make_request("/emoji/#{id.strip}")
32
+ res&.is_a?(Net::HTTPSuccess) ? res.body : DEFAULT_EMOJI
33
+ end
34
+
35
+ def self.raw_file(fileid)
36
+ data, res = make_request("/files.info/#{fileid}")
37
+ res&.is_a?(Net::HTTPSuccess) ? data : {}
22
38
  end
23
39
 
24
40
  def self.raw_user(userid)
25
- uri = URI("#{host}/users.info/#{userid}")
26
- res = Net::HTTP.get_response(uri)
41
+ data, res = make_request("/users.info/#{userid}")
42
+ res&.is_a?(Net::HTTPSuccess) ? data : {}
43
+ end
27
44
 
28
- data = JSON.parse(res.body)
29
- if res.is_a?(Net::HTTPSuccess)
30
- data
31
- else
32
- {}
33
- end
45
+ def self.raw_usergroup(groupid)
46
+ data, res = make_request("/usergroup/#{groupid}")
47
+ res&.is_a?(Net::HTTPSuccess) ? data : {}
34
48
  end
35
49
 
36
- def self.resolve_username(userid)
37
- uri = URI("#{host}/users.info/#{userid}")
38
- res = Net::HTTP.get_response(uri)
50
+ def self.resolve_usergroup(groupid)
51
+ data = raw_usergroup(groupid)
52
+ data["handle"] || "unknown"
53
+ end
39
54
 
40
- begin
41
- data = JSON.parse(res.body)
42
- if res.is_a?(Net::HTTPSuccess)
43
- data.dig("user", "name") || "unknown"
44
- else
45
- "unknown"
46
- end
47
- rescue
48
- "unavailable"
49
- end
55
+ def self.resolve_username(userid)
56
+ data, res = make_request("/users.info/#{userid}")
57
+ res&.is_a?(Net::HTTPSuccess) ? (data.dig("user", "name") || "unknown") : "unknown"
58
+ rescue
59
+ "unavailable"
50
60
  end
51
61
 
52
62
  def self.raw_channel(channelid)
53
- uri = URI("#{host}/conversations.info/#{channelid}")
54
- res = Net::HTTP.get_response(uri)
55
-
56
- data = JSON.parse(res.body)
57
- if res.is_a?(Net::HTTPSuccess)
58
- data
59
- else
60
- {}
61
- end
63
+ data, res = make_request("/conversations.info/#{channelid}")
64
+ res&.is_a?(Net::HTTPSuccess) ? data : {}
62
65
  end
63
66
 
64
67
  def self.resolve_channel(channelid)
65
- uri = URI("#{host}/conversations.info/#{channelid}")
66
- res = Net::HTTP.get_response(uri)
67
-
68
- begin
69
- data = JSON.parse(res.body)
70
- if res.is_a?(Net::HTTPSuccess)
71
- data.dig("channel", "name") || "unknown"
72
- else
73
- "unknown"
74
- end
75
- rescue
76
- "unavailable"
77
- end
68
+ data, res = make_request("/conversations.info/#{channelid}")
69
+ res&.is_a?(Net::HTTPSuccess) ? (data.dig("channel", "name") || "unknown") : "unknown"
70
+ rescue
71
+ "unavailable"
78
72
  end
79
73
  end
data/lib/tags/file.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "jekyll"
2
+ require_relative "../server-bridge"
3
+
4
+ module Jekyll
5
+ class HackclubFileTag < Liquid::Tag
6
+ def initialize(tagName, content, tokens)
7
+ super
8
+ content = content.strip()
9
+ @id, keys, pipes = ArgsParser.split_args(content)
10
+ pipes = pipes || ""
11
+ keys = keys || ""
12
+ keys = keys.split(".")
13
+
14
+ if keys == []
15
+ rawfile = HackclubRequest.raw_file(@id).dig("file")
16
+ permalink = rawfile.dig("permalink")
17
+ name = rawfile.dig("name")
18
+ @data = '<a href="'+permalink+'" class="hackclub-file" target="_blank">'+name+'</a>'
19
+ else
20
+ @data = HackclubRequest.raw_file(@id).dig("file").dig(*keys)
21
+ @data = ArgsParser.eval_pipes_with_val(@data, pipes.split(" | ").map(&:strip))
22
+ end
23
+ end
24
+
25
+ def render(context)
26
+ if @data == nil
27
+ %Q{null}
28
+ else
29
+ %Q{#{@data}}
30
+ end
31
+ end
32
+ end
33
+
34
+ Liquid::Template.register_tag "file", HackclubFileTag
35
+ end
data/lib/tags/mentions.rb CHANGED
@@ -16,7 +16,6 @@ module Jekyll
16
16
  @pipes = parts[1..-1]
17
17
 
18
18
  if @id && @id.start_with?("U")
19
-
20
19
  display_name = @args[1] || HackclubRequest.resolve_username(@id)
21
20
  display_name = ArgsParser.eval_pipes_with_val(display_name, @pipes)
22
21
  %Q{<a href="https://hackclub.slack.com/team/#{@id}" class="hackclub-mention hackclub-user" target="_blank">@#{display_name}</a>}
@@ -24,6 +23,10 @@ module Jekyll
24
23
  display_name = @args[1] || HackclubRequest.resolve_channel(@id)
25
24
  display_name = ArgsParser.eval_pipes_with_val(display_name, @pipes)
26
25
  %Q{<a href="https://hackclub.slack.com/archives/#{@id}" class="hackclub-mention hackclub-channel" target="_blank">##{display_name}</a>}
26
+ elsif @id && @id.start_with?("S")
27
+ display_name = @args[1] || HackclubRequest.resolve_usergroup(@id)
28
+ display_name = ArgsParser.eval_pipes_with_val(display_name, @pipes)
29
+ %Q{@#{display_name}}
27
30
  end
28
31
  end
29
32
  end
@@ -0,0 +1,26 @@
1
+ require "jekyll"
2
+ require_relative "../server-bridge"
3
+
4
+ module Jekyll
5
+ class HackclubPFP < Liquid::Tag
6
+ def initialize(tagName, content, tokens)
7
+ super
8
+ if content.strip =~ /^(U\w+)(?:@(.+))?$/
9
+ id = Regexp.last_match(1)
10
+ data = HackclubRequest.raw_user(id)
11
+ @resolution = Regexp.last_match(2) || "original"
12
+
13
+ @img_url = data.dig("user", "profile", "image_"+@resolution)
14
+ @name = data.dig("user", "name")
15
+ else
16
+ raise ArgumentError, "Invalid profilepic tag format: #{content}"
17
+ end
18
+ end
19
+
20
+ def render(context)
21
+ %Q{<img src="#{@img_url}" title="#{@name}'s profile picture" alt="#{@name}'s profile picture" class="hackclub-pfp res-#{@resolution}">}
22
+ end
23
+ end
24
+
25
+ Liquid::Template.register_tag "profilepic", HackclubPFP
26
+ end
@@ -0,0 +1,27 @@
1
+ require "jekyll"
2
+ require_relative "../server-bridge"
3
+
4
+ module Jekyll
5
+ class HackclubUsergroupTag < Liquid::Tag
6
+ def initialize(tagName, content, tokens)
7
+ super
8
+ content = content.strip()
9
+ @id, keys, pipes = ArgsParser.split_args(content)
10
+ pipes = pipes || ""
11
+ keys = keys.split(".")
12
+
13
+ @data = HackclubRequest.raw_usergroup(@id).dig(*keys)
14
+ @data = ArgsParser.eval_pipes_with_val(@data, pipes.split(" | ").map(&:strip))
15
+ end
16
+
17
+ def render(context)
18
+ if @data == nil
19
+ %Q{null}
20
+ else
21
+ %Q{#{@data}}
22
+ end
23
+ end
24
+ end
25
+
26
+ Liquid::Template.register_tag "usergroup", HackclubUsergroupTag
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-hackclub
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MathiasDPX
@@ -43,12 +43,18 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - lib/args-parser.rb
45
45
  - lib/jekyll-hackclub.rb
46
+ - lib/pipes/arrays.rb
47
+ - lib/pipes/numbers.rb
48
+ - lib/pipes/strings.rb
46
49
  - lib/server-bridge.rb
47
50
  - lib/tags/channel-tag.rb
48
51
  - lib/tags/emoji.rb
52
+ - lib/tags/file.rb
49
53
  - lib/tags/mentions.rb
50
54
  - lib/tags/parser.rb
55
+ - lib/tags/profile-pic.rb
51
56
  - lib/tags/user-tag.rb
57
+ - lib/tags/usergroup-tag.rb
52
58
  licenses:
53
59
  - MIT
54
60
  metadata: {}