Class: Ace::Core::Molecules::OutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/core/molecules/output_formatter.rb

Overview

Formats aggregated content into various output formats

Constant Summary collapse

FORMATS =

Supported output formats

%w[markdown yaml xml markdown-xml json].freeze

Instance Method Summary collapse

Constructor Details

#initialize(format = "markdown") ⇒ OutputFormatter

Returns a new instance of OutputFormatter.



13
14
15
# File 'lib/ace/core/molecules/output_formatter.rb', line 13

def initialize(format = "markdown")
  @format = validate_format(format)
end

Instance Method Details

#format(data) ⇒ String

Format aggregated content

Parameters:

  • data (Hash)

    Aggregated data with files and commands

Returns:

  • (String)

    Formatted output



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ace/core/molecules/output_formatter.rb', line 20

def format(data)
  case @format
  when "markdown"
    format_markdown(data)
  when "yaml"
    format_yaml(data)
  when "xml"
    format_xml(data)
  when "markdown-xml"
    format_markdown_xml(data)
  when "json"
    format_json(data)
  else
    format_markdown(data)
  end
end

#format_json(data) ⇒ String

Format as JSON

Parameters:

  • data (Hash)

    Data to format

Returns:

  • (String)

    JSON formatted output



151
152
153
154
155
# File 'lib/ace/core/molecules/output_formatter.rb', line 151

def format_json(data)
  require "json"
  clean_data = prepare_for_serialization(data)
  JSON.pretty_generate(clean_data)
end

#format_markdown(data) ⇒ String

Format as pure markdown

Parameters:

  • data (Hash)

    Data to format

Returns:

  • (String)

    Markdown formatted output



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
69
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ace/core/molecules/output_formatter.rb', line 40

def format_markdown(data)
  # If we have source content (embed_document_source mode), use raw content + XML blocks format
  if data[:content] && !data[:content].to_s.empty?
    return format_embedded_source(data)
  end

  output = []

  # Add header
  output << "# Context"
  output << ""

  # Add metadata if present
  if data[:metadata]
    # If we have original frontmatter YAML, output it as-is
    if data[:metadata][:frontmatter_yaml]
      output << "---"
      output << data[:metadata][:frontmatter_yaml]
      output << "---"
      output << ""
    else
      # Otherwise use bulleted format
      output << "## Metadata"
      output << ""
      data[:metadata].each do |key, value|
        output << "- **#{key}**: #{value}"
      end
      output << ""
    end
  end

  # Add files section
  if data[:files] && !data[:files].empty?
    output << "## Files"
    output << ""

    data[:files].each do |file|
      output << "### #{file[:path]}"
      output << ""
      output << "```"
      output << file[:content]
      output << "```"
      output << ""
    end
  end

  # Add commands section
  if data[:commands] && !data[:commands].empty?
    output << "## Commands"
    output << ""

    data[:commands].each do |cmd|
      output << "### Command: `#{cmd[:command]}`"
      output << ""

      if cmd[:success]
        output << "**Output:**"
        output << "```"
        output << cmd[:output]
        output << "```"
      else
        output << "**Error:** #{cmd[:error]}"
      end
      output << ""
    end
  end

  # Add diffs section
  if data[:diffs] && !data[:diffs].empty?
    output << "## Git Diffs"
    output << ""

    data[:diffs].each do |diff|
      output << "### Diff: `#{diff[:range]}`"
      output << ""

      if diff[:success]
        output << "```diff"
        output << diff[:output]
        output << "```"
      else
        output << "**Error:** #{diff[:error]}"
      end
      output << ""
    end
  end

  # Add errors section
  if data[:errors] && !data[:errors].empty?
    output << "## Errors"
    output << ""
    data[:errors].each do |error|
      output << "- #{error}"
    end
    output << ""
  end

  output.join("\n").strip
end

#format_markdown_xml(data) ⇒ String

Format as markdown with embedded XML (hybrid format)

Parameters:

  • data (Hash)

    Data to format

Returns:

  • (String)

    Markdown-XML formatted output



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/ace/core/molecules/output_formatter.rb', line 233

