Class: SiSU_TextUtils::Wrap

Inherits:
Object
  • Object
show all
Defined in:
lib/sisu/txt_shared.rb

Instance Method Summary collapse

Constructor Details

#initialize(para = '', n_char_max = 76, n_indent = 0, n_hang = nil, post = '') ⇒ Wrap

Returns a new instance of Wrap.



60
61
62
63
64
# File 'lib/sisu/txt_shared.rb', line 60

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_wrapObject



121
122
123
124
125
126
127
128
129
# File 'lib/sisu/txt_shared.rb', line 121

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_lineObject



65
66
67
# File 'lib/sisu/txt_shared.rb', line 65

def break_line
  "\n"
end

#line_wrapObject



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
111
112
# File 'lib/sisu/txt_shared.rb', line 68

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_endnoteObject



117
118
119
120
# File 'lib/sisu/txt_shared.rb', line 117

def line_wrap_endnote
  @n_indent,@n_hang=4,2
  line_wrap
end

#line_wrap_indent1Object



113
114
115
116
# File 'lib/sisu/txt_shared.rb', line 113

def line_wrap_indent1
  @n_indent,@n_hang=2,2
  line_wrap
end

#no_wrapObject



130
131
132
# File 'lib/sisu/txt_shared.rb', line 130

def no_wrap
  @para
end

#no_wrap_no_breaksObject



133
134
135
# File 'lib/sisu/txt_shared.rb', line 133

def no_wrap_no_breaks
  @para.gsub(/\n/m,' ').gsub(/\s\s+/,' ')
end