jekyll-hashtags 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '095c38c828b59386d64ccbd146cd436d648dbee0'
4
+ data.tar.gz: d401e722673026f6de0ab63659fafbc752783a06
5
+ SHA512:
6
+ metadata.gz: 88f0763e84d5d8a27641efe4617b4852dae26479602dbbf45a8c2a5e4ce035572a8c62c74fc91b10a59e202b8082bcd048e62eeb63382c7cbedbd6a0aeddbce2
7
+ data.tar.gz: c49e73b37e070e95f036ac72f4a4e7e05fcb844d8f3d391bd9ee929dad4867044a82a91206ab4595a7223bfba50df5dd90a5436cb3e6c40b0e72b37a29039821
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .bundle
2
+ /*.gem
3
+ /tmp
4
+ vendor/
5
+ .idea/
6
+ bin
7
+ Gemfile.lock
8
+ pkg/*.gem
9
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --order random
data/.rspec_status ADDED
@@ -0,0 +1,9 @@
1
+ example_id | status | run_time |
2
+ ------------------------------------- | ------ | --------------- |
3
+ ./spec/jekyll/hashtags_spec.rb[1:1] | passed | 0.00816 seconds |
4
+ ./spec/jekyll/hashtags_spec.rb[1:2] | passed | 0.01133 seconds |
5
+ ./spec/jekyll/hashtags_spec.rb[1:3] | passed | 0.00876 seconds |
6
+ ./spec/jekyll/hashtags_spec.rb[1:4] | passed | 0.33524 seconds |
7
+ ./spec/jekyll/hashtags_spec.rb[1:5:1] | passed | 0.00738 seconds |
8
+ ./spec/jekyll/hashtags_spec.rb[1:5:2] | passed | 0.00604 seconds |
9
+ ./spec/jekyll/hashtags_spec.rb[1:5:3] | passed | 0.0059 seconds |
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in jekyll-hashtags.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Mynux
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Jekyll::Hashtags
2
+
3
+ #hashtags support for you jekyll site.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jekyll-hashtags'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jekyll-hashtags
20
+
21
+ ## Usage
22
+
23
+
24
+ Add the following to your site's Gemfile
25
+
26
+ gem 'jekyll-hashtags'
27
+ And add the following to your site's _config.yml
28
+
29
+ plugins:
30
+ - jekyll-hashtags
31
+ Note: if jekyll --version is less than 3.5 use:
32
+
33
+ gems:
34
+ - jekyll-hashtags
35
+
36
+ In any page or post, use @hashtags as you would normally, e.g.
37
+
38
+ Hey #TradeWar, what do you think of this?
39
+
40
+ ## Configuration
41
+ Have your own social network? No problem. We allow you to configure the base URL of all the hashtags.
42
+
43
+ To change it, add the following to your Jekyll configuration:
44
+
45
+ jekyll-hashtags:
46
+ base_url: https://hengwei.me
47
+
48
+ An example of Twitter hashtags using jekyll-hashtags:
49
+
50
+ plugins:
51
+ - jekyll-hashtags
52
+
53
+ jekyll-hashtags:
54
+ base_url: https://hengwei.me
55
+
56
+ ## Development
57
+
58
+ 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.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mynux/jekyll-hashtags.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jekyll-hashtags/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-hashtags"
8
+ spec.version = JekyllHashtags::VERSION
9
+ spec.authors = ["Wei Heng"]
10
+ spec.email = ["jj@hengwei.me"]
11
+
12
+ spec.summary = %q{#hashtag support for your Jekyll site}
13
+ spec.description = %q{#hashtag support for your Jekyll site with twitter-like feature.}
14
+ spec.homepage = "https://github.com/mynux/jekyll-hashtags"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ #end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "html-pipeline-hashtag", "~> 0.1.1"
36
+ spec.add_dependency "jekyll", "~> 3.0"
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.16"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll'
4
+ require 'html/pipeline/hashtag/hashtag_filter'
5
+
6
+ module Jekyll
7
+ class Hashtags
8
+ HASH_TAG_SYMBOL = '#'
9
+ DEFAULT_HASHTAG_BASE_URL = ''
10
+ DEFAULT_HASHTAG_URL = '/tags/%{tag}'
11
+ DEFAULT_HASHTAG_PATTERN = '/#([\p{L}\w\-]+)/'
12
+ BODY_START_TAG = '<body'
13
+ OPENING_BODY_TAG_REGEX = %r!<body(.*)>\s*!
14
+ BODY_END_TAG = '</body>'
15
+
16
+ class << self
17
+ def hashtag_it(doc)
18
+ content = doc.output
19
+ return unless content.include?(HASH_TAG_SYMBOL)
20
+ base_url = get_base_url(doc.site.config)
21
+ if content.include? BODY_START_TAG
22
+ head, opener, tail = content.partition(OPENING_BODY_TAG_REGEX)
23
+ body, *rest = tail.partition(BODY_END_TAG)
24
+ return unless body =~ get_hashtag_pattern(doc.site.config)
25
+ processed_markup = filter_with_hashtag(doc.site.config, base_url).call(body)[:output].to_s
26
+ doc.output = String.new(head) << opener << processed_markup << rest.join
27
+ end
28
+ end
29
+
30
+ # Public: Create or fetch the filter for the given {{src}} base URL.
31
+ #
32
+ # :doc
33
+ # :base_url - the base URL (e.g. http://mynux.cn)
34
+ #
35
+ # Returns an HTML::Pipeline instance for the given base URL.
36
+ #
37
+ def filter_with_hashtag(config = {}, base_url = DEFAULT_HASHTAG_BASE_URL)
38
+ filters[base_url] ||= HTML::Pipeline.new([HTML::Pipeline::HashtagFilter],
39
+ :hashtag_pattern => get_hashtag_pattern(config),
40
+ :tag_url => get_tag_url(config)
41
+ )
42
+ end
43
+
44
+ # Public: Filters hash where the key is the mention base URL.
45
+ # Effectively a cache.
46
+ def filters
47
+ @filters ||= {}
48
+ end
49
+
50
+ # Public: Calculate the tag pattern to use for hashtag.
51
+ # The custom tag patter can be defined in the config as
52
+ # jekyll-hashtag.tag_pattern or jekyll-hashtags, and must
53
+ # be a valid regex.
54
+ #
55
+ # config - the hash-like configuration of the document's site
56
+ #
57
+ # Returns a regex valid string to use as the tag pattern for hashtags.
58
+ # Defaults is #{DEFAULT_HASHTAG_PATTERN}
59
+ #
60
+ def get_hashtag_pattern(config = {})
61
+ hashtag_config = config["jekyll-hashtags"]
62
+ case hashtag_config
63
+ when nil, NilClass
64
+ Regexp.new(HTML::Pipeline::HashtagFilter::HashtagPattern)
65
+ when String
66
+ Regexp.new(hashtag_config.to_s)
67
+ when Hash
68
+ Regexp.new(hashtag_config.fetch("tag_pattern", DEFAULT_HASHTAG_PATTERN))
69
+ else
70
+ raise Class.new(Jekyll::Errors::FatalException),
71
+ "Your jekyll-hashtag config has to either be a" \
72
+ " string or a hash. It's a #{hashtag_config.class} right now."
73
+ end
74
+ end
75
+
76
+ # Public: Calculate the base URL to use for hashtag.
77
+ # The custom base URL can be defined in the config as
78
+ # jekyll-hashtag.base_url or jekyll-hashtags, and must
79
+ # be a valid URL (i.e. it must include a protocol and valid domain)
80
+ # It should _not_ have a trailing slash.
81
+ #
82
+ # config - the hash-like configuration of the document's site
83
+ #
84
+ # Returns a URL to use as the base URL for hashtags.
85
+ # Defaults is blank.
86
+ #
87
+ def get_base_url(config = {})
88
+ hashtag_config = config["jekyll-hashtags"]
89
+ case hashtag_config
90
+ when nil, NilClass
91
+ DEFAULT_HASHTAG_BASE_URL
92
+ when String
93
+ hashtag_config.to_s
94
+ when Hash
95
+ hashtag_config.fetch("base_url", DEFAULT_HASHTAG_BASE_URL)
96
+ else
97
+ raise Class.new(Jekyll::Errors::FatalException),
98
+ "Your jekyll-hashtag config has to either be a" \
99
+ " string or a hash. It's a #{hashtag_config.class} right now."
100
+ end
101
+ end
102
+
103
+ # Public: Calculate the default tag url to use for hashtag.
104
+ # The custom tag patter can be defined in the config as
105
+ # jekyll-hashtag.tag_url or jekyll-hashtags.
106
+ #
107
+ # config - the hash-like configuration of the document's site
108
+ #
109
+ # Returns a regex valid string to use as the tag pattern for hashtags.
110
+ # Defaults is #{DEFAULT_HASHTAG_URL}
111
+ #
112
+ def get_tag_url(config = {})
113
+ hashtag_config = config["jekyll-hashtags"]
114
+ case hashtag_config
115
+ when nil, NilClass
116
+ DEFAULT_HASHTAG_URL
117
+ when String
118
+ hashtag_config.to_s
119
+ when Hash
120
+ hashtag_config.fetch("tag_url", DEFAULT_HASHTAG_BASE_URL)
121
+ else
122
+ raise Class.new(Jekyll::Errors::FatalException),
123
+ "Your jekyll-hashtag config has to either be a" \
124
+ " string or a hash. It's a #{hashtag_config.class} right now."
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ Jekyll::Hooks.register %i[pages documents], :post_render do |doc|
132
+ Jekyll::Hashtags.hashtag_it(doc)
133
+ end
@@ -0,0 +1,3 @@
1
+ module JekyllHashtags
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-hashtags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wei Heng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: html-pipeline-hashtag
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: "#hashtag support for your Jekyll site with twitter-like feature."
84
+ email:
85
+ - jj@hengwei.me
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rspec_status"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - jekyll-hashtags.gemspec
98
+ - lib/jekyll-hashtags.rb
99
+ - lib/jekyll-hashtags/version.rb
100
+ homepage: https://github.com/mynux/jekyll-hashtags
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.6.14
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: "#hashtag support for your Jekyll site"
124
+ test_files: []