jekyll-openmoji 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-openmoji.rb +97 -0
  3. metadata +161 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f25f6b719bed630089dffd7f118461bab6559c00396e9908a6cf15b0a66473e8
4
+ data.tar.gz: ea5df93a9b97740c8a88718ffb5b82c91901ee1ec294a0bfa3ba91060a0fb948
5
+ SHA512:
6
+ metadata.gz: c8578f6200b7a0c757b5dd93a987ed65ca8d2f27254b2f3552450f77e15d899229695946abce944672afc7408ff8cf0b453c66356cac3639783d7d9c1aaad54e
7
+ data.tar.gz: 57774ca876f4f3967bf784de5cb3fe11e872464972fe2402de99537abc481c4fd9741c3213198bcacbe6df80b06305748e660f67a356f0c14da9eb28db29fe6e
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+ require 'gemoji'
5
+ require "negarmoji"
6
+ require "html/pipeline"
7
+
8
+ module Jekyll
9
+ class Emoji
10
+ OPENMOJI_ASSET_HOST_URL = "https://cdn.jsdeliver.net/gh/azadeh-afzar/OpenMoji-Jekyll-Plugin"
11
+ ASSET_PATH = "/images/color/svg"
12
+ BODY_START_TAG = "<body"
13
+ OPENING_BODY_TAG_REGEX = %r!<body(.*?)>\s*!m.freeze
14
+
15
+ class << self
16
+ def emojify(doc)
17
+ return unless doc.output =~ HTML::Pipeline::EmojiFilter.emoji_pattern
18
+
19
+ doc.output = if doc.output.include? BODY_START_TAG
20
+ replace_document_body(doc)
21
+ else
22
+ src = emoji_src(doc.site.config)
23
+ filter_with_emoji(src).call(doc.output)[:output].to_s
24
+ end
25
+ end
26
+
27
+ # Public: Create or fetch the filter for the given {{src}} asset root.
28
+ #
29
+ # src - the asset root URL (e.g. https://github.githubassets.com/images/icons/)
30
+ #
31
+ # Returns an HTML::Pipeline instance for the given asset root.
32
+ def filter_with_emoji(src)
33
+ filters[src] ||= HTML::Pipeline.new([
34
+ HTML::Pipeline::EmojiFilter,
35
+ ], :asset_root => src, :img_attrs => { :align => nil })
36
+ end
37
+
38
+ # Public: Filters hash where the key is the asset root source.
39
+ # Effectively a cache.
40
+ def filters
41
+ @filters ||= {}
42
+ end
43
+
44
+ # Public: Calculate the asset root source for the given config.
45
+ # The custom emoji asset root can be defined in the config as
46
+ # emoji.src, and must be a valid URL (i.e. it must include a
47
+ # protocol and valid domain)
48
+ #
49
+ # config - the hash-like configuration of the document's site
50
+ #
51
+ # Returns a full URL to use as the asset root URL. Defaults to the root
52
+ # URL for assets provided by an ASSET_HOST_URL environment variable,
53
+ # otherwise the root URL for emoji assets at assets-cdn.github.com.
54
+ def emoji_src(config = {})
55
+ if config.key?("emoji") && config["emoji"].key?("src")
56
+ config["emoji"]["src"]
57
+ else
58
+ default_asset_root
59
+ end
60
+ end
61
+
62
+ # Public: Defines the conditions for a document to be emojiable.
63
+ #
64
+ # doc - the Jekyll::Document or Jekyll::Page
65
+ #
66
+ # Returns true if the doc is written & is HTML.
67
+ def emojiable?(doc)
68
+ (doc.is_a?(Jekyll::Page) || doc.write?) &&
69
+ doc.output_ext == ".html" || (doc.permalink&.end_with?("/"))
70
+ end
71
+
72
+ private
73
+
74
+ def default_asset_root
75
+ if !ENV["ASSET_HOST_URL"].to_s.empty?
76
+ # Ensure that any trailing "/" is trimmed
77
+ asset_host_url = ENV["ASSET_HOST_URL"].chomp("/")
78
+ "#{asset_host_url}#{ASSET_PATH}"
79
+ else
80
+ "#{OPENMOJI_ASSET_HOST_URL}#{ASSET_PATH}"
81
+ end
82
+ end
83
+
84
+ def replace_document_body(doc)
85
+ src = emoji_src(doc.site.config)
86
+ head, opener, tail = doc.output.partition(OPENING_BODY_TAG_REGEX)
87
+ body_content, *rest = tail.partition("</body>")
88
+ processed_markup = filter_with_emoji(src).call(body_content)[:output].to_s
89
+ String.new(head) << opener << processed_markup << rest.join
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ Jekyll::Hooks.register [:pages, :documents], :post_render do |doc|
96
+ Jekyll::Emoji.emojify(doc) if Jekyll::Emoji.emojiable?(doc)
97
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-openmoji
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mohammad Mahdi Bgahbani Pourvahid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: negarmoji
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: gemoji
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: html-pipeline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jekyll
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '5.0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '3.0'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: bundler
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '2.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rake
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '12.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '12.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rspec
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rubocop-jekyll
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.4'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '0.4'
131
+ description:
132
+ email: MahdiBaghbani@protonmail.com
133
+ executables: []
134
+ extensions: []
135
+ extra_rdoc_files: []
136
+ files:
137
+ - lib/jekyll-openmoji.rb
138
+ homepage: https://github.com/jekyll/jekyll-openmoji
139
+ licenses:
140
+ - GPL-3.0
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 2.3.0
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubygems_version: 3.0.4
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: OpenMoji emoji plugin for Jekyll
161
+ test_files: []