Class: SimpleFormat::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_format.rb

Instance Method Summary collapse

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

链接转换



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, options = {})
  options = { elements: sanitized_allowed_tags, attributes: { all: sanitized_allowed_attributes } } if options.empty?
  Sanitize.clean(html, options)
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