Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/bashly/extensions/string.rb
Instance Method Summary collapse
- #color(marker) ⇒ Object
- #expand_tabs(tabstop = 2) ⇒ Object
- #for_manpage ⇒ Object
- #for_markdown ⇒ Object
- #indent(offset) ⇒ Object
- #lint ⇒ Object
- #nl2br ⇒ Object
- #remove_front_matter ⇒ Object
- #sanitize_for_print ⇒ Object
- #to_hyphen ⇒ Object
- #to_path ⇒ Object
- #to_underscore ⇒ Object
- #wrap(length = 80) ⇒ Object
Instance Method Details
#color(marker) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/bashly/extensions/string.rb', line 61 def color(marker) color = Bashly::Settings.usage_colors[marker.to_s] return self unless color text, spaces = match(/(.*?)(\s*)$/).captures %[$(#{color} "#{text}")#{spaces}] end |
#expand_tabs(tabstop = 2) ⇒ Object
55 56 57 58 59 |
# File 'lib/bashly/extensions/string.rb', line 55 def (tabstop = 2) gsub(/^( {#{tabstop}}+)/) do "\t" * (::Regexp.last_match(1).size / tabstop) end end |
#for_manpage ⇒ Object
10 11 12 |
# File 'lib/bashly/extensions/string.rb', line 10 def for_manpage gsub('<', '\\<').gsub('>', '\\>').gsub('`', '**').gsub(" \n", "\n\n") end |
#for_markdown ⇒ Object
6 7 8 |
# File 'lib/bashly/extensions/string.rb', line 6 def for_markdown gsub('<', '\\<').gsub('>', '\\>').nl2br end |
#indent(offset) ⇒ Object
18 19 20 21 22 |
# File 'lib/bashly/extensions/string.rb', line 18 def indent(offset) return self unless offset.positive? lines.indent(offset).join end |
#lint ⇒ Object
47 48 49 |
# File 'lib/bashly/extensions/string.rb', line 47 def lint gsub(/\s+\n/m, "\n\n").lines.grep_v(/^\s*##/).join end |
#nl2br ⇒ Object
14 15 16 |
# File 'lib/bashly/extensions/string.rb', line 14 def nl2br gsub("\n", " \n") end |
#remove_front_matter ⇒ Object
51 52 53 |
# File 'lib/bashly/extensions/string.rb', line 51 def remove_front_matter split(/^---\s*/).last end |
#sanitize_for_print ⇒ Object
2 3 4 |
# File 'lib/bashly/extensions/string.rb', line 2 def sanitize_for_print gsub("\n", '\\n').gsub('"', '\"').gsub('`', '\\\\`').gsub('%', '%%') end |
#to_hyphen ⇒ Object
28 29 30 |
# File 'lib/bashly/extensions/string.rb', line 28 def to_hyphen tr(' ', '-').gsub(/([a-z])([A-Z])/, '\1-\2').downcase end |
#to_path ⇒ Object
32 33 34 |
# File 'lib/bashly/extensions/string.rb', line 32 def to_path tr(' ', '/').downcase end |
#to_underscore ⇒ Object
24 25 26 |
# File 'lib/bashly/extensions/string.rb', line 24 def to_underscore gsub(/(.)([A-Z])/, '\1_\2').gsub(/[- ]/, '_').downcase end |
#wrap(length = 80) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bashly/extensions/string.rb', line 36 def wrap(length = 80) strip! split("\n").collect! do |line| if line.length > length line.gsub(/(.{1,#{length}})(\s+|$)/, "\\1\n").rstrip else line end end * "\n" end |