Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#267) Don’t show “Public X” header without contents #314

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 62 additions & 20 deletions lib/puppet-strings/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,59 @@

# module for parsing Yard Registries and generating markdown
module PuppetStrings::Markdown
require_relative 'markdown/puppet_classes'
require_relative 'markdown/functions'
require_relative 'markdown/defined_types'
require_relative 'markdown/data_types'
require_relative 'markdown/resource_types'
require_relative 'markdown/puppet_tasks'
require_relative 'markdown/puppet_plans'
require_relative 'markdown/table_of_contents'
require_relative 'markdown/puppet_class'
require_relative 'markdown/function'
require_relative 'markdown/defined_type'
require_relative 'markdown/data_type'
require_relative 'markdown/resource_type'
require_relative 'markdown/puppet_task'
require_relative 'markdown/puppet_plan'

# Get classes that handle collecting and rendering each section/group.
#
# @return [Array[class]] The classes
def self.groups
[
PuppetStrings::Markdown::PuppetClass,
PuppetStrings::Markdown::DefinedType,
PuppetStrings::Markdown::ResourceType,
PuppetStrings::Markdown::Function,
PuppetStrings::Markdown::DataType,
PuppetStrings::Markdown::PuppetTask,
PuppetStrings::Markdown::PuppetPlan,
]
end

# generates markdown documentation
# @return [String] markdown doc
def self.generate
final = "# Reference\n\n"
final += "<!-- DO NOT EDIT: This document was generated by Puppet Strings -->\n\n"
final += PuppetStrings::Markdown::TableOfContents.render
final += PuppetStrings::Markdown::PuppetClasses.render
final += PuppetStrings::Markdown::DefinedTypes.render
final += PuppetStrings::Markdown::ResourceTypes.render
final += PuppetStrings::Markdown::Functions.render
final += PuppetStrings::Markdown::DataTypes.render
final += PuppetStrings::Markdown::PuppetTasks.render
final += PuppetStrings::Markdown::PuppetPlans.render

final
output = [
"# Reference\n\n",
"<!-- DO NOT EDIT: This document was generated by Puppet Strings -->\n\n",
"## Table of Contents\n\n",
]

# Create table of contents
template = erb(File.join(__dir__, 'markdown', 'templates', 'table_of_contents.erb'))
groups.each do |group|
group_name = group.group_name
items = group.items.map { |item| item.toc_info }
has_private = items.any? { |item| item[:private] }
has_public = items.any? { |item| !item[:private] }

output << template.result(binding)
end

# Create actual contents
groups.each do |group|
items = group.items.reject { |item| item.private? }
unless items.empty?
output << "## #{group.group_name}\n\n"
output.append(items.map { |item| item.render })
end
end

output.join('')
end

# mimicks the behavior of the json render, although path will never be nil
Expand All @@ -41,4 +70,17 @@ def self.render(path = nil)
YARD::Logger.instance.debug "Wrote markdown to #{path}"
end
end

# Helper function to load an ERB template.
#
# @param [String] path The full path to the template file.
# @return [ERB] Template
def self.erb(path)
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this, but later on I realized you could also write it with the if outside of def so it's only ran once, though it probably doesn't matter much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm… I thought about it, but I’m inclined not to since it ends up duplicating the doc comment. Or maybe I should just put the docs above the outer if if I do that? Pretty sure the performance difference will be negligible.

