Class: Ace::Compressor::Organisms::ExactCompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/compressor/organisms/exact_compressor.rb

Constant Summary collapse

SUPPORTED_EXTENSIONS =
%w[.md .markdown .mdown .mkd .txt .text].freeze
BINARY_SNIFF_BYTES =
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, verbose: false, mode_label: "exact") ⇒ ExactCompressor

Returns a new instance of ExactCompressor.



15
16
17
18
19
20
21
22
# File 'lib/ace/compressor/organisms/exact_compressor.rb', line 15

def initialize(paths, verbose: false, mode_label: "exact")
  @paths = Array(paths)
  @verbose = verbose
  @mode_label = mode_label
  @parser = Ace::Compressor::Atoms::MarkdownParser.new
  @transformer = Ace::Compressor::Atoms::CanonicalBlockTransformer
  @ignored_paths = []
end

Instance Attribute Details

#ignored_pathsObject (readonly)

Returns the value of attribute ignored_paths.



13
14
15
# File 'lib/ace/compressor/organisms/exact_compressor.rb', line 13

def ignored_paths
  @ignored_paths
end

Instance Method Details

#callObject



24
25
26
# File 'lib/ace/compressor/organisms/exact_compressor.rb', line 24

def call
  compress_sources(resolve_sources)
end

#compress_sources(sources, source_paths: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ace/compressor/organisms/exact_compressor.rb', line 68

def compress_sources(sources, source_paths: nil)
  lines = [Ace::Compressor::Models::ContextPack.header("exact")]

  sources.each do |source|
    source_label = source_label(display_source(source, source_paths))
    lines << Ace::Compressor::Models::ContextPack.file_line(source_label)
    text = File.read(source)
    if text.strip.empty?
      raise Ace::Compressor::Error, "Input file is empty. #{mode_title} mode requires content: #{source}"
    end
    blocks = @parser.call(text)
    if blocks.empty?
      lines << text
      next
    end
    lines.concat transformed_lines(source_label, blocks)
  end

  lines.join("\n")
end

#compress_text(text, label:) ⇒ String

Compress a content string directly without filesystem access. Returns the compressed ContextPack text (without header line).

Parameters:

  • text (String)

    markdown/text content to compress

  • label (String)

    display label for the source (e.g. original file path)

Returns:

  • (String)

    compressed ContextPack records (no header)



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ace/compressor/organisms/exact_compressor.rb', line 94

def compress_text(text, label:)
  return text if text.to_s.strip.empty?

  blocks = @parser.call(text)
  return text if blocks.empty?

  lines = []
  lines << Ace::Compressor::Models::ContextPack.file_line(label)
  lines.concat @transformer.new(label).call(blocks)
  lines.join("\n")
end

#resolve_sourcesObject



28
29
30
31
32
33
34
35
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
# File 'lib/ace/compressor/organisms/exact_compressor.rb', line 28

def resolve_sources
  explicit_set = Set.new
  resolved = []

  @paths.each do |raw_path|
    path = raw_path.to_s
    expanded = File.expand_path(path)

    if File.directory?(expanded)
      directory_files = collect_supported_directory_files(expanded)
      if directory_files.empty?
        raise Ace::Compressor::Error,
          "Directory has no supported markdown/text sources: #{path}. Supported extensions: #{SUPPORTED_EXTENSIONS.join(", ")}"
      end
      directory_files.each do |file|
        next if explicit_set.include?(file)

        explicit_set << file
        resolved << file
      end
    elsif File.file?(expanded)
      validate_explicit_file!(expanded, path)
      next if explicit_set.include?(expanded)

      explicit_set << expanded
      resolved << expanded
    else
      raise Ace::Compressor::Error, "Input source not found: #{path}"
    end
  end

  sorted = resolved.sort
  if sorted.empty?
    raise Ace::Compressor::Error,
      "No supported markdown/text sources found. Supported extensions: #{SUPPORTED_EXTENSIONS.join(", ")}"
  end

  sorted
end