| Class | Text |
| In: |
lib/text.rb
|
| Parent: | Object |
| Copyright: | Copyright (c) 2005 Nicolas Pouillard. All rights reserved. |
| Author: | Nicolas Pouillard <ertai@lrde.epita.fr>. |
| License: | Gnu General Public License. |
| Revision: | $Id: /w/fey/ruby_ex/trunk/lib/text.rb 7964 2005-09-16T10:05:22.743435Z ertai $ |
| cut_if_needed | [RW] | |
| text | [RW] | |
| width | [RW] |
options:
width: the maximal line width (default 78) cut_if_needed: see cropping
# File lib/text.rb, line 15 def initialize ( text, options={} ) options = @@default_options.merge options @text = text @width = options[:width] @cut_if_needed = options[:cut_if_needed] end
# File lib/text.rb, line 112 def clip! ( head_size=100, tail_size=30, message="\n[...clipped...]\n\n", &block ) input = @text.to_a size = input.size return identity!(&block) if head_size + tail_size >= size head = (head_size - 1 < 0)? [] : input[0 .. head_size - 1] tail = input[[0, size - tail_size, head_size].max .. -1] @text, block = '', method(:adder) if block.nil? head.each(&block) message.each(&block) tail.each(&block) self end
# File lib/text.rb, line 95 def crop! ( width=@width, message=" ...\n", &block ) input = @text @text, block = '', method(:adder) if block.nil? message = message.to_s width2 = width - 1 - message.chomp.width raise ArgumentError, "Bad width: too low" if width2 < 0 input.each do |line| if line.width > width block[line[0..width2], message] else block[line] end end self end
# File lib/text.rb, line 24 def justify! ( &block ) input = @text.to_a @text, block = '', method(:adder) if block.nil? last_line = input.size - 1 input.each_with_index do |line, i| line =~ /^(\s*)(.*)\s*?(\n?)$/ indent, base, eol = $1, $2, $3 words = base.split(/\s+/) base = words.join(' ') width = indent.width + base.width unless width > @width or base.empty? or i == last_line spaces = words.size - 1 padding_size = @width - width if spaces > 0 nb = ' ' * (padding_size / spaces + 1) nb2 = nb + ' ' rest = padding_size % spaces + 1 base.gsub!(/ /) do rest -= 1 (rest >= 0) ? nb2 : nb end end end block[indent, base, eol] end self end
Redirect calls like `justify’ to `dup.justify!’
# File lib/text.rb, line 153 def method_missing ( meth, *args, &block ) in_place_meth = "#{meth}!""#{meth}!" return super unless respond_to? in_place_meth dup.send(in_place_meth, *args, &block) end
# File lib/text.rb, line 54 def split! ( &block ) last_new_line = (@text[-1] == ?\n)? "\n" : '' indent = @text[/\A([ \t]*)/, 1] words = @text.split(/\s+/) @text, block = '', method(:adder) if block.nil? line = '' while not words.empty? words.shift if words.first.empty? word = words.first if indent.width + line.width + word.width + 1 <= @width line += ' ' unless line.empty? line += word words.shift elsif line.empty? if @cut_if_needed line = word[0..width-1] words[0] = word[width..-1] block[indent, line, "\n"] line = '' else block[indent, word, "\n"] words.shift end else block[indent, line, "\n"] line = '' end end block[indent, line, last_new_line] unless line.empty? self end