Module: LlmDocsBuilder::Helpers Private

Defined in:
lib/llm_docs_builder/helpers/prune_trailing_unsafe_link_separator.rb,
lib/llm_docs_builder/helpers.rb,
lib/llm_docs_builder/helpers/squeeze_blank_lines_outside_fences.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helper methods for content transformation

Class Method Summary collapse

Class Method Details

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Removes trailing pipe characters and whitespace from array of string parts

Parameters:

  • parts (Array<String>)

    array of string parts to process



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm_docs_builder/helpers/prune_trailing_unsafe_link_separator.rb', line 12

def prune_trailing_unsafe_link_separator!(parts)
  while parts.any?
    last = parts.last
    new_last = last.sub(/[ \t]*\|\s*\z/, '')

    if new_last != last
      trimmed = new_last.rstrip
      parts[-1] = trimmed
      parts.pop if trimmed.empty?
    elsif last.strip.empty?
      parts.pop
    else
      break
    end
  end
end

.squeeze_blank_lines_outside_fences(text, max_blank: 2, fence_chars: %w[` ~],, min_fence: 3) ⇒ String

Reduces consecutive blank lines outside of code fences

Parameters:

  • text (String)

    input text to process

  • max_blank (Integer) (defaults to: 2)

    maximum number of consecutive blank lines to allow

  • fence_chars (Array<String>) (defaults to: %w[` ~],)

    characters that can be used for code fences

  • min_fence (Integer) (defaults to: 3)

    minimum length of fence character sequence

Returns:

  • (String)

    processed text with squeezed blank lines



12
13
14
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
# File 'lib/llm_docs_builder/helpers/squeeze_blank_lines_outside_fences.rb', line 12

def squeeze_blank_lines_outside_fences(text, max_blank: 2, fence_chars: %w[` ~], min_fence: 3)
  return '' if text.to_s.empty?

  lines = text.split("\n", -1)

  inside_fence = false
  fence_indent = ''.dup
  fence_char   = nil
  fence_len    = 0

  # Build a fast “does this look like an opening fence?” regex
  # e.g., leading spaces + ``` or ~~~ (length >= min_fence) + optional info string
  fence_set = Regexp.escape(fence_chars.join)
  open_re   = /\A(\s*)([#{fence_set}])\2{#{min_fence - 1},}.*\z/

  out = []
  blank_streak = 0

  lines.each_with_index do |line, _idx|
    if inside_fence
      out << line
      # Closing fence must match indent, char, and fence length
      if line.match?(/\A#{Regexp.escape(fence_indent)}#{Regexp.escape(fence_char * fence_len)}\s*\z/)
        inside_fence = false
        fence_indent = ''.dup
        fence_char   = nil
        fence_len    = 0
      end
      next
    end

    if (m = line.match(open_re))
      # Enter fenced block; compute the *actual* fence length from the line
      fence_indent = m[1]
      fence_char   = m[2]
      after_indent = line[fence_indent.length..]
      fence_len    = after_indent[/\A#{Regexp.escape(fence_char)}+/].length
      inside_fence = true
      blank_streak = 0
      out << line
      next
    end

    # Outside fences: squeeze blank lines
    if line.strip.empty?
      blank_streak += 1
      # Keep at most max_blank blank lines; skip extras
      out << line if blank_streak <= max_blank
    else
      blank_streak = 0
      out << line
    end
  end

  out.join("\n")
end