indent_code 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +175 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/indent +33 -0
- data/bin/setup +7 -0
- data/indent_code.gemspec +26 -0
- data/lib/indent_code/cleaner.rb +91 -0
- data/lib/indent_code/indent_cpp.rb +97 -0
- data/lib/indent_code/version.rb +3 -0
- data/lib/indent_code.rb +147 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 39b0755154953912cd205b464cabed3adc2088f4
|
4
|
+
data.tar.gz: 01ccfb635a163344c296929e8b50014b22e36cbe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cfdec35c1bc2f09bf68b8b4a79d67af3d92e8bf3e5ce0b17febf4fd53496c9139e1b70b3bdf5eb3d8a9086294f46be8ed2832432a6623f0356f2213d9c4dd89f
|
7
|
+
data.tar.gz: 96818e42acfe06d8508f615b181d4bec515c6d3f869bff37868069d81609073ebbe4ddbc26971f7f13933824295adea45057e496043d8ed7053a46aad0c57886
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 anton
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
# IndentCode
|
2
|
+
|
3
|
+
This utilities with CLI (Command Line Interface) for normalize indentation in your source code.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'indent_code'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install indent_code
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
For details use option `-h` or `--help`.
|
24
|
+
File for normalize 'test.c':
|
25
|
+
```
|
26
|
+
#include <stdlib.h>
|
27
|
+
|
28
|
+
///Test function - 1. }
|
29
|
+
void test1 ( void )
|
30
|
+
{
|
31
|
+
switch(c) {
|
32
|
+
case 4:
|
33
|
+
c = "** \" { \" ";
|
34
|
+
break;
|
35
|
+
}
|
36
|
+
|
37
|
+
if (c)
|
38
|
+
{
|
39
|
+
return 1;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
#if EDEF
|
44
|
+
//Some text - 1.
|
45
|
+
# if 0
|
46
|
+
This is deleted text.
|
47
|
+
# endif
|
48
|
+
//Some text - 2.
|
49
|
+
#endif
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Test function - 2. {
|
53
|
+
*/
|
54
|
+
void test2 ( void )
|
55
|
+
{
|
56
|
+
int a;
|
57
|
+
/*
|
58
|
+
* Simple comment block.
|
59
|
+
* Check align into comments.
|
60
|
+
*/
|
61
|
+
{
|
62
|
+
a++;
|
63
|
+
}
|
64
|
+
return a;
|
65
|
+
}
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
Run `indent` gem:
|
70
|
+
|
71
|
+
$ indent test.c
|
72
|
+
|
73
|
+
and get follow result after normalize:
|
74
|
+
```
|
75
|
+
#include <stdlib.h>
|
76
|
+
|
77
|
+
///Test function - 1. }
|
78
|
+
void test1 ( void )
|
79
|
+
{
|
80
|
+
switch(c) {
|
81
|
+
case 4:
|
82
|
+
c = "** \" { \" ";
|
83
|
+
break;
|
84
|
+
}
|
85
|
+
|
86
|
+
if (c)
|
87
|
+
{
|
88
|
+
return 1;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
#if EDEF
|
93
|
+
//Some text - 1.
|
94
|
+
# if 0
|
95
|
+
This is deleted text.
|
96
|
+
# endif
|
97
|
+
//Some text - 2.
|
98
|
+
#endif
|
99
|
+
|
100
|
+
/**
|
101
|
+
* Test function - 2. {
|
102
|
+
*/
|
103
|
+
void test2 ( void )
|
104
|
+
{
|
105
|
+
int a;
|
106
|
+
/*
|
107
|
+
* Simple comment block.
|
108
|
+
* Check align into comments.
|
109
|
+
*/
|
110
|
+
{
|
111
|
+
a++;
|
112
|
+
}
|
113
|
+
return a;
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
Also you can set indent size and clean source code, by used `-c` option.
|
118
|
+
This option deleted code blocks framed is a: `#if 0` ... `#endif`.
|
119
|
+
|
120
|
+
$ indent test.c -c
|
121
|
+
```
|
122
|
+
#include <stdlib.h>
|
123
|
+
|
124
|
+
///Test function - 1. }
|
125
|
+
void test1 ( void )
|
126
|
+
{
|
127
|
+
switch(c) {
|
128
|
+
case 4:
|
129
|
+
c = "** \" { \" ";
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
|
133
|
+
if (c)
|
134
|
+
{
|
135
|
+
return 1;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
#if EDEF
|
140
|
+
//Some text - 1.
|
141
|
+
//Some text - 2.
|
142
|
+
#endif
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Test function - 2. {
|
146
|
+
*/
|
147
|
+
void test2 ( void )
|
148
|
+
{
|
149
|
+
int a;
|
150
|
+
/*
|
151
|
+
* Simple comment block.
|
152
|
+
* Check align into comments.
|
153
|
+
*/
|
154
|
+
{
|
155
|
+
a++;
|
156
|
+
}
|
157
|
+
return a;
|
158
|
+
}
|
159
|
+
```
|
160
|
+
|
161
|
+
## Development
|
162
|
+
|
163
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
164
|
+
|
165
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
166
|
+
|
167
|
+
## Contributing
|
168
|
+
|
169
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/indent_code. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
170
|
+
|
171
|
+
|
172
|
+
## License
|
173
|
+
|
174
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
175
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "indent_code"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/indent
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright (c) 2015 Anton S. Gerasimov (gera-gas)
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to
|
8
|
+
# deal in the Software without restriction, including without limitation the
|
9
|
+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10
|
+
# sell copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
22
|
+
# IN THE SOFTWARE.
|
23
|
+
#++
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'rubygems'
|
27
|
+
gem 'indent_code'
|
28
|
+
rescue LoadError
|
29
|
+
end
|
30
|
+
|
31
|
+
require 'indent_code'
|
32
|
+
|
33
|
+
IndentCode::Application.new.run
|
data/bin/setup
ADDED
data/indent_code.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'indent_code/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "indent_code"
|
8
|
+
spec.version = IndentCode::VERSION
|
9
|
+
spec.authors = ["anton"]
|
10
|
+
spec.email = ["gera_box@mail.ru"]
|
11
|
+
|
12
|
+
spec.summary = %q{This utilities with CLI (Command Line Interface) for normalize indentation in your source code.}
|
13
|
+
spec.description = %q{This gem use the 'iparser'gem as a parser engine. For details gem, use '--help' option.}
|
14
|
+
spec.homepage = "https://github.com/gera-gas/indent_code"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "bin"
|
19
|
+
spec.executables = "indent"
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
|
25
|
+
spec.add_dependency "iparser", "~> 1.1.6"
|
26
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'iparser'
|
2
|
+
|
3
|
+
module IndentCode
|
4
|
+
|
5
|
+
class Cleaner
|
6
|
+
include IndentCode
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@parser = Iparser::Machine.new
|
10
|
+
|
11
|
+
@st_idle = Iparser::State.new('idle')
|
12
|
+
@st_cline = Iparser::State.new('comment-line')
|
13
|
+
@st_cblock = Iparser::State.new('comment-block')
|
14
|
+
@st_string = Iparser::State.new('string')
|
15
|
+
@st_screen = Iparser::State.new('screen')
|
16
|
+
@st_cpp = Iparser::State.new('cpp')
|
17
|
+
|
18
|
+
@parser.addstate @st_idle
|
19
|
+
@parser.addstate @st_cpp
|
20
|
+
@parser.addstate @st_cline
|
21
|
+
@parser.addstate @st_cblock
|
22
|
+
@parser.addstate @st_string
|
23
|
+
@parser.addstate @st_screen
|
24
|
+
|
25
|
+
# Setting idle state.
|
26
|
+
@st_idle.ignore << ' '
|
27
|
+
@st_idle.branches << @parser.state_index( @st_cpp )
|
28
|
+
@st_idle.branches << @parser.state_index( @st_cline )
|
29
|
+
@st_idle.branches << @parser.state_index( @st_cblock )
|
30
|
+
@st_idle.branches << @parser.state_index( @st_string )
|
31
|
+
|
32
|
+
# Setting comment-line state.
|
33
|
+
@st_cline.entry << '/'
|
34
|
+
@st_cline.entry << '/'
|
35
|
+
@st_cline.leave << /[\n\r]/
|
36
|
+
|
37
|
+
# Setting comment-block state.
|
38
|
+
@st_cblock.entry << '/'
|
39
|
+
@st_cblock.entry << '*'
|
40
|
+
@st_cblock.leave << '*'
|
41
|
+
@st_cblock.leave << '/'
|
42
|
+
|
43
|
+
# Setting string state.
|
44
|
+
@st_string.entry << '"'
|
45
|
+
@st_string.leave << '"'
|
46
|
+
@st_string.branches << @parser.state_index( @st_screen )
|
47
|
+
|
48
|
+
# Setting screen state.
|
49
|
+
@st_screen.entry << '\\'
|
50
|
+
@st_screen.leave << /./
|
51
|
+
|
52
|
+
# Setting preprocessor handler state.
|
53
|
+
@st_cpp.entry << '#'
|
54
|
+
@st_cpp.entry << 'i'
|
55
|
+
@st_cpp.entry << 'f'
|
56
|
+
@st_cpp.entry << '0'
|
57
|
+
@st_cpp.leave << '#'
|
58
|
+
@st_cpp.leave << 'e'
|
59
|
+
@st_cpp.leave << 'n'
|
60
|
+
@st_cpp.leave << 'd'
|
61
|
+
@st_cpp.leave << 'i'
|
62
|
+
@st_cpp.leave << 'f'
|
63
|
+
@st_cpp.ignore << ' '
|
64
|
+
@st_cpp.branches << @parser.state_index( @st_cline )
|
65
|
+
@st_cpp.branches << @parser.state_index( @st_cblock )
|
66
|
+
@st_cpp.branches << @parser.state_index( @st_string )
|
67
|
+
|
68
|
+
@st_cpp.init( method( :cpp_init ) )
|
69
|
+
@st_cpp.fini( method( :cpp_fini ) )
|
70
|
+
|
71
|
+
@parser.prestart
|
72
|
+
end
|
73
|
+
|
74
|
+
def cpp_init ( ary )
|
75
|
+
@@context[:indent] = 'lock'; return nil
|
76
|
+
end
|
77
|
+
|
78
|
+
def cpp_fini ( ary )
|
79
|
+
@@context[:indent] = 'unlock'; return nil
|
80
|
+
end
|
81
|
+
|
82
|
+
private :cpp_init, :cpp_fini
|
83
|
+
|
84
|
+
# clean method handler.
|
85
|
+
def clean ( c )
|
86
|
+
return @parser.parse( c )
|
87
|
+
end
|
88
|
+
|
89
|
+
end # class Cleaner
|
90
|
+
|
91
|
+
end # module IndentCode
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'iparser'
|
2
|
+
|
3
|
+
module IndentCode
|
4
|
+
|
5
|
+
class IndentCPP
|
6
|
+
include IndentCode
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@parser = Iparser::Machine.new
|
10
|
+
|
11
|
+
@st_cline = Iparser::State.new('comment-line')
|
12
|
+
@st_cblock = Iparser::State.new('comment-block')
|
13
|
+
@st_string = Iparser::State.new('string')
|
14
|
+
@st_screen = Iparser::State.new('screen')
|
15
|
+
@st_scope = Iparser::State.new('scope')
|
16
|
+
|
17
|
+
@parser.addstate @st_scope
|
18
|
+
@parser.addstate @st_cline
|
19
|
+
@parser.addstate @st_cblock
|
20
|
+
@parser.addstate @st_string
|
21
|
+
@parser.addstate @st_screen
|
22
|
+
|
23
|
+
# Setting comment-line state.
|
24
|
+
@st_cline.entry << '/'
|
25
|
+
@st_cline.entry << '/'
|
26
|
+
@st_cline.leave << /[\n\r]/
|
27
|
+
|
28
|
+
# Setting comment-block state.
|
29
|
+
@st_cblock.entry << '/'
|
30
|
+
@st_cblock.entry << '*'
|
31
|
+
@st_cblock.leave << '*'
|
32
|
+
@st_cblock.leave << '/'
|
33
|
+
|
34
|
+
@st_cblock.handler( method( :cblock_handler ) )
|
35
|
+
@st_cblock.fini( method( :cblock_fini ) )
|
36
|
+
|
37
|
+
# Setting string state.
|
38
|
+
@st_string.entry << '"'
|
39
|
+
@st_string.leave << '"'
|
40
|
+
@st_string.branches << @parser.state_index( @st_screen )
|
41
|
+
|
42
|
+
# Setting screen state.
|
43
|
+
@st_screen.entry << '\\'
|
44
|
+
@st_screen.leave << /./
|
45
|
+
|
46
|
+
# Setting open state.
|
47
|
+
@st_scope.entry << '{'
|
48
|
+
@st_scope.leave << '}'
|
49
|
+
@st_scope.branches << @parser.state_index( @st_scope )
|
50
|
+
@st_scope.branches << @parser.state_index( @st_cline )
|
51
|
+
@st_scope.branches << @parser.state_index( @st_cblock )
|
52
|
+
@st_scope.branches << @parser.state_index( @st_string )
|
53
|
+
|
54
|
+
@st_scope.init( method( :scope_init ) )
|
55
|
+
@st_scope.fini( method( :scope_fini ) )
|
56
|
+
|
57
|
+
@parser.prestart
|
58
|
+
end
|
59
|
+
|
60
|
+
def cblock_handler ( c )
|
61
|
+
if c == ?\n then
|
62
|
+
if( (@@context[:linetxt].length > 0) && (@@context[:linetxt][0] == ?*) ) then
|
63
|
+
@@context[:linetxt] = ' ' + @@context[:linetxt]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
return nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def cblock_fini ( str )
|
70
|
+
@@context[:linetxt] = ' ' + @@context[:linetxt]
|
71
|
+
return nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def scope_init ( str )
|
75
|
+
@@context[:indent] += @@options[:tab]
|
76
|
+
return nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def scope_fini ( str )
|
80
|
+
if @@context[:indent] < @@options[:tab] then
|
81
|
+
@@context[:message] = 'Extra closing brace or missing a left brace'
|
82
|
+
return 'break'
|
83
|
+
end
|
84
|
+
@@context[:indent] -= @@options[:tab]
|
85
|
+
return nil
|
86
|
+
end
|
87
|
+
|
88
|
+
private :cblock_handler, :cblock_fini, :scope_init, :scope_fini
|
89
|
+
|
90
|
+
# indent method for this language.
|
91
|
+
def indent ( c )
|
92
|
+
return @parser.parse( c )
|
93
|
+
end
|
94
|
+
|
95
|
+
end # class IndentCPP
|
96
|
+
|
97
|
+
end # module IndentCode
|
data/lib/indent_code.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'indent_code/indent_cpp'
|
4
|
+
require 'indent_code/cleaner'
|
5
|
+
require 'indent_code/version'
|
6
|
+
|
7
|
+
module IndentCode
|
8
|
+
|
9
|
+
# command line options.
|
10
|
+
@@options = {
|
11
|
+
:tab => 4,
|
12
|
+
:lang => 'cpp',
|
13
|
+
:style => '',
|
14
|
+
:clean => false,
|
15
|
+
:delete => false,
|
16
|
+
}
|
17
|
+
|
18
|
+
# indent parse context.
|
19
|
+
@@context = {
|
20
|
+
:indent => 0,
|
21
|
+
:linenum => 1,
|
22
|
+
:linetxt => '',
|
23
|
+
:message => '',
|
24
|
+
}
|
25
|
+
|
26
|
+
# file descriptors.
|
27
|
+
@@files = { :fin => nil, :fout => nil, }
|
28
|
+
|
29
|
+
# error handler, display error message.
|
30
|
+
def self.error ( m = '' )
|
31
|
+
if m == '' then
|
32
|
+
puts "ERROR : #{@@context[:message]}."
|
33
|
+
puts "LINE : #{@@context[:linenum]}"
|
34
|
+
puts "TEXT : #{@@context[:linetxt]}"
|
35
|
+
else
|
36
|
+
puts "ERROR : #{m}."
|
37
|
+
end
|
38
|
+
exit
|
39
|
+
end
|
40
|
+
|
41
|
+
# Class for application control methods.
|
42
|
+
class Application
|
43
|
+
include IndentCode
|
44
|
+
|
45
|
+
def initialize
|
46
|
+
# parse command line options.
|
47
|
+
parser = OptionParser.new do |opts|
|
48
|
+
opts.banner = "Usage: indent file [options]"
|
49
|
+
|
50
|
+
opts.on('-i', '--indent size', 'Set indent size (TAB size): --indent 4') do |size|
|
51
|
+
@@options[:tab] = size.to_i;
|
52
|
+
end
|
53
|
+
opts.on('-c', '--clean', 'Delete code blocks from source code is framed as: #if 0 ... #endif') do
|
54
|
+
@@options[:clean] = true;
|
55
|
+
end
|
56
|
+
opts.on('-d', '--delete', 'Delete temporary files after work (*~ and *~~)') do
|
57
|
+
@@options[:delete] = true;
|
58
|
+
end
|
59
|
+
opts.on('-h', '--help', 'Displays Help') do
|
60
|
+
puts opts
|
61
|
+
exit
|
62
|
+
end
|
63
|
+
end
|
64
|
+
parser.parse!
|
65
|
+
|
66
|
+
# check command line arguments.
|
67
|
+
if ARGV.size == 0 then
|
68
|
+
IndentCode.error 'no input file, run with option -h or --help'
|
69
|
+
end
|
70
|
+
if !File.file?( ARGV[0] ) then
|
71
|
+
IndentCode.error 'unable to open file ' + ARGV[0]
|
72
|
+
end
|
73
|
+
exit if File.size( ARGV[0] ) == 0
|
74
|
+
end
|
75
|
+
|
76
|
+
# start indent handling.
|
77
|
+
def run
|
78
|
+
# create source file copy for backup.
|
79
|
+
FileUtils.copy( ARGV[0], ARGV[0] + ?~ )
|
80
|
+
|
81
|
+
parser = nil
|
82
|
+
# create indent parser.
|
83
|
+
case @@options[:lang]
|
84
|
+
when 'cpp'
|
85
|
+
parser = IndentCPP.new
|
86
|
+
end
|
87
|
+
|
88
|
+
f = File.new( ARGV[0], 'w' )
|
89
|
+
# parse input file.
|
90
|
+
File.open( ARGV[0] + ?~, 'r' ).each do |line|
|
91
|
+
@@context[:linetxt] = line.strip
|
92
|
+
indent = @@context[:indent]
|
93
|
+
|
94
|
+
# parse each char from string.
|
95
|
+
line.each_char do |c|
|
96
|
+
IndentCode.error() if !parser.indent( c )
|
97
|
+
end
|
98
|
+
|
99
|
+
indent = @@context[:indent] if @@context[:indent] < indent
|
100
|
+
spaces = ' ' * indent
|
101
|
+
|
102
|
+
f.puts spaces + @@context[:linetxt]
|
103
|
+
@@context[:linenum] = @@context[:linenum] + 1
|
104
|
+
end
|
105
|
+
f.close
|
106
|
+
|
107
|
+
# clean source code.
|
108
|
+
if @@options[:clean] then
|
109
|
+
# create source file copy for backup.
|
110
|
+
FileUtils.copy( ARGV[0], ARGV[0] + '~~' )
|
111
|
+
|
112
|
+
cleaner = IndentCode::Cleaner.new
|
113
|
+
@@context[:indent] = ''
|
114
|
+
|
115
|
+
f = File.new( ARGV[0], 'w' )
|
116
|
+
# parse input file.
|
117
|
+
File.open( ARGV[0] + '~~', 'r' ).each do |line|
|
118
|
+
@@context[:linetxt] = line
|
119
|
+
|
120
|
+
# parse each char from string.
|
121
|
+
line.each_char do |c|
|
122
|
+
IndentCode.error() if !cleaner.clean( c )
|
123
|
+
end
|
124
|
+
|
125
|
+
# check result of paring.
|
126
|
+
case @@context[:indent]
|
127
|
+
when ''
|
128
|
+
f.print @@context[:linetxt]
|
129
|
+
when 'lock'
|
130
|
+
when 'unlock'
|
131
|
+
@@context[:indent] = ''
|
132
|
+
end
|
133
|
+
|
134
|
+
@@context[:linenum] = @@context[:linenum] + 1
|
135
|
+
end
|
136
|
+
f.close
|
137
|
+
end
|
138
|
+
|
139
|
+
# delete temporary files.
|
140
|
+
if @@options[:delete] then
|
141
|
+
FileUtils.remove( ARGV[0] + '~' )
|
142
|
+
FileUtils.remove( ARGV[0] + '~~' )
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end # class Application
|
146
|
+
|
147
|
+
end # module IndentCode
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: indent_code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- anton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-11 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: iparser
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.1.6
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.1.6
|
55
|
+
description: This gem use the 'iparser'gem as a parser engine. For details gem, use
|
56
|
+
'--help' option.
|
57
|
+
email:
|
58
|
+
- gera_box@mail.ru
|
59
|
+
executables:
|
60
|
+
- indent
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/indent
|
72
|
+
- bin/setup
|
73
|
+
- indent_code.gemspec
|
74
|
+
- lib/indent_code.rb
|
75
|
+
- lib/indent_code/cleaner.rb
|
76
|
+
- lib/indent_code/indent_cpp.rb
|
77
|
+
- lib/indent_code/version.rb
|
78
|
+
homepage: https://github.com/gera-gas/indent_code
|
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.2.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: This utilities with CLI (Command Line Interface) for normalize indentation
|
102
|
+
in your source code.
|
103
|
+
test_files: []
|
104
|
+
has_rdoc:
|