aef-breakverter 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,53 @@
1
+ === 1.2.0 / 2009-04-05
2
+
3
+ * 2 major enhancements
4
+
5
+ * Namespaced into module Aef
6
+ * Instance method added for on demand inclusion into the String class
7
+
8
+ * 5 minor enhancements
9
+
10
+ * Better metadata for gem
11
+ * Test refactoring
12
+ * Minor refactorings
13
+ * Improved documentation
14
+ * Added test for --version command
15
+
16
+ * 1 minor bugfix
17
+
18
+ * Fixed some bugs related to Ruby 1.8.6
19
+
20
+ === 1.1.1 / 2009-03-08
21
+
22
+ * minor enhancement
23
+
24
+ * Added a --version command to output version and licensing information
25
+ * Some test refactoring
26
+ * Some more comments
27
+
28
+ === 1.1.0 / 2009-03-01
29
+
30
+ * 2 major enhancements
31
+
32
+ * Added automated tests for the commandline tool
33
+ * Tested compatibility with ruby 1.9.1
34
+
35
+ * 1 minor enhancement
36
+
37
+ * Added support for custom string linebreak encodings in library
38
+
39
+ === 1.0.1 / 2009-02-24
40
+
41
+ * 1 minor enhancement
42
+
43
+ * Now tested on Windows XP
44
+
45
+ * 1 minor bugfix
46
+
47
+ * Bugfixes for commandline use
48
+
49
+ === 1.0.0 / 2009-02-24
50
+
51
+ * 1 major enhancement
52
+
53
+ * Birthday!
data/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ COPYING.txt
5
+ Rakefile
6
+ bin/breakverter
7
+ lib/breakverter.rb
8
+ lib/breakverter/breakverter.rb
9
+ spec/breakverter_spec.rb
10
+ spec/fixtures/unix.txt
11
+ spec/fixtures/windows.txt
12
+ spec/fixtures/mac.txt
data/README.rdoc ADDED
@@ -0,0 +1,161 @@
1
+ = BreakVerter
2
+
3
+ * Project: https://rubyforge.org/projects/aef/
4
+ * RDoc: http://aef.rubyforge.org/breakverter/
5
+ * Github: http://github.com/aef/breakverter/
6
+
7
+ == DESCRIPTION:
8
+
9
+ BreakVerter is a Ruby library and commandline tool for conversion of text
10
+ between linebreak encoding formats of unix, windows or mac.
11
+
12
+ == FEATURES/PROBLEMS:
13
+
14
+ * Usable as library and commandline tool
15
+ * Tested and fully working on:
16
+ * Ubuntu Linux 8.10 i386_64 (Ruby 1.8.7 and 1.9.1p0)
17
+ * Debian GNU/Linux 4.0 i386 (Ruby 1.8.6)
18
+ * Windows XP i386 (Ruby 1.8.6)
19
+ * The commandline tool doesn't work with Ruby 1.9.x because the user-choices gem
20
+ is not yet updated. A patch is available here:
21
+ https://rubyforge.org/tracker/index.php?func=detail&aid=24307&group_id=4192&atid=16176
22
+
23
+ == SYNOPSIS:
24
+
25
+ === Commandline
26
+
27
+ The default output encoding is unix. You can also choose mac and windows.
28
+
29
+ breakverter -o windows unix.txt windows.txt
30
+
31
+ If no target file is specified the output will be sent to STDOUT.
32
+
33
+ breakverter -o windows mac.txt > windows.txt
34
+
35
+ You can set the default with the environment variable BREAKVERTER_OUTPUT.
36
+
37
+ export BREAKVERTER_OUTPUT=mac
38
+
39
+ breakverter windows.txt mac.txt
40
+
41
+ === Library
42
+
43
+ First of all you have to load the BreakVerter gem:
44
+
45
+ require 'breakverter'
46
+
47
+ You can put strings or objects responding to to_s into the convert method and
48
+ optionally define a target linebreak encoding. The default encoding is :unix.
49
+ You can also choose :mac and :windows. Notice that the :mac encoding is
50
+ deprecated. Modern Apple machines also use :unix linebreak encoding.
51
+
52
+ windows_string = "Abcdef\r\nAbcdef\r\nAbcdef"
53
+
54
+ Aef::BreakVerter.convert(windows_string, :unix) #=> "Abcdef\nAbcdef\nAbcdef"
55
+
56
+ Alternatively you could include BreakVerter into the String class and use it in
57
+ the following way:
58
+
59
+ class String
60
+ include Aef::BreakVerter
61
+ end
62
+
63
+ "Abcdef\nAbcdef\nAbcdef".linebreaks(:mac) #=> "Abcdef\rAbcdef\rAbcdef"
64
+
65
+ == REQUIREMENTS:
66
+
67
+ * rubygems
68
+
69
+ === Additional for commandline
70
+ * user-choices
71
+
72
+ === Additional for automated testing
73
+ * hoe
74
+ * rspec
75
+ * sys-uname
76
+
77
+ == INSTALL:
78
+
79
+ === Normal
80
+
81
+ gem install breakverter
82
+
83
+ Additionally for the commandline tool:
84
+
85
+ gem install user-choices
86
+
87
+ === High security (recommended)
88
+
89
+ There is a high security installation option available through rubygems. It is
90
+ highly recommended over the normal installation, although it may be a bit less
91
+ comfortable. To use the installation method, you will need my public key, which
92
+ I use for cryptographic signatures on all my gems. You can find the public key
93
+ and more detailed verification information in the aef-certificates section of my
94
+ rubyforge project[https://rubyforge.org/frs/?group_id=7890&release_id=31749]
95
+
96
+ Add the key to your rubygems' trusted certificates by the following command:
97
+
98
+ gem cert --add aef.pem
99
+
100
+ Now you can install the gem while automatically verifying it's signature by the
101
+ following command:
102
+
103
+ gem install breakverter --ignore-dependencies -P HighSecurity
104
+
105
+ Please notice that you will need other keys for dependent libraries, so you may
106
+ have to install dependencies manually.
107
+
108
+ === Automated testing
109
+
110
+ You can test this package through rspec on your system. First find the path
111
+ where the gem was installed to:
112
+
113
+ gem which breakverter
114
+
115
+ Go into the root directory of the installed gem and run the following command
116
+ to start the test runner:
117
+
118
+ rake spec
119
+
120
+ On Windows systems you have to run the following instead:
121
+
122
+ spec spec/**/*_spec.rb
123
+
124
+ If something goes wrong you should be noticed through failing examples.
125
+
126
+ == DEVELOPMENT:
127
+
128
+ This software is developed in the source code management system git hosted
129
+ at github.com. You can download the most recent sourcecode through the following
130
+ command:
131
+
132
+ git clone git://github.com/aef/breakverter.git
133
+
134
+ Help on making this software better is always very appreciated. If you want your
135
+ changes to be included in the official release, please send me a patch through
136
+ the project's tracker[https://rubyforge.org/tracker/?group_id=7890] at
137
+ rubyforge.org. You can generate a patch-file by the following command:
138
+
139
+ git diff > patch.diff
140
+
141
+ Please make sure to write tests for your changes and notice that I can't promise
142
+ to include your changes before reviewing them.
143
+
144
+ == LICENSE:
145
+
146
+ Copyright 2009 Alexander E. Fischer <aef@raxys.net>
147
+
148
+ This file is part of BreakVerter.
149
+
150
+ BreakVerter is free software: you can redistribute it and/or modify
151
+ it under the terms of the GNU General Public License as published by
152
+ the Free Software Foundation, either version 3 of the License, or
153
+ (at your option) any later version.
154
+
155
+ This program is distributed in the hope that it will be useful,
156
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
157
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
158
+ GNU General Public License for more details.
159
+
160
+ You should have received a copy of the GNU General Public License
161
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/breakverter.rb'
6
+
7
+ Hoe.new('breakverter', Aef::BreakVerter::VERSION) do |p|
8
+ p.rubyforge_name = 'aef'
9
+ p.developer('Alexander E. Fischer', 'aef@raxys.net')
10
+ p.extra_dev_deps = %w{user-choices rspec sys-uname}
11
+ p.url = 'https://rubyforge.org/projects/aef/'
12
+ p.readme_file = 'README.rdoc'
13
+ p.extra_rdoc_files = %w{README.rdoc}
14
+ p.spec_extras = {
15
+ :rdoc_options => ['--main', 'README.rdoc', '--inline-source', '--line-numbers', '--title', 'BreakVerter']
16
+ }
17
+ end
18
+
19
+ # vim: syntax=Ruby
data/bin/breakverter ADDED
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
4
+ #
5
+ # This file is part of BreakVerter.
6
+ #
7
+ # BreakVerter is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ # TODO: If user-choices patch gets accepted, use :one_way => true for --version
21
+ # TODO: If user-choices patch gets accepted, use :upcase for environment variables
22
+
23
+ # If library is not locally accessible, use gem to include it.
24
+ begin
25
+ require 'lib/breakverter'
26
+ rescue LoadError
27
+ require 'rubygems'
28
+ require 'breakverter'
29
+ end
30
+
31
+ # User friendly message if user-choices is not available
32
+ begin
33
+ require 'user-choices'
34
+ rescue LoadError
35
+ warn "This command needs the user-choices gem to be installed.\n\nSolution: gem install user-choices"; exit false
36
+ end
37
+
38
+ # Application class for commandline interface
39
+ class Aef::BreakVerter::Application < UserChoices::Command
40
+ include UserChoices
41
+
42
+ # Prepare configuration sources
43
+ def add_sources(builder)
44
+ builder.add_source(
45
+ CommandLineSource, :usage,
46
+ "Usage: #$PROGRAM_NAME [options] input-file [output-file]\n\n",
47
+ "Convert a file with any linebreak encoding to a specific linebreak encoding.\n",
48
+ "Use the environment variable BREAKVERTER_OUTPUT to specify a default output encoding.\n"
49
+ )
50
+
51
+ builder.add_source(EnvironmentSource, :mapping,
52
+ :output => 'BREAKVERTER_OUTPUT'
53
+ )
54
+ end
55
+
56
+ # Define configuration options
57
+ def add_choices(builder)
58
+ output_values = Aef::BreakVerter::BREAKS.keys.map{|key| key.to_s}
59
+ builder.add_choice(:output, :default => 'unix', :type => output_values) do |cli|
60
+ cli.uses_option('-o', '--output PLATFORM',
61
+ "Output formatting. Possible settings: #{Aef::BreakVerter::BREAKS.keys.join(', ')}")
62
+ end
63
+
64
+ builder.add_choice(:version, :default => false, :type => :boolean) do |cli|
65
+ cli.uses_switch('-v', '--version', 'Display version and licensing information')
66
+ end
67
+
68
+ builder.add_choice(:filenames, :length => 0..2) {|cli| cli.uses_arglist}
69
+ end
70
+
71
+ # Manual option post processing
72
+ def postprocess_user_choices
73
+ version if @user_choices[:version]
74
+
75
+ @user_choices[:input_filename] = @user_choices[:filenames][0]
76
+ @user_choices[:output_filename] = @user_choices[:filenames][1] || '-'
77
+ end
78
+
79
+ # Version and licensing information output
80
+ def version
81
+ name = 'BreakVerter'
82
+ puts "#{name} #{Aef::BreakVerter::VERSION}"
83
+ puts
84
+ puts 'Project: https://rubyforge.org/projects/aef/'
85
+ puts "RDoc: http://aef.rubyforge.org/#{name.downcase}/"
86
+ puts "Github: http://github.com/aef/#{name.downcase}/"
87
+ puts
88
+ puts 'Copyright 2009 Alexander E. Fischer <aef@raxys.net>'
89
+ # Read and print licensing information from the top of this file
90
+ if Gem::Version.new(RUBY_VERSION) <= Gem::Version.new('1.8.6')
91
+ puts File.read(__FILE__).map{|line| line[2..-1]}[5..17]
92
+ else
93
+ puts File.read(__FILE__).lines.map{|line| line[2..-1]}[5..17]
94
+ end
95
+ exit
96
+ end
97
+
98
+ # Main program
99
+ def execute
100
+ unless @user_choices[:input_filename]
101
+ warn 'No input file given'; exit false
102
+ end
103
+
104
+ unless File.exists?(@user_choices[:input_filename])
105
+ warn "Input file not found (#{@user_choices[:input_filename]})"; exit false
106
+ end
107
+
108
+ unless File.readable?(@user_choices[:input_filename])
109
+ warn "Input file access denied (#{@user_choices[:input_filename]})"; exit false
110
+ end
111
+
112
+ result = Aef::BreakVerter.convert(File.read(@user_choices[:input_filename]), @user_choices[:output].to_sym)
113
+
114
+ if @user_choices[:output_filename] == '-'
115
+ puts result
116
+ else
117
+ @user_choices[:output_filename]
118
+
119
+ unless File.writable?(File.dirname(@user_choices[:output_filename]))
120
+ warn "Output file access denied (#{@user_choices[:output_filename]})"; exit false
121
+ end
122
+
123
+ File.open(@user_choices[:output_filename], 'w') {|f| f.write(result)}
124
+ end
125
+ end
126
+ end
127
+
128
+ S4tUtils.with_pleasant_exceptions {Aef::BreakVerter::Application.new.execute}
@@ -0,0 +1,29 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of BreakVerter.
4
+ #
5
+ # BreakVerter 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/>.
17
+
18
+ # Namespace for projects of Alexander E. Fischer <aef@raxys.net>
19
+ #
20
+ # If you want to be able to simply type Example instead of Aef::Example to
21
+ # address classes in this namespace simply write the following before using the
22
+ # classes:
23
+ #
24
+ # include Aef
25
+ module Aef
26
+
27
+ end
28
+
29
+ require File.join(File.dirname(__FILE__), 'breakverter/breakverter')
@@ -0,0 +1,45 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of BreakVerter.
4
+ #
5
+ # BreakVerter 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/>.
17
+
18
+ # BreakVerter is Ruby library and commandline tool for conversion of text
19
+ # between linebreak encoding formats of unix, windows or mac.
20
+ module Aef::BreakVerter
21
+ VERSION = '1.2.0'
22
+
23
+ BREAKS = {
24
+ :unix => "\n",
25
+ :windows => "\r\n",
26
+ :mac => "\r"
27
+ }
28
+
29
+ # Convert a file with any linebreak encoding to a specific linebreak encoding.
30
+ #
31
+ # If given output_encoding is not a key of BREAKS, all linebreaks are replaced
32
+ # with output_encoding's content itself.
33
+ def self.convert(input, output_encoding = :unix)
34
+ if input.respond_to?(:to_s) then input = input.to_s
35
+ else raise 'Input has to be a string or must support to_s' end
36
+
37
+ input.gsub(/\r(?:\n)?|\n/, BREAKS[output_encoding] || output_encoding)
38
+ end
39
+
40
+ # Shortcut for direct receiver conversion, for example if BreakVerter is
41
+ # included into the string class
42
+ def linebreaks(output_encoding = :unix)
43
+ Aef::BreakVerter.convert(self, output_encoding)
44
+ end
45
+ end