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
-
.extract_content_preview(node, max_length = 50) ⇒ String
Extract a content preview from a node.
-
.truncate_text(text, max_length) ⇒ String
Truncate text to a maximum length with ellipsis.
-
.visualize_whitespace(text) ⇒ String
Visualize whitespace characters in text.
Class Method Details
.extract_content_preview(node, max_length = 50) ⇒ String
Extract a content preview from a node
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
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 ¬
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 |