html-pipeline-mrkdwn 0.1.10 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a719d28f9a177bf2309038577b5fa5ca0fde8becbe75596bb6c802b836a92f00
4
- data.tar.gz: 1a7233f84889b309b7e32517ff302327507c577c19dfd5b0a655dab73738fe75
3
+ metadata.gz: 865e7c4e26f60927fe0ee325097aead119cffa7ae67bb8c607ec515516a81484
4
+ data.tar.gz: 899c386db5debf2898fe4e7a42aebe1332a09456bb6dde00a9b64f15fa659dbc
5
5
  SHA512:
6
- metadata.gz: 5ebe821d1f8aa9216bf46b10d1c1bc91f09d5561131896f6eb60817e53949aa5c222aa84617b6f69653042d2f6ed046f408388b94f8f373c29b5a10e2fc93cc3
7
- data.tar.gz: 7e4836e583e4f76a845a0c21bed986947242dcd2938db8446ae11d3af03b56eec9249992d23d7922a03076c05e1b005b7e1184c870bb4753fdfa0cacb47c35b9
6
+ metadata.gz: 72a042625fb8173c6b645ad0d1620b18a928ef36849f7e62593a52a1ce728ae033d137c89c4858da6d69da8acdb208966163385d463222d481b602c373d71ebc
7
+ data.tar.gz: 1c00cc52f3bfed425c2aecc6363ccdeb94e7bbf4062382f7b940fa16e20b48deb91543f82451da2c0ef57d16798cc5c1f82e5919bf3dd90c3be907f319d47dc6
@@ -0,0 +1,3 @@
1
+ require_relative "../../mrkdwn"
2
+
3
+ PIPELINE = HTML::Pipeline.new [ HTML::Pipeline::PlainTextInputFilter, HTML::Pipeline::Mrkdwn ]
@@ -12,6 +12,12 @@ module HTML
12
12
  # :slack_channels - a hash of Slack channel ids and channel names
13
13
  # :slack_users - a hash of Slack user or bot ids and display names
14
14
  class Mrkdwn < Filter
15
+ attr_reader :large_emoji
16
+
17
+ EMOJI_AND_WHITESPACE_PATTERN = /(?::[\w+-_]+?:|\s)+/.freeze
18
+
19
+ NON_CAPTURING_EMOJI_PATTERN = /:[\w+-_]+?:/.freeze
20
+
15
21
  MULTILINE_CODE_PATTERN = /```(.+?)```/m.freeze
16
22
 
17
23
  CODE_PATTERN = /`(?=\S)(.+?)(?<=\S)`/.freeze
@@ -33,7 +39,7 @@ module HTML
33
39
 
34
40
  LINE_BREAK_PATTERN = /\n/.freeze
35
41
 
36
- EMOJI_PATTERN = /:([\w+-_]+):/.freeze
42
+ EMOJI_PATTERN = /:([\w+-_]+?):/.freeze
37
43
 
38
44
  STYLE_PATTERN = /
