Class: TRuby::DocsExampleExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/docs_example_extractor.rb

Overview

Extracts code examples from Markdown documentation files.

Supports extracting:

  • T-Ruby code blocks (“‘trb, “`t-ruby, “`ruby with type annotations)

  • Ruby code blocks for comparison

  • RBS type definitions

Examples:

extractor = DocsExampleExtractor.new
examples = extractor.extract_from_file("docs/getting-started.md")
examples.each { |ex| puts ex.code }

Defined Under Namespace

Classes: CodeExample

Constant Summary collapse

CODE_FENCE_PATTERN =

Code fence pattern: “‘language title=“file.ext” metadata Supports Docusaurus format: “`ruby title=“example.trb”

/^```(\w+)?(?:\s+title="([^"]*)")?(?:\s*\{([^}]*)\})?/

Instance Method Summary collapse

Instance Method Details

#extract_from_content(content, file_path = "<string>") ⇒ Array<CodeExample>

Extract all code examples from content

Parameters:

  • Markdown content

  • (defaults to: "<string>")

    Source file path (for reference)

Returns:

  • Extracted code examples



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/t_ruby/docs_example_extractor.rb', line 69

def extract_from_content(content, file_path = "<string>")
  examples = []
  lines = content.lines
  in_code_block = false
  current_block = nil
  block_start_line = 0

  lines.each_with_index do |line, index|
    line_number = index + 1

    if !in_code_block && (match = line.match(CODE_FENCE_PATTERN))
      in_code_block = true
      block_start_line = line_number
      lang = match[1] || "text"
      title = match[2]
       = match[3]

      # If title ends with .trb, treat as T-Ruby regardless of language tag
      if title&.end_with?(".trb")
        lang = "trb"
      end

      current_block = {
        language: lang,
        metadata: ,
        title: title,
        lines: [],
      }
    elsif in_code_block && line.match(/^```\s*$/)
      in_code_block = false

      # Only include relevant languages
      if relevant_language?(current_block[:language])
        examples << CodeExample.new(
          code: current_block[:lines].join,
          language: normalize_language(current_block[:language]),
          file_path: file_path,
          line_number: block_start_line,
          metadata: current_block[:metadata]
        )
      end

      current_block = nil
    elsif in_code_block
      current_block[:lines] << line
    end
  end

  examples
end

#extract_from_file(file_path) ⇒ Array<CodeExample>

Extract all code examples from a file

Parameters:

  • Path to the markdown file

Returns:

  • Extracted code examples



59
60
61
62
# File 'lib/t_ruby/docs_example_extractor.rb', line 59

def extract_from_file(file_path)
  content = File.read(file_path, encoding: "UTF-8")
  extract_from_content(content, file_path)
end

#extract_from_glob(pattern) ⇒ Array<CodeExample>

Extract from multiple files using glob pattern

Parameters:

  • Glob pattern (e.g., “docs/*/.md”)

Returns:

  • All extracted examples



124
125
126
# File 'lib/t_ruby/docs_example_extractor.rb', line 124

def extract_from_glob(pattern)
  Dir.glob(pattern).flat_map { |file| extract_from_file(file) }
end

#statistics(examples) ⇒ Hash

Get statistics about extracted examples

Parameters:

  • Code examples

Returns:

  • Statistics



132
133
134
135
136
137
138
139
140
141
# File 'lib/t_ruby/docs_example_extractor.rb', line 132

def statistics(examples)
  {
    total: examples.size,
    trb: examples.count(&:trb?),
    ruby: examples.count(&:ruby?),
    rbs: examples.count(&:rbs?),
    verifiable: examples.count(&:should_verify?),
    files: examples.map(&:file_path).uniq.size,
  }
end