Class: SiSU_TextUtils::Wrap
- Inherits:
-
Object
- Object
- SiSU_TextUtils::Wrap
- Defined in:
- lib/sisu/txt_shared.rb
Instance Method Summary collapse
- #array_wrap ⇒ Object
- #break_line ⇒ Object
-
#initialize(para = '', n_char_max = 76, n_indent = 0, n_hang = nil, post = '') ⇒ Wrap
constructor
A new instance of Wrap.
- #line_wrap ⇒ Object
- #line_wrap_endnote ⇒ Object
- #line_wrap_indent1 ⇒ Object
- #no_wrap ⇒ Object
- #no_wrap_no_breaks ⇒ Object
Constructor Details
#initialize(para = '', n_char_max = 76, n_indent = 0, n_hang = nil, post = '') ⇒ Wrap
Returns a new instance of Wrap.
58 59 60 61 62 |
# File 'lib/sisu/txt_shared.rb', line 58 def initialize(para='',n_char_max=76,n_indent=0,n_hang=nil,post='') @para,@n_char_max,@n_indent,@post,=para,n_char_max,n_indent,post @n_char_max_extend = n_char_max @n_hang=n_hang ? n_hang : @n_indent end |
Instance Method Details
#array_wrap ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/sisu/txt_shared.rb', line 119 def array_wrap if @para.is_a?(Array) @arr=[] @para.each do |line| @arr << SiSU_TextUtils::Wrap.new(line,@n_char_max,@n_indent,@n_hang).line_wrap end end @arr end |
#break_line ⇒ Object
63 64 65 |
# File 'lib/sisu/txt_shared.rb', line 63 def break_line "\n" end |
#line_wrap ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sisu/txt_shared.rb', line 66 def line_wrap space=' ' spaces_indent,spaces_hang="#{break_line}#{space*@n_indent}",space*@n_hang line=0 out=[] out[line]='' @para=@para.gsub(/<br>/,' \\ '). gsub(/#{Mx[:br_nl]}/,"\n\n") words=@para.scan(/\n\n|\s+\\\s+|<br>|\S+/m) while words != '' word=words.shift if not word out[line] unless out[line].empty? #check break elsif word =~/<br>/ word=nil out[line]=out[line].gsub(/<br>/,'') line=line elsif word =~/\n\n/ word="\n" @n_char_max_extend = @n_char_max line += 1 elsif (out[line].length + word.length) > (@n_char_max_extend - @n_indent) \ and out[line] =~/\S+/ @n_char_max_extend = @n_char_max out[line].squeeze!(' ') line += 1 end if word out[line]=if out[line] \ and out[line] !~/\S+$/m "#{out[line]}#{word}" elsif out[line] \ and out[line] =~/\S+/ "#{out[line]} #{word}" else "#{word.strip}" end end @oldword=word if word =~/\S+/ end post=(@post.empty?) \ ? '' : "\n" + (' '*@n_indent) +@post spaces_hang + out.join(spaces_indent) + post end |
#line_wrap_endnote ⇒ Object
115 116 117 118 |
# File 'lib/sisu/txt_shared.rb', line 115 def line_wrap_endnote @n_indent,@n_hang=4,2 line_wrap end |
#line_wrap_indent1 ⇒ Object
111 112 113 114 |
# File 'lib/sisu/txt_shared.rb', line 111 def line_wrap_indent1 @n_indent,@n_hang=2,2 line_wrap end |
#no_wrap ⇒ Object
128 129 130 |
# File 'lib/sisu/txt_shared.rb', line 128 def no_wrap @para end |
#no_wrap_no_breaks ⇒ Object
131 132 133 |
# File 'lib/sisu/txt_shared.rb', line 131 def no_wrap_no_breaks @para.gsub(/\n/m,' ').gsub(/\s\s+/,' ') end |