Sha256: 1ec8fea38fd8f5e29aa671d2fd8d7a727ee40ba36e4f07fcb9d962848fb01304

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

require 'erb'
require_relative '../../validator/errors'

class I18nFlow::CLI::LintCommand
  class MarkdownRenderer
    FILE = __dir__ + '/markdown.erb'

    attr_reader :errors
    attr_reader :url_formatter

    def initialize(errors, url_formatter:)
      @errors = errors
      @url_formatter = url_formatter
    end

    def render
      with_link(erb.result(binding))
    end

    def file_count
      @file_count ||= errors.size
    end

    def error_count
      @error_count ||= errors.sum { |_, errs| errs.size }
    end

    def summary_line
      @summary_line ||= 'Found %d %s in %d %s' % [
        error_count,
        error_count == 1 ? 'violation' : 'violations',
        file_count,
        file_count == 1 ? 'file' : 'files',
      ]
    end

  private

    def with_link(str)
      str.gsub(/\[([^\]]+)\]\(@([^\):]+)(?::(\d+))?\)/) do
        '[%s](%s)' % [$1, format_link(path: $2, line: $3)]
      end
    end

    def format_link(path:, line:)
      url_formatter
        .gsub(/%f\b/, path)
        .gsub(/%l\b/, line.to_s)
    end

    def erb
      @erb ||= ERB.new(File.read(FILE), 0, '-')
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
i18n_flow-0.2.3 lib/i18n_flow/cli/lint_command/markdown_renderer.rb
i18n_flow-0.2.2 lib/i18n_flow/cli/lint_command/markdown_renderer.rb
i18n_flow-0.2.1 lib/i18n_flow/cli/lint_command/markdown_renderer.rb
i18n_flow-0.2.0 lib/i18n_flow/cli/lint_command/markdown_renderer.rb
i18n_flow-0.1.0 lib/i18n_flow/cli/lint_command/markdown_renderer.rb