Class: String
- Includes:
- MaRuKu::Strings
- Defined in:
- lib/amp/dependencies/maruku/input/type_detection.rb,
lib/amp/dependencies/zip/stdrubyext.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/dependencies/maruku/attributes.rb,
lib/amp/dependencies/maruku/output/to_ansi.rb,
lib/amp/dependencies/maruku/output/to_ansi.rb,
lib/amp/dependencies/maruku/output/to_html.rb,
lib/amp/dependencies/maruku/output/to_markdown.rb,
lib/amp/dependencies/maruku/structures_inspect.rb,
lib/amp/dependencies/amp_support/ruby_amp_support.rb,
lib/amp/dependencies/maruku/input_textile2/t2_parser.rb,
lib/amp/support/support.rb
Overview
–
Copyright (C) 2006 Andrea Censi <andrea (at) rubyforge.org>
This file is part of Maruku.
Maruku is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Maruku is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Maruku; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
++
Constant Summary collapse
- Textile2_EmptyLine =
/^\s*$/- Textile2_Signature =
/^(\S+\.?)\.\s(.*)/
Constants included from MaRuKu::Strings
MaRuKu::Strings::Abbreviation, MaRuKu::Strings::AttributeDefinitionList, MaRuKu::Strings::Definition, MaRuKu::Strings::EMailAddress, MaRuKu::Strings::FootnoteText, MaRuKu::Strings::HeaderWithAttributes, MaRuKu::Strings::HeaderWithId, MaRuKu::Strings::IncompleteLink, MaRuKu::Strings::InlineAttributeList, MaRuKu::Strings::LinkRegex, MaRuKu::Strings::MightBeTableHeader, MaRuKu::Strings::Sep, MaRuKu::Strings::TabSize, MaRuKu::Strings::TableSeparator
Instance Method Summary collapse
-
#absolute(root) ⇒ Object
returns the path as an absolute path with
rootROOT MUST BE ABSOLUTE. -
#add_slashes ⇒ String
Adds minimal slashes to escape the string.
-
#any? ⇒ Boolean
String doesn’t include Enumerable in Ruby 1.9, so we lose #any?.
- #blink ⇒ Object
-
#bold ⇒ Object
Returns the string, colored red.
-
#colorize(color_code, closing_tag = 39) ⇒ String
Returns the string, encoded for a tty terminal with the given color code.
-
#end_with?(suffix) ⇒ Boolean
Does the string end with the given suffix?.
- #ends_with(aString) ⇒ Object
- #ensure_end(aString) ⇒ Object
-
#hexlify ⇒ Object
Converts this text into hex.
-
#hide_password ⇒ String
removes the password from a url.
- #inspect_more(a = nil, b = nil) ⇒ Object
-
#is_binary_data? ⇒ Boolean
(also: #binary?)
Attempts to discern if the string represents binary data or not.
- #lchop ⇒ Object
-
#lines ⇒ Object
DON’T USE String#each.
-
#md5 ⇒ Digest::MD5
easy md5!.
- #md_type ⇒ Object
-
#mysplit ⇒ Object
“ andrea censi ” => [“ andrea ”, “censi ”].
-
#not_null? ⇒ Boolean
Am I not equal to the NULL_ID used in revision logs?.
-
#null? ⇒ Boolean
Am I equal to the NULL_ID used in revision logs?.
-
#ord ⇒ Fixnum
Returns the numeric, ascii value of the first character in the string.
- #quote_if_needed ⇒ Object
-
#relative_path(root) ⇒ String
Returns the path from
rootto the path represented by the string. -
#remove_slashes ⇒ String
Removes minimal slashes to unescape the string.
-
#run(options = {}, args = []) ⇒ Amp::Command
If the string is the name of a command, run it.
-
#sha1 ⇒ Digest::SHA1
easy sha1! This is unsafe, as SHA1 kinda sucks.
-
#shift(char) ⇒ String
(also: #lchomp)
Pops the given character off the front of the string, but only if the string starts with the given character.
-
#short_hex ⇒ Object
Converts this text into hex, and trims it a little for readability.
-
#split_lines_better ⇒ Array<String>
Newer version of split_newlines that works better.
-
#split_newlines(add_newlines = true) ⇒ Object
Splits on newlines only, removing extra blank line at end if there is one.
-
#start_with?(prefix) ⇒ Boolean
Does the string start with the given prefix?.
- #starts_with(aString) ⇒ Object
- #t2_contains_signature? ⇒ Boolean
- #t2_empty? ⇒ Boolean
-
#t2_get_signature ⇒ Object
sig, rest = t2_get_signature.
-
#to_ansi ⇒ Object
escapes special characters.
-
#to_html ⇒ Object
A string is rendered into HTML by creating a REXML::Text node.
-
#to_md(c = nil) ⇒ Object
XXX: markdown escaping.
- #underline ⇒ Object
-
#unhexlify ⇒ Object
Converts a string of hexademical into its binary representation.
Methods included from MaRuKu::Strings
#add_tabs, #dbg_describe_ary, #force_linebreak?, #line_md_type, #normalize_key_and_value, #num_leading_hashes, #number_of_leading_spaces, #parse_email_headers, #sanitize_ref_id, #spaces_before_first_char, #split_lines, #strip_hashes, #strip_indent, #unquote
Instance Method Details
#absolute(root) ⇒ Object
returns the path as an absolute path with root ROOT MUST BE ABSOLUTE
877 878 879 880 |
# File 'lib/amp/support/support.rb', line 877 def absolute(root) return self if self[0] == ?/ "#{root}/#{self}" end |
#add_slashes ⇒ String
Adds minimal slashes to escape the string
861 862 863 |
# File 'lib/amp/support/support.rb', line 861 def add_slashes self.gsub(/\\/,"\\\\").gsub(/\n/,"\\n").gsub(/\r/,"\\r").gsub("\0","\\0") end |
#any? ⇒ Boolean
String doesn’t include Enumerable in Ruby 1.9, so we lose #any?. Luckily it’s quite easy to implement.
44 45 46 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 44 def any? size > 0 end |
#blink ⇒ Object
42 |
# File 'lib/amp/dependencies/maruku/output/to_ansi.rb', line 42 def blink; colorize(5, 25); end |
#bold ⇒ Object
Returns the string, colored red.
40 |
# File 'lib/amp/dependencies/maruku/output/to_ansi.rb', line 40 def bold; colorize(1, 22); end |
#colorize(color_code, closing_tag = 39) ⇒ String
Returns the string, encoded for a tty terminal with the given color code.
17 18 19 |
# File 'lib/amp/dependencies/maruku/output/to_ansi.rb', line 17 def colorize(color_code, closing_tag = 39) "\e[#{color_code}m#{self}\e[#{closing_tag}m" end |
#end_with?(suffix) ⇒ Boolean
Does the string end with the given suffix?
749 750 751 |
# File 'lib/amp/support/support.rb', line 749 def end_with?(suffix) self[-suffix.size, suffix.size] == suffix # self =~ /#{str}$/ end |
#ends_with(aString) ⇒ Object
40 41 42 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 40 def ends_with(aString) index(aString, -aString.size) end |
#ensure_end(aString) ⇒ Object
44 45 46 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 44 def ensure_end(aString) ends_with(aString) ? self : self + aString end |
#hexlify ⇒ Object
Converts this text into hex. each letter is replaced with it’s hex counterpart
829 830 831 832 833 834 835 |
# File 'lib/amp/support/support.rb', line 829 def hexlify str = "" self.each_byte do |i| str << i.to_s(16).rjust(2, "0") end str end |
#hide_password ⇒ String
removes the password from a url. else, just returns self
846 847 848 849 850 851 852 853 854 855 856 |
# File 'lib/amp/support/support.rb', line 846 def hide_password if s = self.match(/^http(?:s)?:\/\/[^:]+(?::([^:]+))?(@)/) string = '' string << self[0..s.begin(1)-1] # get from beginning to the pass string << '***' string << self[s.begin(2)..-1] string else self end end |
#inspect_more(a = nil, b = nil) ⇒ Object
24 25 26 |
# File 'lib/amp/dependencies/maruku/structures_inspect.rb', line 24 def inspect_more(a=nil,b=nil) inspect end |
#is_binary_data? ⇒ Boolean Also known as: binary?
Attempts to discern if the string represents binary data or not. Not 100% accurate. Is part of the YAML code that comes with ruby, but since we don’t load rubygems, we don’t get this method for free.
889 890 891 |
# File 'lib/amp/support/support.rb', line 889 def is_binary_data? ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty? end |
#lchop ⇒ Object
48 49 50 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 48 def lchop slice(1, length) end |
#lines ⇒ Object
DON’T USE String#each. Use String#each_line
10 11 12 13 14 15 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 10 def lines return to_enum(:lines) if !block_given? self.split(/^/).each do |l| yield l end end |
#md5 ⇒ Digest::MD5
easy md5!
799 800 801 |
# File 'lib/amp/support/support.rb', line 799 def md5 Digest::MD5.new.update(self) end |
#md_type ⇒ Object
23 24 25 |
# File 'lib/amp/dependencies/maruku/input/type_detection.rb', line 23 def md_type() @md_type ||= line_md_type(self) end |
#mysplit ⇒ Object
“ andrea censi ” => [“ andrea ”, “censi ”]
29 30 31 |
# File 'lib/amp/dependencies/maruku/output/to_markdown.rb', line 29 def mysplit split.map{|x| x+" "} end |
#not_null? ⇒ Boolean
Am I not equal to the NULL_ID used in revision logs?
731 732 733 |
# File 'lib/amp/support/support.rb', line 731 def not_null? !null? end |
#null? ⇒ Boolean
Am I equal to the NULL_ID used in revision logs?
726 727 728 |
# File 'lib/amp/support/support.rb', line 726 def null? self == Amp::Mercurial::RevlogSupport::Node::NULL_ID end |
#ord ⇒ Fixnum
Returns the numeric, ascii value of the first character in the string.
22 23 24 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 22 def ord self[0] end |
#quote_if_needed ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/amp/dependencies/maruku/attributes.rb', line 23 def quote_if_needed if /[\s\'\"]/.match self inspect else self end end |
#relative_path(root) ⇒ String
Returns the path from root to the path represented by the string. Will fail if the string is not inside root.
717 718 719 720 721 722 723 |
# File 'lib/amp/support/support.rb', line 717 def relative_path(root) return '' if self == root # return a more local path if possible... return self[root.length..-1] if start_with? root self # else we're outside the repo end |
#remove_slashes ⇒ String
Removes minimal slashes to unescape the string
868 869 870 |
# File 'lib/amp/support/support.rb', line 868 def remove_slashes self.gsub(/\\0/,"\0").gsub(/\\r/,"\r").gsub(/\\n/,"\n").gsub(/\\\\/,"\\") end |
#run(options = {}, args = []) ⇒ Amp::Command
If the string is the name of a command, run it. Else, raise hell.
819 820 821 822 823 824 825 |
# File 'lib/amp/support/support.rb', line 819 def run(={}, args=[]) if cmd = Amp::Command[self] cmd.run , args else raise "No such command #{self}" end end |
#sha1 ⇒ Digest::SHA1
easy sha1! This is unsafe, as SHA1 kinda sucks.
808 809 810 |
# File 'lib/amp/support/support.rb', line 808 def sha1 Digest::SHA1.new.update(self) end |
#shift(char) ⇒ String Also known as: lchomp
Pops the given character off the front of the string, but only if the string starts with the given character. Otherwise, nothing happens. Often used to remove troublesome leading slashes. Much like an “lchomp” method.
760 761 762 763 764 |
# File 'lib/amp/support/support.rb', line 760 def shift(char) return '' if self.empty? return self[1..-1] if self.start_with? char self end |
#short_hex ⇒ Object
Converts this text into hex, and trims it a little for readability.
839 840 841 |
# File 'lib/amp/support/support.rb', line 839 def short_hex hexlify[0..9] end |
#split_lines_better ⇒ Array<String>
Newer version of split_newlines that works better. This splits on newlines, but includes the newline in each entry in the resultant string array.
789 790 791 792 793 |
# File 'lib/amp/support/support.rb', line 789 def split_lines_better result = [] each_line {|l| result << l} result end |
#split_newlines(add_newlines = true) ⇒ Object
Splits on newlines only, removing extra blank line at end if there is one. This is how mercurial does it and i’m sticking to it. This method is evil. DON’T USE IT.
772 773 774 775 776 777 778 779 780 781 782 |
# File 'lib/amp/support/support.rb', line 772 def split_newlines(add_newlines=true) return [] if self.empty? lines = self.split("\n").map {|l| l + (add_newlines ? "\n" : "") } return lines if lines.size == 1 if (add_newlines && lines.last == "\n") || (!add_newlines && lines.last.empty?) lines.pop else lines[-1] = lines[-1][0..-2] if lines[-1][-1,1] == "\n" end lines end |
#start_with?(prefix) ⇒ Boolean
Does the string start with the given prefix?
740 741 742 |
# File 'lib/amp/support/support.rb', line 740 def start_with?(prefix) self[0,prefix.size] == prefix # self =~ /^#{str}/ end |
#starts_with(aString) ⇒ Object
36 37 38 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 36 def starts_with(aString) rindex(aString, 0) == 0 end |
#t2_contains_signature? ⇒ Boolean
9 10 11 |
# File 'lib/amp/dependencies/maruku/input_textile2/t2_parser.rb', line 9 def t2_contains_signature? self =~ Textile2_Signature end |
#t2_empty? ⇒ Boolean
5 6 7 |
# File 'lib/amp/dependencies/maruku/input_textile2/t2_parser.rb', line 5 def t2_empty? self =~ Textile2_EmptyLine end |
#t2_get_signature ⇒ Object
sig, rest = t2_get_signature
14 15 16 17 |
# File 'lib/amp/dependencies/maruku/input_textile2/t2_parser.rb', line 14 def t2_get_signature self =~ Textile2_Signature return Textile2Signature.new($1), $2 end |
#to_ansi ⇒ Object
escapes special characters
5 6 7 |
# File 'lib/amp/dependencies/maruku/output/to_ansi.rb', line 5 def to_ansi self end |
#to_html ⇒ Object
A string is rendered into HTML by creating a REXML::Text node. REXML takes care of all the encoding.
34 35 36 |
# File 'lib/amp/dependencies/maruku/output/to_html.rb', line 34 def to_html REXML::Text.new(self) end |
#to_md(c = nil) ⇒ Object
XXX: markdown escaping
24 25 26 |
# File 'lib/amp/dependencies/maruku/output/to_markdown.rb', line 24 def to_md(c=nil) to_s end |
#underline ⇒ Object
41 |
# File 'lib/amp/dependencies/maruku/output/to_ansi.rb', line 41 def underline; colorize(4, 24); end |
#unhexlify ⇒ Object
Converts a string of hexademical into its binary representation. Method on strings. When the data in the string is hexademical (such as “DEADBEEF”), this method decodes the hex and converts every 2 bytes into its 1-byte binary representation.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 80 def unhexlify str = "\000" * (size/2) c = 0 (0..size-2).step(2) do |i| hex = self[i,2].to_i(16) str[c] = hex c += 1 end str end |