diff --git a/ruby/.rubocop.yml b/ruby/.rubocop.yml index 9f8fefc1..5fb4e130 100644 --- a/ruby/.rubocop.yml +++ b/ruby/.rubocop.yml @@ -1,10 +1,15 @@ +AllCops: + TargetRubyVersion: 2.7 + SuggestExtensions: false + NewCops: enable + Style/Encoding: Enabled: false Layout/EndOfLine: EnforcedStyle: lf -Metrics/LineLength: +Layout/LineLength: Max: 180 Metrics/ClassLength: @@ -24,3 +29,6 @@ Metrics/AbcSize: Metrics/ParameterLists: Max: 7 + +Gemspec/RequiredRubyVersion: + Enabled: false diff --git a/ruby/.ruby-version b/ruby/.ruby-version index 57cf282e..37c2961c 100644 --- a/ruby/.ruby-version +++ b/ruby/.ruby-version @@ -1 +1 @@ -2.6.5 +2.7.2 diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock index a23ddbf0..63e1fcde 100644 --- a/ruby/Gemfile.lock +++ b/ruby/Gemfile.lock @@ -8,45 +8,50 @@ GEM remote: https://rubygems.org/ specs: asciidoctor (2.0.11) - ast (2.4.1) - diff-lcs (1.3) - jaro_winkler (1.5.4) - parallel (1.19.1) - parser (2.7.1.3) - ast (~> 2.4.0) + ast (2.4.2) + diff-lcs (1.4.4) + parallel (1.21.0) + parser (3.0.2.0) + ast (~> 2.4.1) rainbow (3.0.0) - rake (12.3.3) - rspec (3.8.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-core (3.8.2) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.6) + rake (13.0.6) + regexp_parser (2.1.1) + rexml (3.2.5) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-mocks (3.8.2) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.3) - rubocop (0.74.0) - jaro_winkler (~> 1.5.1) + rspec-support (~> 3.10.0) + rspec-support (3.10.2) + rubocop (1.21.0) parallel (~> 1.10) - parser (>= 2.6) + parser (>= 3.0.0.0) rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.9.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 1.7) - ruby-progressbar (1.10.1) - unicode-display_width (1.6.1) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.11.0) + parser (>= 3.0.1.1) + ruby-progressbar (1.11.0) + unicode-display_width (2.0.0) PLATFORMS ruby DEPENDENCIES asciidoctor-kroki! - rake (~> 12.3.2) - rspec (~> 3.8.0) - rubocop (~> 0.74.0) + rake (~> 13.0.6) + rspec (~> 3.10.0) + rubocop (~> 1.21) BUNDLED WITH 2.2.17 diff --git a/ruby/asciidoctor-kroki.gemspec b/ruby/asciidoctor-kroki.gemspec index 3ab96941..08b16150 100644 --- a/ruby/asciidoctor-kroki.gemspec +++ b/ruby/asciidoctor-kroki.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'asciidoctor', '~> 2.0' - s.add_development_dependency 'rake', '~> 12.3.2' - s.add_development_dependency 'rspec', '~> 3.8.0' - s.add_development_dependency 'rubocop', '~> 0.74.0' + s.add_development_dependency 'rake', '~> 13.0.6' + s.add_development_dependency 'rspec', '~> 3.10.0' + s.add_development_dependency 'rubocop', '~> 1.21' end diff --git a/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb b/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb index 4473ab14..30552eb5 100644 --- a/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb +++ b/ruby/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb @@ -67,7 +67,7 @@ def process(parent, target, attrs) begin diagram_text = read(path) - rescue => e # rubocop:disable RescueStandardError + rescue => e # rubocop:disable Style/RescueStandardError logger.error "Failed to read #{diagram_type} file: #{path}. #{e}." return create_block(parent, 'paragraph', unresolved_block_macro_message(diagram_type, path), {}) end @@ -89,7 +89,7 @@ def read_allowed?(_target) def read(target) if target.start_with?('http://') || target.start_with?('https://') require 'open-uri' - URI.open(target, &:read) + ::OpenURI.open_uri(target, &:read) else File.open(target, &:read) end @@ -135,6 +135,7 @@ class KrokiProcessor TEXT_FORMATS = %w[txt atxt utxt].freeze class << self + # rubocop:disable Metrics/AbcSize def process(processor, parent, attrs, diagram_type, diagram_text, logger) doc = parent.document diagram_text = prepend_plantuml_config(diagram_text, diagram_type, doc, logger) @@ -164,6 +165,7 @@ def process(processor, parent, attrs, diagram_type, diagram_text, logger) block.assign_caption(caption, 'figure') block end + # rubocop:enable Metrics/AbcSize private @@ -174,7 +176,7 @@ def prepend_plantuml_config(diagram_text, diagram_type, doc, logger) plantuml_include_path = doc.normalize_system_path(doc.attr('kroki-plantuml-include')) if ::File.readable? plantuml_include_path config = File.read(plantuml_include_path) - diagram_text = config + "\n" + diagram_text + diagram_text = "#{config}\n#{diagram_text}" else logger.warn "Unable to read plantuml-include. File not found or not readable: #{plantuml_include_path}." end @@ -258,9 +260,7 @@ class KrokiDiagram require 'zlib' require 'digest' - attr_reader :type - attr_reader :text - attr_reader :format + attr_reader :type, :text, :format def initialize(type, format, text) @text = text @@ -280,11 +280,10 @@ def save(output_dir_path, kroki_client) diagram_url = get_diagram_uri(kroki_client.server_url) diagram_name = "diag-#{Digest::SHA256.hexdigest diagram_url}.#{@format}" file_path = File.join(output_dir_path, diagram_name) - encoding = if @format == 'txt' || @format == 'atxt' || @format == 'utxt' + encoding = case @format + when 'txt', 'atxt', 'utxt' 'utf8' - elsif @format == 'svg' - 'binary' - else + when 'svg', 'binary' 'binary' end # file is either (already) on the file system or we should read it from Kroki @@ -303,12 +302,12 @@ def save(output_dir_path, kroki_client) def _join_uri_segments(base, *uris) segments = [] # remove trailing slashes - segments.push(base.gsub(%r{[/]+$}, '')) + segments.push(base.gsub(%r{/+$}, '')) segments.concat(uris.map do |uri| # remove leading and trailing slashes uri.to_s - .gsub(%r{^[/]+}, '') - .gsub(%r{[/]+$}, '') + .gsub(%r{^/+}, '') + .gsub(%r{/+$}, '') end) segments.join('/') end @@ -317,9 +316,7 @@ def _join_uri_segments(base, *uris) # Kroki client # class KrokiClient - attr_reader :server_url - attr_reader :method - attr_reader :max_uri_length + attr_reader :server_url, :method, :max_uri_length SUPPORTED_HTTP_METHODS = %w[get post adaptive].freeze diff --git a/ruby/spec/asciidoctor_kroki_block_macro_spec.rb b/ruby/spec/asciidoctor_kroki_block_macro_spec.rb index 3825290a..b775d304 100644 --- a/ruby/spec/asciidoctor_kroki_block_macro_spec.rb +++ b/ruby/spec/asciidoctor_kroki_block_macro_spec.rb @@ -1,3 +1,4 @@ +# rubocop:disable Lint/ConstantDefinitionInBlock # frozen_string_literal: true require 'rspec_helper' @@ -126,3 +127,4 @@ def unresolved_block_macro_message(name, target) end end end +# rubocop:enable Lint/ConstantDefinitionInBlock diff --git a/ruby/spec/asciidoctor_kroki_spec.rb b/ruby/spec/asciidoctor_kroki_spec.rb index ff8f1e0d..ed4d2c22 100644 --- a/ruby/spec/asciidoctor_kroki_spec.rb +++ b/ruby/spec/asciidoctor_kroki_spec.rb @@ -134,7 +134,7 @@ it 'should instantiate block processor without warning' do original_stderr = $stderr $stderr = StringIO.new - ::AsciidoctorExtensions::KrokiBlockProcessor.new 'plantuml'.to_sym, {} + ::AsciidoctorExtensions::KrokiBlockProcessor.new :plantuml, {} output = $stderr.string (expect output).to eql '' ensure