indent_code 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76f657306f4257bd86b32c566b81791117c366f9
4
- data.tar.gz: 28765cef27b123bf0dfea985b33d3c03514807dd
3
+ metadata.gz: 588d2cd7b2e6265a93f6b6cbdb634032af59caaa
4
+ data.tar.gz: ff28b2e8cb7e759ffccc22b5c9084e7afbc0d13d
5
5
  SHA512:
6
- metadata.gz: 3df88d5004a19450f39354beb13ef7ea6fea9fca189d9afc5f823862e03a5133257a2d1a004beb1692ea5999769b125b1aae4f524f6815e789efa15f6344e748
7
- data.tar.gz: 19bbad14bdb21efed72749f9aedc5026c28149c236df9cd80d491f6c7921c95472baeab7da5572e32ca473ba70005a9d313ceb6fcbd09be641410f08d1ebd169
6
+ metadata.gz: fe12819b1727874feb223984bf5ad063969003f29322c0cc88513f05228f221a299f50e1ebf3787d2d4ba60c3c21cddba7f2d7c20663966bcc59a5f1944fb72e
7
+ data.tar.gz: 1d8dea8d764b18747384e0969916f13a7cd47d2ac8313fe2844dc98a0305d3a20dc32db5b21d7d0943ccf1a93f313a7d022c8eac1741a3337a59a64883152145
@@ -0,0 +1,120 @@
1
+ require 'iparser'
2
+
3
+ module IndentCode
4
+
5
+ # Class for application control methods.
6
+ class Application
7
+ include IndentCode
8
+
9
+ def initialize
10
+ # parse command line options.
11
+ parser = OptionParser.new do |opts|
12
+ opts.banner = "Usage: indent file [options]"
13
+
14
+ opts.on('-i', '--indent size', 'Set indent size (TAB size): --indent 4.') do |size|
15
+ @@options[:tab] = size.to_i;
16
+ end
17
+ opts.on('-c', '--clean', 'Delete code blocks from source code is framed as: #if 0 ... #endif.') do
18
+ @@options[:clean] = true;
19
+ end
20
+ opts.on('-d', '--delete', 'Delete temporary files after work: ...~ and ...~~ files.') do
21
+ @@options[:delete] = true;
22
+ end
23
+ opts.on('-V', '--version', 'Display the program version.') do
24
+ puts "indent, version #{IndentCode::VERSION}"
25
+ exit
26
+ end
27
+ opts.on('-h', '--help', 'Displays Help') do
28
+ puts opts
29
+ exit
30
+ end
31
+ end
32
+ parser.parse!
33
+
34
+ # check command line arguments.
35
+ if ARGV.size == 0 then
36
+ IndentCode.error 'no input file, run with option -h or --help'
37
+ end
38
+ if !File.file?( ARGV[0] ) then
39
+ IndentCode.error 'unable to open file ' + ARGV[0]
40
+ end
41
+ exit if File.size( ARGV[0] ) == 0
42
+ end
43
+
44
+
45
+ # start indent handling.
46
+ def run
47
+ # create source file copy for backup.
48
+ FileUtils.copy( ARGV[0], ARGV[0] + ?~ )
49
+
50
+ parser = nil
51
+ # create indent parser.
52
+ case @@options[:lang]
53
+ when 'cpp'
54
+ parser = IndentCPP.new
55
+ else
56
+ IndentCode.error 'unknown options - ' + @@options[:lang]
57
+ end
58
+
59
+ f = File.new( ARGV[0], 'w' )
60
+ # parse input file.
61
+ File.open( ARGV[0] + ?~, 'r' ).each do |line|
62
+ @@context[:linetxt] = line.strip
63
+ indent = @@context[:indent]
64
+
65
+ # parse each char from string.
66
+ line.each_char do |c|
67
+ IndentCode.error() if !parser.indent( c )
68
+ end
69
+
70
+ indent = @@context[:indent] if @@context[:indent] < indent
71
+ spaces = ' ' * indent
72
+
73
+ f.puts spaces + @@context[:linetxt]
74
+ @@context[:linenum] = @@context[:linenum] + 1
75
+ end
76
+ f.close
77
+
78
+ # clean source code.
79
+ if @@options[:clean] then
80
+ # create source file copy for backup.
81
+ FileUtils.copy( ARGV[0], ARGV[0] + '~~' )
82
+
83
+ cleaner = IndentCode::Cleaner.new
84
+ @@context[:indent] = ''
85
+
86
+ f = File.new( ARGV[0], 'w' )
87
+ # parse input file.
88
+ File.open( ARGV[0] + '~~', 'r' ).each do |line|
89
+ @@context[:linetxt] = line
90
+
91
+ # parse each char from string.
92
+ line.each_char do |c|
93
+ IndentCode.error() if !cleaner.clean( c )
94
+ end
95
+
96
+ # check result of paring.
97
+ case @@context[:indent]
98
+ when ''
99
+ f.print @@context[:linetxt]
100
+ when 'lock'
101
+ when 'unlock'
102
+ @@context[:indent] = ''
103
+ end
104
+
105
+ @@context[:linenum] = @@context[:linenum] + 1
106
+ end
107
+ f.close
108
+ end
109
+
110
+
111
+ # delete temporary files.
112
+ if @@options[:delete] then
113
+ FileUtils.remove( ARGV[0] + '~' )
114
+ FileUtils.remove( ARGV[0] + '~~' )
115
+ end
116
+ end
117
+
118
+ end # class Application
119
+
120
+ end # module IndentCode
@@ -10,6 +10,7 @@ module IndentCode
10
10
 
