Class: Markdown::Merge::WhitespaceNormalizer
- Inherits:
-
Object
- Object
- Markdown::Merge::WhitespaceNormalizer
- Defined in:
- lib/markdown/merge/whitespace_normalizer.rb
Overview
Normalizes whitespace in markdown documents.
Supports multiple normalization modes:
:basic(ortrue) - 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
Constant Summary collapse
- MODES =
Valid normalization modes
%i[basic link_refs strict].freeze
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The original content.
-
#mode ⇒ Symbol
readonly
The normalization mode.
-
#problems ⇒ DocumentProblems
readonly
Problems found during normalization.
Class Method Summary collapse
-
.normalize(content, mode: :basic) ⇒ String
Normalize whitespace in content (class method for convenience).
Instance Method Summary collapse
-
#changed? ⇒ Boolean
Check if normalization made any changes.
-
#initialize(content, mode: :basic) ⇒ WhitespaceNormalizer
constructor
Initialize a new normalizer.
-
#normalization_count ⇒ Integer
Get count of normalizations performed.
-
#normalize ⇒ String
Normalize whitespace based on the configured mode.
Constructor Details
#initialize(content, mode: :basic) ⇒ WhitespaceNormalizer
Initialize a new normalizer.
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
#content ⇒ String (readonly)
Returns The original content.
38 39 40 |
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 38 def content @content end |
#mode ⇒ Symbol (readonly)
Returns The normalization mode.
41 42 43 |
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 41 def mode @mode end |
#problems ⇒ DocumentProblems (readonly)
Returns Problems found during normalization.
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).
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.
86 87 88 |
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 86 def changed? !@problems.empty? end |
#normalization_count ⇒ Integer
Get count of normalizations performed.
93 94 95 |
# File 'lib/markdown/merge/whitespace_normalizer.rb', line 93 def normalization_count @problems.count end |
#normalize ⇒ String
Normalize whitespace based on the configured mode.
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 |