format 0.0.1

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.
Files changed (5) hide show
  1. data/Rakefile +27 -0
  2. data/bin/format +46 -0
  3. data/lib/format.rb +129 -0
  4. data/tests/tc_format.rb +16 -0
  5. metadata +68 -0
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'rdoc/task'
2
+
3
+ task :default => [:documentation, :test, :install]
4
+
5
+ Rake::RDocTask.new(:documentation) do |rd|
6
+ rd.main = 'format.rb'
7
+ rd.rdoc_files.include('format.rb')
8
+ rd.options << '--all'
9
+ end
10
+
11
+ task :test do
12
+ if !system('cd tests && ruby tc_format.rb') then
13
+ exit 1
14
+ end
15
+ end
16
+
17
+ task :install do
18
+ cp 'format.rb', '/usr/local/bin'
19
+ end
20
+
21
+ task :clean do
22
+ rm Dir['tests/test.java.ugly*']
23
+ end
24
+
25
+ task :uninstall => [:clobber_documentation] do
26
+ rm '/usr/local/bin/format.rb'
27
+ end
data/bin/format ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'format'
4
+
5
+ case ARGV[0]
6
+ when 'uglify'
7
+ indent = nil
8
+ case ARGV[1]
9
+ when 'tab'
10
+ indent = "\t"
11
+ when 'two_spaces'
12
+ indent = ' '
13
+ when 'three_spaces'
14
+ indent = ' '
15
+ when 'four_spaces'
16
+ indent = ' '
17
+ when 'eight_spaces'
18
+ indent = ' '
19
+ else
20
+ puts 'Assuming that you want four-space indentation...'
21
+ indent = ' '
22
+ ARGV.insert(1, nil)
23
+ end
24
+ ARGV.shift(2)
25
+ ARGV.each {|arg|
26
+ Format.uglify(arg, :indentation => indent)
27
+ }
28
+ when 'prettify'
29
+ ARGV.shift
30
+ ARGV.each {|arg|
31
+ Format.prettify(arg)
32
+ }
33
+ when 'help'
34
+ puts "\nHelp Menu\n---------"
35
+ puts 'format.rb command [options] files'
36
+ puts "\nCommands\n--------"
37
+ puts 'uglify => brackets on separate line'
38
+ puts 'prettify => brackets on same line'
39
+ puts 'help => this menu'
40
+ puts "\nOptions (for 'uglify')\n----------------------"
41
+ puts 'two_spaces => two space indentation'
42
+ puts 'three_spaces => three space indentation'
43
+ puts 'four_spaces (default) => four space indentation'
44
+ puts 'eight_spaces => eight space indentation'
45
+ puts "tab => tab indentation\n\n"
46
+ end
data/lib/format.rb ADDED
@@ -0,0 +1,129 @@
1
+ =begin rdoc
2
+ ----------------------------------------------------
3
+ :section: Core Library Improvements
4
+ Improves the core library by adding useful methods.
5
+ ----------------------------------------------------
6
+ =end
7
+
8
+ class String
9
+ ##
10
+ # Determines whether the String is entirely blank.
11
+ #
12
+ def is_space
13
+ if /^[[:blank:]]+$/.match(self) then
14
+ return true
15
+ else
16
+ return false
17
+ end
18
+ end
19
+ end
20
+
21
+ =begin rdoc
22
+ ----------------------------------------------------
23
+ :section: Formatter
24
+ Allows for C-like source code to be formatted using
25
+ the two most popular bracketing styles.
26
+ ----------------------------------------------------
27
+ =end
28
+
29
+ class Format
30
+ private
31
+
32
+ @@keywords = ['else', 'while']
33
+
34
+ ##
35
+ # Get the leading whitespace (the indentation)
36
+ # from the input string.
37
+ #
38
+ def self.get_leading_space(str)
39
+ result = ''
40
+ str.each_char {|ch|
41
+ if ch.is_space then
42
+ result += ch
43
+ else
44
+ break
45
+ end
46
+ }
47
+ return result
48
+ end
49
+
50
+ public
51
+
52
+ ##
53
+ # Formats the input file according to the
54
+ # uglier (in my opinion) of the two styles.
55
+ #
56
+ def self.uglify(file, options={})
57
+ indent = options[:indentation]
58
+ options = nil
59
+ if indent == nil then
60
+ indent = ' '
61
+ end
62
+ output = File.new(file + '.ugly', 'w+')
63
+ IO.readlines(file).each {|line|
64
+ indentlevel = self.get_leading_space(line).scan(/#{indent}/).size
65
+ if /^[[:blank:]]*}.*$/.match(line) then
66
+ indentlevel.times do
67
+ output.print(indent)
68
+ end
69
+ output.puts('}')
70
+ line.sub!(/}[[:blank:]]*/, '')
71
+ if line.lstrip == '' then
72
+ next
73
+ end
74
+ end
75
+ if /[[:graph:]][[:blank:]]*{[[:blank:]]*$/.match(line) then
76
+ output.puts("#{line.rstrip.chop.rstrip}")
77
+ indentlevel.times do
78
+ output.print(indent)
79
+ end
80
+ output.puts('{')
81
+ else
82
+ output.puts(line)
83
+ end
84
+ }
85
+ output.close
86
+ end
87
+
88
+ ##
89
+ # Formats the input file according to the
90
+ # prettier (in my opinion) of the two styles.
91
+ #
92
+ def self.prettify(file)
93
+ output = File.new(file + '.pretty', 'w+')
94
+ prev_line = 'IGNORE THIS LINE'
95
+ data = IO.readlines(file)
96
+ index = 0
97
+ data.each {|line|
98
+ if /^[[:blank:]]*{[[:blank:]]*$/.match(line) and
99
+ !(/^[[:blank:]]*}[[:blank:]]*$/.match(prev_line)) then
100
+ prev_line = prev_line.rstrip + ' {'
101
+ line = 'IGNORE THIS LINE'
102
+ else
103
+ @@keywords.each {|key|
104
+ if /^[[:blank:]]*#{Regexp.quote(key)}([[:blank:]]|\()?.*$/.match(line) and
105
+ /^[[:blank:]]*}[[:blank:]]*$/.match(prev_line) then
106
+ prev_line = prev_line.rstrip + ' ' + line.lstrip
107
+ if index + 1 < data.length then
108
+ if /^[[:blank:]]*{[[:blank:]]*$/.match(data[index + 1]) then
109
+ prev_line = prev_line.rstrip + ' {'
110
+ data[index + 1] = 'IGNORE THIS LINE'
111
+ end
112
+ end
113
+ line = 'IGNORE THIS LINE'
114
+ break
115
+ end
116
+ }
117
+ end
118
+ if prev_line != 'IGNORE THIS LINE' then
119
+ output.puts(prev_line)
120
+ end
121
+ prev_line = line
122
+ index += 1
123
+ }
124
+ if prev_line != 'IGNORE THIS LINE' then
125
+ output.puts(prev_line)
126
+ end
127
+ output.close
128
+ end
129
+ end
@@ -0,0 +1,16 @@
1
+ require '../format'
2
+ require 'test/unit'
3
+
4
+ class TestFormat < Test::Unit::TestCase
5
+ def test_simple
6
+ Format.uglify('test.java', :indentation => ' ')
7
+ Format.prettify('test.java.ugly')
8
+ test = IO.read('test.java')
9
+ ugly = IO.read('test.java.ugly')
10
+ pretty = IO.read('test.java.ugly.pretty')
11
+ correct_ugly = IO.read('test.java.correct_ugly')
12
+ assert_equal(test, pretty)
13
+ assert_equal(ugly, correct_ugly)
14
+ # TODO: add (more random) tests here
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: format
3
+ version: !ruby/object:Gem::Version
4
+ hash: 856480538658449761
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Arcterus
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-07 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A formatter for the two most popular C language bracketing styles
22
+ email: arcterus@mail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - Rakefile
31
+ - lib/format.rb
32
+ - bin/format
33
+ - tests/tc_format.rb
34
+ homepage: http://rubygems.org/gems/format
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 2002549777813010636
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 2002549777813010636
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.12
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: See description
67
+ test_files:
68
+ - tests/tc_format.rb