11
11
  @st_cline = Iparser::State.new('comment-line')
12
12
  @st_cblock = Iparser::State.new('comment-block')
13
+ @st_char = Iparser::State.new('char')
13
14
  @st_string = Iparser::State.new('string')
14
15
  @st_screen = Iparser::State.new('screen')
15
16
  @st_scope = Iparser::State.new('scope')
@@ -17,6 +18,7 @@ module IndentCode
17
18
  @parser.addstate @st_scope
18
19
  @parser.addstate @st_cline
19
20
  @parser.addstate @st_cblock
21
+ @parser.addstate @st_char
20
22
  @parser.addstate @st_string
21
23
  @parser.addstate @st_screen
22
24
 
@@ -30,10 +32,15 @@ module IndentCode
30
32
  @st_cblock.entry << '*'
31
33
  @st_cblock.leave << '*'
32
34
  @st_cblock.leave << '/'
33
-
35
+
34
36
  @st_cblock.handler( method( :cblock_handler ) )
35
37
  @st_cblock.fini( method( :cblock_fini ) )
36
38
 
39
+ # Setting char state.
40
+ @st_char.entry << "'"
41
+ @st_char.leave << "'"
42
+ @st_char.branches << @parser.state_index( @st_screen )
43
+
37
44
  # Setting string state.
38
45
  @st_string.entry << '"'
39
46
  @st_string.leave << '"'
@@ -50,6 +57,7 @@ module IndentCode
50
57
  @st_scope.branches << @parser.state_index( @st_cline )
51
58
  @st_scope.branches << @parser.state_index( @st_cblock )
52
59
  @st_scope.branches << @parser.state_index( @st_string )
60
+ @st_scope.branches << @parser.state_index( @st_char )
53
61
 
54
62
  @st_scope.init( method( :scope_init ) )
55
63
  @st_scope.fini( method( :scope_fini ) )
@@ -1,3 +1,3 @@
1
1
  module IndentCode
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/indent_code.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'optparse'
2
2
  require 'fileutils'
3
+ require 'indent_code/application'
3
4
  require 'indent_code/indent_cpp'
4
5
  require 'indent_code/cleaner'
5
6
  require 'indent_code/version'
