Class: Canon::PrettyPrinter::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/pretty_printer/html.rb

Overview

Pretty printer for HTML with consistent indentation.

Two modes:

  1. Default mode (+fixture_ready: false+): retains the existing behaviour for callers that use the pretty-printer as a structural normaliser (the canon round-trip tests, the diff-pipeline apply_pretty_print stage, etc). These callers do not require actual indentation; they require structural equivalence to the input.

  2. Fixture-ready mode (+fixture_ready: true+): emits actually-indented XHTML-shaped output via libxml’s FORMAT save flag. Used by DiffFormatter#prettyprint_for_display (the CANON_<FORMAT>_DIFF_SHOW_PRETTYPRINT_RECEIVED surface) so the user can read or paste the formatted output directly into a fixture heredoc. Output is XHTML-shaped (void elements self-closed, non-void paired) via the AS_XHTML save flag; the NO_DECLARATION flag suppresses the <?xml …?> prefix.

See lutaml/canon#133, lutaml/canon#135.

Instance Method Summary collapse

Constructor Details

#initialize(indent: 2, indent_type: "space", fixture_ready: false) ⇒ Html

Returns a new instance of Html.



32
33
34
35
36
# File 'lib/canon/pretty_printer/html.rb', line 32

def initialize(indent: 2, indent_type: "space", fixture_ready: false)
  @indent = indent.to_i
  @indent_type = indent_type
  @fixture_ready = fixture_ready
end

Instance Method Details

#format(html_string) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/canon/pretty_printer/html.rb', line 38

def format(html_string)
  return format_fixture_ready(html_string) if @fixture_ready

  if xhtml?(html_string)
    format_as_xhtml(html_string)
  else
    format_as_html(html_string)
  end
end