Indentinator 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 (4) hide show
  1. data/bin/indentinator +46 -0
  2. data/lib/indentinator.rb +65 -0
  3. data/test/tests.rb +111 -0
  4. metadata +69 -0
data/bin/indentinator ADDED
@@ -0,0 +1,46 @@
1
+ #! /usr/bin/env ruby
2
+ require 'getopt/std'
3
+ require 'indentinator'
4
+ include Indentinator
5
+
6
+ opts = Getopt::Std.getopts('c:vV')
7
+ @to_amount = opts['c']
8
+ @very_verbose = opts['V']
9
+ @verbose = opts['v'] or @very_verbose
10
+ @files = ARGV
11
+ if @files.size == 0
12
+ puts "Usage:
13
+
14
+ indentinator [-c INDENT_AMOUNT] <file> [<file2>...]
15
+
16
+ Options:
17
+
18
+ -c Modify the indentation amount to INDENT_AMOUNT
19
+ -v Verbose output
20
+ -V Very verbose output
21
+
22
+ "
23
+ exit(0)
24
+ end
25
+
26
+ @files.each do |path|
27
+ begin
28
+ File.open(path) do |file|
29
+ lines = file.read.split("\n")
30
+ amount = indent_amount(lines)
31
+ if @to_amount.nil?
32
+ if amount.nil?
33
+ puts "Failed for #{path}. Is it binary?"
34
+ elsif amount == 0
35
+ puts "#{path} has no indentation."
36
+ else
37
+ puts "#{path} uses #{amount} spaces."
38
+ end
39
+ else
40
+ puts convert_lines(lines, amount, @to_amount).join("\n")
41
+ end
42
+ end
43
+ rescue Errno::ENOENT
44
+ puts "Can't open #{path}"
45
+ end
46
+ end
@@ -0,0 +1,65 @@
1
+ module Indentinator
2
+
3
+ def indent_amount(lines)
4
+ curr = 0
5
+ diffs = {}
6
+ lines.each_with_index do |line, idx|
7
+ spaces = line.match(/^( *)/)[0]
8
+ diff = spaces.size - curr
9
+ if @very_verbose
10
+ puts "#{'%- 5d' % (idx + 1)}: #{diff}"
11
+ end
12
+ if diff > 0
13
+ diffs[diff] ||= 0
14
+ diffs[diff] = diffs[diff] + 1
15
+ end
16
+ curr = spaces.size
17
+ end
18
+ if diffs.size > 0
19
+ sorted = diffs.sort{|a, b| b[1] - a[1]}
20
+ if @verbose
21
+ puts sorted.map{|p| ('%- 5d=> ' % p[0]) + p[1].to_s}.join("\n")
22
+ end
23
+ sorted[0][0]
24
+ else
25
+ 0
26
+ end
27
+ rescue ArgumentError
28
+ # Probably a binary file
29
+ nil
30
+ end
31
+
32
+ def convert_indentation(text, from, to)
33
+ convert_lines(text.split("\n"), from, to).join("\n")
34
+ end
35
+
36
+ def convert_lines(lines, from, to)
37
+ ratio = to.to_f / from
38
+ curr_indent_org = 0
39
+ curr_indent = 0
40
+ lines.map do |line|
41
+ if line.empty?
42
+ line
43
+ else
44
+ m = line.match(/^( *)(.*)$/)
45
+ spaces = m[1]
46
+ rest = m[2]
47
+ indent = spaces.size - curr_indent_org
48
+ curr_indent_org = spaces.size
49
+ if indent == from # standard indent
50
+ curr_indent += (indent * ratio).to_i
51
+ elsif indent > 0 # non-standard indent
52
+ curr_indent += indent
53
+ elsif indent == 0 # no indent
54
+
55
+ else # de-indent
56
+ # reset back to use original indentation
57
+ curr_indent = (curr_indent_org * ratio).round
58
+ end
59
+ ' ' * curr_indent + rest
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+
data/test/tests.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'rspec'
2
+ require 'indentinator.rb'
3
+ describe 'convert_file' do
4
+ it "converts basic indentation" do
5
+ text = "
6
+ line 1
7
+ line 2
8
+ line 3
9
+ line 4"
10
+ convert_indentation(text, 2, 4).should == "
11
+ line 1
12
+ line 2
13
+ line 3
14
+ line 4"
15
+ end
16
+
17
+ it "retains non-standard indentation when not multiple
18
+ of org indentation amount" do
19
+ text = "
20
+ line 1
21
+ line 2"
22
+ convert_indentation(text, 2, 4).should == text
23
+ end
24
+
25
+ it "retains non-standard indentation even when is
26
+ multiple of org indentation amount" do
27
+ text = "
28
+ line 1
29
+ line 2
30
+ line 3
31
+ line 2"
32
+ convert_indentation(text, 2, 4).should == "
33
+ line 1
34
+ line 2
35
+ line 3
36
+ line 2"
37
+ end
38
+
39
+ it "retains line-up of non-standard indented lines" do
40
+ text = "
41
+ line 1
42
+ line 2
43
+ line 3
44
+ line 4"
45
+ convert_indentation(text, 4, 2).should == "
46
+ line 1
47
+ line 2
48
+ line 3
49
+ line 4"
50
+ end
51
+
52
+ it "retains line-up of non-standard indented lines(ex 2)" do
53
+ text = "
54
+ line 0
55
+ line 1
56
+ line 2
57
+ line 3
58
+ line 4"
59
+ convert_indentation(text, 4, 2).should == "
60
+ line 0
61
+ line 1
62
+ line 2
63
+ line 3
64
+ line 4"
65
+ end
66
+
67
+ it "should de-indent properly from non-standard indent" do
68
+ text = "
69
+ line 0
70
+ line 2
71
+ line 3
72
+ line 4"
73
+ convert_indentation(text, 2, 4).should == "
74
+ line 0
75
+ line 2
76
+ line 3
77
+ line 4"
78
+ end
79
+
80
+ it "should ignore empty lines" do
81
+ text = "
82
+ line 0
83
+ line 1
84
+ line 2
85
+ line 3
86
+
87
+ line 4"
88
+ convert_indentation(text, 4, 2).should == "
89
+ line 0
90
+ line 1
91
+ line 2
92
+ line 3
93
+
94
+ line 4"
95
+ end
96
+
97
+ it "should de-indent property(ex 2)" do
98
+ text = "
99
+ line 1
100
+ line 2
101
+ line 3
102
+
103
+ line 4"
104
+ convert_indentation(text, 4, 2).should == "
105
+ line 1
106
+ line 2
107
+ line 3
108
+
109
+ line 4"
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Indentinator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Toby Ho
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-24 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: airportyh@gmail.com
24
+ executables:
25
+ - indentinator
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/indentinator.rb
32
+ - bin/indentinator
33
+ - test/tests.rb
34
+ has_rdoc: true
35
+ homepage: http://tobyho.com
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.6.2
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Indentinator is a Ruby script that inspects and changes the indentation in your source files.
68
+ test_files:
69
+ - test/tests.rb