Module: Canon::DiffFormatter::DiffDetailFormatterHelpers::TextUtils

Defined in:
lib/canon/diff_formatter/diff_detail_formatter/text_utils.rb

Overview

Text utility methods for diff formatting

Provides helper methods for text manipulation and visualization.

Class Method Summary collapse

Class Method Details

.extract_content_preview(node, max_length = 50) ⇒ String

Extract a content preview from a node

Parameters:

  • node (Object)

    Node to extract from

  • max_length (Integer) (defaults to: 50)

    Maximum length of preview

Returns:

  • (String)

    Content preview



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/canon/diff_formatter/diff_detail_formatter/text_utils.rb', line 41

def self.extract_content_preview(node, max_length = 50)
  return "" unless node

  text = if node.respond_to?(:text)
           node.text
         elsif node.respond_to?(:content)
           node.content
         else
           node.to_s
         end

  return "" if text.nil? || text.empty?

  # Clean up whitespace
  text = text.strip.gsub(/\s+/, " ")
  truncate_text(text, max_length)
end

.truncate_text(text, max_length) ⇒ String

Truncate text to a maximum length with ellipsis

Parameters:

  • text (String)

    Text to truncate

  • max_length (Integer)

    Maximum length

Returns:

  • (String)

    Truncated text



15
16
17
18
19
# File 'lib/canon/diff_formatter/diff_detail_formatter/text_utils.rb', line 15

def self.truncate_text(text, max_length)
  return "" if text.nil?

  text.length > max_length ? "#{text[0...max_length]}..." : text
end

.visualize_whitespace(text) ⇒ String

Visualize whitespace characters in text

Shows spaces as ·, tabs as →, newlines as ¬

Parameters:

  • text (String)

    Text to visualize

Returns:

  • (String)

    Text with visible whitespace



27
28
29
30
31
32
33
34
# File 'lib/canon/diff_formatter/diff_detail_formatter/text_utils.rb', line 27

def self.visualize_whitespace(text)
  return "" if text.nil?

  text
    .gsub(" ", "·")
    .gsub("\t", "")
    .gsub("\n", "¬")
end