Table of Contents
1. Purpose
This document explains how to extend Canon with custom functionality, including creating custom comparators, formatters, and adapters for different document formats.
2. Overview
Canon is designed to be extensible at multiple layers:
-
Layer 1: Custom preprocessing/normalization
-
Layer 2: Custom comparison algorithms
-
Layer 3: Custom match options and dimensions
-
Layer 4: Custom diff formatters and renderers
3. Adapter Pattern
Canon uses an adapter pattern to work with different parsing libraries (Nokogiri, Moxml, etc.).
4. Custom Comparators
5. Custom Formatters
6. Custom Match Options
6.1. Defining Custom Dimensions
module Canon
module Comparison
class CustomDimension
def self.key
:custom_dimension
end
def self.compare(node1, node2, behavior, opts)
# Your comparison logic for this dimension
case behavior
when :strict
node1 == node2
when :normalize
normalize(node1) == normalize(node2)
when :ignore
true
end
end
end
end
end
Register your dimension:
Canon::Comparison.register_dimension(CustomDimension)
7. Best Practices
9. See Also
-
Architecture - 4-layer architecture overview
-
Diff Formatting - Layer 4 rendering options
-
Comparison Pipeline - Technical pipeline details