jekyll-disqus-plugin 1.0.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.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Jekyll-Disqus
2
+
3
+ This is a Jekyll plugin that provides the Liquid tags to render the Disqus Javascript codes inside the
4
+ theme template.
5
+
6
+ It also associates a different *ID* to every *post* using its publishing date and ensures the uniqueness
7
+ of that ID among all the posts.
8
+
9
+ Although the *ID* can be automatically generated is still possible to declare a custom *ID*, which is
10
+ mandatory for those pages that don't contain a publishing date but recall one of the Disqus tags in
11
+ their layouts.
12
+
13
+ ## Installation
14
+
15
+ Inside the Gemfile add the gem name in the `:jekyll_plugins` group:
16
+ ```ruby
17
+ group :jekyll_plugins do
18
+ ...
19
+ gem 'jekyll-disqus-plugin'
20
+ end
21
+ ```
22
+
23
+ Then run from the terminal:
24
+ ```shell
25
+ $ bundle install
26
+ ```
27
+
28
+ ## Configuration
29
+
30
+ Inside the `_config.yml` file can set up the following fields:
31
+ ```yaml
32
+ jekyll-disqus:
33
+ shortname:
34
+ id_prefix:
35
+ post_selector: 'include.post'
36
+ ui:
37
+ layouts:
38
+ - 'post'
39
+ counter:
40
+ layouts:
41
+ - 'all'
42
+ ```
43
+
44
+ *shortname*
45
+ : It is the same *Shortname* provided by Disqus for each site added.
46
+
47
+ *id_prefix*
48
+ : It is a code that will be prepended to any *Disqus Id*, just to add another level of customization.
49
+
50
+ *post_selector*
51
+ : This is how the script will read the post data inside a paginator layout.
52
+
53
+ *ui.layouts*/*counter.layouts*
54
+ : A list of layouts where the two couple of tags will be printed.
55
+
56
+ To disable the tags for specific pages or posts, the *no_disqus* field can be set up to true on the front
57
+ matter of the specific post or page itself.
58
+
59
+ By default, this addon associates a unique *ID* to each *post* using the related publishing date, but
60
+ where needed, a *disqus_id* field can be declared in the front matter of the page.
61
+ ```yaml
62
+ ---
63
+ title: ...
64
+ description: ...
65
+ author: ...
66
+ ...
67
+ disqus_id: 'CUSTOM_DISQUS_ID'
68
+ ...
69
+ ```
70
+
71
+ There is a deep relationship between the *disqus_id* and the related post, so be sure that:
72
+ - The custom disqus_id will remain unique, otherwise an error will be raised at the building time;
73
+ - the custom disqus_id won't change after the page has been published, otherwise all the comments
74
+ associated with the post will be lost;
75
+ - the publishing date, where the *ID* is not customized, doesn't change, or all the comments associated
76
+ with the post will be lost.
77
+
78
+ ## Usage
79
+
80
+ Below are the provided tags:
81
+
82
+ | Tag | Description | Template |
83
+ |:---------------------------:|-----------------------------------------------------|:--------:|
84
+ | {% disqus_counter %} |The count of comments for a certain post | Post |
85
+ | {% disqus_script_counter %} |The required Javascript that renders the counter | Default |
86
+ | {% disqus_ui %} |The comment box | Post |
87
+ | {% disqus_script_ui %} |The required Javascript that renders the comment box | Default |
88
+
89
+ The *script* tags must be included once for page and, providing a Javascript code, their position should
90
+ be in the bottom part of the main layout, just before the end of the *body* HTML tag.
91
+
92
+ The *disqus_ui* renders the comment box and should be put in the post or page layout, depending
93
+ where you want to show it, usually after the *content* has been printed.
94
+
95
+ The *disqus_counter* shows the number of comments for a post (or page), and it is
96
+ bound to the specific content through the *disqus_id* aforementioned.
97
+ Usually, it is displayed among the content *data*, just after the title, and it works for the full
98
+ rendered post as well as the paginated content.
99
+
100
+ In the last case, the plugin will detect the post included in the pagination template using a selector.
101
+ By default, it is `include.post`, but can be changed in the configuration section of the `_config.yml`
102
+ file.
103
+
104
+ ## More Help
105
+
106
+ - the [project page on the Freeaptitude blog][project_page];
107
+ - the [Jekyll-Disqus Github wiki][jekyll_disqus_wiki].
108
+
109
+ [project_page]: https://freeaptitude.altervista.org/projects/jekyll-disqus.html "Project page on the Freeaptitude blog"
110
+ [jekyll_disqus_wiki]: https://github.com/fabiomux/jekyll-disqus-plugin/wiki "Jekyll-Disqus wiki page on GitHub"
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/jekyll/disqus/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jekyll-disqus-plugin"
7
+ spec.version = Jekyll::Disqus::VERSION
8
+ spec.authors = ["Fabio Mucciante"]
9
+ spec.email = ["fabio.mucciante@gmail.com"]
10
+
11
+ spec.summary = "Jekyll plugin to render the Javascript Disqus codes required."
12
+ spec.description = "It provides the tags for the comment box, the counter and the related Javascript initializers."
13
+ spec.homepage = "https://freeaptitude.altervista.org/projects/jekyll-disqus.html"
14
+ spec.license = "GPL-3.0"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/fabiomux/jekyll-disqus-plugin"
21
+ spec.metadata["changelog_uri"] = "https://freeaptitude.altervista.org/projects/jekyll-disqus.html#changelog"
22
+ spec.metadata["wiki_uri"] = "https://github.com/fabiomux/jekyll-disqus-plugin/wiki"
23
+ spec.metadata["rubygems_mfa_required"] = "true"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(__dir__) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
30
+ end
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ # Uncomment to register a new dependency of your gem
37
+ spec.add_dependency "jekyll", ">= 3.7", "< 5.0"
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Jekyll
6
+ module Disqus
7
+ #
8
+ # Contains the methods to calculate the Disqus ID associated to
9
+ # a post or a page.
10
+ #
11
+ module Helper
12
+ def disabled_tag?(tag, context)
13
+ return true if context.registers[:page]["no_disqus"]
14
+ return false if context.registers[:site].config["jekyll-disqus"][tag]["layouts"].include?("all")
15
+
16
+ layout = context.registers[:page]["layout"]
17
+ return false if context.registers[:site].config["jekyll-disqus"][tag]["layouts"].include?(layout)
18
+
19
+ true
20
+ end
21
+
22
+ def post_selector(registers)
23
+ registers[:site].config["jekyll-disqus"]["post_selector"] || "include.post"
24
+ end
25
+
26
+ def page_disqus_id(registers)
27
+ id = registers[:page]["disqus_id"]
28
+ raise MissingDisqusId, registers[:page]["url"] if id.nil? && registers[:page]["date"].nil?
29
+
30
+ begin
31
+ id = DateTime.parse(registers[:page]["date"].to_s).to_time.to_i if id.nil?
32
+ rescue ArgumentError
33
+ raise InvalidDate, registers[:page]["url"]
34
+ end
35
+
36
+ registers[:site].config["jekyll-disqus"]["id_prefix"].to_s + id.to_s
37
+ end
38
+
39
+ def post_disqus_id(context)
40
+ selector = post_selector(context.registers)
41
+ return unless context["#{selector}.title"]
42
+
43
+ id = context["#{selector}.disqus_id"]
44
+ raise MissingDisqusId, registers[:page]["url"] if id.nil? && context["#{selector}.date"].nil?
45
+
46
+ begin
47
+ id = DateTime.parse(context["#{selector}.date"].to_s).to_time.to_i if id.nil?
48
+ rescue ArgumentError
49
+ raise InvalidDate, registers[:page]["url"]
50
+ end
51
+
52
+ context.registers[:site].config["jekyll-disqus"]["id_prefix"].to_s + id.to_s
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Disqus
5
+ #
6
+ # Anchor tag where rendering the comment box.
7
+ #
8
+ class UI < Liquid::Tag
9
+ include Helper
10
+
11
+ def initialize(tag_name, text, tokens)
12
+ super
13
+ @text = text
14
+ end
15
+
16
+ def render(context)
17
+ return "" if disabled_tag?("ui", context)
18
+
19
+ "<div id=\"disqus_thread\"></div>"
20
+ end
21
+ end
22
+
23
+ #
24
+ # Anchor tag where rendering the comment counter.
25
+ #
26
+ class Counter < Liquid::Tag
27
+ include Helper
28
+
29
+ def initialize(tag_name, text, tokens)
30
+ super
31
+ params = text.scan(/(\w+)=(?:["'])([^'"]+)\K/).to_h
32
+ @label = params.key("label") || "Comments"
33
+ end
34
+
35
+ def render(context)
36
+ return "" if disabled_tag?("counter", context)
37
+
38
+ id = post_disqus_id(context)
39
+ id = page_disqus_id(context.registers) if id.nil?
40
+ return "" if id.nil?
41
+
42
+ "<span class=\"disqus-comment-count\" data-disqus-identifier=\"#{id}\">#{@label}</span>"
43
+ end
44
+ end
45
+
46
+ #
47
+ # Print the Javascript code to render the counter.
48
+ #
49
+ class ScriptCounter < Liquid::Tag
50
+ include Helper
51
+
52
+ def initialize(tag_name, text, tokens)
53
+ super
54
+ @text = text
55
+ end
56
+
57
+ def render(context)
58
+ return "" if disabled_tag?("counter", context)
59
+
60
+ "<script
61
+ id=\"dsq-count-scr\"
62
+ src=\"//#{context.registers[:site].config["jekyll-disqus"]["shortname"]}.disqus.com/count.js\"
63
+ async></script>".gsub(/\n/, " ").gsub(/ +/, " ")
64
+ end
65
+ end
66
+
67
+ #
68
+ # Print the Javascript code to render the comment box.
69
+ #
70
+ class ScriptUI < Liquid::Tag
71
+ include Helper
72
+
73
+ @@ids = {}
74
+
75
+ def initialize(tag_name, param, tokens)
76
+ super
77
+ @param = param
78
+ end
79
+
80
+ def render(context)
81
+ return "" if disabled_tag?("ui", context)
82
+
83
+ raise MissingShortname unless context.registers[:site].config["jekyll-disqus"]["shortname"]
84
+
85
+ id = page_disqus_id(context.registers)
86
+ url = context.registers[:page]["url"]
87
+ if @@ids.key?(id) && (@@ids[id] != url)
88
+ raise DuplicatedDisqusId, { current_url: url, duplicated_url: @@ids[id], id: id }
89
+ end
90
+
91
+ @@ids[id] = url
92
+ js_string(base_url: context.registers[:site].config["url"],
93
+ page_url: url,
94
+ disqus_shortname: context.registers[:site].config["jekyll-disqus"]["shortname"],
95
+ disqus_id: id)
96
+ end
97
+
98
+ private
99
+
100
+ def js_string(args)
101
+ "<script>
102
+ var disqus_config = function () {
103
+ this.page.url = '#{args[:base_url]}#{args[:page_url]}';
104
+ this.page.identifier = '#{args[:disqus_id]}';
105
+ };
106
+ (function() {
107
+ var d = document, s = d.createElement('script');
108
+ s.src = '//#{args[:disqus_shortname]}.disqus.com/embed.js';
109
+ s.setAttribute('data-timestamp', +new Date());
110
+ (d.head || d.body).appendChild(s);
111
+ })();
112
+ </script>
113
+ <noscript>
114
+ Please enable JavaScript to view the
115
+ <a href=\"https://disqus.com/?ref_noscript\">comments powered by Disqus.</a>
116
+ </noscript>"
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Disqus
5
+ #
6
+ # Invalid date format.
7
+ #
8
+ class InvalidDate < StandardError
9
+ def initialize(url)
10
+ super "The page at the URL: #{url} doesn't contains a valid 'date' field!"
11
+ end
12
+ end
13
+
14
+ #
15
+ # Missing Disqus ID field error.
16
+ #
17
+ class MissingDisqusId < StandardError
18
+ def initialize(url)
19
+ super "The page at the URL: #{url} doesn't contains any 'disqus_id' field!"
20
+ end
21
+ end
22
+
23
+ #
24
+ # Missing the Shortname param error.
25
+ #
26
+ class MissingShortname < StandardError
27
+ def initialize
28
+ super "The required Disqus param 'shortname' is missing!"
29
+ end
30
+ end
31
+
32
+ #
33
+ # Duplicated Disqus ID among pages and posts.
34
+ #
35
+ class DuplicatedDisqusId < StandardError
36
+ def initialize(args)
37
+ super "The page at the URL: #{args[:current_url]}
38
+ has the same Disqus ID of: #{args[duplicated_url]} (#{args[id]})!"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Disqus
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+
5
+ require_relative "disqus/helper"
6
+ require_relative "disqus/tags"
7
+ require_relative "disqus/utils"
8
+
9
+ Liquid::Template.register_tag("disqus_ui", Jekyll::Disqus::UI)
10
+ Liquid::Template.register_tag("disqus_script_ui", Jekyll::Disqus::ScriptUI)
11
+ Liquid::Template.register_tag("disqus_counter", Jekyll::Disqus::Counter)
12
+ Liquid::Template.register_tag("disqus_script_counter", Jekyll::Disqus::ScriptCounter)
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "jekyll/disqus"
@@ -0,0 +1,6 @@
1
+ module Jekyll
2
+ module Disqus
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-disqus-plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Mucciante
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.7'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ description: It provides the tags for the comment box, the counter and the related
34
+ Javascript initializers.
35
+ email:
36
+ - fabio.mucciante@gmail.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - ".rspec"
42
+ - ".rubocop.yml"
43
+ - CODE_OF_CONDUCT.md
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - jekyll-disqus-plugin.gemspec
50
+ - lib/jekyll-disqus-plugin.rb
51
+ - lib/jekyll/disqus.rb
52
+ - lib/jekyll/disqus/helper.rb
53
+ - lib/jekyll/disqus/tags.rb
54
+ - lib/jekyll/disqus/utils.rb
55
+ - lib/jekyll/disqus/version.rb
56
+ - sig/jekyll/disqus.rbs
57
+ homepage: https://freeaptitude.altervista.org/projects/jekyll-disqus.html
58
+ licenses:
59
+ - GPL-3.0
60
+ metadata:
61
+ homepage_uri: https://freeaptitude.altervista.org/projects/jekyll-disqus.html
62
+ source_code_uri: https://github.com/fabiomux/jekyll-disqus-plugin
63
+ changelog_uri: https://freeaptitude.altervista.org/projects/jekyll-disqus.html#changelog
64
+ wiki_uri: https://github.com/fabiomux/jekyll-disqus-plugin/wiki
65
+ rubygems_mfa_required: 'true'
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.6.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.2.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Jekyll plugin to render the Javascript Disqus codes required.
85
+ test_files: []