slack_mrkdwn 1.1.0 → 2.0.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/slack_mrkdwn.rb +130 -19
  3. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6fb434ec4e2a6b8b07bd993d085d084384d789f0d4d70d9565cf645bf7105fe5
4
- data.tar.gz: 1a0d9b3fd9cf9d77e9ece675ad831f539bef09f2c9295959a5a39c1159d19287
3
+ metadata.gz: b38208ce080dffeea4bbdacfcc5b455b78b91d872391cb96a0127d52feb98be7
4
+ data.tar.gz: aea90c46698e27b6b5d2290352773bd60b6e5446cdfe3932babdab647038c027
5
5
  SHA512:
6
- metadata.gz: 3343f8f4ae7d41bfcb657dde186a61a3483ca8133b94592305b061c94cc5eb800cf59264a62fd7706f502dcc93a1e26eac8ec53a272dbe9a4c8d29f71e404c38
7
- data.tar.gz: 31a7cc9df0fae701736c6acbfeef1da3beaf20162a2a3d89d860e6c538204a3f31ff50cf9b5c1ef677b562561dc7a39b97d0110a80c0033510990529da9c0f62
6
+ metadata.gz: 8ce667a87b45df0569e24ee3af209ceae51b2ef7108d95ed211c521ed6fa1e6a836f10bc0bcbd1c54474f68363665f14a50a824bdde6807a188c5a6139db3523
7
+ data.tar.gz: b2f82329e82c219743f6cdf418eb0ed00310988424e0b5b10b1014044c124abb8289c9f26b6ad84ab8e9e5a1d656b9a48e7f094bd64a501fcea0880ce2303460
data/lib/slack_mrkdwn.rb CHANGED
@@ -1,19 +1,130 @@
1
- module SlackMrkdwn
2
- def self.from(markdown)
3
- markdown
4
- .gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;') # Encode Slack restricted characters
5
- .gsub(/\*(.*?)\*/, '_\1_') # Emphasis
6
- .gsub(/\*{2}(.+?)\*{2}/, '*\1*').gsub(/_{2}(.+?)_{2}/, '*\1*') # Strong emphasis
7
- .gsub(/~{2}(.+?)~{2}/, '~\1~') # Strike-through
8
- .gsub(/^\#{1,6} (.+?)$/, '*\1*').gsub(/(.*?)\n^=+$/, '*\1*').gsub(/(.*?)\n^-+$/, '*\1*') # Headings
9
- .gsub(/`{3}\w*(.*?)`{3}/m, '```\1```') # Codeblock
10
- .gsub(/^ +/, '\t') # Leading spaces
11
- .gsub(/^((?:\\t)*?)[\*\+-] (.+)/, '\1- \2') # Unordered lists
12
- .gsub(/\!\[.*?\]\((.+?)(?: .*?)?\)/, '\1') # Image
13
- .gsub(/\[(.+?)\]\((.+?)(?: .*?)?\)/, '<\2|\1>') # Classic link
14
- .gsub(/\[(.+?)\]\[(.+?)\]/) { |m| markdown.match?(/\[#{$2}\]: (.+)/i) ? ("|#{$1}>".prepend(/\[#{$2}\]: (.+)/i.match(markdown) { |n| "<#{$1}" })) : "[#{$1}][#{$2}]" } # Reference-style links
15
- .gsub(/\[(.+?)\]/) { |m| markdown.match?(/\[#{$1}\]: (.+)/i) ? ("|#{$1}>".prepend(/\[#{$1}\]: (.+)/i.match(markdown) { |n| "<#{$1}" })) : "[#{$1}]" } # Reference-style - Text only links
16
- .gsub(/\^<.+?|.+?>: .+/, '') # Remove link references
17
- .strip
18
- end
19
- end
1
+ # frozen_string_literal: true
2
+ require 'redcarpet'
3
+
4
+ class SlackMrkdwn < Redcarpet::Render::Base
5
+ # Methods where the first argument is the text content
6
+ [
7
+ # block-level calls
8
+ :block_html,
9
+
10
+ :autolink,
11
+ :raw_html,
12
+
13
+ :table, :table_row, :table_cell,
14
+
15
+ :superscript, :highlight,
16
+
17
+ # footnotes
18
+ :footnotes, :footnote_def, :footnote_ref,
19
+
20
+ :hrule,
21
+
22
+ # low level rendering
23
+ :entity, :normal_text,
24
+
25
+ :doc_header, :doc_footer,
26
+ ].each do |method|
27
+ define_method method do |*args|
28
+ args.first
29
+ end
30
+ end
31
+
32
+ # Encode Slack restricted characters
33
+ def preprocess(content)
34
+ content.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
35
+ end
36
+
37
+ def postprocess(content)
38
+ content.rstrip
39
+ end
40
+
41
+ # ~~strikethrough~~
42
+ def strikethrough(content)
43
+ "~#{content}~"
44
+ end
45
+
46
+ # _italic_
47
+ def underline(content)
48
+ "_#{content}_"
49
+ end
50
+
51
+ # *italic*
52
+ def emphasis(content)
53
+ "_#{content}_"
54
+ end
55
+
56
+ # **bold**
57
+ def double_emphasis(content)
58
+ "*#{content}*"
59
+ end
60
+
61
+ # ***bold and italic***
62
+ def triple_emphasis(content)
63
+ "*_#{content}_*"
64
+ end
65
+
66
+ # ``` code block ```
67
+ def block_code(content, _language)
68
+ "```\n#{content}```\n"
69
+ end
70
+
71
+ # > quote
72
+ def block_quote(content)
73
+ "&gt; #{content}"
74
+ end
75
+
76
+ # `code`
77
+ def codespan(content)
78
+ "`#{content}`"
79
+ end
80
+
81
+ # links
82
+ def link(link, _title, content)
83
+ "<#{link}|#{content}>"
84
+ end
85
+
86
+ # lists
87
+ def list(entries, list_type)
88
+ reset_list(list_type)
89
+ entries
90
+ end
91
+
92
+ def reset_list(list_type)
93
+ @list_item_count = 0 if list_type == :ordered
94
+ end
95
+
96
+ def list_prefix(list_type)
97
+ return "-" if list_type == :unordered
98
+ @list_item_count = 0 unless @list_item_count
99
+ @list_item_count += 1
100
+ "#{@list_item_count}."
101
+ end
102
+
103
+ def list_item(entry, list_type)
104
+ "#{list_prefix(list_type)} #{entry}"
105
+ end
106
+
107
+ # ![](image)
108
+ def image(link, _title, _alt_text)
109
+ link
110
+ end
111
+
112
+ def linebreak()
113
+ "\n"
114
+ end
115
+
116
+ def paragraph(text)
117
+ "#{text}\n"
118
+ end
119
+
120
+ def header(text, _header_level)
121
+ "*#{text}*\n"
122
+ end
123
+
124
+ class << self
125
+ def from(markdown)
126
+ renderer = SlackMrkdwn.new
127
+ Redcarpet::Markdown.new(renderer, strikethrough: true, underline: true, fenced_code_blocks: true).render(markdown)
128
+ end
129
+ end
130
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack_mrkdwn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Cousineau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-12 00:00:00.000000000 Z
11
+ date: 2019-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redcarpet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -71,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
85
  requirements:
72
86
  - - ">="
73
87
  - !ruby/object:Gem::Version
74
- version: 2.4.6
88
+ version: 1.9.2
75
89
  required_rubygems_version: !ruby/object:Gem::Requirement
76
90
  requirements:
77
91
  - - ">="