jekyll-patreon 1.0.0 → 1.1.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.
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+
3
+ module Jekyll
4
+ module Patreon::Parsers
5
+ class PatreonParser
6
+ def self.parseJson(jsonStr)
7
+ return JSON.parse(jsonStr)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Jekyll
2
+ module Patreon
3
+ def self.get_language(context)
4
+ language = context.registers[:page]['language']
5
+ return language
6
+ end
7
+ end
8
+ end
@@ -7,26 +7,30 @@
7
7
  # {% patreon z3nth10n %}
8
8
  # Output: Patreon donation widget
9
9
 
10
- require "jekyll-patreon/version"
11
10
  require 'net/http'
12
11
 
13
12
  module Jekyll
14
13
  module Patreon::Tags
15
14
  class PatreonTag < Liquid::Tag
16
- PatreonWebsiteURL = "https://www.patreon.com/"
17
- PatreonUserAPIURL = "https://api.patreon.com/user/"
18
-
19
15
  @inc = nil
20
16
  @username = nil
21
17
  @PatreonID = nil
22
18
  @config = nil
19
+ @json = nil
20
+ @language = nil
21
+ @confDefLang = nil
23
22
 
24
23
  def initialize(tag_name, markup, tokens)
25
24
  super
26
-
27
- @inc = File.expand_path("../../_inc", __FILE__)
28
- @username = markup.strip
25
+
26
+ @inc = File.expand_path(File.join("..", "..", "_inc"), __FILE__)
27
+
29
28
  @config = Jekyll::Patreon::Generator::PatreonGenerator.getConfig
29
+
30
+ @username = @config["username"]
31
+ @confDefLang = @config["default_lang"]
32
+ @PatreonID = Jekyll::Patreon::Generator::PatreonGenerator.getPatreonID
33
+ @json = Jekyll::Patreon::Generator::PatreonGenerator.getJSON
30
34
  end
31
35
 
32
36
  def render(context)
@@ -34,17 +38,24 @@ module Jekyll
34
38
  return
35
39
  end
36
40
 
37
- if @PatreonID.nil?
38
- @PatreonID = getPatreonID(@username)
41
+ @language = Jekyll::Patreon.get_language(context)
42
+
43
+ if @language.to_s.empty? and @confDefLang.to_s.empty
44
+ @language = "en"
39
45
  end
40
46
 
41
- json = Net::HTTP.get_response(URI.parse("#{PatreonUserAPIURL}#{@PatreonID}")).body.force_encoding('UTF-8').escape_json
42
-
43
- source = "<script>" + File.read(File.join(@inc, "js", "patreon.js")) + "</script>"
44
- source += File.read(File.join(@inc, "design_" + @config['design'] + ".html")).interpolate({ json: json, showgoaltext: @config['showgoaltext'], toptext: @config['toptext'], metercolor: @config['metercolor'], bottomtext: @config['bottomtext'], patreon_button: @config['patreon_button'] })
47
+ trFile = File.expand_path(File.join('..', '..', 'langs', "#{@language}.yml"), __FILE__)
48
+ ymlConf = YAML.load_file(trFile)
49
+
50
+ source = "<script>" + File.read(File.join(@inc, "js", "patreon.js")).interpolate({per: ymlConf["per"], month: ymlConf["month"], patrons: ymlConf["patrons"], of: ymlConf["of"], reached: ymlConf["reached"]}) + "</script>"
51
+ source += File.read(File.join(@inc, "design_" + @config['design'] + ".html")).interpolate({ json: translateJson(context, @json), showgoaltext: @config['showgoaltext'], toptext: @config['toptext'], metercolor: @config['metercolor'], bottomtext: @config['bottomtext'], patreon_button: @config['patreon_button'] })
45
52
 
46
53
  if @config['showbutton']
47
- source += File.read(File.join(@inc, "button.html")).interpolate(pid: @PatreonID)
54
+ if @language == @confDefLang
55
+ source += File.read(File.join(@inc, "button.html")).interpolate(pid: @PatreonID)
56
+ else
57
+ source += File.read(File.join(@inc, "translated_button.html")).interpolate({pid: @PatreonID, caption: ymlConf["caption"]})
58
+ end
48
59
  end
49
60
 
50
61
  source += "<style>" + File.read(File.join(@inc, "css", "design_" + @config['design'] + ".css")) + "</style>"
