Class: TRuby::DocGenerator

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

Overview

API Documentation Generator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ DocGenerator

Returns a new instance of DocGenerator.



11
12
13
14
15
16
17
18
19
20
# File 'lib/t_ruby/doc_generator.rb', line 11

def initialize(config = nil)
  @config = config || Config.new
  @docs = {
    types: {},
    interfaces: {},
    functions: {},
    modules: {}
  }
  @parser = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/t_ruby/doc_generator.rb', line 9

def config
  @config
end

#docsObject (readonly)

Returns the value of attribute docs.



9
10
11
# File 'lib/t_ruby/doc_generator.rb', line 9

def docs
  @docs
end

Instance Method Details

#generate(paths, output_dir: "docs") ⇒ Object

Generate documentation from source files



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/t_ruby/doc_generator.rb', line 23

def generate(paths, output_dir: "docs")
  puts "Generating T-Ruby API Documentation..."

  # Parse all files
  files = collect_files(paths)
  files.each { |file| parse_file(file) }

  # Generate output
  FileUtils.mkdir_p(output_dir)

  generate_index(output_dir)
  generate_type_docs(output_dir)
  generate_interface_docs(output_dir)
  generate_function_docs(output_dir)
  generate_module_docs(output_dir)
  generate_search_index(output_dir)

  puts "Documentation generated in #{output_dir}/"
  @docs
end

#generate_json(paths, output_path: "api.json") ⇒ Object

Generate JSON documentation



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/t_ruby/doc_generator.rb', line 145

def generate_json(paths, output_path: "api.json")
  files = collect_files(paths)
  files.each { |file| parse_file(file) }

  File.write(output_path, JSON.pretty_generate({
    generated_at: Time.now.iso8601,
    version: TRuby::VERSION,
    types: @docs[:types],
    interfaces: @docs[:interfaces],
    functions: @docs[:functions],
    modules: @docs[:modules]
  }))

  puts "JSON documentation generated: #{output_path}"
end

#generate_markdown(paths, output_path: "API.md") ⇒ Object

Generate single-file markdown documentation



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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/t_ruby/doc_generator.rb', line 45

def generate_markdown(paths, output_path: "API.md")
  files = collect_files(paths)
  files.each { |file| parse_file(file) }

  md = []
  md << "# T-Ruby API Documentation"
  md << ""
  md << "**Generated:** #{Time.now}"
  md << ""
  md << "## Table of Contents"
  md << ""
  md << "- [Types](#types)"
  md << "- [Interfaces](#interfaces)"
  md << "- [Functions](#functions)"
  md << ""

  # Types section
  md << "## Types"
  md << ""
  @docs[:types].each do |name, info|
    md << "### `#{name}`"
    md << ""
    md << "```typescript"
    md << "type #{name} = #{info[:definition]}"
    md << "```"
    md << ""
    md << info[:description] if info[:description]
    md << ""
    md << "**Source:** `#{info[:source]}`" if info[:source]
    md << ""
  end

  # Interfaces section
  md << "## Interfaces"
  md << ""
  @docs[:interfaces].each do |name, info|
    md << "### `#{name}`"
    md << ""
    md << info[:description] if info[:description]
    md << ""

    if info[:type_params]&.any?
      md << "**Type Parameters:** `<#{info[:type_params].join(', ')}>`"
      md << ""
    end

    if info[:members]&.any?
      md << "#### Members"
      md << ""
      md << "| Name | Type | Description |"
      md << "|------|------|-------------|"
      info[:members].each do |member|
        md << "| `#{member[:name]}` | `#{member[:type]}` | #{member[:description] || '-'} |"
      end
      md << ""
    end

    md << "**Source:** `#{info[:source]}`" if info[:source]
    md << ""
  end

  # Functions section
  md << "## Functions"
  md << ""
  @docs[:functions].each do |name, info|
    md << "### `#{name}`"
    md << ""
    md << info[:description] if info[:description]
    md << ""

    # Signature
    params = info[:params]&.map { |p| "#{p[:name]}: #{p[:type]}" }&.join(", ") || ""
    type_params = info[:type_params]&.any? ? "<#{info[:type_params].join(', ')}>" : ""
    md << "```ruby"
    md << "def #{name}#{type_params}(#{params}): #{info[:return_type] || 'void'}"
    md << "```"
    md << ""

    if info[:params]&.any?
      md << "#### Parameters"
      md << ""
      md << "| Name | Type | Description |"
      md << "|------|------|-------------|"
      info[:params].each do |param|
        md << "| `#{param[:name]}` | `#{param[:type]}` | #{param[:description] || '-'} |"
      end
      md << ""
    end

    md << "**Returns:** `#{info[:return_type]}`" if info[:return_type]
    md << ""
    md << "**Source:** `#{info[:source]}`" if info[:source]
    md << ""
  end

  File.write(output_path, md.join("\n"))
  puts "Documentation generated: #{output_path}"
end