Class: Slamdown::Conversion::Normaliser::Breaks

Inherits:
Object
  • Object
show all
Defined in:
lib/slamdown/conversion/normaliser/breaks.rb

Overview

Interprets line-break runs and bubbles recovered paragraph boundaries through inline wrappers without leaking internal sentinels.

Instance Method Summary collapse

Constructor Details

#initialize(node_factory, content, diagnostics) ⇒ Breaks

Returns a new instance of Breaks.



9
10
11
12
13
# File 'lib/slamdown/conversion/normaliser/breaks.rb', line 9

def initialize(node_factory, content, diagnostics)
	@node_factory = node_factory
	@content = content
	@diagnostics = diagnostics
end

Instance Method Details

#line_break?(node) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/slamdown/conversion/normaliser/breaks.rb', line 85

def line_break?(node)
	node.representation == :semantic && node.semantic_kind == :line_break
end

#paragraph_boundary?(node) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/slamdown/conversion/normaliser/breaks.rb', line 89

def paragraph_boundary?(node)
	node.representation == :boundary && node.semantic_kind == :paragraph_boundary
end

#split_break_runs(children) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slamdown/conversion/normaliser/breaks.rb', line 15

def split_break_runs(children)
	fragments = []
	current = []
	index = 0

	while index < children.length
		if paragraph_boundary?(children[index])
			if @content.visible_content?(current) && @content.visible_content?(children[(index + 1)..-1] || [])
				fragments << current
				current = []
			end
			index += 1
			next
		end

		unless line_break?(children[index])
			current << children[index]
			index += 1
			next
		end

		run_path = children[index].source_path
		break_count = 0
		interstitial = []
		cursor = index
		loop do
			if cursor < children.length && line_break?(children[cursor])
				break_count += 1
				cursor += 1
			elsif cursor < children.length && @content.whitespace_text?(children[cursor]) && next_break_index(children, cursor)
				interstitial << children[cursor]
				cursor += 1
			else
				break
			end
		end

		content_before = @content.visible_content?(current)
		content_after = @content.visible_content?(children[cursor..-1] || [])
		if break_count >= 2
			if content_before && content_after
				fragments << current
				current = []
				@diagnostics.add(:paragraph_break_recovered, :info, "Converted a repeated line-break run to a paragraph boundary", run_path)
			end
		elsif content_before && content_after
			current << children[index]
			current.concat(interstitial)
		end
		index = cursor
	end

	fragments << current if @content.visible_content?(current)
	fragments
end

#split_inline_fragments(children) ⇒ Object



79
80
81
82
83
# File 'lib/slamdown/conversion/normaliser/breaks.rb', line 79

def split_inline_fragments(children)
	return [children] unless children.any? { |child| line_break?(child) || paragraph_boundary?(child) }

	split_break_runs(children)
end

#wrap_inline_fragments(children, path) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/slamdown/conversion/normaliser/breaks.rb', line 71

def wrap_inline_fragments(children, path)
	fragments = split_inline_fragments(children)
	fragments.each_with_index.each_with_object([]) do |(fragment, index), result|
		result << @node_factory.paragraph_boundary(fragment.first ? fragment.first.source_path : path) if index > 0
		result.concat(yield(fragment))
	end
end