class StringMaster

Attributes

modified_string[R]
string[R]

Public Class Methods

new(string) { |self| ... } click to toggle source
# File lib/string_master/string_master.rb, line 12
def initialize(string)
  @initial_string  = String.new(string.html_safe)
  @modified_string = String.new(string.html_safe)
  yield(self) if block_given?
end

Public Instance Methods

break_long_words(length=75, break_char=" ") { |str| ... } click to toggle source

Breaks words that are longer than 'length'

# File lib/string_master/string_master.rb, line 84
def break_long_words(length=75, break_char=" ", &block)
  @modified_string.gsub!(/https?:\/\/\S+|<a [^>]+>|<img [^>]+>|([^\s^\n^\^^\A^\t^\r<]{#{length},}?)|<\/a>/) do |s|
    if $1
      ns = block_given? ? yield($1) : $1
      last_pos, result_string = 0, ''
        while string = ns[last_pos..(last_pos + length)]
        result_string += string + break_char
        last_pos += (length + 1)
      end
      result_string
    else
      s
    end
  end
  self
end
close_tags() click to toggle source

Closes all unclosed tags that need to be closed (i.e. skips <img>,
etc.)

# File lib/string_master/string_master.rb, line 19
def close_tags
  text = @modified_string

  open_tags = []
  text.scan(/<([a-zA-Z0-9]+?)(\s[^>]*)?>/).each { |t| open_tags.unshift(t[0]) }
  text.scan(/<\/\s*?([a-zA-Z0-9]+)\s*?>/).each  { |t| open_tags.slice!(open_tags.index(t[0])) unless open_tags.index(t[0]).nil? }
  open_tags.each { |t| text += "</#{t}>" unless %w(img br hr).include?(t.to_s) }

  @modified_string = text
  return self
end
cut(length, cut_at, options = {}) click to toggle source

Cuts a string starting at 'cut_at' if the string is longer than 'length'. Appends characters (if specified in :append) to the end of the cut string.

# File lib/string_master/string_master.rb, line 103
def cut(length, cut_at, options = {})
  append = options[:append] || ''
  @modified_string.size > (length) ? @modified_string = @modified_string.mb_chars[0...cut_at] + append : @modified_message
  self
end
html_escape(options={}) click to toggle source

escapes all tags except the ones, that are listed in :except option

# File lib/string_master/string_master.rb, line 33
def html_escape(options={})
  except = options[:except] || %w()
  close_tags
  @modified_string.gsub!(/<\/?(.*?)(\s.*?)?\/?>/) do |tag|
    if except.include?($1)
      # sanitize attributes
      tag.gsub(/\s(.+?)=('|").*?\2(?=.*?>)/) do |a|
        ["href", "src", "lang"].include?($1) ? a : ""
      end
    else
      h(tag)
    end
  end
  # Convert all unclosed left tag brackets (<) into &lt;
  @modified_string.gsub!(/<+([^>]*)\Z/, '&lt;\1')
  # Convert all unopened right tag brackets (>) into &gt;
  @modified_string.gsub!(/\A([^<]*)>+/, '\1&gt;')
  self
end
newlines_to_br() click to toggle source
# File lib/string_master/string_master.rb, line 109
def newlines_to_br
  @modified_string.gsub!("\n", "<br/>")
  self
end
preserve_whitespace_within(tag) click to toggle source

Preserves whitespace within a given tag. Each occurence of 2 or more spaces is transformed into a &nbsp; entities.

# File lib/string_master/string_master.rb, line 164
def preserve_whitespace_within(tag)
  @modified_string.gsub!(/<#{tag}>(.|\n)+?<\/#{tag}>/) do |match|
    match.gsub(/( )( )+/) { |m| "&nbsp;"*m.length }
  end
  self
end
to_s() click to toggle source
# File lib/string_master/string_master.rb, line 182
def to_s
  modified_string.html_safe
end
urls_to_images(options = {}) click to toggle source

Creates <img> tags for all urls that look like images.

# File lib/string_master/string_master.rb, line 54
def urls_to_images(options = {})
  wrap_with    = options[:wrap_with]    || ['','']
  html_options = options[:html_options] || ''
  @modified_string.gsub!(
    /(\s|^|\A|\n|\t|\r)((http|https):\/\/.*?\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF))([,.])?(\s|$|\n|\Z|\t|\r)/,
    "#{wrap_with[0]}<img src=\"\\2\" alt=\"\" #{html_options}/>\\6#{wrap_with[1]}"
  )
  self
end
wrap_backticks_code(opening_tag="<code>", closing_tag="</code>") click to toggle source

Same as wrap_inline_code, but spans across multiplelines

# File lib/string_master/string_master.rb, line 177
def wrap_backticks_code(opening_tag="<code>", closing_tag="</code>")
  @modified_string.gsub!(/`(.+?)`/m, opening_tag + '\1' + closing_tag)
  self
end
wrap_code() click to toggle source

Finds all lines that start with 4 spaces and wraps them into <code> tags. It also transforms each occurence of 2 or more spaces into an &nbsp; entity, which is available as a standalone method preserve_whitespace

# File lib/string_master/string_master.rb, line 157
def wrap_code
  wrap_lines("code", /\A\s{4}/, remove_newlines: true)
  preserve_whitespace_within("code") # already returns `self`
end
wrap_inline_code(opening_tag="<span class=\"inlineCode\">", closing_tag="</span>") click to toggle source
# File lib/string_master/string_master.rb, line 171
def wrap_inline_code(opening_tag="<span class=\"inlineCode\">", closing_tag="</span>")
  @modified_string.gsub!(/`(.+?)`/m, opening_tag + '\1' + closing_tag)
  self
end
wrap_lines(tag, regexp, options={remove_newlines: false}) click to toggle source

Finds lines of text that satisfy a 'regexp' and wraps them into an opening and closing 'tag'. Best example of usage is wrap_code.

Also receives an option :remove_newlines (default is false) which removes n around the tag that wraps the lines of text. Useful if you're using this method with newlines_to_br to avoid extra space that may or may not be in the user's input.

# File lib/string_master/string_master.rb, line 121
def wrap_lines(tag, regexp, options={remove_newlines: false})
  code_tag = nil; result = ""
  @modified_string.each_line do |line|
    if line =~ regexp
      unless code_tag == :opened
        result.chomp! if options[:remove_newlines]
        result += "<#{tag}>"
        code_tag = :opened
      end
      result += line.sub(regexp, '')
    else
      if code_tag == :opened
        code_tag = :closed
        result.chomp!
        result += "</#{tag}>"
      elsif code_tag == :closed
        code_tag = nil
        result.chomp! if options[:remove_newlines]
      end
      result += line
    end
  end

  # Make sure there's always a closing tag
  if code_tag == :opened
    result.chomp!
    result += "</#{tag}>\n"
  end

  @modified_string = result
  self
end

Private Instance Methods

validate_url_contains_ascii(string, html_options, wrap_with) click to toggle source

Checking references for the presence of non-latin characters. If such characters are found, the link will not be displayed like html link. Instead of it, will be returned a warning message.

# File lib/string_master/string_master.rb, line 191
def validate_url_contains_ascii(string, html_options, wrap_with)
  string.gsub!(/((http|https)[^<\s,]{7,}\b)/im) do |link|
    if link.ascii_only?
      "<a href='#{link}'#{html_options}>#{link}</a>"
    else
      "[WARNING, URL CONTAINS NON-LATIN LETTERS: #{link}]"
    end
  end
end