linebreak 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/aef/linebreak.rb CHANGED
@@ -1,27 +1,149 @@
1
- # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
- #
3
- # This file is part of Linebreak.
4
- #
5
- # Linebreak is free software: you can redistribute it and/or modify
6
- # it under the terms of the GNU General Public License as published by
7
- # the Free Software Foundation, either version 3 of the License, or
8
- # (at your option) any later version.
9
- #
10
- # This program is distributed in the hope that it will be useful,
11
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- # GNU General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU General Public License
16
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Linebreak.
17
6
 
18
- # Namespace for projects of Alexander E. Fischer <aef@raxys.net>
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'set'
21
+ require 'aef/linebreak/version'
22
+
23
+ # Namespace for projects of Alexander E. Fischer <aef@raxys.net>.
19
24
  #
20
25
  # If you want to be able to simply type Example instead of Aef::Example to
21
26
  # address classes in this namespace simply write the following before using the
22
- # classes:
27
+ # classes.
23
28
  #
24
- # include Aef
29
+ # @example Including the namespace
30
+ # include Aef
31
+ # @author Alexander E. Fischer
25
32
  module Aef
26
- autoload :Linebreak, 'aef/linebreak/linebreak'
33
+
34
+ # Namespace for the linebreak library
35
+ module Linebreak
36
+
37
+ # Mapping table from symbol to actual linebreak sequence
38
+ #
39
+ # @private
40
+ BREAK_BY_SYSTEM = {
41
+ :unix => "\n",
42
+ :windows => "\r\n",
43
+ :mac => "\r"
44
+ }
45
+
46
+ # Mapping table from actual linebreak sequence to symbol
47
+ #
48
+ # @private
49
+ SYSTEM_BY_BREAK = BREAK_BY_SYSTEM.invert
50
+
51
+ # Regular expression for linebreak detection and extraction
52
+ #
53
+ # @private
54
+ BREAK_REGEXP = /(\r\n|[\r\n])/
55
+
56
+ # Detects encoding systems of a string.
57
+ #
58
+ # @param [String] input a String to be analysed
59
+ # @return [Set<Symbol>] the encoding systems present in the given String
60
+ def self.encodings(input)
61
+ if input.respond_to?(:to_s) then input = input.to_s
62
+ else raise ArgumentError, 'Input needs to be a string or must support to_s' end
63
+
64
+ occurences = Set.new
65
+
66
+ input.scan(BREAK_REGEXP).each do |linebreak|
67
+ occurences << SYSTEM_BY_BREAK[linebreak.first]
68
+ end
69
+
70
+ occurences
71
+ end
72
+
73
+ # Checks whether a string includes linebreaks of all the given encoding
74
+ # systems.
75
+ #
76
+ # @param [String] input a String to be analysed
77
+ # @param [Array<Symbol>] encodings one or more encoding systems
78
+ # @return [true, false] true if all of the given linebreak systems are
79
+ # present in the given String
80
+ def self.encoding?(input, *encodings)
81
+ systems = BREAK_BY_SYSTEM.keys
82
+
83
+ encodings.flatten!
84
+ encodings.each do |encoding|
85
+ unless systems.include?(encoding)
86
+ raise ArgumentError,
87
+ %{Invalid encoding system. Available systems: #{systems.join(', ')}. Arguments are expected as symbols or an array of symbols.}
88
+ end
89
+ end
90
+
91
+ Aef::Linebreak.encodings(input) == Set.new(encodings)
92
+ end
93
+
94
+ # Create a copy of a string with all the string's linebreaks replaced by
95
+ # linebreaks of a specific system or a given replacement.
96
+ #
97
+ # @overload encode(input, system)
98
+ # @param [String] input a String as conversion template
99
+ # @param [:unix, :windows, :mac] system a target linebreak system
100
+ #
101
+ # @overload encode(input, replacement)
102
+ # @param [String] input a String as conversion template
103
+ # @param [String] replacement a String to be the replacement for all
104
+ # linebreaks in the template
105
+ def self.encode(input, system_or_replacement = :unix)
106
+ if input.respond_to?(:to_s) then input = input.to_s
107
+ else raise ArgumentError, 'Input needs to be a string or must support to_s' end
108
+
109
+ input.gsub(BREAK_REGEXP,
110
+ BREAK_BY_SYSTEM[system_or_replacement] || system_or_replacement)
111
+ end
112
+
113
+ # Detects encoding systems of a string.
114
+ #
115
+ # This method is supposed to be used as a method of String.
116
+ #
117
+ # @return [Set<Symbol>] the encoding systems present in the String
118
+ def linebreak_encodings
119
+ Aef::Linebreak.encodings(self)
120
+ end
121
+
122
+ # Checks whether a string includes linebreaks of all the given encoding
123
+ # systems.
124
+ #
125
+ # This method is supposed to be used as a method of String.
126
+ #
127
+ # @param [Array<Symbol>] encodings one or more encoding systems
128
+ # @return [true, false] true if all of the given linebreak systems are
129
+ # present in the given String
130
+ def linebreak_encoding?(*encodings)
131
+ Aef::Linebreak.encoding?(self, encodings)
132
+ end
133
+
134
+ # Create a copy of a string with all the string's linebreaks replaced by
135
+ # linebreaks of a specific system or a given replacement.
136
+ #
137
+ # This method is supposed to be used as a method of String.
138
+ #
139
+ # @overload encode(system)
140
+ # @param [:unix, :windows, :mac] system a target linebreak system
141
+ #
142
+ # @overload encode(replacement)
143
+ # @param [String] replacement a String to be the replacement for all
144
+ # linebreaks in the template
145
+ def linebreak_encode(system_or_replacement = :unix)
146
+ Aef::Linebreak.encode(self, system_or_replacement)
147
+ end
148
+ end
27
149
  end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Linebreak.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ module Aef
21
+ module Linebreak
22
+
23
+ # The currently loaded library version
24
+ #
25
+ # Using Semantic Versioning (2.0.0-rc.1) rules
26
+ # @see http://semver.org/
27
+ VERSION = '2.0.0'.freeze
28
+
29
+ end
30
+ end
data/lib/linebreak.rb ADDED
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Linebreak.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'aef/linebreak'
21
+
22
+ String.send(:include, Aef::Linebreak)
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Linebreak.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'aef/linebreak'
data/linebreak.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Linebreak.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require File.expand_path('../lib/aef/linebreak/version', __FILE__)
21
+
22
+ Gem::Specification.new do |s|
23
+ s.name = 'linebreak'
24
+ s.version = Aef::Linebreak::VERSION.dup
25
+ s.authors = ['Alexander E. Fischer']
26
+ s.email = ['aef@raxys.net']
27
+ s.homepage = 'http://github.com/aef/linebreak'
28
+ s.license = 'ISC'
29
+ s.summary = 'Easy linebreak system conversion library'
30
+ s.description = <<-DESCRIPTION
31
+ Linebreak is a Ruby library for conversion of text between linebreak encoding
32
+ formats of unix, windows or mac.
33
+ DESCRIPTION
34
+
35
+ s.rubyforge_project = nil
36
+ s.has_rdoc = 'yard'
37
+ s.extra_rdoc_files = ['HISTORY.md', 'LICENSE.md']
38
+
39
+ s.files = `git ls-files`.lines.map(&:chomp)
40
+ s.test_files = `git ls-files -- {test,spec,features}/*`.lines.map(&:chomp)
41
+ s.executables = `git ls-files -- bin/*`.lines.map{|f| File.basename(f.chomp) }
42
+ s.require_paths = ["lib"]
43
+
44
+ s.required_ruby_version = '>= 1.8.7'
45
+
46
+ s.add_development_dependency('bundler', '~> 1.0.21')
47
+ s.add_development_dependency('rake', '~> 0.9.2')
48
+ s.add_development_dependency('rspec', '~> 2.8.0')
49
+ s.add_development_dependency('simplecov', '~> 0.6.1')
50
+ s.add_development_dependency('pry', '~> 0.9.8')
51
+ s.add_development_dependency('yard', '~> 0.7.5')
52
+
53
+ s.cert_chain = "#{ENV['GEM_CERT_CHAIN']}".split(':')
54
+ s.signing_key = ENV['GEM_SIGNING_KEY']
55
+ end
@@ -1,24 +1,34 @@
1
- # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
- #
3
- # This file is part of Linebreak.
4
- #
5
- # Linebreak is free software: you can redistribute it and/or modify it under the
6
- # terms of the GNU General Public License as published by the Free Software
7
- # Foundation, either version 3 of the License, or (at your option) any later
8
- # version.
9
- #
10
- # This program is distributed in the hope that it will be useful, but WITHOUT
11
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
- # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
- # details.
14
- #
15
- # You should have received a copy of the GNU General Public License along with
16
- # this program. If not, see <http://www.gnu.org/licenses/>.
17
-
18
- require 'spec/spec_helper'
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2009-2012
4
+
5
+ This file is part of Linebreak.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'spec_helper'
19
21
 
20
22
  describe Aef::Linebreak do
21
- include LinebreakSpecHelper
23
+ let(:unix_fixture) { "Abcdef\nAbcdef\nAbcdef" }
24
+ let(:windows_fixture) { "Abcdef\r\nAbcdef\r\nAbcdef" }
25
+ let(:mac_fixture) { "Abcdef\rAbcdef\rAbcdef" }
26
+ let(:custom_fixture) { "AbcdeffnordAbcdeffnordAbcdef" }
27
+ let(:none_fixture) { "AbcdefAbcdefAbcdef" }
28
+ let(:unix_windows_fixture) { unix_fixture + windows_fixture }
29
+ let(:windows_mac_fixture) { windows_fixture + mac_fixture }
30
+ let(:mac_unix_fixture) { mac_fixture + unix_fixture }
31
+ let(:unix_windows_mac_fixture) { unix_fixture + windows_fixture + mac_fixture }
22
32
 
23
33
  context 'library' do
24
34
  describe 'method "encode"' do
@@ -263,118 +273,4 @@ describe Aef::Linebreak do
263
273
 
264
274
  end
265
275
  end
266
-
267
- context 'commandline tool' do
268
- describe '"version" command' do
269
- it 'should display correct version and licensing information with the version argument' do
270
- `#{executable_path} version`.should eql(version_message)
271
- end
272
-
273
- it 'should display correct version and licensing information with the --version argument' do
274
- `#{executable_path} --version`.should eql(version_message)
275
- end
276
-
277
- it 'should display correct version and licensing information with the -v argument' do
278
- `#{executable_path} -v`.should eql(version_message)
279
- end
280
-
281
- it 'should display correct version and licensing information with the -V argument' do
282
- `#{executable_path} -V`.should eql(version_message)
283
- end
284
- end
285
-
286
- describe '"encode" command' do
287
- it 'should use unix as default format' do
288
- `#{executable_path} encode #{fixture_path('windows.txt')}`.should eql(unix_fixture)
289
- end
290
-
291
- it 'should accept -s option to specify output format' do
292
- `#{executable_path} encode -s mac #{fixture_path('unix.txt')}`.should eql(mac_fixture)
293
- end
294
-
295
- it 'should also accept --system option to specify output format' do
296
- `#{executable_path} encode --system windows #{fixture_path('mac.txt')}`.should eql(windows_fixture)
297
- end
298
-
299
- it 'should abort on invalid output formats' do
300
- POpen4.popen4("#{executable_path} encode -s fnord #{fixture_path('mac.txt')}") do |stdout, stderr, stdin, pid|
301
- stdout.read.should be_empty
302
- stderr.read.should_not be_empty
303
- end
304
- end
305
-
306
- it 'should accept LINEBREAK_SYSTEM environment variable to specify output format' do
307
- env(:LINEBREAK_SYSTEM => 'mac') do
308
- `#{executable_path} encode #{fixture_path('windows.txt')}`.should eql(mac_fixture)
309
- end
310
- end
311
-
312
- it 'should use output format specified with -s even if LINEBREAK_SYSTEM environment variable is set' do
313
- env(:LINEBREAK_SYSTEM => 'windows') do
314
- `#{executable_path} encode -s mac #{fixture_path('unix.txt')}`.should eql(mac_fixture)
315
- end
316
- end
317
-
318
- it 'should use a second argument as target file' do
319
- temp_file = Tempfile.open('linebreak_spec')
320
- location = Pathname(temp_file.path)
321
- temp_file.close
322
- location.delete
323
-
324
- `#{executable_path} encode --system windows #{fixture_path('unix.txt')} #{location}`.should be_empty
325
-
326
- location.read.should eql(windows_fixture)
327
- location.delete
328
- end
329
- end
330
-
331
- describe '"encodings" command' do
332
- it "should correctly detect the linebreak encodings of a file" do
333
- POpen4.popen4("#{executable_path} encodings #{fixture_path('mac.txt')}") do |stdout, stderr, stdin, pid|
334
- stdout.read.should eql("#{fixture_path('mac.txt')}: mac\n")
335
- end.exitstatus.should eql(0)
336
- end
337
-
338
- it "should correctly detect the linebreak encodings of multiple files" do
339
- files = %w{unix_windows.txt windows_mac.txt mac_unix.txt unix_windows_mac.txt}
340
- files = files.map{|file| fixture_path(file)}
341
-
342
- POpen4.popen4("#{executable_path} encodings #{files.join(' ')}") do |stdout, stderr, stdin, pid|
343
- output = stdout.read
344
- output.should include("#{files[0]}: unix,windows\n")
345
- output.should include("#{files[1]}: windows,mac\n")
346
- output.should include("#{files[2]}: unix,mac\n")
347
- output.should include("#{files[3]}: unix,windows,mac\n")
348
- end.exitstatus.should eql(0)
349
- end
350
-
351
- it "should correctly ensure the linebreak encodings of a valid file" do
352
- POpen4.popen4("#{executable_path} encodings --ensure unix #{fixture_path('unix.txt')}") do |stdout, stderr, stdin, pid|
353
- output = stdout.read
354
- output.should eql("#{fixture_path('unix.txt')}: unix\n")
355
- end.exitstatus.should eql(0)
356
- end
357
-
358
- it "should correctly ensure the linebreak encodings of an invalid file" do
359
- POpen4.popen4("#{executable_path} encodings --ensure mac #{fixture_path('unix_windows.txt')}") do |stdout, stderr, stdin, pid|
360
- output = stdout.read
361
- output.should eql("#{fixture_path('unix_windows.txt')}: unix,windows (failed)\n")
362
- end.exitstatus.should eql(1)
363
- end
364
-
365
- it "should correctly ensure the linebreak encodings of multiple files" do
366
- files = %w{unix_windows.txt windows_mac.txt mac_unix.txt unix_windows_mac.txt}
367
- files = files.map{|file| fixture_path(file)}
368
-
369
- POpen4.popen4("#{executable_path} encodings --ensure windows,unix #{files.join(' ')}") do |stdout, stderr, stdin, pid|
370
- output = stdout.read
371
- output.should include("#{files[0]}: unix,windows\n")
372
- output.should_not include("#{files[0]}: unix,windows (failed)\n")
373
- output.should include("#{files[1]}: windows,mac (failed)\n")
374
- output.should include("#{files[2]}: unix,mac (failed)\n")
375
- output.should include("#{files[3]}: unix,windows,mac (failed)\n")
376
- end.exitstatus.should eql(1)
377
- end
378
- end
379
- end
380
276
  end