textwrap 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.txt +19 -0
- data/lib/textwrap.rb +130 -0
- metadata +48 -0
    
        data/README.txt
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            = testwrap.rb
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Copyright (c) 2007 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            == Example
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                require 'textwrap'
         | 
| 8 | 
            +
                include Textwrap
         | 
| 9 | 
            +
                puts Textwrap.fill(<<EOS, 40)
         | 
| 10 | 
            +
                The wrap() method is just like fill() except that it returns
         | 
| 11 | 
            +
                a list of strings instead of one big string with newlines to separate
         | 
| 12 | 
            +
                the wrapped lines.
         | 
| 13 | 
            +
                EOS
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                # The wrap() method is just like fill()
         | 
| 16 | 
            +
                # except that it returns a list of strings
         | 
| 17 | 
            +
                # instead of one big string with newlines
         | 
| 18 | 
            +
                # to separate the wrapped lines.
         | 
| 19 | 
            +
             | 
    
        data/lib/textwrap.rb
    ADDED
    
    | @@ -0,0 +1,130 @@ | |
| 1 | 
            +
            #
         | 
| 2 | 
            +
            # = testwrap.rb
         | 
| 3 | 
            +
            #
         | 
| 4 | 
            +
            # Copyright (c) 2007 SUGAWARA Genki <sgwr_dts@yahoo.co.jp>
         | 
| 5 | 
            +
            #
         | 
| 6 | 
            +
            # == Example
         | 
| 7 | 
            +
            #
         | 
| 8 | 
            +
            #     require 'textwrap'
         | 
| 9 | 
            +
            #     include Textwrap
         | 
| 10 | 
            +
            #     puts Textwrap.fill(<<EOS, 40)
         | 
| 11 | 
            +
            #     The wrap() method is just like fill() except that it returns
         | 
| 12 | 
            +
            #     a list of strings instead of one big string with newlines to separate
         | 
| 13 | 
            +
            #     the wrapped lines.
         | 
| 14 | 
            +
            #     EOS
         | 
| 15 | 
            +
            #
         | 
| 16 | 
            +
            #     # The wrap() method is just like fill()
         | 
| 17 | 
            +
            #     # except that it returns a list of strings
         | 
| 18 | 
            +
            #     # instead of one big string with newlines
         | 
| 19 | 
            +
            #     # to separate the wrapped lines.
         | 
| 20 | 
            +
            #
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            require 'strscan'
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            module Textwrap
         | 
| 25 | 
            +
              def wrap(text, width = 70, options = {})
         | 
| 26 | 
            +
                if width.kind_of?(Hash)
         | 
| 27 | 
            +
                  options = width
         | 
| 28 | 
            +
                  width = options.fetch(:width, 70)
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                initial_indent    = options.fetch(:initial_indent, '')
         | 
| 32 | 
            +
                subsequent_indent = options.fetch(:subsequent_indent, '')
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                if options.fetch(:expand_tabs, true)
         | 
| 35 | 
            +
                  text = text.gsub(/\t/, '        ') # SP x 8
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                if options.fetch(:replace_whitespace, true)
         | 
| 39 | 
            +
                  text = text.gsub(/\s/, ' ')
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                if options.fetch(:break_long_words, true)
         | 
