Class: Suma::Cli::CheckSvgQuality

Inherits:
Object
  • Object
show all
Defined in:
lib/suma/cli/check_svg_quality.rb

Overview

Check SVG quality using svg_conform Validator API - thin CLI wrapper

Constant Summary collapse

DATA_PATH =
"schemas"
DEFAULT_PATTERN =
"**/*.svg"
DEFAULT_PROFILE =
:metanorma

Instance Method Summary collapse

Constructor Details

#initialize(pattern: DEFAULT_PATTERN, profile: DEFAULT_PROFILE, format: "terminal", output: nil, min_errors: nil, summary_only: false, progress: false, limit: nil, sort: "errors") ⇒ CheckSvgQuality

Returns a new instance of CheckSvgQuality.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/suma/cli/check_svg_quality.rb', line 19

def initialize(pattern: DEFAULT_PATTERN, profile: DEFAULT_PROFILE,
              format: "terminal", output: nil, min_errors: nil,
              summary_only: false, progress: false, limit: nil,
              sort: "errors")
  @options = {
    pattern: pattern,
    profile: profile,
    format: format,
    output: output,
    min_errors: min_errors,
    summary_only: summary_only,
    progress: progress,
    limit: limit,
    sort: sort.to_sym,
  }
end

Instance Method Details

#analyze_single_file(path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/suma/cli/check_svg_quality.rb', line 70

def analyze_single_file(path)
  validator = SvgConform::Validator.new
  result = validator.validate_file(path.to_s, profile: options[:profile])

  puts "📄 SVG Quality Report: #{path}"
  puts ""
  puts "  Valid: #{result.valid? ? 'YES ✅' : 'NO ❌'}"
  puts "  Errors: #{result.error_count}"
  puts ""

  if result.errors.any?
    puts "  📋 Error Details"
    puts ""

    # Group errors by requirement_id
    by_req = result.errors.group_by(&:requirement_id)

    by_req.each do |req_id, errors|
      puts "  #{req_id} (#{errors.size} occurrences)"
      errors.first(5).each do |e|
        puts "    - #{e.message}"
      end
      if errors.size > 5
        puts "    ... and #{errors.size - 5} more"
      end
      puts ""
    end
  end
end

#run(path = DATA_PATH) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/suma/cli/check_svg_quality.rb', line 36

def run(path = DATA_PATH)
  require "svg_conform"

  path_obj = Pathname.new(path).expand_path

  # Enable progress by default when outputting to terminal
  show_progress = options[:progress] || ($stdout.tty? && !options[:output])
  if show_progress
    $stdout.sync = true
    $stderr.sync = true
  end

  if path_obj.file?
    # Single file mode - show detailed errors
    analyze_single_file(path_obj)
  else
    # Directory mode - show batch report
    svg_files = find_svg_files(path_obj)

    if svg_files.empty?
      puts "No SVG files found in #{path}"
      return
    end

    puts "🔍 Scanning #{svg_files.size} SVG files..."
    puts

    reports = analyze_files_one_by_one(svg_files, show_progress)
    batch_report = SvgQuality::BatchReport.new(reports)
    sorted_report = sort_report(batch_report)
    output_report(sorted_report)
  end
end