slack_markdown 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 +7 -0
- data/.gitignore +305 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +2 -0
- data/lib/slack_markdown.rb +4 -0
- data/lib/slack_markdown/filters/bold_filter.rb +32 -0
- data/lib/slack_markdown/filters/code_filter.rb +34 -0
- data/lib/slack_markdown/filters/convert_filter.rb +65 -0
- data/lib/slack_markdown/filters/emoji_filter.rb +34 -0
- data/lib/slack_markdown/filters/ignorable_ancestor_tags.rb +16 -0
- data/lib/slack_markdown/filters/italic_filter.rb +32 -0
- data/lib/slack_markdown/filters/line_break_filter.rb +24 -0
- data/lib/slack_markdown/filters/multiple_code_filter.rb +34 -0
- data/lib/slack_markdown/filters/multiple_quote_filter.rb +35 -0
- data/lib/slack_markdown/filters/quote_filter.rb +26 -0
- data/lib/slack_markdown/processor.rb +39 -0
- data/lib/slack_markdown/version.rb +3 -0
- data/slack_markdown.gemspec +30 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2611046eb15a220ec98bcea27792812706be189c
|
4
|
+
data.tar.gz: 45f06db6f554b3875572b16efe9b45ef907cb43b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6863c68f300a0fc182e7285826c9dccd43fd910bd858caec5057de951df860d0672e5b49316d138ec029e558ac638c8e826bf7b19019484cd33451f946fb3c29
|
7
|
+
data.tar.gz: 5d99c4c02dfb27ec3b1fb64b5483063a8377248efaa590e3dfeeeff4267fdc0bbd805df69d9becfa3cc6580077685620770f59c8e62e258ebbf84a0f7d04a8a3
|
data/.gitignore
ADDED
@@ -0,0 +1,305 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
|
11
|
+
# Created by https://www.gitignore.io
|
12
|
+
|
13
|
+
### OSX ###
|
14
|
+
.DS_Store
|
15
|
+
.AppleDouble
|
16
|
+
.LSOverride
|
17
|
+
|
18
|
+
# Icon must end with two \r
|
19
|
+
Icon
|
20
|
+
|
21
|
+
|
22
|
+
# Thumbnails
|
23
|
+
._*
|
24
|
+
|
25
|
+
# Files that might appear in the root of a volume
|
26
|
+
.DocumentRevisions-V100
|
27
|
+
.fseventsd
|
28
|
+
.Spotlight-V100
|
29
|
+
.TemporaryItems
|
30
|
+
.Trashes
|
31
|
+
.VolumeIcon.icns
|
32
|
+
|
33
|
+
# Directories potentially created on remote AFP share
|
34
|
+
.AppleDB
|
35
|
+
.AppleDesktop
|
36
|
+
Network Trash Folder
|
37
|
+
Temporary Items
|
38
|
+
.apdisk
|
39
|
+
|
40
|
+
|
41
|
+
### Windows ###
|
42
|
+
# Windows image file caches
|
43
|
+
Thumbs.db
|
44
|
+
ehthumbs.db
|
45
|
+
|
46
|
+
# Folder config file
|
47
|
+
Desktop.ini
|
48
|
+
|
49
|
+
# Recycle Bin used on file shares
|
50
|
+
$RECYCLE.BIN/
|
51
|
+
|
52
|
+
# Windows Installer files
|
53
|
+
*.cab
|
54
|
+
*.msi
|
55
|
+
*.msm
|
56
|
+
*.msp
|
57
|
+
|
58
|
+
# Windows shortcuts
|
59
|
+
*.lnk
|
60
|
+
|
61
|
+
|
62
|
+
### Linux ###
|
63
|
+
*~
|
64
|
+
|
65
|
+
# KDE directory preferences
|
66
|
+
.directory
|
67
|
+
|
68
|
+
# Linux trash folder which might appear on any partition or disk
|
69
|
+
.Trash-*
|
70
|
+
|
71
|
+
|
72
|
+
### Vim ###
|
73
|
+
[._]*.s[a-w][a-z]
|
74
|
+
[._]s[a-w][a-z]
|
75
|
+
*.un~
|
76
|
+
Session.vim
|
77
|
+
.netrwhist
|
78
|
+
*~
|
79
|
+
|
80
|
+
|
81
|
+
### Emacs ###
|
82
|
+
# -*- mode: gitignore; -*-
|
83
|
+
*~
|
84
|
+
\#*\#
|
85
|
+
/.emacs.desktop
|
86
|
+
/.emacs.desktop.lock
|
87
|
+
*.elc
|
88
|
+
auto-save-list
|
89
|
+
tramp
|
90
|
+
.\#*
|
91
|
+
|
92
|
+
# Org-mode
|
93
|
+
.org-id-locations
|
94
|
+
*_archive
|
95
|
+
|
96
|
+
# flymake-mode
|
97
|
+
*_flymake.*
|
98
|
+
|
99
|
+
# eshell files
|
100
|
+
/eshell/history
|
101
|
+
/eshell/lastdir
|
102
|
+
|
103
|
+
# elpa packages
|
104
|
+
/elpa/
|
105
|
+
|
106
|
+
# reftex files
|
107
|
+
*.rel
|
108
|
+
|
109
|
+
# AUCTeX auto folder
|
110
|
+
/auto/
|
111
|
+
|
112
|
+
# cask packages
|
113
|
+
.cask/
|
114
|
+
|
115
|
+
|
116
|
+
### SublimeText ###
|
117
|
+
# cache files for sublime text
|
118
|
+
*.tmlanguage.cache
|
119
|
+
*.tmPreferences.cache
|
120
|
+
*.stTheme.cache
|
121
|
+
|
122
|
+
# workspace files are user-specific
|
123
|
+
*.sublime-workspace
|
124
|
+
|
125
|
+
# project files should be checked into the repository, unless a significant
|
126
|
+
# proportion of contributors will probably not be using SublimeText
|
127
|
+
# *.sublime-project
|
128
|
+
|
129
|
+
# sftp configuration file
|
130
|
+
sftp-config.json
|
131
|
+
|
132
|
+
|
133
|
+
### Intellij ###
|
134
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
|
135
|
+
|
136
|
+
*.iml
|
137
|
+
|
138
|
+
## Directory-based project format:
|
139
|
+
.idea/
|
140
|
+
# if you remove the above rule, at least ignore the following:
|
141
|
+
|
142
|
+
# User-specific stuff:
|
143
|
+
# .idea/workspace.xml
|
144
|
+
# .idea/tasks.xml
|
145
|
+
# .idea/dictionaries
|
146
|
+
|
147
|
+
# Sensitive or high-churn files:
|
148
|
+
# .idea/dataSources.ids
|
149
|
+
# .idea/dataSources.xml
|
150
|
+
# .idea/sqlDataSources.xml
|
151
|
+
# .idea/dynamic.xml
|
152
|
+
# .idea/uiDesigner.xml
|
153
|
+
|
154
|
+
# Gradle:
|
155
|
+
# .idea/gradle.xml
|
156
|
+
# .idea/libraries
|
157
|
+
|
158
|
+
# Mongo Explorer plugin:
|
159
|
+
# .idea/mongoSettings.xml
|
160
|
+
|
161
|
+
## File-based project format:
|
162
|
+
*.ipr
|
163
|
+
*.iws
|
164
|
+
|
165
|
+
## Plugin-specific files:
|
166
|
+
|
167
|
+
# IntelliJ
|
168
|
+
/out/
|
169
|
+
|
170
|
+
# mpeltonen/sbt-idea plugin
|
171
|
+
.idea_modules/
|
172
|
+
|
173
|
+
# JIRA plugin
|
174
|
+
atlassian-ide-plugin.xml
|
175
|
+
|
176
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
177
|
+
com_crashlytics_export_strings.xml
|
178
|
+
crashlytics.properties
|
179
|
+
crashlytics-build.properties
|
180
|
+
|
181
|
+
|
182
|
+
### RubyMine ###
|
183
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
|
184
|
+
|
185
|
+
*.iml
|
186
|
+
|
187
|
+
## Directory-based project format:
|
188
|
+
.idea/
|
189
|
+
# if you remove the above rule, at least ignore the following:
|
190
|
+
|
191
|
+
# User-specific stuff:
|
192
|
+
# .idea/workspace.xml
|
193
|
+
# .idea/tasks.xml
|
194
|
+
# .idea/dictionaries
|
195
|
+
|
196
|
+
# Sensitive or high-churn files:
|
197
|
+
# .idea/dataSources.ids
|
198
|
+
# .idea/dataSources.xml
|
199
|
+
# .idea/sqlDataSources.xml
|
200
|
+
# .idea/dynamic.xml
|
201
|
+
# .idea/uiDesigner.xml
|
202
|
+
|
203
|
+
# Gradle:
|
204
|
+
# .idea/gradle.xml
|
205
|
+
# .idea/libraries
|
206
|
+
|
207
|
+
# Mongo Explorer plugin:
|
208
|
+
# .idea/mongoSettings.xml
|
209
|
+
|
210
|
+
## File-based project format:
|
211
|
+
*.ipr
|
212
|
+
*.iws
|
213
|
+
|
214
|
+
## Plugin-specific files:
|
215
|
+
|
216
|
+
# IntelliJ
|
217
|
+
/out/
|
218
|
+
|
219
|
+
# mpeltonen/sbt-idea plugin
|
220
|
+
.idea_modules/
|
221
|
+
|
222
|
+
# JIRA plugin
|
223
|
+
atlassian-ide-plugin.xml
|
224
|
+
|
225
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
226
|
+
com_crashlytics_export_strings.xml
|
227
|
+
crashlytics.properties
|
228
|
+
crashlytics-build.properties
|
229
|
+
|
230
|
+
|
231
|
+
### Ruby ###
|
232
|
+
*.gem
|
233
|
+
*.rbc
|
234
|
+
/.config
|
235
|
+
/coverage/
|
236
|
+
/InstalledFiles
|
237
|
+
/pkg/
|
238
|
+
/spec/reports/
|
239
|
+
/test/tmp/
|
240
|
+
/test/version_tmp/
|
241
|
+
/tmp/
|
242
|
+
|
243
|
+
## Specific to RubyMotion:
|
244
|
+
.dat*
|
245
|
+
.repl_history
|
246
|
+
build/
|
247
|
+
|
248
|
+
## Documentation cache and generated files:
|
249
|
+
/.yardoc/
|
250
|
+
/_yardoc/
|
251
|
+
/doc/
|
252
|
+
/rdoc/
|
253
|
+
|
254
|
+
## Environment normalisation:
|
255
|
+
/.bundle/
|
256
|
+
/vendor/bundle
|
257
|
+
/lib/bundler/man/
|
258
|
+
|
259
|
+
# for a library or gem, you might want to ignore these files since the code is
|
260
|
+
# intended to run in multiple environments; otherwise, check them in:
|
261
|
+
# Gemfile.lock
|
262
|
+
# .ruby-version
|
263
|
+
# .ruby-gemset
|
264
|
+
|
265
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
266
|
+
.rvmrc
|
267
|
+
|
268
|
+
|
269
|
+
### Rails ###
|
270
|
+
*.rbc
|
271
|
+
capybara-*.html
|
272
|
+
.rspec
|
273
|
+
/log
|
274
|
+
/tmp
|
275
|
+
/db/*.sqlite3
|
276
|
+
/db/*.sqlite3-journal
|
277
|
+
/public/system
|
278
|
+
/coverage/
|
279
|
+
/spec/tmp
|
280
|
+
**.orig
|
281
|
+
rerun.txt
|
282
|
+
pickle-email-*.html
|
283
|
+
|
284
|
+
# TODO Comment out these rules if you are OK with secrets being uploaded to the repo
|
285
|
+
#config/initializers/secret_token.rb
|
286
|
+
#config/secrets.yml
|
287
|
+
|
288
|
+
## Environment normalisation:
|
289
|
+
/.bundle
|
290
|
+
/vendor/bundle
|
291
|
+
|
292
|
+
# these should all be checked in to normalise the environment:
|
293
|
+
# Gemfile.lock, .ruby-version, .ruby-gemset
|
294
|
+
|
295
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
296
|
+
.rvmrc
|
297
|
+
|
298
|
+
# if using bower-rails ignore default bower_components path bower.json files
|
299
|
+
/vendor/assets/bower_components
|
300
|
+
*.bowerrc
|
301
|
+
bower.json
|
302
|
+
|
303
|
+
# Ignore pow environment settings
|
304
|
+
.powenv
|
305
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Ru/MuckRu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# SlackMarkdown
|
2
|
+
|
3
|
+
SlackMarkdown (https://api.slack.com/docs/formatting) to HTML converter.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'slack_markdown'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install slack_markdown
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'slack_markdown'
|
25
|
+
|
26
|
+
processor = SlackMarkdown::Processor.new(
|
27
|
+
on_slack_channel_id: -> (uid) {
|
28
|
+
# TODO: use Slack API
|
29
|
+
return { url: '/general', text: 'general' }
|
30
|
+
},
|
31
|
+
on_slack_user_id: -> (uid) {
|
32
|
+
# TODO: use Slack API
|
33
|
+
return { url: '/ru_shalm', text: 'ru_shalm' }
|
34
|
+
},
|
35
|
+
asset_root: '/',
|
36
|
+
original_emoji_set: { ... },
|
37
|
+
)
|
38
|
+
|
39
|
+
processor.call("<@U12345> hello *world*")[:output].to_s
|
40
|
+
# => "<a href="/ru_shalm">@ru_shalm</a> hello <b>world</b>"
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/rutan/slack_markdown/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class BoldFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
doc.search(".//text()").each do |node|
|
13
|
+
content = node.to_html
|
14
|
+
next if has_ancestor?(node, ignored_ancestor_tags)
|
15
|
+
next unless content.include?('*')
|
16
|
+
html = bold_filter(content)
|
17
|
+
next if html == content
|
18
|
+
node.replace(html)
|
19
|
+
end
|
20
|
+
doc
|
21
|
+
end
|
22
|
+
|
23
|
+
def bold_filter(text)
|
24
|
+
text.gsub(BOLD_PATTERN) do
|
25
|
+
"<b>#{$1}</b>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
BOLD_PATTERN = /(?<=^|\W)\*(.+)\*(?=\W|$)/
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class CodeFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
doc.search(".//text()").each do |node|
|
13
|
+
content = node.to_html
|
14
|
+
next if has_ancestor?(node, ignored_ancestor_tags)
|
15
|
+
next unless content.include?('`')
|
16
|
+
html = code_filter(content)
|
17
|
+
next if html == content
|
18
|
+
node.replace(html)
|
19
|
+
end
|
20
|
+
doc
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def code_filter(text)
|
26
|
+
text.gsub(CODE_PATTERN) do
|
27
|
+
"<code>#{$1}</code>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
CODE_PATTERN = /(?<=^|\W)`(.+)`(?=\W|$)/
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'escape_utils'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
# https://api.slack.com/docs/formatting
|
9
|
+
class ConvertFilter < ::HTML::Pipeline::TextFilter
|
10
|
+
def call
|
11
|
+
html = @text.gsub(/<([^>\|]+)(?:\|([^>]+))?>/) do |match|
|
12
|
+
link_data = $1
|
13
|
+
link_text = $2
|
14
|
+
create_link(link_data, link_text)
|
15
|
+
end
|
16
|
+
Nokogiri::HTML.fragment(html)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def create_link(data, override_text = nil)
|
22
|
+
klass, link, text =
|
23
|
+
case data
|
24
|
+
when /\A#(C.+)\z/ # channel
|
25
|
+
channel = self.context.include?(:on_slack_channel_id) ? self.context[:on_slack_channel_id].call($1) : nil
|
26
|
+
if channel
|
27
|
+
['channel', channel[:url], "##{channel[:text]}"]
|
28
|
+
else
|
29
|
+
['channel', data, data]
|
30
|
+
end
|
31
|
+
when /\A@((?:U|B).+)/ # user or bot
|
32
|
+
user = self.context.include?(:on_slack_user_id) ? self.context[:on_slack_user_id].call($1) : nil
|
33
|
+
if user
|
34
|
+
['mention', user[:url], "@#{user[:text]}"]
|
35
|
+
else
|
36
|
+
['mention', nil, data]
|
37
|
+
end
|
38
|
+
when /\A@(.+)/ # user name
|
39
|
+
user = self.context.include?(:on_slack_user_name) ? self.context[:on_slack_user_name].call($1) : nil
|
40
|
+
if user
|
41
|
+
['mention', user[:url], "@#{user[:text]}"]
|
42
|
+
else
|
43
|
+
['mention', nil, data]
|
44
|
+
end
|
45
|
+
when /\A!/ # special command
|
46
|
+
['link', nil, data]
|
47
|
+
else # normal link
|
48
|
+
['link', data, data]
|
49
|
+
end
|
50
|
+
|
51
|
+
if link
|
52
|
+
escaped_link =
|
53
|
+
if self.context[:cushion_link] && link.match(/\A([A-Za-z0-9]+:)?\/\//)
|
54
|
+
"#{EscapeUtils.escape_html self.context[:cushion_link]}#{EscapeUtils.escape_url link}"
|
55
|
+
else
|
56
|
+
"#{EscapeUtils.escape_html(link)}"
|
57
|
+
end
|
58
|
+
"<a href=\"#{escaped_link}\" class=\"#{EscapeUtils.escape_html(klass)}\">#{EscapeUtils.escape_html(override_text || text)}</a>"
|
59
|
+
else
|
60
|
+
EscapeUtils.escape_html(override_text || text)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
|
5
|
+
module SlackMarkdown
|
6
|
+
module Filters
|
7
|
+
class EmojiFilter < ::HTML::Pipeline::EmojiFilter
|
8
|
+
|
9
|
+
def emoji_url(name)
|
10
|
+
emoji_names.include?(name) ? super : original_emoji_path(name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def emoji_pattern
|
14
|
+
@emoji_pattern ||= /:(#{(emoji_names + original_emoji_names).map { |name| Regexp.escape(name) }.join('|')}):/
|
15
|
+
end
|
16
|
+
|
17
|
+
def emoji_names
|
18
|
+
self.class.superclass.emoji_names
|
19
|
+
end
|
20
|
+
|
21
|
+
def original_emoji_set
|
22
|
+
context[:original_emoji_set] || {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def original_emoji_names
|
26
|
+
original_emoji_set.keys
|
27
|
+
end
|
28
|
+
|
29
|
+
def original_emoji_path(name)
|
30
|
+
original_emoji_set[name]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SlackMarkdown
|
4
|
+
module Filters
|
5
|
+
module IgnorableAncestorTags
|
6
|
+
DEFAULT_IGNORED_ANCESTOR_TAGS = %w(pre code tt).freeze
|
7
|
+
def ignored_ancestor_tags
|
8
|
+
if context[:ignored_ancestor_tags]
|
9
|
+
DEFAULT_IGNORED_ANCESTOR_TAGS | context[:ignored_ancestor_tags]
|
10
|
+
else
|
11
|
+
DEFAULT_IGNORED_ANCESTOR_TAGS
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class ItalicFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
doc.search(".//text()").each do |node|
|
13
|
+
content = node.to_html
|
14
|
+
next if has_ancestor?(node, ignored_ancestor_tags)
|
15
|
+
next unless content.include?('_')
|
16
|
+
html = italic_filter(content)
|
17
|
+
next if html == content
|
18
|
+
node.replace(html)
|
19
|
+
end
|
20
|
+
doc
|
21
|
+
end
|
22
|
+
|
23
|
+
def italic_filter(text)
|
24
|
+
text.gsub(ITALIC_PATTERN) do
|
25
|
+
"<i>#{$1}</i>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ITALIC_PATTERN = /(?<=^|\W)_(.+)_(?=\W|$)/
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class LineBreakFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
doc.search(".//text()").each do |node|
|
13
|
+
content = node.to_html
|
14
|
+
next if has_ancestor?(node, ignored_ancestor_tags)
|
15
|
+
next unless content.include?("\n")
|
16
|
+
html = content.gsub("\n", '<br>')
|
17
|
+
next if html == content
|
18
|
+
node.replace(html)
|
19
|
+
end
|
20
|
+
doc
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class MultipleCodeFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
doc.search(".//text()").each do |node|
|
13
|
+
content = node.to_html
|
14
|
+
next if has_ancestor?(node, ignored_ancestor_tags)
|
15
|
+
next unless content.include?('`')
|
16
|
+
html = multiple_code_filter(content)
|
17
|
+
next if html == content
|
18
|
+
node.replace(html)
|
19
|
+
end
|
20
|
+
doc
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def multiple_code_filter(text)
|
26
|
+
text.gsub(CODE_PATTERN) do
|
27
|
+
"<pre><code>#{$1}</code></pre>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
CODE_PATTERN = /(?<=^)```\n?((?:.|\n)+)```(?=$)/
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class MultipleQuoteFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
doc.search(".//text()").each do |node|
|
13
|
+
content = node.to_html
|
14
|
+
next if has_ancestor?(node, ignored_ancestor_tags)
|
15
|
+
next unless content.include?('>>>')
|
16
|
+
html = quote_filter(content)
|
17
|
+
next if html == content
|
18
|
+
node.replace(html)
|
19
|
+
end
|
20
|
+
doc
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def quote_filter(text)
|
26
|
+
lines = text.split(/^>>>/, 2)
|
27
|
+
if lines.size < 2
|
28
|
+
text
|
29
|
+
else
|
30
|
+
"#{lines.join('<blockquote>')}</blockquote>"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/ignorable_ancestor_tags'
|
5
|
+
|
6
|
+
module SlackMarkdown
|
7
|
+
module Filters
|
8
|
+
class QuoteFilter < ::HTML::Pipeline::Filter
|
9
|
+
include IgnorableAncestorTags
|
10
|
+
|
11
|
+
def call
|
12
|
+
html = doc.to_s.gsub(/^>\s*(.+)(?:\n|$)/) do
|
13
|
+
"<blockquote>#{$1}\n</blockquote>"
|
14
|
+
end
|
15
|
+
doc = Nokogiri::HTML.fragment(html)
|
16
|
+
doc.search('blockquote + blockquote').each do |node|
|
17
|
+
next unless node.previous.name == 'blockquote'
|
18
|
+
html = "<blockquote>#{node.previous.inner_html}#{node.inner_html}</blockquote>"
|
19
|
+
node.previous.remove
|
20
|
+
node.replace(html)
|
21
|
+
end
|
22
|
+
doc
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'html/pipeline'
|
4
|
+
require 'slack_markdown/filters/convert_filter'
|
5
|
+
require 'slack_markdown/filters/multiple_quote_filter'
|
6
|
+
require 'slack_markdown/filters/quote_filter'
|
7
|
+
require 'slack_markdown/filters/multiple_code_filter'
|
8
|
+
require 'slack_markdown/filters/code_filter'
|
9
|
+
require 'slack_markdown/filters/emoji_filter'
|
10
|
+
require 'slack_markdown/filters/bold_filter'
|
11
|
+
require 'slack_markdown/filters/italic_filter'
|
12
|
+
require 'slack_markdown/filters/line_break_filter'
|
13
|
+
|
14
|
+
module SlackMarkdown
|
15
|
+
class Processor
|
16
|
+
def initialize(context = {})
|
17
|
+
@context = context
|
18
|
+
end
|
19
|
+
attr_reader :context
|
20
|
+
|
21
|
+
def filters
|
22
|
+
@filters ||= [
|
23
|
+
SlackMarkdown::Filters::ConvertFilter, # must first run
|
24
|
+
SlackMarkdown::Filters::MultipleQuoteFilter,
|
25
|
+
SlackMarkdown::Filters::QuoteFilter,
|
26
|
+
SlackMarkdown::Filters::MultipleCodeFilter,
|
27
|
+
SlackMarkdown::Filters::CodeFilter,
|
28
|
+
SlackMarkdown::Filters::EmojiFilter,
|
29
|
+
SlackMarkdown::Filters::BoldFilter,
|
30
|
+
SlackMarkdown::Filters::ItalicFilter,
|
31
|
+
SlackMarkdown::Filters::LineBreakFilter,
|
32
|
+
]
|
33
|
+
end
|
34
|
+
|
35
|
+
def call(src_text, context = {}, result = nil)
|
36
|
+
HTML::Pipeline.new(self.filters, self.context).call(src_text, context, result)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slack_markdown/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'slack_markdown'
|
8
|
+
spec.version = SlackMarkdown::VERSION
|
9
|
+
spec.authors = ['Ru/MuckRu']
|
10
|
+
spec.email = ['ru_shalm@hazimu.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Convert Slack message markdown to HTML.}
|
13
|
+
spec.description = %q{Convert Slack message markdown to HTML.}
|
14
|
+
spec.homepage = 'https://github.com/rutan/slack_markdown'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'html-pipeline', '~> 1.11.0'
|
23
|
+
spec.add_dependency 'escape_utils', '~> 1.0.1'
|
24
|
+
spec.add_dependency 'gemoji', '~> 2.1.0'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slack_markdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ru/MuckRu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-22 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: 1.11.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.11.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: escape_utils
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: gemoji
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.1.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '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'
|
111
|
+
description: Convert Slack message markdown to HTML.
|
112
|
+
email:
|
113
|
+
- ru_shalm@hazimu.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- lib/slack_markdown.rb
|
124
|
+
- lib/slack_markdown/filters/bold_filter.rb
|
125
|
+
- lib/slack_markdown/filters/code_filter.rb
|
126
|
+
- lib/slack_markdown/filters/convert_filter.rb
|
127
|
+
- lib/slack_markdown/filters/emoji_filter.rb
|
128
|
+
- lib/slack_markdown/filters/ignorable_ancestor_tags.rb
|
129
|
+
- lib/slack_markdown/filters/italic_filter.rb
|
130
|
+
- lib/slack_markdown/filters/line_break_filter.rb
|
131
|
+
- lib/slack_markdown/filters/multiple_code_filter.rb
|
132
|
+
- lib/slack_markdown/filters/multiple_quote_filter.rb
|
133
|
+
- lib/slack_markdown/filters/quote_filter.rb
|
134
|
+
- lib/slack_markdown/processor.rb
|
135
|
+
- lib/slack_markdown/version.rb
|
136
|
+
- slack_markdown.gemspec
|
137
|
+
homepage: https://github.com/rutan/slack_markdown
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.4.5
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: Convert Slack message markdown to HTML.
|
161
|
+
test_files: []
|