39
45
  ([`*_~])
@@ -51,22 +57,30 @@ module HTML
51
57
  link: %w[&lt;],
52
58
  line_break: %W[\n \r\n],
53
59
  emoji: %w[:],
54
- style: %w[* _ ~]
60
+ style: %w[* _ ~],
61
+ unescape: %w[&amp;lowbar;]
55
62
  }.freeze
56
63
 
57
64
  def initialize(doc, context = nil, result = nil)
58
65
  super
59
66
 
60
- @emoji_image_tag = ->(emoji) { "<img src=\"#{emoji.image_filename}\" alt=\"#{emoji.name}\" class=\"emoji\">" }
61
- @slack_channels = {}
62
- @slack_users = {}
67
+ default_emoji_image_tag = ->(emoji) { "<img src=\"#{emoji.image_filename}\" alt=\"#{emoji.name}\" class=\"emoji\">" }
63
68
 
64
- @emoji_image_tag = @context[:emoji_image_tag] if @context[:emoji_image_tag]
65
- @slack_channels = @context[:slack_channels] if @context[:slack_channels]
66
- @slack_users = @context[:slack_users] if @context[:slack_users]
69
+ @emoji_image_tag = @context[:emoji_image_tag] || default_emoji_image_tag
70
+ @large_emoji_image_tag = @context[:large_emoji_image_tag] || @emoji_image_tag
71
+ @large_emoji_class = @context[:large_emoji_class] || "emoji-lg"
72
+ @slack_channels = @context[:slack_channels] || {}
73
+ @slack_users = @context[:slack_users] || {}
67
74
  end
68
75
 
69
76
  def call
77
+ div = doc.at_css("div")
78
+
79
+ if wants_large_emoji?(div.content)
80
+ @large_emoji = true
81
+ div.add_class(@large_emoji_class)
82
+ end
83
+
70
84
  ELEMENTS.each do |element, includes|
71
85
  process_text_nodes(includes) { |content| call_filter(element, content) }
72
86
  end
@@ -80,6 +94,11 @@ module HTML
80
94
  "context[:emoji_image_tag] should return a Proc that takes an Emoji object as in the 'gemoji' gem."
81
95
  end
82
96
 
97
+ if context[:large_emoji_image_tag] && !context[:large_emoji_image_tag].is_a?(Proc)
98
+ raise ArgumentError,
99
+ "context[:large_emoji_image_tag] should return a Proc that takes an Emoji object as in the 'gemoji' gem."
100
+ end
101
+
83
102
  if context[:slack_channels] && !context[:slack_channels].is_a?(Hash)
84
103
  raise ArgumentError,
85
104
  "context[:slack_channels] should return a Hash whose keys are Slack channel ids and values are their names."
@@ -183,10 +202,16 @@ module HTML
183
202
  content.gsub(EMOJI_PATTERN) do |match|
184
203
  emoji = Emoji.find_by_alias(Regexp.last_match[1])
185
204
 
186
- if emoji
187
- emoji.raw || @emoji_image_tag.call(emoji) || match
205
+ if emoji&.custom?
206
+ if @large_emoji
207
+ @large_emoji_image_tag.call(emoji)
208
+ else
209
+ @emoji_image_tag.call(emoji)
210
+ end
211
+ elsif emoji
212
+ emoji.raw
188
213
  else
189
- match
214
+ match.gsub(/_/, "&lowbar;")
190
215
  end
191
216
  end
192
217
  end
@@ -212,6 +237,26 @@ module HTML
212
237
  end
213
238
  end
214
239
  end
240
+
241
+ def unescape_filter(content)
242
+ content.gsub("&amp;lowbar;", "_")
243
+ end
244
+
245
+ private
246
+
247
+ def only_emoji_and_whitespace?(string)
248
+ matchdata = EMOJI_AND_WHITESPACE_PATTERN.match(string)
249
+
250
+ matchdata && matchdata[0] == string
251
+ end
252
+
253
+ def emoji_count(string)
254
+ string.scan(NON_CAPTURING_EMOJI_PATTERN).size
255
+ end
256
+
257
+ def wants_large_emoji?(string)
258
+ only_emoji_and_whitespace?(string) && emoji_count(string).between?(1, 23)
259
+ end
215
260
  end
216
261
  end
217
262
  end
@@ -5,7 +5,7 @@ require "html/pipeline"
5
5
  module HTML
6
6
  class Pipeline
7
7
  class Mrkdwn < Filter
8
- VERSION = "0.1.10"
8
+ VERSION = "0.2.1"
9
9
  end
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-pipeline-mrkdwn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Gorst
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-29 00:00:00.000000000 Z
11
+ date: 2024-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -138,6 +138,7 @@ files:
138
138
  - Rakefile
139
139
  - bin/mrkdwn
140
140
  - lib/html/pipeline/mrkdwn.rb
141
+ - lib/html/pipeline/mrkdwn/helpers/pipeline.rb
141
142
  - lib/html/pipeline/mrkdwn/mrkdwn_filter.rb
142
143
  - lib/html/pipeline/mrkdwn/version.rb
143
144
  homepage: https://github.com/jasongorst/html-pipeline-mrkdwn