templates/default/fulldoc/markdown/setup.rb in yard-markdown-0.2.0 vs templates/default/fulldoc/markdown/setup.rb in yard-markdown-0.2.1

- old
+ new

@@ -1,15 +1,15 @@ # frozen_string_literal: true # https://github.com/lsegal/yard/blob/2d197a381c5d4cc5c55b2c60fff992b31c986361/docs/CodeObjects.md require "erb" +require "csv" -def init - # here I need to copy README.md if there is one. - # I also need to write index.md files +include Helpers::ModuleHelper +def init options.objects = objects = run_verifier(options.objects) options.delete(:objects) options.delete(:files) @@ -19,62 +19,83 @@ objects.each do |object| next if object.name == :root begin - Templates::Engine.with_serializer(object, options.serializer) do - serialize(object) - end + Templates::Engine.with_serializer(object, options.serializer) { serialize(object) } rescue => e path = options.serializer.serialized_path(object) log.error "Exception occurred while generating '#{path}'" log.backtrace(e) end end serialize_index(objects) end - -require 'csv' - def serialize_index(objects) filepath = "#{options.serializer.basepath}/index.csv" - CSV.open(filepath, 'wb') do |csv| + CSV.open(filepath, "wb") do |csv| csv << %w[name type path] objects.each do |object| next if object.name == :root if object.type == :class - csv << [object.name, 'Class', options.serializer.serialized_path(object)] + csv << [object.name, "Class", options.serializer.serialized_path(object)] elsif object.type == :module - csv << [object.name, 'Module', options.serializer.serialized_path(object)] + csv << [object.name, "Module", options.serializer.serialized_path(object)] end if constant_listing.size.positive? - constant_listing.each { |cnst| csv << [cnst.name(false), 'Constant', (options.serializer.serialized_path(object) + "#" +aref(cnst))] } + constant_listing.each do |cnst| + csv << [ + cnst.name(false), + "Constant", + (options.serializer.serialized_path(object) + "#" + aref(cnst)), + ] + end end if (insmeths = public_instance_methods(object)).size > 0 - insmeths.each { |item| csv << [item.name(false), 'Method', options.serializer.serialized_path(object) + "#" + aref(item)] } + insmeths.each do |item| + csv << [ + item.name(false), + "Method", + options.serializer.serialized_path(object) + "#" + aref(item), + ] + end end if (pubmeths = public_class_methods(object)).size > 0 - pubmeths.each { |item| csv << [item.name(false), 'Method', options.serializer.serialized_path(object) + '#' + aref(item)]} + pubmeths.each do |item| + csv << [ + item.name(false), + "Method", + options.serializer.serialized_path(object) + "#" + aref(item), + ] + end end if (attrs = attr_listing(object)).size > 0 - attrs.each { |item| csv << [item.name(false), 'Attribute', options.serializer.serialized_path(object) + "#" + aref(item)]} + attrs.each do |item| + csv << [ + item.name(false), + "Attribute", + options.serializer.serialized_path(object) + "#" + aref(item), + ] + end end end end end def serialize(object) - template = ERB.new('# <%= format_object_title object %> + template = + ERB.new( + '# <%= format_object_title object %> | | | | -----------------: | :----- | <% if CodeObjects::ClassObject === object && object.superclass %> | **Inherits:** | <%= object.superclass %> | <% end %> @@ -94,11 +115,11 @@ <% if constant_listing.size > 0 %> <% groups(constant_listing, "Constants") do |list, name| %> # <%= name %> <% list.each do |cnst| %> ## <%= cnst.name %> = [](#<%=aref(cnst)%>) - (<%= cnst.value %>) <%= cnst.docstring %> + <%= cnst.docstring %> <% end %> <% end %> <% end %> @@ -125,11 +146,13 @@ ## <%= item.name %><%= item.reader? ? "[RW]" : "[R]" %> [](#<%=aref(item)%>) <%= item.docstring %> <% end %> <% end %> - '.gsub(/^ /, ''), trim_mode: '%<>') + '.gsub(/^ /, ""), + trim_mode: "%<>", + ) template.result(binding) end def aref(object) @@ -148,14 +171,15 @@ @constants = object.constants(included: false, inherited: false) @constants += object.cvars @constants end -include Helpers::ModuleHelper - def public_method_list(object) - prune_method_listing(object.meths(inherited: false, visibility: [:public]), included: false).sort_by { |m| m.name.to_s } + prune_method_listing( + object.meths(inherited: false, visibility: [:public]), + included: false, + ).sort_by { |m| m.name.to_s } end def public_class_methods(object) public_method_list(object).select { |o| o.scope == :class } end @@ -164,22 +188,23 @@ public_method_list(object).select { |o| o.scope == :instance } end def attr_listing(object) @attrs = [] - object.inheritance_tree(true).each do |superclass| - next if superclass.is_a?(CodeObjects::Proxy) - next if !options.embed_mixins.empty? && - !options.embed_mixins_match?(superclass) - [:class, :instance].each do |scope| - superclass.attributes[scope].each do |_name, rw| - attr = prune_method_listing([rw[:read], rw[:write]].compact, false).first - @attrs << attr if attr + object + .inheritance_tree(true) + .each do |superclass| + next if superclass.is_a?(CodeObjects::Proxy) + next if !options.embed_mixins.empty? && !options.embed_mixins_match?(superclass) + %i[class instance].each do |scope| + superclass.attributes[scope].each do |_name, rw| + attr = prune_method_listing([rw[:read], rw[:write]].compact, false).first + @attrs << attr if attr + end end + break if options.embed_mixins.empty? end - break if options.embed_mixins.empty? - end sort_listing @attrs end def generate_method_list @items = prune_method_listing(Registry.all(:method), false) @@ -191,11 +216,11 @@ # generate_list_contents # binding.irb end def sort_listing(list) - list.sort_by {|o| [o.scope.to_s, o.name.to_s.downcase] } + list.sort_by { |o| [o.scope.to_s, o.name.to_s.downcase] } end def groups(list, type = "Method") groups_data = object.groups if groups_data @@ -206,16 +231,10 @@ yield(items, name) unless items.empty? end else others = [] group_data = {} - list.each do |itm| - if itm.group - (group_data[itm.group] ||= []) << itm - else - others << itm - end - end + list.each { |itm| itm.group ? (group_data[itm.group] ||= []) << itm : others << itm } group_data.each { |group, items| yield(items, group) unless items.empty? } end return if others.empty? if others.first.respond_to?(:scope)