of_if 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 866744c69f8497c6aabe717358c8dc0fc4ab21d329e7cf3d63f6e99a4a9d22d1
4
+ data.tar.gz: 109d70562d20203a64886fe6398381bd8935efa2d83585e2fa318fba98f52db3
5
+ SHA512:
6
+ metadata.gz: f4ca337c13f2edefa5d5d8f1b9f9eb106761659d867ed3815d6d95facf2460f1c2c05abb64832df38a24af5e26c413f7e8fd00bf5e1511a73e772c4bee7420a6
7
+ data.tar.gz: 22d8755123a0b57ee3b14595a8ed8699f32d08a58db7a572ec8f6e6fc406ae2cf8cfcc0265aaf389a51a29ce6db45d084409f154bbc4b2fdb57df03a6023fa34
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 3.1
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in of_if.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
9
+ gem 'rake', '~> 13.0.6'
10
+ gem 'rspec', '~> 3.12.0'
11
+ gem 'rubocop', '~> 1.48.0'
12
+ gem 'rubocop-rake', '~> 0.6.0'
13
+ gem 'rubocop-rspec', '~> 2.19.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ of_if (0.1.0)
5
+ chunky_png (~> 1.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ chunky_png (1.4.0)
12
+ coderay (1.1.3)
13
+ diff-lcs (1.5.0)
14
+ json (2.6.3)
15
+ method_source (1.0.0)
16
+ parallel (1.22.1)
17
+ parser (3.2.1.1)
18
+ ast (~> 2.4.1)
19
+ pry (0.14.2)
20
+ coderay (~> 1.1)
21
+ method_source (~> 1.0)
22
+ rainbow (3.1.1)
23
+ rake (13.0.6)
24
+ regexp_parser (2.7.0)
25
+ rexml (3.2.5)
26
+ rspec (3.12.0)
27
+ rspec-core (~> 3.12.0)
28
+ rspec-expectations (~> 3.12.0)
29
+ rspec-mocks (~> 3.12.0)
30
+ rspec-core (3.12.1)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-expectations (3.12.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-mocks (3.12.3)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-support (3.12.0)
39
+ rubocop (1.48.0)
40
+ json (~> 2.3)
41
+ parallel (~> 1.10)
42
+ parser (>= 3.2.0.0)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml (>= 3.2.5, < 4.0)
46
+ rubocop-ast (>= 1.26.0, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 2.4.0, < 3.0)
49
+ rubocop-ast (1.27.0)
50
+ parser (>= 3.2.1.0)
51
+ rubocop-capybara (2.17.1)
52
+ rubocop (~> 1.41)
53
+ rubocop-rake (0.6.0)
54
+ rubocop (~> 1.0)
55
+ rubocop-rspec (2.19.0)
56
+ rubocop (~> 1.33)
57
+ rubocop-capybara (~> 2.17)
58
+ ruby-progressbar (1.13.0)
59
+ unicode-display_width (2.4.2)
60
+
61
+ PLATFORMS
62
+ x86_64-linux
63
+
64
+ DEPENDENCIES
65
+ of_if!
66
+ pry
67
+ rake (~> 13.0.6)
68
+ rspec (~> 3.12.0)
69
+ rubocop (~> 1.48.0)
70
+ rubocop-rake (~> 0.6.0)
71
+ rubocop-rspec (~> 2.19.0)
72
+
73
+ BUNDLED WITH
74
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Christoph Lipautz
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,29 @@
1
+ # Of-If
2
+
3
+ Generate images of Ruby source code that focus on identation. Discuss on the
4
+ style and find heavy blocks with ease.
5
+
6
+ ![](./lib_of_if_file.png)
7
+
8
+ ## Usage
9
+
10
+ ```sh
11
+ # install the gem
12
+ $ gem install of_if
13
+ # run in your project directory, creates a new dir: ./of_if/
14
+ $ of-if
15
+ $ ls of_if/
16
+ lib_version.png lib_mygem_application.rb
17
+ ```
18
+
19
+ ## Development
20
+
21
+ ```sh
22
+ $ bundle install
23
+ $ bundle exec rake specs
24
+ ```
25
+
26
+ ## License
27
+
28
+ The gem is available as open source under the terms of the [MIT
29
+ License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new :spec
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
data/bin/of-if ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'of_if'
6
+
7
+ TARGET_DIR = './of_if'
8
+
9
+ def create_target_directory
10
+ FileUtils.mkdir_p TARGET_DIR
11
+ end
12
+
13
+ def targetfile_from(sourcefile)
14
+ path, file = File.split sourcefile
15
+ target_filename = "#{path.gsub('/', '_')}_#{file.split('.')[0]}.png"
16
+ File.join TARGET_DIR, target_filename
17
+ end
18
+
19
+ def find_source_files = Dir.glob('**/*.rb')
20
+
21
+ def exclude_vendor_files(files) = files.reject { |file| file.start_with? 'vendor' }
22
+
23
+ create_target_directory
24
+ source_files = exclude_vendor_files find_source_files
25
+ source_files.each do |file|
26
+ OfIf::Image.from_sourcefile(file).write targetfile_from(file)
27
+ end
data/lib/of_if/file.rb ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OfIf
4
+ # Read a source file and provide each line with indentation and content
5
+ # length.
6
+ class File
7
+ # Line responds to content and indentation length of a single line.
8
+ Line = Struct.new(:raw) do
9
+ def comment? = raw.lstrip.start_with?('#')
10
+ def length = raw.length
11
+ def content_length = raw.lstrip.length
12
+ def indent_length = length - content_length
13
+ end
14
+
15
+ def initialize(path) = @path = path
16
+
17
+ def lines
18
+ @lines ||= ::File.readlines(@path).map { |line| Line.new(line.rstrip) }
19
+ end
20
+
21
+ def to_s
22
+ lines.map do |line|
23
+ "#{' ' * line.indent_length}#{'█' * line.content_length}"
24
+ end.join "\n"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'chunky_png'
4
+
5
+ module OfIf
6
+ # Image renders a source file only by its indentation and obfuscates the code
7
+ # by displaying only color blocks.
8
+ class Image
9
+ DEFAULTS = {
10
+ margin: 100,
11
+ char_width: 10,
12
+ line_height: 20,
13
+ minimal_width: 480
14
+ }.freeze
15
+
16
+ attr_reader :width
17
+
18
+ def initialize
19
+ @width = minimal_width
20
+ @image_operations = []
21
+ @current_line_index = 0
22
+ end
23
+
24
+ def self.from_sourcefile(sourcefile)
25
+ file = File.new sourcefile
26
+
27
+ new.tap do |image|
28
+ file.lines.each { |line| image << line }
29
+ end
30
+ end
31
+
32
+ def <<(line)
33
+ @image_operations << create_image_operation(line, @current_line_index)
34
+ update_width line.length
35
+ @current_line_index += 1
36
+ end
37
+
38
+ def height = offset + (@current_line_index * line_height)
39
+
40
+ def write(filename)
41
+ png = ChunkyPNG::Image.new width, height, ChunkyPNG::Color::WHITE
42
+ @image_operations.each { |op| op.call png }
43
+ png.save filename
44
+ end
45
+
46
+ DEFAULTS.each_key do |attr_with_default|
47
+ attr_writer attr_with_default
48
+
49
+ define_method(attr_with_default) do
50
+ instance_variable_get(:"@#{attr_with_default}") || \
51
+ DEFAULTS[attr_with_default]
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def update_width(length)
58
+ @width = [@width, offset + (length * char_width)].max
59
+ end
60
+
61
+ # ChunkyPNG::Color::PREDEFINED_COLORS[:dimgrey]
62
+ def comment_color = ChunkyPNG::Color.rgba(0, 0, 0, 55)
63
+
64
+ # ChunkyPNG::Color::PREDEFINED_COLORS[:green]
65
+ def code_color = ChunkyPNG::Color.rgba(0, 255, 0, 255)
66
+
67
+ def border_color = ChunkyPNG::Color::TRANSPARENT
68
+
69
+ def line_color(line)
70
+ line.comment? ? comment_color : code_color
71
+ end
72
+
73
+ def offset = (2 * margin)
74
+
75
+ def spacer = Integer(line_height / 3)
76
+
77
+ def create_image_operation(line, line_index)
78
+ lambda { |png|
79
+ return if line.length.zero?
80
+
81
+ from = rect_start_point line, line_index
82
+ to = rect_end_point line, line_index
83
+ png.rect from[0], from[1], to[0], to[1], border_color, line_color(line)
84
+ }
85
+ end
86
+
87
+ def rect_start_point(line, index)
88
+ [margin + (line.indent_length * char_width),
89
+ margin + (index * line_height)]
90
+ end
91
+
92
+ def rect_end_point(line, index)
93
+ [margin + (line.length * char_width) - spacer,
94
+ margin + (index * line_height) + line_height - spacer]
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OfIf
4
+ VERSION = '0.1.0'
5
+ end
data/lib/of_if.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'of_if/version'
4
+ require_relative 'of_if/file'
5
+ require_relative 'of_if/image'
6
+
7
+ module OfIf
8
+ class Error < StandardError; end
9
+ end
Binary file
data/of_if.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/of_if/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'of_if'
7
+ spec.version = OfIf::VERSION
8
+ spec.authors = ['Christoph Lipautz']
9
+ spec.email = ['christoph@lipautz.org']
10
+
11
+ spec.summary = 'Generate images of identation from Ruby source code'
12
+ spec.description = <<~DESC
13
+ Generate images of Ruby source code that focus on identation. Discuss on
14
+ the style and find heavy blocks with ease.
15
+ DESC
16
+ spec.homepage = 'https://github.com/unused/of-if'
17
+ spec.license = 'MIT'
18
+ spec.required_ruby_version = '>= 3.1.0'
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = spec.homepage
22
+ # spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
23
+
24
+ # Specify which files should be added to the gem when it is released. The
25
+ # `git ls-files -z` loads the files in the RubyGem that have been added into
26
+ # git.
27
+ spec.files = Dir.chdir(__dir__) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
30
+ end
31
+ end
32
+ spec.bindir = 'bin'
33
+ spec.executables << 'of-if'
34
+ spec.require_paths = ['lib']
35
+
36
+ # Uncomment to register a new dependency of your gem
37
+ spec.add_dependency 'chunky_png', '~> 1.4'
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ spec.metadata['rubygems_mfa_required'] = 'true'
42
+ end
data/sig/of_if.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module OfIf
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: of_if
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christoph Lipautz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chunky_png
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ description: |
28
+ Generate images of Ruby source code that focus on identation. Discuss on
29
+ the style and find heavy blocks with ease.
30
+ email:
31
+ - christoph@lipautz.org
32
+ executables:
33
+ - of-if
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/of-if
45
+ - lib/of_if.rb
46
+ - lib/of_if/file.rb
47
+ - lib/of_if/image.rb
48
+ - lib/of_if/version.rb
49
+ - lib_of_if_file.png
50
+ - of_if.gemspec
51
+ - sig/of_if.rbs
52
+ homepage: https://github.com/unused/of-if
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/unused/of-if
57
+ source_code_uri: https://github.com/unused/of-if
58
+ rubygems_mfa_required: 'true'
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.1.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.4.1
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Generate images of identation from Ruby source code
78
+ test_files: []