jekyll-metrics 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cca79edf5493463c41e66e3880cd3f10014c85a22bc6f9297bcca43c0b0553cc
4
- data.tar.gz: e1700fae8cd2fbd2294d552e5a8327a3b90f97eaed5de354191e4c2c8b071015
3
+ metadata.gz: 4d10f3144cb520c23e18a7c4c131a5f99aeef177f56b4f306c0f86d8378b8a7b
4
+ data.tar.gz: 8b8972b925b68a626b14f2804dedbe92623a11b5926ba9ccff0c779f86fb4c54
5
5
  SHA512:
6
- metadata.gz: 5e64fa202273407eed86e0ad177dc6f38a60d02490bdc2dd6cc3eac8ef71c0e4c1eb9892c1470fb054a6a3267d72e886eddfd683ee6f4882db2ee4411a2b44a3
7
- data.tar.gz: 93ca011990065383140c1ac3188af678627ca962e9c85de485972c50b3c9978cbd292d93375e326c5a5a689c1f8e22f4aa9a3b678834321c4cc70662725da8e8
6
+ metadata.gz: 83947034b5c3e02037273ece94bd2771531990d7603313761724c1b971d23e080c2eb3bed1bd9edf405757e0da1195b5ad14da03b21885e8048e7b64b356fb0a
7
+ data.tar.gz: 67dba32ee8963ce314cc3d2c15d549aeb20abfd974d0e1242e2da3f0f7fdc10cddcd24c9ea60aabbc6170448d872494cefc3b2e371629e6eff45ec71801ee54c
data/README.md CHANGED
@@ -1,11 +1,16 @@
1
1
  # JekyllMetrics
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jekyll-metrics`. To experiment with that code, run `bin/console` for an interactive prompt.
4
3
 
5
- TODO: Delete this and the text above, and describe your gem
4
+ [![Build Status](https://travis-ci.org/zinovyev/jekyll-metrics.svg?branch=master)](https://travis-ci.org/zinovyev/jekyll-metrics)
5
+ [![Gem Version](https://img.shields.io/gem/v/jekyll-metrics.svg?label=Latest%20Release)][rubygems]
6
+
7
+
8
+ A Jekyll plugin to inject Google Analytics and Yandex Metrica counters into your pages
9
+
6
10
 
7
11
  ## Installation
8
12
 
13
+
9
14
  Add this line to your application's Gemfile:
10
15
 
11
16
  ```ruby
@@ -20,25 +25,71 @@ Or install it yourself as:
20
25
 
21
26
  $ gem install jekyll-metrics
22
27
 
28
+
23
29
  ## Usage
24
30
 
25
- TODO: Write usage instructions here
31
+
32
+ The scripts will be automatically injected to the top of your pages as soon as the plugin is enabled:
33
+ * Above the first `<script>` in the `<head>` tag;
34
+ * Or just to the bottom of the `<head>` tag if no other scripts are loaded;
35
+
36
+ To enable the plugin, add it to the list of plugins:
37
+
38
+ ```yaml
39
+ plugins:
40
+ ...
41
+ - jekyll-metrics
42
+ ```
43
+
44
+ To make the plugin work properly, the following configuration options should be added to your `_config.yml` file:
45
+
46
+ ```yaml
47
+ jekyll_metrics:
48
+ yandex_metrica_id: 11111111,
49
+ google_analytics_id: 22-222222222-2
50
+ ```
51
+
52
+ The `11111111` should be replaced with your personal Yandex Metrica counter ID and the `22-222222222-2` - with one
53
+ from your Google Analytics account accordinately.
54
+
55
+
56
+ ## Advanced configuration
57
+
58
+
59
+ Actually the plugin may be used to inject any script you like. You'll just need to replace the template of the
60
+ substitution scripts. To do so create a file (e.g. `_includes/metrics.html.liquid`) in your site directory and add an
61
+ extra parameter to the configuration section described above:
62
+
63
+ ```yaml
64
+ jekyll_metrics:
65
+ template: _includes/metrics.html.liquid
66
+ yandex_metrica_id: 11111111,
67
+ google_analytics_id: 22-222222222-2
68
+ ```
69
+
26
70
 
27
71
  ## Development
28
72
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
73
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+ After checking out the repo, run `rake spec` to run the tests.
75
+
32
76
 
33
77
  ## Contributing
