class StringMaster
Attributes
Public Class Methods
# 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
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
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
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 < @modified_string.gsub!(/<+([^>]*)\Z/, '<\1') # Convert all unopened right tag brackets (>) into > @modified_string.gsub!(/\A([^<]*)>+/, '\1>') self end
# File lib/string_master/string_master.rb, line 109 def newlines_to_br @modified_string.gsub!("\n", "<br/>") self end
Preserves whitespace within a given tag. Each occurence of 2 or more spaces is transformed into a 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| " "*m.length } end self end
# File lib/string_master/string_master.rb, line 182 def to_s modified_string.html_safe end
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
Creates <a> tags for all urls. IMPORTANT: make sure you've used urls_to_images
method first if you wanted all images urls to become <img> tags.
# File lib/string_master/string_master.rb, line 67 def urls_to_links(options = {}) wrap_with = options[:wrap_with] || ['',''] html_options = options[:html_options] || '' # ascii_only option - show URLs with warning about contain non-latin characters if options[:ascii_only] validate_url_contains_ascii(@modified_string, html_options, wrap_with) else @modified_string.gsub!( /(\s|^|\A|\n|\t|\r)((http|https):\/\/.*?)([,.])?(\s|$|\n|\Z|\t|\r|<)/, '\1<a href="\2" ' + html_options + '>\2</a>\4\5' ) end self end
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
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 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
# 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
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
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