class String
Here be monkey patches
Public Instance Methods
dedent!()
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 181 def dedent! first_line_indent = self.match(/^\s*/)[0] self.gsub!(/^#{first_line_indent}/, "") end
deprecated()
click to toggle source
# File pantograph_core/lib/pantograph_core/ui/interface.rb, line 202 def deprecated self.bold.blue end
markdown_clean_heredoc!()
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 176 def markdown_clean_heredoc! self.chomp! # remove the last new line added by the heredoc self.dedent! # remove the leading whitespace (similar to the squigly heredoc `<<~`) end
markdown_details(is_first)
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 170 def markdown_details(is_first) self.prepend("\n") unless is_first self << "\n>" # continue the quote self.markdown_preserve_newlines end
markdown_list(is_first = false)
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 163 def markdown_list(is_first = false) self.markdown_clean_heredoc! self.gsub!(/^/, "- ") # add list dashes self.prepend(">") unless is_first # the empty line that will be added breaks the quote self.markdown_details(is_first) end
markdown_preserve_newlines()
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 154 def markdown_preserve_newlines self.gsub(/(\n|$)/, '|\1') # prepend new lines with "|" so the erb template knows *not* to replace them with "<br>"s end
markdown_sample(is_first = false)
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 158 def markdown_sample(is_first = false) self.markdown_clean_heredoc! self.markdown_details(is_first) end
middle_truncate(length = 20, options = {})
click to toggle source
Base taken from: stackoverflow.com/a/12202205/1945875
# File pantograph_core/lib/pantograph_core/string_filters.rb, line 43 def middle_truncate(length = 20, options = {}) omission = options[:omission] || '...' return self if self.length <= length + omission.length return self[0..length] if length < omission.length len = (length - omission.length) / 2 s_len = len - length % 2 self[0..s_len] + omission + self[self.length - len..self.length] end
pantograph_class()
click to toggle source
# File pantograph_core/lib/pantograph_core/core_ext/string.rb, line 2 def pantograph_class split('_').collect!(&:capitalize).join end
pantograph_underscore()
click to toggle source
def pantograph_module
self == "pem" ? 'PEM' : self.pantograph_class
end
# File pantograph_core/lib/pantograph_core/core_ext/string.rb, line 10 def pantograph_underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). tr("-", "_"). downcase end
remove_markdown()
click to toggle source
# File pantograph/lib/pantograph/action.rb, line 187 def remove_markdown string = self.gsub(/^>/, "") # remove Markdown quotes string = string.gsub(/\[http[^\]]+\]\(([^)]+)\)/, '\1 🔗') # remove Markdown links string = string.gsub(/\[([^\]]+)\]\(([^\)]+)\)/, '"\1" (\2 🔗)') # remove Markdown links with custom text string = string.gsub("|", "") # remove new line preserve markers return string end
shellescape()
click to toggle source
# File pantograph_core/lib/pantograph_core/core_ext/shellwords.rb, line 8 def shellescape CrossplatformShellwords.shellescape(self) end
truncate(truncate_at, options = {})
click to toggle source
Truncates a given text
after a given length
if text
is longer than length
:
'Once upon a time in a world far far away'.truncate(27) # => "Once upon a time in a wo..."
Pass a string or regexp :separator
to truncate text
at a natural break:
'Once upon a time in a world far far away'.truncate(27, separator: ' ') # => "Once upon a time in a..." 'Once upon a time in a world far far away'.truncate(27, separator: /\s/) # => "Once upon a time in a..."
The last characters will be replaced with the :omission
string (defaults to “…”) for a total length not exceeding length
:
'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)') # => "And they f... (continued)"
# File pantograph_core/lib/pantograph_core/string_filters.rb, line 20 def truncate(truncate_at, options = {}) return dup unless length > truncate_at omission = options[:omission] || '...' length_with_room_for_omission = truncate_at - omission.length stop = \ if options[:separator] rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission else length_with_room_for_omission end "#{self[0, stop]}#{omission}" end
wordwrap(length = 80)
click to toggle source
Base taken from: www.ruby-forum.com/topic/57805
# File pantograph_core/lib/pantograph_core/string_filters.rb, line 36 def wordwrap(length = 80) return [] if length == 0 self.gsub!(/(\S{#{length}})(?=\S)/, '\1 ') self.scan(/.{1,#{length}}(?:\s+|$)/) end