html-pipeline-negarmoji 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ ## Negareh
File without changes
@@ -0,0 +1 @@
1
+ ## Negareh Emoji HTML Pipeline Support
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "html/pipeline/negarmoji-pipeline/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "html-pipeline-negarmoji"
9
+ spec.version = Html::Pipeline::NegarMojiHtmlPipeline::VERSION
10
+ spec.platform = Gem::Platform::RUBY
11
+ spec.authors = ["Mohammad Mahdi Baghbani Pourvahid"]
12
+ spec.email = "MahdiBaghbani@protonmail.com"
13
+ spec.homepage = "https://gitlab.com/Azadeh-Afzar/Web-Development/Negareh-Emoji-HTML-Pipeline"
14
+ spec.description = "%(Negareh emoji library emoji filter for html pipeline, this package is more
15
+ flexible and customizable then the original emoji filter present in
16
+ html/pipeline)"
17
+ spec.summary = "Negareh emoji html pipeline"
18
+ spec.licenses = "GPL-3.0"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r!^bin/!) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r!^test/!)
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.required_ruby_version = ">= 2.3.0"
26
+
27
+ spec.add_dependency "html-pipeline", "~> 2.2"
28
+ spec.add_dependency "negarmoji", "~> 0.1.1"
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 12.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "rubocop-jekyll", "~> 0.4"
34
+ spec.add_development_dependency "simplecov", "~> 0.17.0"
35
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "html/pipeline"
4
+ require "html/pipeline/negarmoji-pipeline/version"
5
+
6
+ module HTML
7
+ class Pipeline
8
+ class NegarMojiHtmlPipeline
9
+ # Negareh Emoji HTML Pipeline related filters for html-pipeline.
10
+ # Implements new filters used by Negareh Emoji HTML Pipeline
11
+
12
+ # Custom filter implementations
13
+ autoload :NegarehEmojiFilter, "negarmoji-pipeline/filter"
14
+
15
+ def initialize(filters)
16
+ @filters = filters.flatten.freeze
17
+ end
18
+
19
+ def pipeline
20
+ HTML::Pipeline.new @filters
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+ HTML::Pipeline.require_dependency("negarmoji", "NegarehEmojiFilter")
5
+
6
+ module HTML
7
+ class Pipeline
8
+ class NegarMojiHtmlPipeline
9
+ # HTML filter that replaces :emoji: with images.
10
+ #
11
+ # Context:
12
+ # :asset_root (required) - base url to link to emoji sprite.
13
+ #
14
+ # :asset_path (optional) - url path to link to emoji sprite. :file_name can be used as
15
+ # a placeholder for the sprite file name.
16
+ # If no asset_path is set ":file_name" is used.
17
+ #
18
+ # :extension (optional) - extension to be use for emoji files, default extension is svg.
19
+ #
20
+ # :ignored_ancestor_tags (optional) - Tags to stop the emojification. Node has matched
21
+ # ancestor HTML tags will not be emojified. Default to pre, code, and tt tags. Extra tags
22
+ # please pass in the form of array, e.g., %w(blockquote summary).
23
+ #
24
+ # :img_attrs (optional) - Attributes for generated img tag.
25
+ # E.g. Pass { "draggble" => true, "height" => nil } to set draggable attribute to "true"
26
+ # and clear height attribute of generated img tag.
27
+ class NegarehEmojiFilter < Filter
28
+ DEFAULT_IGNORED_ANCESTOR_TAGS = %w(pre code tt).freeze
29
+
30
+ def call
31
+ doc.search(".//text()").each do |node|
32
+ content = node.text
33
+
34
+ next unless content.include?(":")
35
+ next if has_ancestor?(node, ignored_ancestor_tags)
36
+
37
+ html = emoji_image_filter(content)
38
+
39
+ next if html == content
40
+
41
+ node.replace(html)
42
+ end
43
+ doc
44
+ end
45
+
46
+ # Implementation of validate hook.
47
+ # Errors should raise exceptions or use an existing validator.
48
+ def validate
49
+ needs :asset_root
50
+ end
51
+
52
+ # Replace :emoji: with corresponding images.
53
+ #
54
+ # text - String text to replace :emoji: in.
55
+ #
56
+ # Returns a String with :emoji: replaced with images.
57
+ def emoji_image_filter(text)
58
+ text.gsub(emoji_pattern) do |_match|
59
+ emoji_image_tag(Regexp.last_match(1))
60
+ end
61
+ end
62
+
63
+ # The base url to link emoji sprites
64
+ #
65
+ # Raises ArgumentError if context option has not been provided.
66
+ # Returns the context's asset_root.
67
+ def asset_root
68
+ context[:asset_root]
69
+ end
70
+
71
+ # The url path to link emoji sprites
72
+ #
73
+ # :file_name can be used in the asset_path as a placeholder for the sprite file name.
74
+ # If no asset_path is set in the context ":file_name" is used.
75
+ # Returns the context's asset_path or the default path if no context asset_path is given.
76
+ def asset_path(name)
77
+ if context[:asset_path]
78
+ context[:asset_path].gsub(":file_name", emoji_filename(name))
79
+ else
80
+ File.join(emoji_filename(name))
81
+ end
82
+ end
83
+
84
+ # Emoji file extension.
85
+ #
86
+ # Extension to be use for emoji files, default extension is svg.
87
+ def emoji_extension
88
+ context[:extension] || "svg"
89
+ end
90
+
91
+ private
92
+
93
+ # Build an emoji image tag
94
+ def emoji_image_tag(name)
95
+ require "active_support/core_ext/hash/indifferent_access"
96
+ html_attrs =
97
+ default_img_attrs(name)
98
+ .merge!((context[:img_attrs] || {}).with_indifferent_access)
99
+ .map do |attr, value|
100
+ !value.nil? && %(#{attr}="#{value.respond_to?(:call) && value.call(name) || value}")
101
+ end
102
+ .reject(&:blank?).join(" ")
103
+
104
+ "<img #{html_attrs}>"
105
+ end
106
+
107
+ # Default attributes for img tag
108
+ def default_img_attrs(name)
109
+ {
110
+ :class => "emoji",
111
+ :title => ":#{name}:",
112
+ :alt => ":#{name}:",
113
+ :src => emoji_url(name).to_s,
114
+ :height => "20",
115
+ :width => "20",
116
+ :align => "absmiddle",
117
+ }
118
+ end
119
+
120
+ def emoji_url(name)
121
+ File.join(asset_root, asset_path(name))
122
+ end
123
+
124
+ def emoji_pattern
125
+ self.class.emoji_pattern
126
+ end
127
+
128
+ def emoji_filename(name)
129
+ Emoji.find_by_alias(name).image_filename(emoji_extension)
130
+ end
131
+
132
+ # Return ancestor tags to stop the emojification.
133
+ #
134
+ # @return [Array<String>] Ancestor tags.
135
+ def ignored_ancestor_tags
136
+ if context[:ignored_ancestor_tags]
137
+ DEFAULT_IGNORED_ANCESTOR_TAGS | context[:ignored_ancestor_tags]
138
+ else
139
+ DEFAULT_IGNORED_ANCESTOR_TAGS
140
+ end
141
+ end
142
+
143
+ class << self
144
+ # Build a regexp that matches all valid :emoji: names.
145
+ def self.emoji_pattern
146
+ @emoji_pattern ||= %r!:(#{emoji_names.map { |name| Regexp.escape(name) }.join("|")}):!
147
+ end
148
+
149
+ def self.emoji_names
150
+ Emoji.all.map(&:aliases).flatten.sort
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Html
4
+ module Pipeline
5
+ module NegarMojiHtmlPipeline
6
+ VERSION = "0.0.1"
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html-pipeline-negarmoji
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mohammad Mahdi Baghbani Pourvahid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: html-pipeline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: negarmoji
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.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
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-jekyll
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.17.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.17.0
111
+ description: |-
112
+ %(Negareh emoji library emoji filter for html pipeline, this package is more
113
+ flexible and customizable then the original emoji filter present in
114
+ html/pipeline)
115
+ email: MahdiBaghbani@protonmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rubocop.yml"
122
+ - ".rubocop_todo.yml"
123
+ - CHANGELOG.md
124
+ - CODE_OF_CONDUCT.md
125
+ - CONTRIBUTING.md
126
+ - Gemfile
127
+ - LICENSE
128
+ - README.md
129
+ - ROADMAP.md
130
+ - SUPPORT.md
131
+ - html-pipeline-negarmoji.gemspec
132
+ - lib/html/pipeline/negarmoji-pipeline.rb
133
+ - lib/html/pipeline/negarmoji-pipeline/filter.rb
134
+ - lib/html/pipeline/negarmoji-pipeline/version.rb
135
+ homepage: https://gitlab.com/Azadeh-Afzar/Web-Development/Negareh-Emoji-HTML-Pipeline
136
+ licenses:
137
+ - GPL-3.0
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: 2.3.0
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.0.4
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Negareh emoji html pipeline
158
+ test_files: []