34
78
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jekyll-metrics. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/jekyll-metrics/blob/master/CODE_OF_CONDUCT.md).
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zinovyev/jekyll-metrics.
81
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere
82
+ to the [code of conduct](https://github.com/zinovyev/jekyll-metrics/blob/master/CODE_OF_CONDUCT.md).
36
83
 
37
84
 
38
85
  ## License
39
86
 
87
+
40
88
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
89
 
90
+
42
91
  ## Code of Conduct
43
92
 
44
- Everyone interacting in the JekyllMetrics project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jekyll-metrics/blob/master/CODE_OF_CONDUCT.md).
93
+
94
+ Everyone interacting in the JekyllMetrics project's codebases, issue trackers, chat rooms and mailing lists is
95
+ expected to follow the [code of conduct](https://github.com/[USERNAME]/jekyll-metrics/blob/master/CODE_OF_CONDUCT.md).
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'jekyll'
4
+ require 'nokogiri'
4
5
  require File.expand_path('jekyll-metrics/version', __dir__)
5
6
  require File.expand_path('jekyll-metrics/config', __dir__)
6
7
  require File.expand_path('jekyll-metrics/hook', __dir__)
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JekyllMetrics
2
4
  # Hold the configuration needed for {JekyllMetrics::Hook} to work
3
5
  class Config
4
- CONFIG_NAME = 'jekyll_metrics'.freeze
5
- DEFAULT_TEMPLATE_PATH = 'lib/jekyll-metrics/includes/metrics.html.liquid'.freeze
6
+ CONFIG_NAME = 'jekyll_metrics'
7
+ DEFAULT_TEMPLATE_PATH = 'lib/jekyll-metrics/includes/metrics.html.liquid'
6
8
  DEFAULT_CONFIG = {
7
- 'template' => DEFAULT_TEMPLATE_PATH,
8
- 'yandex_metrica_id' => 'XXXXXXXX',
9
- 'google_analytics_id' => 'XX-XXXXXXXXX-X'
9
+ 'template' => DEFAULT_TEMPLATE_PATH,
10
+ 'yandex_metrica_id' => 'XXXXXXXX',
11
+ 'google_analytics_id' => 'XX-XXXXXXXXX-X',
10
12
  }.freeze
11
13
 
12
14
  class << self
@@ -36,7 +38,7 @@ module JekyllMetrics
36
38
 
37
39
  return default_template_path if custom_path.nil?
38
40
 
39
- if custom_path.match?(%r{^\/})
41
+ if custom_path.match?(%r!^\/!)
40
42
  Pathname.new(custom_path)
41
43
  else
42
44
  site_root_path.join(custom_path)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JekyllMetrics
2
4
  # Compile metrics template and inject it into the page code
3
5
  class Hook
@@ -8,16 +10,50 @@ module JekyllMetrics
8
10
  yield(@page, self) if block_given? && injectable?
9
11
  end
10
12
 
13
+ def inject_scripts
14
+ return unless injectable?
15
+
16
+ document = Nokogiri::HTML(page.output)
17
+ first_head_script = find_first_script(document)
18
+
19
+ if first_head_script
20
+ inject_after_first_script(first_head_script, document)
21
+ else
22
+ inject_before_closing_head(document)
23
+ end
24
+
25
+ page.output.replace(document.to_html)
26
+ end
27
+
28
+ private
29
+
11
30
  def injectable?
12
- ['Jekyll::Document', 'Jekyll::Page'].include?(page.class.name) || page.write? &&
13
- (page.output_ext == '.html' || page.permalink&.end_with?('/'))
31
+ writable? && compiled_to_html_page? && can_be_modified?
14
32
  end
15
33
 
16
- def inject_scripts
17
- page.output.gsub!(%r{<\/head>}, load_scripts)
34
+ def writable?
35
+ [Jekyll::Document, Jekyll::Page].include?(page.class) || page.write?
18
36
  end
19
37
 
20
- private
38
+ def compiled_to_html_page?
39
+ page.output_ext == '.html' || page.permalink&.end_with?('/')
40
+ end
41
+
42
+ def can_be_modified?
43
+ page.output.match?('<html') && page.output.match('<\/head')
44
+ end
45
+
46
+ def inject_after_first_script(first_head_script, _document)
47
+ first_head_script.add_previous_sibling(load_scripts)
48
+ end
49
+
50
+ def inject_before_closing_head(document)
51
+ document&.xpath('//head')&.first&.add_child(load_scripts)
52
+ end
53
+
54
+ def find_first_script(document)
55
+ document&.xpath('//head')&.xpath('script')&.first
56
+ end
21
57
 
22
58
  def prepare_scripts_for(closing_tag)
23
59
  [load_scripts, "</#{closing_tag}>"].compact.join("\n")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllMetrics
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-metrics
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
  - Ivan Zinovyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-23 00:00:00.000000000 Z
11
+ date: 2020-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: nokogiri
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: bundler
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -44,6 +58,20 @@ dependencies:
44
58
  - - ">="
45
59
  - !ruby/object:Gem::Version
46
60
  version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
47
75
  - !ruby/object:Gem::Dependency
48
76
  name: rake
49
77
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +100,20 @@ dependencies:
72
100
  - - ">="
73
101
  - !ruby/object:Gem::Version
74
102
  version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-jekyll
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.11.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.11.0
75
117
  description: Metrics plugin for Jekyll. Supports Yandex Metrics and Google Analytics
76
118
  out of the box.
77
119
  email: