jekyll-webmention_io 3.0.0 → 3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6830a7a6abb84d8ba34c51b817ad817147561818
4
- data.tar.gz: 37956daf8a417c5a9fcbae7e1337c6993a82e746
3
+ metadata.gz: 9aae045b55d22d92d88b0345073c4ffe64206da5
4
+ data.tar.gz: 28b163e15deda415f8ed2c6c5621654545a032cb
5
5
  SHA512:
6
- metadata.gz: 9db99babf8e9e508f875517cb5890d300362018384e56fa9ac3ca30763cca55d500ad836e4153b66516b2f1ad2f7b5549cab682e83c6da833cd7fa9e20e1da5a
7
- data.tar.gz: 788ede8be0748a6baf69e23354996f51155cbd628dec2af8619ea8bcfb855071d5ed6fd57f66a0577884ebd03fc1e10c66991778cafb502fdc261d5f6069c135
6
+ metadata.gz: 13af37e1c35dec286855885086586f7e8f9caca23ca672e7ef8ec64c4e21633a600e54e921466e67111d74e4fcfee3f99601dcab0f15db7ece76ce29406b9450
7
+ data.tar.gz: 7f5c71a216c6deb7cd13bba1c0fde8ea7d103cc9d33a004bd95e0c7796e4d6119e94b3b2aeacd7ac8bf86df5d52fa3ac480690b79401696bfc63c38a8d342c61
@@ -13,12 +13,7 @@ module Jekyll
13
13
  module WebmentionIO
14
14
  class JavaScriptFile < StaticFile
15
15
  def destination_rel_dir
16
- config = {
17
- "destination" => "js",
18
- }
19
- js_config = Jekyll::WebmentionIO.config["js"] || {}
20
- config = config.merge(js_config)
21
- config["destination"]
16
+ Jekyll::WebmentionIO.js_handler.destination
22
17
  end
23
18
  end
24
19
 
@@ -28,38 +23,29 @@ module Jekyll
28
23
  priority :low
29
24
 
30
25
  def generate(site)
26
+ handler = Jekyll::WebmentionIO.js_handler
31
27
  @site = site
32
- @file_name = "JekyllWebmentionIO.js"
28
+ @file_name = handler.resource_name
33
29
 
34
- if @site.config.dig("webmentions", "js") == false
30
+ if handler.disabled?
35
31
  Jekyll::WebmentionIO.log "info", "Skipping JavaScript inclusion."
36
32
  return
37
33
  end
38
34
 
39
- config = {
40
- "destination" => "js",
41
- "uglify" => true,
42
- }
43
- site_config = @site.config.dig("webmentions", "js") || {}
44
- config = config.merge(site_config)
45
-
46
- @source_file_destination = (config["source"] == false ? Dir.mktmpdir : "#{@site.config["source"]}/#{config["destination"]}")
35
+ @source_file_destination = if handler.source?
36
+ @site.in_source_dir(handler.destination)
37
+ else
38
+ Dir.mktmpdir
39
+ end
47
40
 
48
41
  @javascript = +"" # unfrozen String
49
42
 
50
43
  concatenate_asset_files
51
-
52
44
  add_webmention_types
53
45
 
54
- unless config["uglify"] == false
55
- uglify
56
- end
57
-
46
+ uglify if handler.uglify?
58
47
  create_js_file
59
-
60
- unless config["deploy"] == false
61
- deploy_js_file
62
- end
48
+ deploy_js_file if handler.deploy?
63
49
  end
64
50
 
65
51
  private
@@ -75,27 +61,24 @@ module Jekyll
75
61
  JekyllWebmentionIO.types = { TYPES };
76
62
  }(this, this.JekyllWebmentionIO));
77
63
  EOF
78
- @javascript << types_js.sub(/TYPES/, js_types.join(","))
64
+ @javascript << types_js.sub("TYPES", js_types.join(","))
79
65
  end
80
66
 
81
67
  def concatenate_asset_files
82
- source = File.expand_path("../assets/", __dir__)
83
- Dir["#{source}/*.js"].each do |file|
84
- handler = File.open(file, "rb")
85
- @javascript << File.read(handler)
68
+ assets_dir = File.expand_path("../assets/", __dir__)
69
+ Dir["#{assets_dir}/*.js"].each do |file|
70
+ file_handler = File.open(file, "rb")
71
+ @javascript << File.read(file_handler)
86
72
  end
87
73
  end
88
74
 
89
75
  def uglify
90
- uglify_config = {
91
- :harmony => true,
92
- }
93
- @javascript = Uglifier.new(uglify_config).compile(@javascript)
76
+ @javascript = Uglifier.new(:harmony => true).compile(@javascript)
94
77
  end
95
78
 
96
79
  def create_js_file
97
80
  Dir.mkdir(@source_file_destination) unless File.exist?(@source_file_destination)
98
- File.open("#{@source_file_destination}/#{@file_name}", "w") { |f| f.write(@javascript) }
81
+ File.open(File.join(@source_file_destination, @file_name), "wb") { |f| f.write(@javascript) }
99
82
  end
100
83
 
101
84
  def deploy_js_file
@@ -32,8 +32,9 @@ module Jekyll
32
32
  end
33
33
 
34
34
  def template=(template)
35
- supported_templates = Jekyll::WebmentionIO.types + %w(count webmentions)
36
- Jekyll::WebmentionIO.log "error", "#{template.capitalize} is not supported" unless supported_templates.include? template
35
+ unless Jekyll::WebmentionIO.supported_templates.include? template
36
+ Jekyll::WebmentionIO.log "error", "#{template.capitalize} is not supported"
37
+ end
37
38
  @template_name = template
38
39
  @template = Jekyll::WebmentionIO.get_template_contents(template)
39
40
  Jekyll::WebmentionIO.log "info", "#{template.capitalize} template:\n\n#{@template}\n\n"
@@ -10,40 +10,8 @@
10
10
  module Jekyll
11
11
  module WebmentionIO
12
12
  class WebmentionJSTag < Liquid::Tag
13
- def render(context)
14
- site = context.registers[:site]
15
-
16
- # JS can be turned off too
17
- if site.config.dig("webmentions", "js") == false
18
- Jekyll::WebmentionIO.log "info", "JavaScript output is disabled, so the {% webmentions_js %} tag is being skipped"
19
- return ""
20
- end
21
-
22
- config = {
23
- "destination" => "js",
24
- "uglify" => true,
25
- }
26
- site_config = site.config.dig("webmentions", "js") || {}
27
- config = config.merge(site_config)
28
-
29
- # JS file
30
- js = +"" # unfrozen String
31
- unless config["deploy"] == false
32
- destination = config["destination"].chomp("/").reverse.chomp("/").reverse # removes prefix & suffix "/"
33
- js_file_path = "#{site.config["baseurl"]}/#{destination}/JekyllWebmentionIO.js"
34
- js << "<script src=\"#{js_file_path}\" async></script>"
35
- end
36
-
37
- Jekyll::WebmentionIO.log "info", "Gathering templates for JavaScript."
38
- templates = +"" # unfrozen String
39
- template_files = Jekyll::WebmentionIO.types + %w(count webmentions)
40
- template_files.each do |template|
41
- templates << "<template style=\"display:none\" id=\"webmention-#{template}\">"
42
- templates << Jekyll::WebmentionIO.get_template_contents(template)
43
- templates << "</template>"
44
- end
45
-
46
- "#{js}\n#{templates}"
13
+ def render(_context)
14
+ Jekyll::WebmentionIO.js_handler.render
47
15
  end
48
16
  end
49
17
  end
@@ -9,6 +9,7 @@
9
9
  #
10
10
  require_relative "webmention_io/version"
11
11
  require_relative "webmention_io/webmention"
12
+ require_relative "webmention_io/js_handler"
12
13
 
13
14
  require "json"
14
15
  require "net/http"
@@ -22,7 +23,7 @@ module Jekyll
22
23
  class << self
23
24
  # define simple getters and setters
24
25
  attr_reader :config, :jekyll_config, :cache_files, :cache_folder,
25
- :file_prefix, :types
26
+ :file_prefix, :types, :supported_templates, :js_handler
26
27
  attr_writer :api_suffix
27
28
  end
28
29
 
@@ -32,7 +33,11 @@ module Jekyll
32
33
  @api_endpoint = @api_url
33
34
  @api_suffix = ""
34
35
 
35
- @types = %w(bookmarks likes links posts replies reposts rsvps)
36
+ @types = %w(bookmarks likes links posts replies reposts rsvps).freeze
37
+ @supported_templates = (@types + %w(count webmentions)).freeze
38
+
39
+ @template_file_cache = {}
40
+ @template_content_cache = {}
36
41
 
37
42
  EXCEPTIONS = [
38
43
  SocketError, Timeout::Error,
@@ -58,10 +63,10 @@ module Jekyll
58
63
  "lookups" => cache_file("lookups.yml")
59
64
  }
60
65
  @cache_files.each_value do |file|
61
- unless File.exist?(file)
62
- dump_yaml(file)
63
- end
66
+ dump_yaml(file) unless File.exist?(file)
64
67
  end
68
+
69
+ @js_handler = Jekyll::WebmentionIO::JSHandler.new(site)
65
70
  end
66
71
 
67
72
  # Setter
@@ -75,17 +80,11 @@ module Jekyll
75
80
  end
76
81
 
77
82
  def self.get_cache_file_path(key)