@@ -37,111 +38,5 @@ module IndentCode
37
38
  end
38
39
  exit
39
40
  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
41
 
147
42
  end # module IndentCode
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indent_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
- - anton
7
+ - Anton S. Gerasimov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-12 00:00:00.000000000 Z
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,17 +60,9 @@ executables:
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - ".gitignore"
64
- - CODE_OF_CONDUCT.md
65
- - Gemfile
66
- - LICENSE.txt
67
- - README.md
68
- - Rakefile
69
- - bin/console
70
63
  - bin/indent
71
- - bin/setup
72
- - indent_code.gemspec
73
64
  - lib/indent_code.rb
65
+ - lib/indent_code/application.rb
74
66
  - lib/indent_code/cleaner.rb
75
67
  - lib/indent_code/indent_cpp.rb
76
68
  - lib/indent_code/version.rb
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/CODE_OF_CONDUCT.md DELETED
@@ -1,13 +0,0 @@
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 DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in indent_code.gemspec
4
- gemspec
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,149 +0,0 @@
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
-
25
- File for normalize 'test.c':
26
- ```
27
- #include <stdlib.h>
28
-
29
- ///Test function - 1. }
30
- void test1 ( void )
31
- {
32
- switch(c) {
33
- case 4:
34
- c = "** \" { \" ";
35
- break;
36
- }
37
-
38
- if (c)
39
- {
40
- return 1;
41
- }
42
- }
43
-
44
- /**
45
- * Test function - 2. {
46
- */
47
- void test2 ( void )
48
- {
49
- int a;
50
- /*
51
- * Simple comment block.
52
- * Check align into comments.
53
- */
54
- {
55
- a++;
56
- }
57
- return a;
58
- }
59
- ```
60
-
61
- Run `indent` gem:
62
-
63
- $ indent test.c
64
-
65
- and get follow result after normalize:
66
- ```
67
- #include <stdlib.h>
68
-
69
- ///Test function - 1. }
70
- void test1 ( void )
71
- {
72
- switch(c) {
73
- case 4:
74
- c = "** \" { \" ";
75
- break;
76
- }
77
-
78
- if (c)
79
- {
80
- return 1;
81
- }
82
- }
83
-
84
- /**
85
- * Test function - 2. {
86
- */
87
- void test2 ( void )
88
- {
89
- int a;
90
- /*
91
- * Simple comment block.
92
- * Check align into comments.
93
- */
94
- {
95
- a++;
96
- }
97
- return a;
98
- }
99
- ```
100
-
101
- Also you can set indent size `-i 4` (set TAB size = 4 chars) and clean source code, by used `-c` option.
102
- Clean option deleted code blocks framed is a: `#if 0` ... `#endif`.
103
-
104
- $ indent test.c -c
105
- ```
106
- #include <stdlib.h>
107
-
108
- #if EDEF
109
- //Some text - 1.
110
- # if 0
111
- Deleted text.
112
- # endif
113
- //Some text - 2.
114
- #endif
115
- ```
116
-
117
- We get follow result after clean:
118
- ```
119
- #include <stdlib.h>
120
-
121
- #if EDEF
122
- //Some text - 1.
123
- //Some text - 2.
124
- #endif
125
- ```
126
-
127
- ## Patch
128
-
129
- Details information for each patch.
130
-
131
- ##### 0.1.1
132
- * Corrected describe information.
133
-
134
-
135
- ## Development
136
-
137
- 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.
138
-
139
- 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).
140
-
141
- ## Contributing
142
-
143
- 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.
144
-
145
-
146
- ## License
147
-
148
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
149
-
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require "bundler/gem_tasks"
data/bin/console DELETED
@@ -1,14 +0,0 @@
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/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here
data/indent_code.gemspec DELETED
@@ -1,26 +0,0 @@
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. This gem use the 'iparser' gem as a parser engine.}
13
- spec.description = %q{}
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