Skip to content

Commit

Permalink
Add NameList
Browse files Browse the repository at this point in the history
  • Loading branch information
zzak committed May 12, 2023
1 parent 9047b7c commit 137ada7
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Main (3.0.0.alpha)
* [#197](https://github.com/zzak/sdoc/pull/197) Remove sdoc-merge and sdoc template [@zzak](https://github.com/zzak)
* [#195](https://github.com/zzak/sdoc/pull/195) Replace turbolinks with turbo v7.3.0 [@zzak](https://github.com/zzak)
* [#200](https://github.com/zzak/sdoc/pull/200) Add canonical url link to all pages [@zzak](https://github.com/zzak)
* [#222](https://github.com/zzak/sdoc/pull/222) Add NameList for building a static file of classes and methods [@zzak](https://github.com/zzak)

2.6.1
=====
Expand Down
1 change: 1 addition & 0 deletions lib/sdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
module SDoc; end

require 'sdoc/generator'
require 'sdoc/name_list'
133 changes: 133 additions & 0 deletions lib/sdoc/name_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# frozen_string_literal: true

class RDoc::Generator::NameList

include RDoc::Text

attr_reader :index # :nodoc:

RDoc::RDoc.add_generator self

##
# Creates a new generator.

def initialize store, options
@store = store
@options = options

@classes = nil
@files = nil
@index = nil
end

##
# Builds the list of namespaces and methods as a Hash.

def build_index
reset @store.all_files.sort, @store.all_classes_and_modules.sort

index_classes
index_methods

{ :index => @index }
end

##
# Output progress information if debugging is enabled

def debug_msg *msg
return unless $DEBUG_RDOC
$stderr.puts(*msg)
end

##
# Writes the name list to disk

def generate
debug_msg "Generating Name List"
data = build_index

return if @options.dry_run

out_dir = Pathname.new "."

FileUtils.mkdir_p out_dir, :verbose => $DEBUG_RDOC

generate_classes_index data[:index][:classes], out_dir
generate_methods_index data[:index][:methods], out_dir
end

def generate_classes_index data, out_dir
classes_file = out_dir + File.join("classes")

debug_msg " writing classes index to %s" % classes_file
classes_file.open 'w', 0644 do |io|
io.set_encoding Encoding::UTF_8

io.puts data
end
end

def generate_methods_index data, out_dir
methods_file = out_dir + File.join("methods")

debug_msg " writing methods index to %s" % methods_file
methods_file.open 'w', 0644 do |io|
io.set_encoding Encoding::UTF_8

io.puts data
end
end

##
# Adds classes and modules to the index

def index_classes
debug_msg " generating class name index"

documented = @classes.uniq.select do |klass|
klass.document_self_or_methods
end.flatten.sort_by(&:full_name)

documented.each do |klass|
debug_msg " #{klass.full_name}"
@index[:classes] << "#{klass.full_name}"
end
end

##
# Adds methods to the index

def index_methods
debug_msg " generating method name index"

list = @classes.uniq.map do |klass|
klass.method_list
end.flatten.sort_by do |method|
[method.parent.full_name, method.type, method.name]
end

list.each do |method|
debug_msg " #{method.full_name}"
@index[:methods] << "#{method.full_name}"
end
end

def class_dir # :nodoc:
nil
end

def file_dir # :nodoc:
nil
end

def reset files, classes # :nodoc:
@files = files
@classes = classes

@index = {
:classes => [],
:methods => []
}
end
end

0 comments on commit 137ada7

Please sign in to comment.