@@ -53,44 +64,41 @@ module Jekyll
53
64
  source
54
65
  end
55
66
 
56
- def getPatreonID(username)
57
- patreon_url = URI.encode("#{PatreonWebsiteURL}#{username}")
58
-
59
- # Jekyll.logger.info "Patreon profile url:",patreon_url
60
- patreon_source = Net::HTTP.get_response(URI.parse(patreon_url)).body.force_encoding('UTF-8').delete!("\r\n\\")
61
-
62
- patreon_id_index = patreon_source.index("\"creator_id\": ")
63
-
64
- unless patreon_id_index.nil?
65
-
66
- patreon_id_index += 14
67
- endidpos = patreon_source.from(patreon_id_index).index("\n")
68
-
69
- if endidpos.nil?
70
- endidpos = patreon_source.from(patreon_id_index).index("}")
71
- end
72
-
73
- if endidpos.nil?
74
- raiseError()
75
- end
76
-
77
- patreon_id = patreon_source.from(patreon_id_index)[0, endidpos].strip
78
-
79
- # Jekyll.logger.info "Patreon ID:",patreon_id
80
-
81
- if patreon_id.nil?
82
- raiseError()
83
- end
84
-
85
- return Integer(patreon_id)
86
- end
87
-
88
- return -1
89
- end
90
-
91
67
  def raiseError()
92
68
  raise RuntimeError, "An error occurred getting the ID from your Patreon profile"
93
69
  end
70
+
71
+ def translateJson(context, jsonStr)
72
+
73
+
74
+ if @language.to_s.empty? or @language == @confDefLang
75
+ return jsonStr.escape_json
76
+ end
77
+
78
+ json = Jekyll::Patreon::Parsers::PatreonParser.parseJson(jsonStr)
79
+
80
+ incl = json["included"]
81
+ startIndex = 0
82
+
83
+ incl.each_with_index do |item, index|
84
+ startIndex = index
85
+ break if item["type"] == "goal"
86
+ end
87
+
88
+ trFile = File.expand_path(File.join('..', '..', '..', '..', '..', '..', '_data', 'lang', "#{@language}.yml"), __FILE__)
89
+ ymlConf = YAML.load_file(trFile)
90
+
91
+ # Get markdownify pipe
92
+ converter = context.registers[:site].find_converter_instance(Jekyll::Converters::Markdown)
93
+
94
+ for index in (startIndex..incl.length - 1)
95
+ i = index - startIndex
96
+
97
+ json["included"][index]["attributes"]["description"] = converter.convert(ymlConf["patreon_goal_#{i}"].to_s).gsub("<p>", "").gsub("</p>", "")
98
+ end
99
+
100
+ return JSON.dump(json).escape_json
101
+ end
94
102
  end
95
103
  end
96
104
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Patreon
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end # module Patreon
5
5
  end # module Jekyll
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-patreon
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - z3nth10n
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-15 00:00:00.000000000 Z
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -59,7 +59,18 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
62
64
  - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - assets/screenshot-1.png
68
+ - assets/screenshot-2.png
69
+ - assets/screenshot-3.png
70
+ - assets/screenshot-4.png
71
+ - assets/screenshot-5.png
72
+ - assets/screenshot-6.png
73
+ - assets/screenshot-7.png
63
74
  - jekyll-patreon.gemspec
64
75
  - lib/jekyll-patreon.rb
65
76
  - lib/jekyll-patreon/_inc/button.html
@@ -77,8 +88,13 @@ files:
77
88
  - lib/jekyll-patreon/_inc/design_streamlined.html
78
89
  - lib/jekyll-patreon/_inc/design_swapped.html
79
90
  - lib/jekyll-patreon/_inc/js/patreon.js
91
+ - lib/jekyll-patreon/_inc/translated_button.html
80
92
  - lib/jekyll-patreon/generator/defaults.rb
81
93
  - lib/jekyll-patreon/generator/patreonGenerator.rb
94
+ - lib/jekyll-patreon/langs/en.yml
95
+ - lib/jekyll-patreon/langs/es.yml
96
+ - lib/jekyll-patreon/parsers/patreonParser.rb
97
+ - lib/jekyll-patreon/patreonLang.rb
82
98
  - lib/jekyll-patreon/tags/patreonTag.rb
83
99
  - lib/jekyll-patreon/utils/string-utils.rb
84
100
  - lib/jekyll-patreon/version.rb