Class: ZfbenExtend::String

Inherits:
String
  • Object
show all
Defined in:
lib/zfben_extend/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.markdown(text, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zfben_extend/string.rb', line 85

def self.markdown text, options = {}
  options = {
    autolink: true, 
    space_after_headers: true,
    fenced_code_blocks: true,
    no_intra_emphasis: true,
    hard_wrap: true,
    strikethrough: true
  }.merge(options)
  markdown = Redcarpet::Markdown.new(ZfbenExtend::HTMLwithCodeRay, options)
  self.new markdown.render(text).html_safe
end

.to_cnbr(text, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/zfben_extend/string.rb', line 72

def self.to_cnbr text, options = {}
  options = {
    chars: /(。|!|?)/,
    br: '<br />'
  }.merge(options)

  self.new text.gsub(options[:chars]){ |char| char << options[:br] }.html_safe
end

.to_html(text, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zfben_extend/string.rb', line 7

def self.to_html text, options={}
  options = {
    link: true,
    link_regexp: /[a-zA-Z]{3,}:\/\/\S+/,
    tag: true,
    tag_regexp: /&?#[a-zA-Z0-9]+/,
    tag_url: '/tags/',
    mail: true,
    mail_regexp: /[a-zA-Z0-9.-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9.]+/,
    user: true,
    user_regexp: /@[a-zA-Z0-9]+/,
    user_url: '/users/'
  }.merge(options)
  
  data = {}
  
  if options[:link]
    data[:link] = []
    text = text.gsub(options[:link_regexp]){ |link|
      data[:link].push('<a href="' + link + '">' + link + '</a>')
      'ZFBENLINK' + (data[:link].length - 1).to_s + 'ZFBENLINK'
    }
  end
  
  if options[:tag]
    data[:tag] = []
    text = text.gsub(options[:tag_regexp]){ |tag|
      if tag[0] != '&'
        data[:tag].push('<a href="' + options[:tag_url] + tag.gsub('#', '') + '">' + tag + '</a>')
        'ZFBENTAG' + (data[:tag].length - 1).to_s + 'ZFBENTAG'
      else
        tag
      end
    }
  end
  
  if options[:mail]
    data[:mail] = []
    text = text.gsub(options[:mail_regexp]){ |mail|
      data[:mail].push('<a href="mailto:' + mail + '">' + mail + '</a>')
      'ZFBENMAIL' + (data[:mail].length - 1).to_s + 'ZFBENMAIL'
    }
  end
  
  if options[:user]
    data[:user] = []
    text = text.gsub(options[:user_regexp]){ |user|
      data[:user].push('<a href="' + options[:user_url] + user.gsub('@', '') + '">' + user + '</a>')
      'ZFBENUSER' + (data[:user].length - 1).to_s + 'ZFBENUSER'
    }
  end
  
  [:link, :tag, :mail, :user].each do |name|
    if options[name]
      text = text.gsub(Regexp.new('ZFBEN' + name.to_s.upcase + '[0-9]+ZFBEN' + name.to_s.upcase)){ |t| data[name][t.match(/[0-9]+/)[0].to_i] }
    end
  end
  
  self.new text.html_safe
end

Instance Method Details

#markdown(options = {}) ⇒ Object



98
99
100
# File 'lib/zfben_extend/string.rb', line 98

def markdown options = {}
  self.class.markdown self, options
end

#to_cnbr(options = {}) ⇒ Object



81
82
83
# File 'lib/zfben_extend/string.rb', line 81

def to_cnbr options = {}
  self.class.to_cnbr self, options
end

#to_html(options = {}) ⇒ Object



68
69
70
# File 'lib/zfben_extend/string.rb', line 68

def to_html options = {}
  self.class.to_html self, options
end