Class: Markdown::Merge::WhitespaceNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown/merge/whitespace_normalizer.rb

Overview

Normalizes whitespace in markdown documents.

Supports multiple normalization modes:

  • :basic (or true) - Collapse excessive blank lines (3+ → 2)
  • :link_refs - Also remove blank lines between consecutive link reference definitions
  • :strict - All of the above normalizations

Uses LinkParser for detecting link reference definitions, which supports:

  • Standard definitions: [label]: url
  • Definitions with titles: [label]: url "title"
  • Angle-bracketed URLs: [label]: <url>
  • Emoji in labels: [🎨logo]: url

Examples:

Basic normalization (default)

content = "Hello\n\n\n\nWorld"
normalized = WhitespaceNormalizer.normalize(content)
# => "Hello\n\nWorld"

With link_refs mode

content = "[link1]: url1\n\n[link2]: url2"
normalized = WhitespaceNormalizer.normalize(content, mode: :link_refs)
# => "[link1]: url1\n[link2]: url2"

With problem tracking

normalizer = WhitespaceNormalizer.new(content, mode: :link_refs)
result = normalizer.normalize
normalizer.problems.by_category(:link_ref_spacing)

Constant Summary collapse

MODES =

Valid normalization modes

%i[basic link_refs strict].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, mode: :basic) ⇒ WhitespaceNormalizer

Initialize a new normalizer.

Parameters:

  • content (String)

    Content to normalize

  • mode (Symbol, Boolean) (defaults to: :basic)

    Normalization mode (:basic, :link_refs, :strict, or true for :basic)



61
62
63
64
65
66
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 61

def initialize(content, mode: :basic)
  @content = content
  @mode = normalize_mode(mode)
  @problems = DocumentProblems.new
  @link_parser = LinkParser.new
end

Instance Attribute Details

#contentString (readonly)

Returns The original content.

Returns:

  • (String)

    The original content



38
39
40
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 38

def content
  @content
end

#modeSymbol (readonly)

Returns The normalization mode.

Returns:

  • (Symbol)

    The normalization mode



41
42
43
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 41

def mode
  @mode
end

#problemsDocumentProblems (readonly)

Returns Problems found during normalization.

Returns:



44
45
46
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 44

def problems
  @problems
end

Class Method Details

.normalize(content, mode: :basic) ⇒ String

Normalize whitespace in content (class method for convenience).

Parameters:

  • content (String)

    Content to normalize

  • mode (Symbol, Boolean) (defaults to: :basic)

    Normalization mode (:basic, :link_refs, :strict, or true for :basic)

Returns:

  • (String)

    Normalized content



52
53
54
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 52

def normalize(content, mode: :basic)
  new(content, mode: mode).normalize
end

Instance Method Details

#changed?Boolean

Check if normalization made any changes.

Returns:

  • (Boolean)

    true if content had whitespace issues



86
87
88
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 86

def changed?
  !@problems.empty?
end

#normalization_countInteger

Get count of normalizations performed.

Returns:

  • (Integer)

    Number of whitespace issues fixed



93
94
95
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 93

def normalization_count
  @problems.count
end

#normalizeString

Normalize whitespace based on the configured mode.

Returns:

  • (String)

    Normalized content



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 71

def normalize
  result = content.dup

  # Always collapse excessive blank lines (3+ → 2)
  result = collapse_excessive_blank_lines(result)

  # Remove blank lines between link refs if mode requires it
  result = remove_blank_lines_between_link_refs(result) if @mode == :link_refs || @mode == :strict

  result
end