Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- pantograph_core/lib/pantograph_core/core_ext/shellwords.rb,
pantograph/lib/pantograph/action.rb,
pantograph_core/lib/pantograph_core/ui/interface.rb,
pantograph_core/lib/pantograph_core/string_filters.rb,
pantograph_core/lib/pantograph_core/core_ext/string.rb,
pantograph_core/lib/pantograph_core/ui/disable_colors.rb
Overview
Here be monkey patches
Helpers collapse
Instance Method Summary collapse
- #dedent! ⇒ Object
- #markdown_clean_heredoc! ⇒ Object
- #markdown_details(is_first) ⇒ Object
- #markdown_list(is_first = false) ⇒ Object
- #markdown_preserve_newlines ⇒ Object
- #markdown_sample(is_first = false) ⇒ Object
-
#middle_truncate(length = 20, options = {}) ⇒ Object
Base taken from: stackoverflow.com/a/12202205/1945875.
- #pantograph_class ⇒ Object
-
#pantograph_underscore ⇒ Object
def pantograph_module self == “pem” ? ‘PEM’ : self.pantograph_class end.
- #remove_markdown ⇒ Object
-
#shellescape ⇒ Object
CrossplatformShellwords.
-
#truncate(truncate_at, options = {}) ⇒ Object
Truncates a given
text
after a givenlength
iftext
is longer thanlength
:. -
#wordwrap(length = 80) ⇒ Object
Base taken from: www.ruby-forum.com/topic/57805.
Instance Method Details
#dedent! ⇒ Object
181 182 183 184 185 |
# File 'pantograph/lib/pantograph/action.rb', line 181 def dedent! first_line_indent = self.match(/^\s*/)[0] self.gsub!(/^#{first_line_indent}/, "") end |
#deprecated ⇒ Object
202 203 204 |
# File 'pantograph_core/lib/pantograph_core/ui/interface.rb', line 202 def deprecated self.bold.blue end |
#markdown_clean_heredoc! ⇒ Object
176 177 178 179 |
# 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) ⇒ Object
170 171 172 173 174 |
# 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) ⇒ Object
163 164 165 166 167 168 |
# 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 ⇒ Object
154 155 156 |
# 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) ⇒ Object
158 159 160 161 |
# 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 = {}) ⇒ Object
Base taken from: stackoverflow.com/a/12202205/1945875
43 44 45 46 47 48 49 50 |
# File 'pantograph_core/lib/pantograph_core/string_filters.rb', line 43 def middle_truncate(length = 20, = {}) omission = [: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 ⇒ Object
2 3 4 |
# File 'pantograph_core/lib/pantograph_core/core_ext/string.rb', line 2 def pantograph_class split('_').collect!(&:capitalize).join end |
#pantograph_underscore ⇒ Object
def pantograph_module
self == "pem" ? 'PEM' : self.pantograph_class
end
10 11 12 13 14 15 16 |
# 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 ⇒ Object
187 188 189 190 191 192 193 |
# 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 ⇒ Object
CrossplatformShellwords
8 9 10 |
# File 'pantograph_core/lib/pantograph_core/core_ext/shellwords.rb', line 8 def shellescape CrossplatformShellwords.shellescape(self) end |
#truncate(truncate_at, options = {}) ⇒ Object
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)"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'pantograph_core/lib/pantograph_core/string_filters.rb', line 20 def truncate(truncate_at, = {}) return dup unless length > truncate_at omission = [:omission] || '...' length_with_room_for_omission = truncate_at - omission.length stop = \ if [:separator] rindex([: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) ⇒ Object
Base taken from: www.ruby-forum.com/topic/57805
36 37 38 39 40 |
# 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 |