78
- path = false
79
- if @cache_files.key? key
80
- path = @cache_files[key]
81
- end
82
- return path
83
+ @cache_files[key] || false
83
84
  end
84
85
 
85
86
  def self.read_cached_webmentions(which)
86
- unless %w(incoming outgoing).include? which
87
- return {}
88
- end
87
+ return {} unless %w(incoming outgoing).include?(which)
89
88
 
90
89
  cache_file = get_cache_file_path which
91
90
  load_yaml(cache_file)
@@ -136,7 +135,7 @@ module Jekyll
136
135
  if source
137
136
  JSON.parse(source)
138
137
  else
139
- ""
138
+ {}
140
139
  end
141
140
  end
142
141
 
@@ -235,15 +234,36 @@ module Jekyll
235
234
  end
236
235
  end
237
236
 
237
+ def self.template_file(template)
238
+ @template_file_cache[template] ||= begin
239
+ configured_template = @config.dig("templates", template)
240
+ if configured_template
241
+ log "info", "Using custom #{template} template"
242
+ configured_template
243
+ else
244
+ File.expand_path("templates/#{template}.html", __dir__)
245
+ end
246
+ end
247
+ end
248
+
238
249
  def self.get_template_contents(template)
239
- template_file = if @config.dig("templates", template)
240
- log "info", "Using custom #{template} template"
241
- @config["templates"][template]
242
- else
243
- File.expand_path("templates/#{template}.html", __dir__)
244
- end
245
- log "info", "Template file: #{template_file}"
246
- File.read(template_file)
250
+ template_file = template_file(template)
251
+ @template_content_cache[template_file] ||= begin
252
+ log "info", "Template file: #{template_file}"
253
+ File.read(template_file)
254
+ end
255
+ end
256
+
257
+ def self.html_templates
258
+ @html_templates ||= begin
259
+ templates = +"" # unfrozen String
260
+ supported_templates.each do |template|
261
+ templates << "<template style=\"display:none\" id=\"webmention-#{template}\">"
262
+ templates << get_template_contents(template)
263
+ templates << "</template>"
264
+ end
265
+ templates
266
+ end
247
267
  end
248
268
 
249
269
  # Connections
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (c) Aaron Gustafson
4
+ # https://github.com/aarongustafson/jekyll-webmention_io
5
+ # Licence : MIT
6
+ #
7
+
8
+ module Jekyll
9
+ module WebmentionIO
10
+ class JSHandler
11
+ attr_reader :destination, :resource_name, :resource_url
12
+
13
+ DEFAULTS = {
14
+ "destination" => "js",
15
+ "source" => true,
16
+ "deploy" => true,
17
+ "uglify" => true,
18
+ }.freeze
19
+
20
+ def initialize(site)
21
+ js_config = site.config.dig("webmentions", "js")
22
+
23
+ @disabled = js_config == false
24
+
25
+ js_config = {} unless js_config.is_a?(Hash)
26
+ js_config = DEFAULTS.merge(js_config)
27
+
28
+ @deploy, @uglify, @source, @destination = js_config.values_at("deploy", "uglify", "source", "destination")
29
+ @resource_name = "JekyllWebmentionIO.js"
30
+ @resource_url = File.join(
31
+ "", site.config["baseurl"], @destination, @resource_name
32
+ )
33
+ end
34
+
35
+ def disabled?
36
+ @disabled == true
37
+ end
38
+
39
+ def deploy?
40
+ @deploy != false
41
+ end
42
+
43
+ def uglify?
44
+ @uglify != false
45
+ end
46
+
47
+ def source?
48
+ @source != false
49
+ end
50
+
51
+ def render
52
+ if disabled?
53
+ Jekyll::WebmentionIO.log "info",
54
+ "JavaScript output is disabled, so the {% webmentions_js %} tag is being skipped"
55
+ return ""
56
+ end
57
+
58
+ js_file = deploy? ? "<script src=\"#@resource_url\" async></script>" : ""
59
+
60
+ Jekyll::WebmentionIO.log "info", "Gathering templates for JavaScript."
61
+ "#{js_file}\n#{Jekyll::WebmentionIO.html_templates}"
62
+ end
63
+ end
64
+ end
65
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module WebmentionIO
5
- VERSION = "3.0.0"
5
+ VERSION = "3.1.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-webmention_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Gustafson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-10 00:00:00.000000000 Z
11
+ date: 2018-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -255,6 +255,7 @@ files:
255
255
  - lib/jekyll/templates/rsvps.html
256
256
  - lib/jekyll/templates/webmentions.html
257
257
  - lib/jekyll/webmention_io.rb
258
+ - lib/jekyll/webmention_io/js_handler.rb
258
259
  - lib/jekyll/webmention_io/version.rb
259
260
  - lib/jekyll/webmention_io/webmention.rb
260
261
  homepage: https://github.com/aarongustafson/jekyll-webmention_io