Module: Tiqbi::Utils

Extended by:
Utils
Included in:
Tiqbi, Utils, View::Base, View::DetailView, ViewController
Defined in:
lib/tiqbi/utils.rb

Constant Summary collapse

ENTITY_MAP =
{
  "&lt;" => "<",
  "&gt;" => ">",
}

Instance Method Summary collapse

Instance Method Details

#format_str(str, length, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/tiqbi/utils.rb', line 12

def format_str(str, length, &block)
  clean_strs = split_str_with_width(unescape_entity(Sanitize.clean(str)), length).map { |s|
    s.split(/\n|\r\n/)
  }.flatten
  clean_strs.each(&:chomp!)
  return clean_strs[0] unless block_given?
  clean_strs.each do |s|
    yield s
  end
end

#split_str_with_width(str, width = 40) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tiqbi/utils.rb', line 23

def split_str_with_width(str, width = 40)
  s = i = r = 0
  str.each_char.reduce([]) { |res, c|
   i += c.ascii_only? ? 1 : 2
   r += 1
   next res if i < width

   res << str[s,r]
   s += r
   i = r = 0
   res
  } << str[s,r]
end

#unescape_entity(str) ⇒ Object



37
38
39
# File 'lib/tiqbi/utils.rb', line 37

def unescape_entity(str)
  str.gsub(/#{Regexp.union(ENTITY_MAP.keys)}/o) {|key| ENTITY_MAP[key] }
end