Class: LlmDocsBuilder::HtmlToMarkdown::FigureCodeBlockRenderer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_docs_builder/html_to_markdown/figure_code_block_renderer.rb

Overview

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

Converts <figure> elements that actually contain syntax-highlighted code back into fenced Markdown.

Constant Summary collapse

GENERIC_CODE_CLASSES =

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

Generic CSS class names commonly used for code formatting that should be ignored

%w[highlight code main gutter numbers line-numbers line-number line wrap table].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, inline_collapser:, fence_calculator:) ⇒ FigureCodeBlockRenderer

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.

Initialize a new figure code block renderer

Parameters:

  • element (Nokogiri::XML::Node)

    the figure element to render

  • inline_collapser (Proc)

    callable for collapsing inline content

  • fence_calculator (Proc)

    callable for calculating fence length



25
26
27
28
29
# File 'lib/llm_docs_builder/html_to_markdown/figure_code_block_renderer.rb', line 25

def initialize(element, inline_collapser:, fence_calculator:)
  @element = element
  @inline_collapser = inline_collapser
  @fence_calculator = fence_calculator
end

Instance Attribute Details

#code_block_nodeNokogiri::XML::Node? (readonly)

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.

Returns the identified code block node.

Returns:

  • (Nokogiri::XML::Node, nil)

    the identified code block node



18
19
20
# File 'lib/llm_docs_builder/html_to_markdown/figure_code_block_renderer.rb', line 18

def code_block_node
  @code_block_node
end

Instance Method Details

#renderString?

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.

Render the figure as a fenced code block

Returns:

  • (String, nil)

    markdown fenced code block or nil if not a code figure



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/llm_docs_builder/html_to_markdown/figure_code_block_renderer.rb', line 34

def render
  @code_block_node = nil
  return unless code_figure?

  lines = extract_figure_code_lines
  return if lines.empty?

  language = detect_code_language
  caption = caption_text
  info_string = [language, caption].compact.reject(&:empty?).join(' ')
  code_body = lines.join("\n")
  fence = fence_calculator.call(code_body)
  opening_fence = info_string.empty? ? fence : "#{fence}#{info_string}"
  "#{opening_fence}\n#{code_body}\n#{fence}"
end