def format_markdown_xml(data)
  # If we have source content (embed_document_source mode), use raw content + XML blocks format
  if data[:content] && !data[:content].to_s.empty?
    return format_embedded_source(data)
  end

  output = []

  output << "# Context"
  output << ""

  # Add metadata
  if data[:metadata]
    # If we have original frontmatter YAML, output it as-is
    if data[:metadata][:frontmatter_yaml]
      output << "---"
      output << data[:metadata][:frontmatter_yaml]
      output << "---"
      output << ""
    else
      # Otherwise use bulleted format
      output << "## Metadata"
      output << ""
      data[:metadata].each do |key, value|
        output << "- **#{key}**: #{value}"
      end
      output << ""
    end
  end

  # Add files as XML blocks
  if data[:files] && !data[:files].empty?
    output << "## Files"
    output << ""

    data[:files].each do |file|
      size_info = file[:size] ? " size=\"#{file[:size]}\"" : ""
      output << "<file path=\"#{escape_xml(file[:path])}\"#{size_info}>"
      output << file[:content]
      output << "</file>"
      output << ""
    end
  end

  # Add commands
  if data[:commands] && !data[:commands].empty?
    output << "## Commands"
    output << ""

    data[:commands].each do |cmd|
      success_attr = cmd[:success] ? "true" : "false"
      error_attr = cmd[:error] ? " error=\"#{escape_xml(cmd[:error])}\"" : ""

      output << "<output command=\"#{escape_xml(cmd[:command])}\" success=\"#{success_attr}\"#{error_attr}>"
      output << ""
      output << cmd[:output] if cmd[:output]
      output << "</output>"
      output << ""
    end
  end

  # Add diffs
  if data[:diffs] && !data[:diffs].empty?
    output << "## Git Diffs"
    output << ""

    data[:diffs].each do |diff|
      success_attr = diff[:success] ? "true" : "false"
      error_attr = diff[:error] ? " error=\"#{escape_xml(diff[:error])}\"" : ""

      output << "<diff range=\"#{escape_xml(diff[:range])}\" success=\"#{success_attr}\"#{error_attr}>"
      output << ""
      output << diff[:output] if diff[:output]
      output << "</diff>"
      output << ""
    end
  end

  # Add errors
  if data[:errors] && !data[:errors].empty?
    output << "## Errors"
    output << ""
    data[:errors].each do |error|
      output << "- #{error}"
    end
  end

  output.join("\n").strip
end

#format_xml(data) ⇒ String

Format as XML

Parameters:

  • data (Hash)

    Data to format

Returns:

  • (String)

    XML formatted output



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ace/core/molecules/output_formatter.rb', line 160

def format_xml(data)
  output = []
  output << '<?xml version="1.0" encoding="UTF-8"?>'
  output << "<context>"

  # Add metadata
  if data[:metadata]
    output << "  <metadata>"
    data[:metadata].each do |key, value|
      output << "    <#{key}>#{escape_xml(value.to_s)}</#{key}>"
    end
    output << "  </metadata>"
  end

  # Add files
  if data[:files] && !data[:files].empty?
    output << "  <files>"
    data[:files].each do |file|
      output << "    <file path=\"#{escape_xml(file[:path])}\" size=\"#{file[:size]}\">"
      output << "      <content><![CDATA[#{file[:content]}]]></content>"
      output << "    </file>"
    end
    output << "  </files>"
  end

  # Add commands
  if data[:commands] && !data[:commands].empty?
    output << "  <commands>"
    data[:commands].each do |cmd|
      output << "    <command name=\"#{escape_xml(cmd[:command])}\" success=\"#{cmd[:success]}\">"
      if cmd[:output]
        output << "      <output><![CDATA[#{cmd[:output]}]]></output>"
      end
      if cmd[:error]
        output << "      <error>#{escape_xml(cmd[:error])}</error>"
      end
      output << "    </command>"
    end
    output << "  </commands>"
  end

  # Add diffs
  if data[:diffs] && !data[:diffs].empty?
    output << "  <diffs>"
    data[:diffs].each do |diff|
      output << "    <diff range=\"#{escape_xml(diff[:range])}\" success=\"#{diff[:success]}\">"
      if diff[:output]
        output << "      <output><![CDATA[#{diff[:output]}]]></output>"
      end
      if diff[:error]
        output << "      <error>#{escape_xml(diff[:error])}</error>"
      end
      output << "    </diff>"
    end
    output << "  </diffs>"
  end

  # Add errors
  if data[:errors] && !data[:errors].empty?
    output << "  <errors>"
    data[:errors].each do |error|
      output << "    <error>#{escape_xml(error)}</error>"
    end
    output << "  </errors>"
  end

  output << "</context>"
  output.join("\n")
end

#format_yaml(data) ⇒ String

Format as YAML

Parameters:

  • data (Hash)

    Data to format

Returns:

  • (String)

    YAML formatted output



143
144
145
146
# File 'lib/ace/core/molecules/output_formatter.rb', line 143

def format_yaml(data)
  clean_data = prepare_for_serialization(data)
  YAML.dump(clean_data)
end