| 43 | 
            +
                  regexps = [/\s{1,#{width}}/, /\S{1,#{width}}/]
         | 
| 44 | 
            +
                else
         | 
| 45 | 
            +
                  regexps = [/\s+/, /\S+/]
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                s = StringScanner.new(text.split($/).join)
         | 
| 49 | 
            +
                lines = []
         | 
| 50 | 
            +
                indent = initial_indent
         | 
| 51 | 
            +
                words = [s.scan(regexps.reverse!.first) || s.scan(regexps.reverse!.first) || '']
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                until s.eos?
         | 
| 54 | 
            +
                  word = s.scan(regexps.reverse!.first) or next
         | 
| 55 | 
            +
                  line = indent + words.join.strip
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                  if (line + word).length > width
         | 
| 58 | 
            +
                    lines << line
         | 
| 59 | 
            +
                    words.clear
         | 
| 60 | 
            +
                    indent = subsequent_indent
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                  words << word
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                unless words.empty?
         | 
| 67 | 
            +
                  lines << indent + words.join.strip
         | 
| 68 | 
            +
                end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                return lines
         | 
| 71 | 
            +
              end # wrap
         | 
| 72 | 
            +
             | 
| 73 | 
            +
              def fill(text, width = 70, options = {})
         | 
| 74 | 
            +
                wrap(text, width, options).join($/)
         | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
              module_function :wrap, :fill
         | 
| 78 | 
            +
            end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            class TextWrapper
         | 
| 81 | 
            +
              @@kwargs = {
         | 
| 82 | 
            +
                :width => 70,
         | 
| 83 | 
            +
                :expand_tabs => true,
         | 
| 84 | 
            +
                :replace_whitespace =>  true,
         | 
| 85 | 
            +
                :initial_indent => '',
         | 
| 86 | 
            +
                :subsequent_indent => '',
         | 
| 87 | 
            +
                :break_long_words => true
         | 
| 88 | 
            +
              }
         | 
| 89 | 
            +
             | 
| 90 | 
            +
              attr_accessor *@@kwargs.keys
         | 
| 91 | 
            +
             | 
| 92 | 
            +
              def initialize(options = {})
         | 
| 93 | 
            +
                @@kwargs.each do |kw, defval|
         | 
| 94 | 
            +
                  self.instance_variable_set("@#{kw}", options.fetch(kw, defval))
         | 
| 95 | 
            +
                end
         | 
| 96 | 
            +
              end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
              def wrap(text, width = nil, options = {})
         | 
| 99 | 
            +
                prepare(text, width, options) do |t, w, o|
         | 
| 100 | 
            +
                  Textwrap.wrap(t, w, o)
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
              end
         | 
| 103 | 
            +
             | 
| 104 | 
            +
              def fill(text, width = nil, options = {})
         | 
| 105 | 
            +
                prepare(text, width, options) do |t, w, o|
         | 
| 106 | 
            +
                  Textwrap.fill(t, w, o)
         | 
| 107 | 
            +
                end
         | 
| 108 | 
            +
              end
         | 
| 109 | 
            +
             | 
| 110 | 
            +
              private
         | 
| 111 | 
            +
              def prepare(text, width, options)
         | 
| 112 | 
            +
                if width.kind_of?(Hash)
         | 
| 113 | 
            +
                  options = width
         | 
| 114 | 
            +
                end
         | 
| 115 | 
            +
             | 
| 116 | 
            +
                instance_options = {}
         | 
| 117 | 
            +
             | 
| 118 | 
            +
                @@kwargs.each do |kw, defval|
         | 
| 119 | 
            +
                  instance_options[kw] = self.instance_variable_get("@#{kw}")
         | 
| 120 | 
            +
                end
         | 
| 121 | 
            +
             | 
| 122 | 
            +
                options = instance_options.merge(options)
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                unless width
         | 
| 125 | 
            +
                  width = options[:width]
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                yield(text, width, options)
         | 
| 129 | 
            +
              end
         | 
| 130 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,48 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            rubygems_version: 0.9.2
         | 
| 3 | 
            +
            specification_version: 1
         | 
| 4 | 
            +
            name: textwrap
         | 
| 5 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 6 | 
            +
              version: 0.1.0
         | 
| 7 | 
            +
            date: 2007-10-20 00:00:00 +09:00
         | 
| 8 | 
            +
            summary: Text wrapping and filling library like Python textwrap module.
         | 
| 9 | 
            +
            require_paths: 
         | 
| 10 | 
            +
            - lib
         | 
| 11 | 
            +
            email: sgwr_dts@yahoo.co.jp
         | 
| 12 | 
            +
            homepage: http://textwrap.rubyforge.org
         | 
| 13 | 
            +
            rubyforge_project: textwrap
         | 
| 14 | 
            +
            description: Text wrapping and filling library like Python textwrap module.
         | 
| 15 | 
            +
            autorequire: 
         | 
| 16 | 
            +
            default_executable: 
         | 
| 17 | 
            +
            bindir: bin
         | 
| 18 | 
            +
            has_rdoc: true
         | 
| 19 | 
            +
            required_ruby_version: !ruby/object:Gem::Version::Requirement 
         | 
| 20 | 
            +
              requirements: 
         | 
| 21 | 
            +
              - - ">"
         | 
| 22 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 23 | 
            +
                  version: 0.0.0
         | 
| 24 | 
            +
              version: 
         | 
| 25 | 
            +
            platform: ruby
         | 
| 26 | 
            +
            signing_key: 
         | 
| 27 | 
            +
            cert_chain: 
         | 
| 28 | 
            +
            post_install_message: 
         | 
| 29 | 
            +
            authors: 
         | 
| 30 | 
            +
            - winebarrel
         | 
| 31 | 
            +
            files: 
         | 
| 32 | 
            +
            - README.txt
         | 
| 33 | 
            +
            - lib/textwrap.rb
         | 
| 34 | 
            +
            test_files: []
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            rdoc_options: 
         | 
| 37 | 
            +
            - --main
         | 
| 38 | 
            +
            - README.txt
         | 
| 39 | 
            +
            extra_rdoc_files: 
         | 
| 40 | 
            +
            - README.txt
         | 
| 41 | 
            +
            executables: []
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            extensions: []
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            requirements: []
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            dependencies: []
         | 
| 48 | 
            +
             |