Class: MontyAI::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/monty_ai/formatter.rb

Class Method Summary collapse

Class Method Details

.format(explanation, code = nil, filename = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/monty_ai/formatter.rb', line 6

def self.format(explanation, code = nil, filename = nil)
  # If no code is provided, just return the explanation
  return explanation unless code

  # Initialize highlighter
  highlighter = SyntaxHighlighter.new
  language = filename ? highlighter.detect_language(filename) : :text

  # Highlight the code
  highlighted_code = highlighter.highlight(code, language)

  # Format the output with highlighted code
  output = []
  output << "```"
  output << highlighted_code
  output << "```"
  output << ""
  output << "Explanation:"
  output << explanation

  output.join("\n")
end