~ubuntu-branches/ubuntu/utopic/ctioga2/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/ctioga2/commands/doc/wordwrap.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## \file wordwrap.rb small word-wrapping utility
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/commands/commands'
 
16
require 'ctioga2/commands/parsers/command-line'
 
17
 
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 131 $', '$Date: 2010-01-14 22:51:09 +0100 (Thu, 14 Jan 2010) $')
 
21
 
 
22
  module Commands
 
23
 
 
24
    module Documentation
 
25
 
 
26
 
 
27
      # A small utility class to do word wrapping.
 
28
      #
 
29
      # \todo Maybe this belongs in Utils ?
 
30
      class WordWrapper
 
31
        
 
32
        # A regex matching word separation.
 
33
        attr_accessor :word_sep
 
34
 
 
35
        # What to replace the separator with
 
36
        attr_accessor :new_sep
 
37
 
 
38
        def initialize(ws = /\s+/, ns = " ")
 
39
          @word_sep = ws
 
40
          @new_sep = ns
 
41
        end
 
42
 
 
43
        # Split strings into an array of string whose length is each
 
44
        # less than _cols_
 
45
        def wrap(str, cols)
 
46
          words = str.split(@word_sep)
 
47
          lines = [words.shift]
 
48
          while w = words.shift
 
49
            if (lines.last.size + w.size + @new_sep.size) <= cols
 
50
              lines.last.concat("#{@new_sep}#{w}")
 
51
            else
 
52
              lines << w
 
53
            end
 
54
          end
 
55
          return lines
 
56
        end
 
57
 
 
58
        # Calls #wrap for default values of the parameters
 
59
        def self.wrap(str, cols)
 
60
          return WordWrapper.new.wrap(str, cols)
 
61
        end
 
62
 
 
63
      end
 
64
 
 
65
 
 
66
    end
 
67
 
 
68
  end
 
69
 
 
70
end