word_wrap 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4660c08a3ffd74a97b105526d2361f186bfb236a
4
+ data.tar.gz: 7591f43b255fa1e8d80d21342ebd1e41d7ab2b62
5
+ SHA512:
6
+ metadata.gz: 36bb550b86addd349547a880cbaa3c8b705da3a43958b6028254f4d633e2b22b36b2561589790802b696afe1fca37c4dce6b1ec0db0176a8a3d6a53c6c9a42c0
7
+ data.tar.gz: cb381f125177bc732d3276d068ca59ea063e35f38fb6db269e8fb5a2e17e723ff8c783c1b08aa080a89eb45e1b772034acef86363a303095f7443f8a6e47badc
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_store
19
+ *.swp
20
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in word_wrap.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Radek Pazdera
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,121 @@
1
+ # WordWrap
2
+
3
+ This gem is a extremely simple tool to word-wrap texts, which is the one an
4
+ only thing it can do. It comes with a script called `ww` that you can use
5
+ in the command line. And of course, you can get the functionality from within
6
+ Ruby as well.
7
+
8
+ For more information on usage, please refer to the **Usage** section of this
9
+ README bellow.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'word_wrap'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install word_wrap
24
+
25
+ ## Usage
26
+
27
+ You can use either the binary for the command line or the library directly
28
+ from your Ruby scripts. Both use cases are explained bellow.
29
+
30
+ ### Command Line
31
+
32
+ When in shell, you can use the `ww` tool (ww stands for word-wrap). It takes
33
+ only two arguments:
34
+
35
+ * `-w W, --width WIDTH` - The width to which the text should be wrapped.
36
+ It is set to 80 by default.
37
+ * `-f, --fit` - In this case, the program will also rearrange lines, that are
38
+ shorter than 80 to fit them as much as possible to the
39
+ predefined width, in addition to wrapping the lines that exceed
40
+ it. This option is generally better for plain text. For code,
41
+ however, it will mess up your indentation.
42
+ #### Examples
43
+
44
+ The example file looks like this:
45
+ ```bash
46
+ cat hip.txt
47
+ Forage Shoreditch disrupt Pitchfork meh.
48
+
49
+ Mustache 3 wolf moon gluten-free whatever master burn
50
+ vinyl.
51
+ ```
52
+
53
+ ```bash
54
+ ww -w20 hip.txt
55
+ Forage Shoreditch
56
+ disrupt Pitchfork
57
+ meh.
58
+
59
+ Mustache 3 wolf moon
60
+ gluten-free whatever
61
+ master burn
62
+ vinyl.
63
+ ```
64
+
65
+ But you can also use stdin:
66
+ ```bash
67
+ cat hip | ww -w20
68
+ Forage Shoreditch
69
+ disrupt Pitchfork
70
+ meh.
71
+
72
+ Mustache 3 wolf moon
73
+ gluten-free whatever
74
+ master burn
75
+ vinyl.
76
+ ```
77
+
78
+ Note the difference at end of the second paragraph:
79
+ ```bash
80
+ cat hip | ww -w20
81
+ Forage Shoreditch
82
+ disrupt Pitchfork
83
+ meh.
84
+
85
+ Mustache 3 wolf moon
86
+ gluten-free whatever
87
+ master burn
88
+ vinyl.
89
+ ```
90
+
91
+ ### Ruby library
92
+
93
+ If you would like to use the library in Ruby, you have two options:
94
+
95
+ * Use the `WordWrap#ww` function directly
96
+ * Use the `String#wrap` and `String#fit` functions this module adds to the
97
+ standard `String` class.
98
+
99
+ #### Example
100
+
101
+ ```irb
102
+ irb(main):001:0> require 'word_wrap'
103
+ => true
104
+
105
+ irb(main):002:0> WordWrap.ww "123 456 789", 5
106
+ => "123\n456\n789\n"
107
+
108
+ irb(main):003:0> "123 456 789".wrap 5
109
+ => "123\n456\n789\n"
110
+
111
+ irb(main):004:0> "123 456 789".fit 8
112
+ => "123 456\n789\n"
113
+ ```
114
+
115
+ ## Contributing
116
+
117
+ 1. Fork it ( http://github.com/pazdera/word_wrap/fork )
118
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
119
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
120
+ 4. Push to the branch (`git push origin my-new-feature`)
121
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/ww ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright (c) 2014 Radek Pazdera
4
+ # Distributed under the MIT License
5
+
6
+ require 'optparse'
7
+ require 'word_wrap'
8
+
9
+ options = {:width => 80, :fit => false}
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: ww [OPTIONS] [input-file]"
12
+
13
+ opts.on("-f", "--fit", "Fit precisely to the defined width") do |f|
14
+ options[:fit] = f
15
+ end
16
+
17
+ opts.on("-w", "--width COLUMNS", "Set line width (defaults to 80)") do |w|
18
+ options[:width] = w.to_i
19
+ end
20
+ end.parse!
21
+
22
+ if ARGV.length > 0
23
+ File.open(ARGV[0], "r") do |f|
24
+ print WordWrap::ww f.read, options[:width], options[:fit]
25
+ end
26
+ else
27
+ print WordWrap::ww STDIN.read, options[:width], options[:fit]
28
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2014 Radek Pazdera
2
+ # Distributed under the MIT License
3
+
4
+ require "word_wrap/version"
5
+ require "word_wrap/wrapper"
6
+
7
+ module WordWrap
8
+ DEFAULT_WIDTH=80
9
+
10
+ def self.ww(text, width=DEFAULT_WIDTH, fit=false)
11
+ w = Wrapper.new(text, width)
12
+ fit ? w.fit : w.wrap
13
+ end
14
+ end
15
+
16
+ class String
17
+ def wrap(width=DEFAULT_WIDTH)
18
+ WordWrap.ww(self, width, false)
19
+ end
20
+
21
+ def fit(width=DEFAULT_WIDTH)
22
+ WordWrap.ww(self, width, true)
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module WordWrap
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,96 @@
1
+ # Copyright (c) 2014 Radek Pazdera
2
+ # Distributed under the MIT License
3
+
4
+ # The actual implementation of wrapping
5
+ # TODO: share code between wrap and fit implementations
6
+
7
+ module WordWrap
8
+ class Wrapper
9
+ def initialize(text, width)
10
+ @text = text
11
+ @width = width
12
+ end
13
+
14
+ def fit
15
+ lines = []
16
+ next_line = ""
17
+ continued = false
18
+ @text.lines do |line|
19
+ line.chomp! "\n"
20
+ if line.length == 0
21
+ if next_line.length > 0
22
+ lines.push next_line
23
+ next_line = ""
24
+ end
25
+ lines.push ""
26
+ end
27
+
28
+ words = line.split " "
29
+
30
+ words.each do |word|
31
+ word.chomp! "\n"
32
+
33
+ if next_line.length + word.length < @width
34
+ if next_line.length > 0
35
+ next_line << " " << word
36
+ else
37
+ next_line = word
38
+ end
39
+ else
40
+ if word.length >= @width
41
+ lines.push next_line unless next_line == ""
42
+ lines.push word
43
+ next_line = ""
44
+ else
45
+ lines.push next_line
46
+ next_line = word
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ lines.push next_line
53
+ if next_line.length <= 0
54
+ lines.join("\n")
55
+ else
56
+ lines.join("\n") + "\n"
57
+ end
58
+ end
59
+
60
+ def wrap
61
+ output = []
62
+
63
+ @text.lines do |line|
64
+ line.chomp! "\n"
65
+ if line.length > @width
66
+ new_lines = split_line(line, @width)
67
+ while new_lines.length > 1 && new_lines[1].length > @width
68
+ output.push new_lines[0]
69
+ new_lines = split_line new_lines[1], @width
70
+ end
71
+ output += new_lines
72
+ else
73
+ output.push line
74
+ end
75
+ end
76
+ output.map { |s| s.rstrip! }
77
+ output.join("\n") + "\n"
78
+ end
79
+
80
+ def split_line(line, width)
81
+ at = line.index /\s/
82
+ last_at = at
83
+
84
+ while at != nil && at < width
85
+ last_at = at
86
+ at = line.index /\s/, last_at + 1
87
+ end
88
+
89
+ if last_at == nil
90
+ [line]
91
+ else
92
+ [line[0,last_at], line[last_at+1, line.length]]
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,272 @@
1
+ # Copyright (c) 2014 Radek Pazdera
2
+ # Distributed under the MIT License
3
+
4
+ # Several tests for the ww function
5
+
6
+ require "word_wrap"
7
+
8
+ describe WordWrap do
9
+ describe "#ww" do
10
+ it "wraps a single line correctly" do
11
+ text = "0123456789 01234 0123456"
12
+ expected =<<EOF
13
+ 0123456789
14
+ 01234
15
+ 0123456
16
+ EOF
17
+ wrapped = WordWrap.ww(text, 10)
18
+ wrapped.should eql expected
19
+ end
20
+
21
+ it "works without breaks" do
22
+ text =<<EOF
23
+ 0123456789012345678 9
24
+ 0123456 0123456
25
+ EOF
26
+
27
+ expected =<<EOF
28
+ 0123456789012345678
29
+ 9
30
+ 0123456
31
+ 0123456
32
+ EOF
33
+ wrapped = WordWrap.ww(text, 10)
34
+ wrapped.should eql expected
35
+ end
36
+
37
+ it "wraps two paragraphs" do
38
+ text =<<EOF
39
+ Try-hard 3 wolf moon vinyl, authentic disrupt banh mi cliche fixie skateboard biodiesel chillwave before they sold out pop-up direct trade.
40
+
41
+ Mumblecore letterpress iPhone, Brooklyn pork belly distillery cray semiotics.
42
+ EOF
43
+
44
+ expected =<<EOF
45
+ Try-hard 3 wolf
46
+ moon vinyl,
47
+ authentic disrupt
48
+ banh mi cliche
49
+ fixie skateboard
50
+ biodiesel chillwave
51
+ before they sold
52
+ out pop-up direct
53
+ trade.
54
+
55
+ Mumblecore
56
+ letterpress iPhone,
57
+ Brooklyn pork belly
58
+ distillery cray
59
+ semiotics.
60
+ EOF
61
+ wrapped = WordWrap.ww(text, 20)
62
+ wrapped.should eql expected
63
+ end
64
+
65
+ it "wraps a partialy wrapped paragraph" do
66
+ text =<<EOF
67
+ Try-hard 3 wolf moon vinyl,
68
+ authentic
69
+ disrupt banh mi cliche
70
+ fixie skateboard biodiesel chillwave
71
+ before they sold out pop-up direct trade.
72
+ EOF
73
+
74
+ expected =<<EOF
75
+ Try-hard 3 wolf
76
+ moon vinyl,
77
+ authentic
78
+ disrupt banh mi
79
+ cliche
80
+ fixie skateboard
81
+ biodiesel chillwave
82
+ before they sold
83
+ out pop-up direct
84
+ trade.
85
+ EOF
86
+ wrapped = WordWrap.ww(text, 20)
87
+ wrapped.should eql expected
88
+ end
89
+
90
+ it "wrapping keeps whitespace" do
91
+ text =<<EOF
92
+
93
+ word word word word word
94
+
95
+
96
+ word word word
97
+ word word reallylong word
98
+
99
+ extremelylonganduglyword
100
+
101
+ extremelylonganduglyword
102
+
103
+
104
+ EOF
105
+
106
+ expected =<<EOF
107
+
108
+ word word
109
+ word word word
110
+
111
+
112
+ word word word
113
+ word word
114
+ reallylong word
115
+
116
+ extremelylonganduglyword
117
+
118
+
119
+ extremelylonganduglyword
120
+
121
+
122
+ EOF
123
+ wrapped = WordWrap.ww(text, 20)
124
+ wrapped.should eql expected
125
+ end
126
+
127
+ it "wrap as a part of String interface" do
128
+ text =<<EOF
129
+ Try-hard 3 wolf moon vinyl.
130
+
131
+ Mumblecore letterpress iPhone.
132
+ EOF
133
+
134
+ expected =<<EOF
135
+ Try-hard 3 wolf
136
+ moon vinyl.
137
+
138
+ Mumblecore
139
+ letterpress iPhone.
140
+ EOF
141
+ text.wrap(20).should eql expected
142
+ end
143
+
144
+ it "fits a single line correctly" do
145
+ text = "0123456789 01234 0123456"
146
+ expected =<<EOF
147
+ 0123456789
148
+ 01234
149
+ 0123456
150
+ EOF
151
+ wrapped = WordWrap.ww(text, 10, true)
152
+ wrapped.should eql expected
153
+ end
154
+
155
+ it "works without breaks" do
156
+ text =<<EOF
157
+ 0123456789012345678 9
158
+ 0123456 0123456
159
+ EOF
160
+
161
+ expected =<<EOF
162
+ 0123456789012345678
163
+ 9 0123456
164
+ 0123456
165
+ EOF
166
+ wrapped = WordWrap.ww(text, 10, true)
167
+ wrapped.should eql expected
168
+ end
169
+
170
+ it "fits two paragraphs" do
171
+ text =<<EOF
172
+ Try-hard 3 wolf moon vinyl, authentic disrupt banh mi cliche fixie skateboard biodiesel chillwave before they sold out pop-up direct trade.
173
+
174
+ Mumblecore letterpress iPhone, Brooklyn pork belly distillery cray semiotics.
175
+ EOF
176
+
177
+ expected =<<EOF
178
+ Try-hard 3 wolf moon
179
+ vinyl, authentic
180
+ disrupt banh mi
181
+ cliche fixie
182
+ skateboard biodiesel
183
+ chillwave before
184
+ they sold out pop-up
185
+ direct trade.
186
+
187
+ Mumblecore
188
+ letterpress iPhone,
189
+ Brooklyn pork belly
190
+ distillery cray
191
+ semiotics.
192
+ EOF
193
+ wrapped = WordWrap.ww(text, 20, true)
194
+ wrapped.should eql expected
195
+ end
196
+
197
+ it "fits a partialy wrapped paragraph" do
198
+ text =<<EOF
199
+ Try-hard 3 wolf moon vinyl,
200
+ awesome
201
+ disrupt banh mi cliche
202
+ fixie skateboard biodiesel chillwave
203
+ before they sold out pop-up direct trade.
204
+ EOF
205
+
206
+ expected =<<EOF
207
+ Try-hard 3 wolf moon
208
+ vinyl, awesome
209
+ disrupt banh mi
210
+ cliche fixie
211
+ skateboard biodiesel
212
+ chillwave before
213
+ they sold out pop-up
214
+ direct trade.
215
+ EOF
216
+ wrapped = WordWrap.ww(text, 20, true)
217
+ wrapped.should eql expected
218
+ end
219
+
220
+ it "fitting keeps empty lines" do
221
+ text =<<EOF
222
+
223
+ word word word word word
224
+
225
+
226
+ word word word
227
+ word word reallylong word
228
+
229
+ extremelylonganduglyword
230
+
231
+ extremelylonganduglyword
232
+
233
+
234
+ EOF
235
+
236
+ expected =<<EOF
237
+
238
+ word word word word
239
+ word
240
+
241
+
242
+ word word word word
243
+ word reallylong word
244
+
245
+ extremelylonganduglyword
246
+
247
+ extremelylonganduglyword
248
+
249
+
250
+ EOF
251
+ wrapped = WordWrap.ww(text, 20, true)
252
+ wrapped.should eql expected
253
+ end
254
+
255
+ it "fit as a part of String interface" do
256
+ text =<<EOF
257
+ Try-hard 3 wolf moon vinyl.
258
+
259
+ Mumblecore letterpress iPhone.
260
+ EOF
261
+
262
+ expected =<<EOF
263
+ Try-hard 3 wolf moon
264
+ vinyl.
265
+
266
+ Mumblecore
267
+ letterpress iPhone.
268
+ EOF
269
+ text.fit(20).should eql expected
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'word_wrap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "word_wrap"
8
+ spec.version = WordWrap::VERSION
9
+ spec.authors = ["Radek Pazdera"]
10
+ spec.email = ["radek@pazdera.co.uk"]
11
+ spec.summary = %q{Simple tool for word-wrapping text}
12
+ spec.description = %q{This gem contains a very simple CLI tool for
13
+ word-wrapping plain-text files. As simple as
14
+ it gets. You can also use it in your Ruby
15
+ scripts - check out the sources for help.}
16
+ spec.homepage = "http://linuxwell.com/ww"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: word_wrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Radek Pazdera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |-
56
+ This gem contains a very simple CLI tool for
57
+ word-wrapping plain-text files. As simple as
58
+ it gets. You can also use it in your Ruby
59
+ scripts - check out the sources for help.
60
+ email:
61
+ - radek@pazdera.co.uk
62
+ executables:
63
+ - ww
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - .gitignore
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/ww
73
+ - lib/word_wrap.rb
74
+ - lib/word_wrap/version.rb
75
+ - lib/word_wrap/wrapper.rb
76
+ - spec/ww_spec.rb
77
+ - word_wrap.gemspec
78
+ homepage: http://linuxwell.com/ww
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple tool for word-wrapping text
102
+ test_files:
103
+ - spec/ww_spec.rb