Class: SimpleFormat::Converter
- Inherits:
-
Object
- Object
- SimpleFormat::Converter
- Defined in:
- lib/simple_format.rb
Instance Method Summary collapse
-
#auto_link(string) ⇒ Object
链接转换.
-
#emoji_with(string) ⇒ Object
表情符 转 <img>.
- #html_with(html, options = {}) ⇒ Object
-
#initialize(emoji = nil) ⇒ Converter
constructor
A new instance of Converter.
-
#nl2br(string) ⇒ Object
回车 转 <br />.
-
#rich_with(string) ⇒ Object
转 富文本.
-
#text_with(string) ⇒ Object
清除 html.
Constructor Details
#initialize(emoji = nil) ⇒ Converter
Returns a new instance of Converter.
9 10 11 |
# File 'lib/simple_format.rb', line 9 def initialize(emoji=nil) @emoji = emoji if emoji.is_a?(SimpleFormat::Emoji) end |
Instance Method Details
#auto_link(string) ⇒ Object
链接转换
41 42 43 44 |
# File 'lib/simple_format.rb', line 41 def auto_link(string) # 链接转换 @auto_link ||= AutoLink.new @auto_link.all(string) end |
#emoji_with(string) ⇒ Object
表情符 转 <img>
22 23 24 |
# File 'lib/simple_format.rb', line 22 def emoji_with(string) # 表情符 转 <img> emoji.replace_emoji_with_images(string) end |
#html_with(html, options = {}) ⇒ Object
26 27 28 29 |
# File 'lib/simple_format.rb', line 26 def html_with(html, = {}) = { elements: , attributes: { all: sanitized_allowed_attributes } } if .empty? Sanitize.clean(html, ) end |
#nl2br(string) ⇒ Object
回车 转 <br />
35 36 37 38 39 |
# File 'lib/simple_format.rb', line 35 def nl2br(string) # 回车 转 <br /> if string string.gsub("\n\r","<br />").gsub("\r", "").gsub("\n", "<br />") end end |
#rich_with(string) ⇒ Object
转 富文本
13 14 15 16 17 18 19 20 |
# File 'lib/simple_format.rb', line 13 def rich_with(string) # 转 富文本 return string unless string string = emoji_with(string) string = nl2br(string).to_str string = auto_link(string) string = html_with(string) return string end |
#text_with(string) ⇒ Object
清除 html
31 32 33 |
# File 'lib/simple_format.rb', line 31 def text_with(string) # 清除 html Sanitize.clean(string, {elements: [], attributes: { all: [] }}) end |