Skip to content

Commit f6d615c

Browse files
authored
Merge pull request #314 from danielparks/better_public_private
(#267) Don’t show “Public X” header without contents
2 parents f98873d + 64aa800 commit f6d615c

19 files changed

+256
-367
lines changed

lib/puppet-strings/markdown.rb

+62-20
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,59 @@
44

55
# module for parsing Yard Registries and generating markdown
66
module PuppetStrings::Markdown
7-
require_relative 'markdown/puppet_classes'
8-
require_relative 'markdown/functions'
9-
require_relative 'markdown/defined_types'
10-
require_relative 'markdown/data_types'
11-
require_relative 'markdown/resource_types'
12-
require_relative 'markdown/puppet_tasks'
13-
require_relative 'markdown/puppet_plans'
14-
require_relative 'markdown/table_of_contents'
7+
require_relative 'markdown/puppet_class'
8+
require_relative 'markdown/function'
9+
require_relative 'markdown/defined_type'
10+
require_relative 'markdown/data_type'
11+
require_relative 'markdown/resource_type'
12+
require_relative 'markdown/puppet_task'
13+
require_relative 'markdown/puppet_plan'
14+
15+
# Get classes that handle collecting and rendering each section/group.
16+
#
17+
# @return [Array[class]] The classes
18+
def self.groups
19+
[
20+
PuppetStrings::Markdown::PuppetClass,
21+
PuppetStrings::Markdown::DefinedType,
22+
PuppetStrings::Markdown::ResourceType,
23+
PuppetStrings::Markdown::Function,
24+
PuppetStrings::Markdown::DataType,
25+
PuppetStrings::Markdown::PuppetTask,
26+
PuppetStrings::Markdown::PuppetPlan,
27+
]
28+
end
1529

1630
# generates markdown documentation
1731
# @return [String] markdown doc
1832
def self.generate
19-
final = "# Reference\n\n"
20-
final += "<!-- DO NOT EDIT: This document was generated by Puppet Strings -->\n\n"
21-
final += PuppetStrings::Markdown::TableOfContents.render
22-
final += PuppetStrings::Markdown::PuppetClasses.render
23-
final += PuppetStrings::Markdown::DefinedTypes.render
24-
final += PuppetStrings::Markdown::ResourceTypes.render
25-
final += PuppetStrings::Markdown::Functions.render
26-
final += PuppetStrings::Markdown::DataTypes.render
27-
final += PuppetStrings::Markdown::PuppetTasks.render
28-
final += PuppetStrings::Markdown::PuppetPlans.render
29-
30-
final
33+
output = [
34+
"# Reference\n\n",
35+
"<!-- DO NOT EDIT: This document was generated by Puppet Strings -->\n\n",
36+
"## Table of Contents\n\n",
37+
]
38+
39+
# Create table of contents
40+
template = erb(File.join(__dir__, 'markdown', 'templates', 'table_of_contents.erb'))
41+
groups.each do |group|
42+
group_name = group.group_name
43+
items = group.items.map { |item| item.toc_info }
44+
has_private = items.any? { |item| item[:private] }
45+
has_public = items.any? { |item| !item[:private] }
46+
47+
output << template.result(binding)
48+
end
49+
50+
# Create actual contents
51+
groups.each do |group|
52+
items = group.items.reject { |item| item.private? }
53+
unless items.empty?
54+
output << "## #{group.group_name}\n\n"
55+
output.append(items.map { |item| item.render })
56+
end
57+
end
58+
59+
output.join('')
3160
end
3261

3362
# mimicks the behavior of the json render, although path will never be nil
@@ -41,4 +70,17 @@ def self.render(path = nil)
4170
YARD::Logger.instance.debug "Wrote markdown to #{path}"
4271
end
4372
end
73+
74+
# Helper function to load an ERB template.
75+
#
76+
# @param [String] path The full path to the template file.
77+
# @return [ERB] Template
78+
def self.erb(path)
79+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
80+
ERB.new(File.read(path), trim_mode: '-')
81+
else
82+
# This outputs warnings in Ruby 2.6+.
83+
ERB.new(File.read(path), nil, '-')
84+
end
85+
end
4486
end

lib/puppet-strings/markdown/base.rb

+27-13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ module PuppetStrings::Markdown
5050
# ") inherits foo::bar {\n" +
5151
# "}"}
5252
class Base
53+
# Set or return the name of the group
54+
#
55+
# @param [Optional[String]] Name of the group to set
56+
# @return [String] Name of the group
57+
def self.group_name(name = nil)
58+
@group_name = name if name
59+
@group_name
60+
end
61+
62+
# Set or return the types registered with YARD
63+
#
64+
# @param [Optional[Array[Symbol]]] Array of symbols registered with YARD to set
65+
# @return [Array[Symbol]] Array of symbols registered with YARD
66+
def self.yard_types(types = nil)
67+
@yard_types = types if types
68+
@yard_types
69+
end
70+
71+
# @return [Array] list of items
72+
def self.items
73+
yard_types
74+
.flat_map { |type| YARD::Registry.all(type) }
75+
.sort_by(&:name)
76+
.map(&:to_hash)
77+
.map { |i| new(i) }
78+
end
79+
5380
def initialize(registry, component_type)
5481
@type = component_type
5582
@registry = registry
@@ -200,17 +227,4 @@ def clean_link(input)
200227
input.tr('^a-zA-Z0-9_-', '-')
201228
end
202229
end
203-
204-
# Helper function to load an ERB template.
205-
#
206-
# @param [String] path The full path to the template file.
207-
# @return [ERB] Template
208-
def self.erb(path)
209-
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
210-
ERB.new(File.read(path), trim_mode: '-')
211-
else
212-
# This outputs warnings in Ruby 2.6+.
213-
ERB.new(File.read(path), nil, '-')
214-
end
215-
end
216230
end

lib/puppet-strings/markdown/data_type.rb

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class DataType < Base
88
attr_reader :alias_of
99
attr_reader :functions
1010

11+
group_name 'Data types'
12+
yard_types [:puppet_data_type, :puppet_data_type_alias]
13+
1114
def initialize(registry)
1215
@template = 'data_type.erb'
1316
super(registry, 'data type')

lib/puppet-strings/markdown/data_types.rb

-40
This file was deleted.

lib/puppet-strings/markdown/defined_type.rb

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
module PuppetStrings::Markdown
66
# Generates Markdown for a Puppet Defined Type.
77
class DefinedType < Base
8+
group_name 'Defined types'
9+
yard_types [:puppet_defined_type]
10+
811
def initialize(registry)
912
@template = 'classes_and_defines.erb'
1013
super(registry, 'defined type')

lib/puppet-strings/markdown/defined_types.rb

-37
This file was deleted.

lib/puppet-strings/markdown/function.rb

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module PuppetStrings::Markdown
77
class Function < Base
88
attr_reader :signatures
99

10+
group_name 'Functions'
11+
yard_types [:puppet_function]
12+
1013
def initialize(registry)
1114
@template = 'function.erb'
1215
super(registry, 'function')

lib/puppet-strings/markdown/functions.rb

-37
This file was deleted.

lib/puppet-strings/markdown/puppet_class.rb

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
module PuppetStrings::Markdown
66
# Generates Markdown for a Puppet Class.
77
class PuppetClass < Base
8+
group_name 'Classes'
9+
yard_types [:puppet_class]
10+
811
def initialize(registry)
912
@template = 'classes_and_defines.erb'
1013
super(registry, 'class')

lib/puppet-strings/markdown/puppet_classes.rb

-37
This file was deleted.

lib/puppet-strings/markdown/puppet_plan.rb

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
module PuppetStrings::Markdown
66
# Generates Markdown for a Puppet Plan.
77
class PuppetPlan < Base
8+
group_name 'Plans'
9+
yard_types [:puppet_plan]
10+
811
def initialize(registry)
912
@template = 'classes_and_defines.erb'
1013
super(registry, 'plan')

lib/puppet-strings/markdown/puppet_plans.rb

-37
This file was deleted.

0 commit comments

Comments
 (0)