ERB.new(File.read(path), trim_mode: '-')
else
# This outputs warnings in Ruby 2.6+.
ERB.new(File.read(path), nil, '-')
end
end
end
40 changes: 27 additions & 13 deletions lib/puppet-strings/markdown/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ module PuppetStrings::Markdown
# ") inherits foo::bar {\n" +
# "}"}
class Base
# Set or return the name of the group
#
# @param [Optional[String]] Name of the group to set
# @return [String] Name of the group
def self.group_name(name = nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts about splitting the functionality in self.group_name() and self.group_name=(name)? Would that work?

Same comment applies to yard_types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried doing

class << self
  attr_accessor :group_name
  attr_accessor :yard_types
end

but I ran into a few problems. One was that it interpreted yard_types = [...] as trying to set a local variable instead of calling a class method, but maybe I could have called self.yard_types = [...].

I wanted it to look more like functions like attr_accessor since I figured that would feel more natural, but it did seem inconsistent with the way Ruby does things elsewhere.

I don’t have a real good sense for the Ruby Way anymore… been writing other languages for too long.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I suppose this is good enough.

@group_name = name if name
@group_name
end

# Set or return the types registered with YARD
#
# @param [Optional[Array[Symbol]]] Array of symbols registered with YARD to set
# @return [Array[Symbol]] Array of symbols registered with YARD
def self.yard_types(types = nil)
@yard_types = types if types
@yard_types
end

# @return [Array] list of items
def self.items
yard_types
.flat_map { |type| YARD::Registry.all(type) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.sort_by(&:name)
.map(&:to_hash)
.map { |i| new(i) }
Comment on lines +76 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can combine both maps

Suggested change
.map(&:to_hash)
.map { |i| new(i) }
.map { |i| new(i.to_hash) }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

end

def initialize(registry, component_type)
@type = component_type
@registry = registry
Expand Down Expand Up @@ -200,17 +227,4 @@ def clean_link(input)
input.tr('^a-zA-Z0-9_-', '-')
end
end

# Helper function to load an ERB template.
#
# @param [String] path The full path to the template file.
# @return [ERB] Template
def self.erb(path)
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
ERB.new(File.read(path), trim_mode: '-')
else
# This outputs warnings in Ruby 2.6+.
ERB.new(File.read(path), nil, '-')
end
end
end
3 changes: 3 additions & 0 deletions lib/puppet-strings/markdown/data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class DataType < Base
attr_reader :alias_of
attr_reader :functions

group_name 'Data types'
yard_types [:puppet_data_type, :puppet_data_type_alias]

def initialize(registry)
@template = 'data_type.erb'
super(registry, 'data type')
Expand Down
40 changes: 0 additions & 40 deletions lib/puppet-strings/markdown/data_types.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/puppet-strings/markdown/defined_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module PuppetStrings::Markdown
# Generates Markdown for a Puppet Defined Type.
class DefinedType < Base
group_name 'Defined types'
yard_types [:puppet_defined_type]

def initialize(registry)
@template = 'classes_and_defines.erb'
super(registry, 'defined type')
Expand Down
37 changes: 0 additions & 37 deletions lib/puppet-strings/markdown/defined_types.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/puppet-strings/markdown/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module PuppetStrings::Markdown
class Function < Base
attr_reader :signatures

group_name 'Functions'
yard_types [:puppet_function]

def initialize(registry)
@template = 'function.erb'
super(registry, 'function')
Expand Down
37 changes: 0 additions & 37 deletions lib/puppet-strings/markdown/functions.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/puppet-strings/markdown/puppet_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module PuppetStrings::Markdown
# Generates Markdown for a Puppet Class.
class PuppetClass < Base
group_name 'Classes'
yard_types [:puppet_class]

def initialize(registry)
@template = 'classes_and_defines.erb'
super(registry, 'class')
Expand Down
37 changes: 0 additions & 37 deletions lib/puppet-strings/markdown/puppet_classes.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/puppet-strings/markdown/puppet_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module PuppetStrings::Markdown
# Generates Markdown for a Puppet Plan.
class PuppetPlan < Base
group_name 'Plans'
yard_types [:puppet_plan]

def initialize(registry)
@template = 'classes_and_defines.erb'
super(registry, 'plan')
Expand Down
37 changes: 0 additions & 37 deletions lib/puppet-strings/markdown/puppet_plans.rb

This file was deleted.

Loading