Class: OpenGemdocs::YardJsonFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/open_gemdocs/yard_json_formatter.rb

Class Method Summary collapse

Class Method Details

.format_gem_docs(gem_name, object_path = nil) ⇒ Object



10
11
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
# File 'lib/open_gemdocs/yard_json_formatter.rb', line 10

def self.format_gem_docs(gem_name, object_path = nil)
  # Ensure YARD server data is available
  yard_db_path = find_yard_db(gem_name)

  unless yard_db_path
    # Try to generate the .yardoc if it doesn't exist
    gem_path = find_gem_path(gem_name)
    return { error: "Gem '#{gem_name}' not found" } unless gem_path

    # Create a temporary .yardoc path
    yard_db_path = File.join(Dir.tmpdir, "yard_#{gem_name}_#{Process.pid}", ".yardoc")
    FileUtils.mkdir_p(File.dirname(yard_db_path))

    # Generate YARD documentation
    YARD::CLI::Yardoc.run("--no-output", "--db", yard_db_path, gem_path)
  end

  # Load the YARD registry
  YARD::Registry.load(yard_db_path)

  if object_path
    # Return specific object documentation
    obj = YARD::Registry.at(object_path)
    return { error: "Object '#{object_path}' not found in #{gem_name}" } unless obj

    begin
      format_object(obj)
    rescue StandardError => e
      { error: "Failed to format object: #{e.message}\n#{e.backtrace.first(3).join("\n")}" }
    end
  else
    # Return gem overview with main classes and modules
    {
      gem: gem_name,
      summary: get_gem_summary(gem_name),
      namespaces: format_namespaces,
      classes: format_classes,
      modules: format_modules
    }
  end
ensure
  YARD::Registry.clear
end