1. Purpose

:pretty_diff is a text-LCS diff mode that bypasses DiffNodeMapper entirely. It applies display_preprocessing to both sides, then runs Diff::LCS.sdiff on the resulting plain-text lines.

This guarantees that every line-level change is visible, regardless of whether DiffNodeMapper’s DOM-address correlation is accurate.

2. When to use

Use :pretty_diff when:

  • :by_line mode shows no diff output for changes you know are present (the issue #85 class of bug where DiffNodeMapper maps DiffNodes to the wrong post-preprocessing line numbers)

  • You want a quick human-readable text diff and do not need inline character-level highlighting

Use :by_line when:

  • Inline character highlighting within a line matters

  • show_diffs: :normative / :informative filtering is required (these filters rely on DiffNodes that :pretty_diff does not use)

3. Usage

3.1. Basic usage

formatter = Canon::DiffFormatter.new(
  mode: :pretty_diff,
  display_preprocessing: :normalize_pretty_print,  # recommended for mixed-content XML
  context_lines: 3,
  use_color: true,
)
output = formatter.format(differences, :xml, doc1: expected, doc2: actual)

Combining with display_preprocessing: :normalize_pretty_print is strongly recommended for XML with inline markup (Metanorma, DocBook, DITA). It breaks every mixed-content element onto separate lines and visualizes inter-element whitespace, so the LCS diff can pinpoint changes inside <p>, <formattedref>, and similar elements.

display_preprocessing: :pretty_print is sufficient for element-only XML (no mixed content).

3.2. Controlling per-element whitespace visualization

By default, :normalize_pretty_print treats all inter-element whitespace as structural formatting noise and drops it silently — both compact XML and indented XML serialize to identical lines. This is the correct default for purely structural elements such as <formattedref> or <bibdata>.

For mixed-content elements where whitespace presence is meaningful (e.g. the word-boundary space in <p>See <xref/>), opt in with collapse_whitespace_elements:

formatter = Canon::DiffFormatter.new(
  mode: :pretty_diff,
  display_preprocessing: :normalize_pretty_print,
  collapse_whitespace_elements: %w[p li td th],
  context_lines: 3,
  use_color: true,
)

Or via Canon::Config (recommended in spec_helper.rb):

Canon::Config.configure do |cfg|
  cfg.xml.diff.mode = :pretty_diff
  cfg.xml.diff.display_preprocessing = :normalize_pretty_print
  cfg.xml.diff.collapse_whitespace_elements = %w[p li td th]
end

For preformatted elements where every whitespace character is significant, use preserve_whitespace_elements instead (e.g. %w[pre code]).

See display-preprocessing: element classification for the full three-way classification system and HTML format defaults.

4. Output format

The output uses the same header/context/separator conventions as :by_line:

Pretty diff (XML mode):
  <section>
    <title>Introduction</title>
- <para>This describes the <strong>old</strong> behaviour.</para>
+ <para>This describes the <strong>new</strong> behaviour.</para>
    <note>See also: Appendix A.</note>
  </section>
  • Lines prefixed - ` are removals (present in `doc1, absent in doc2)

  • Lines prefixed + ` are additions (absent in `doc1, present in doc2)

  • Lines prefixed ` ` are context (unchanged)

  • --- --- separates non-adjacent change blocks

5. Context windowing

context_lines (default 3) controls how many unchanged lines are shown around each change. Nearby windows whose expanded ranges overlap or are adjacent are merged into a single block. A --- --- separator is emitted between blocks that are not adjacent.

Setting context_lines: 0 shows only the changed lines with no surrounding context.

6. Limitations

:pretty_diff trades DiffNode integration for reliability:

  • show_diffs: :normative / :informative filtering is not supported. All line differences are shown regardless of their classification.

  • Inline character-level highlighting (e.g. highlighting the changed word within a changed line) is not available; granularity is whole-line only.

  • The differences array passed to DiffFormatter#format is ignored; the diff is computed purely from doc1 and doc2 via LCS.

7. Implementation note

Diff::LCS.sdiff is invoked with ::Diff::LCS (absolute constant path) to avoid resolving against Canon::Diff which is an unrelated internal module.

8. See also