Skip to content

Commit 3991e75

Browse files
committed
(puppetlabs#267) Don’t show “Public X” header without contents
Previously, the “Public X” header would be displayed if there were any private X regardless of whether there were any public X. For example, having a private function would make both the “Public Functions” and “Private Functions” headers appear, even if there weren’t any public functions. This changes it so that there are three conditions: * **Only public X:** no “Public” or “Private“ headers are displayed; the X are listed with links to their documentation. (Public is implied.) * **Only private X:** a “Private X” header is is displayed with a list of X. There are no links to documentation for private APIs. * **Both public and private X:** both “Public X” and “Private X” headers are displayed. The public Xs are listed with links to their documentation; the private Xs are just listed with no links. In other words, if there are no private Xs, then it just lists the public once. Otherwise, it splits them up under public/private headers but avoids showing a header if it’s contents will be empty. This also radically simplifies and removes a bunch of boilerplate code around rendering these sections.
1 parent 13f2953 commit 3991e75

12 files changed

+237
-279
lines changed

lib/puppet-strings/markdown.rb

+55-13
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,52 @@ module PuppetStrings::Markdown
1111
require_relative 'markdown/resource_types'
1212
require_relative 'markdown/puppet_tasks'
1313
require_relative 'markdown/puppet_plans'
14-
require_relative 'markdown/table_of_contents'
14+
15+
# Get modules that handle collecting and rendering each section.
16+
#
17+
# @return [Array[module]] The modules
18+
def self.groups
19+
[
20+
PuppetStrings::Markdown::PuppetClasses,
21+
PuppetStrings::Markdown::DefinedTypes,
22+
PuppetStrings::Markdown::ResourceTypes,
23+
PuppetStrings::Markdown::Functions,
24+
PuppetStrings::Markdown::DataTypes,
25+
PuppetStrings::Markdown::PuppetTasks,
26+
PuppetStrings::Markdown::PuppetPlans,
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.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.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

-13
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,4 @@ def clean_link(input)
199199
input.tr('^a-zA-Z0-9_-', '-')
200200
end
201201
end
202-
203-
# Helper function to load an ERB template.
204-
#
205-
# @param [String] path The full path to the template file.
206-
# @return [ERB] Template
207-
def self.erb(path)
208-
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
209-
ERB.new(File.read(path), trim_mode: '-')
210-
else
211-
# This outputs warnings in Ruby 2.6+.
212-
ERB.new(File.read(path), nil, '-')
213-
end
214-
end
215202
end

lib/puppet-strings/markdown/data_types.rb

+5-27
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,19 @@
33
require_relative 'data_type'
44

55
module PuppetStrings::Markdown
6+
# Adapter to get information about data types
67
module DataTypes
8+
def self.name
9+
'Data types'
10+
end
711

812
# @return [Array] list of data types
9-
def self.in_dtypes
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_data_type).map!(&:to_hash)
1115
arr.concat(YARD::Registry.all(:puppet_data_type_alias).map!(&:to_hash))
1216

1317
arr.sort! { |a,b| a[:name] <=> b[:name] }
1418
arr.map! { |a| PuppetStrings::Markdown::DataType.new(a) }
1519
end
16-
17-
def self.contains_private?
18-
result = false
19-
unless in_dtypes.nil?
20-
in_dtypes.find { |type| type.private? }.nil? ? false : true
21-
end
22-
end
23-
24-
def self.render
25-
final = in_dtypes.length > 0 ? "## Data types\n\n" : ""
26-
in_dtypes.each do |type|
27-
final += type.render unless type.private?
28-
end
29-
final
30-
end
31-
32-
33-
def self.toc_info
34-
final = ["Data types"]
35-
36-
in_dtypes.each do |type|
37-
final.push(type.toc_info)
38-
end
39-
40-
final
41-
end
4220
end
4321
end

lib/puppet-strings/markdown/defined_types.rb

+5-26
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,16 @@
33
require_relative 'defined_type'
44

55
module PuppetStrings::Markdown
6+
# Adapter to get information about defined types
67
module DefinedTypes
8+
def self.name
9+
'Defined types'
10+
end
711

812
# @return [Array] list of defined types
9-
def self.in_dtypes
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash)
1115
arr.map! { |a| PuppetStrings::Markdown::DefinedType.new(a) }
1216
end
13-
14-
def self.contains_private?
15-
result = false
16-
unless in_dtypes.nil?
17-
in_dtypes.find { |type| type.private? }.nil? ? false : true
18-
end
19-
end
20-
21-
def self.render
22-
final = in_dtypes.length > 0 ? "## Defined types\n\n" : ""
23-
in_dtypes.each do |type|
24-
final += type.render unless type.private?
25-
end
26-
final
27-
end
28-
29-
def self.toc_info
30-
final = ["Defined types"]
31-
32-
in_dtypes.each do |type|
33-
final.push(type.toc_info)
34-
end
35-
36-
final
37-
end
3817
end
3918
end

lib/puppet-strings/markdown/functions.rb

+5-26
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,17 @@
33
require_relative 'function'
44

55
module PuppetStrings::Markdown
6+
# Adapter to get information about functions
67
module Functions
8+
def self.name
9+
'Functions'
10+
end
711

812
# @return [Array] list of functions
9-
def self.in_functions
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash)
1115
arr.map! { |a| PuppetStrings::Markdown::Function.new(a) }
1216
end
13-
14-
def self.contains_private?
15-
result = false
16-
unless in_functions.nil?
17-
in_functions.find { |func| func.private? }.nil? ? false : true
18-
end
19-
end
20-
21-
def self.render
22-
final = in_functions.length > 0 ? "## Functions\n\n" : ""
23-
in_functions.each do |func|
24-
final += func.render unless func.private?
25-
end
26-
final
27-
end
28-
29-
def self.toc_info
30-
final = ["Functions"]
31-
32-
in_functions.each do |func|
33-
final.push(func.toc_info)
34-
end
35-
36-
final
37-
end
3817
end
3918
end
4019

lib/puppet-strings/markdown/puppet_classes.rb

+5-26
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,16 @@
33
require_relative 'puppet_class'
44

55
module PuppetStrings::Markdown
6+
# Adapter to get information about classes
67
module PuppetClasses
8+
def self.name
9+
'Classes'
10+
end
711

812
# @return [Array] list of classes
9-
def self.in_classes
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash)
1115
arr.map! { |a| PuppetStrings::Markdown::PuppetClass.new(a) }
1216
end
13-
14-
def self.contains_private?
15-
result = false
16-
unless in_classes.nil?
17-
in_classes.find { |klass| klass.private? }.nil? ? false : true
18-
end
19-
end
20-
21-
def self.render
22-
final = in_classes.length > 0 ? "## Classes\n\n" : ""
23-
in_classes.each do |klass|
24-
final += klass.render unless klass.private?
25-
end
26-
final
27-
end
28-
29-
def self.toc_info
30-
final = ["Classes"]
31-
32-
in_classes.each do |klass|
33-
final.push(klass.toc_info)
34-
end
35-
36-
final
37-
end
3817
end
3918
end

lib/puppet-strings/markdown/puppet_plans.rb

+5-26
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,16 @@
33
require_relative 'puppet_plan'
44

55
module PuppetStrings::Markdown
6+
# Adapter to get information about plans
67
module PuppetPlans
8+
def self.name
9+
'Plans'
10+
end
711

812
# @return [Array] list of classes
9-
def self.in_plans
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_plan).sort_by!(&:name).map!(&:to_hash)
1115
arr.map! { |a| PuppetStrings::Markdown::PuppetPlan.new(a) }
1216
end
13-
14-
def self.contains_private?
15-
result = false
16-
unless in_plans.nil?
17-
in_plans.find { |plan| plan.private? }.nil? ? false : true
18-
end
19-
end
20-
21-
def self.render
22-
final = in_plans.length > 0 ? "## Plans\n\n" : ""
23-
in_plans.each do |plan|
24-
final += plan.render unless plan.private?
25-
end
26-
final
27-
end
28-
29-
def self.toc_info
30-
final = ["Plans"]
31-
32-
in_plans.each do |plan|
33-
final.push(plan.toc_info)
34-
end
35-
36-
final
37-
end
3817
end
3918
end

lib/puppet-strings/markdown/puppet_tasks.rb

+5-23
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,16 @@
33
require_relative 'puppet_task'
44

55
module PuppetStrings::Markdown
6+
# Adapter to get information about tasks
67
module PuppetTasks
8+
def self.name
9+
'Tasks'
10+
end
711

812
# @return [Array] list of classes
9-
def self.in_tasks
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_task).sort_by!(&:name).map!(&:to_hash)
1115
arr.map! { |a| PuppetStrings::Markdown::PuppetTask.new(a) }
1216
end
13-
14-
def self.contains_private?
15-
false
16-
end
17-
18-
def self.render
19-
final = in_tasks.length > 0 ? "## Tasks\n\n" : ""
20-
in_tasks.each do |task|
21-
final += task.render unless task.private?
22-
end
23-
final
24-
end
25-
26-
def self.toc_info
27-
final = ["Tasks"]
28-
29-
in_tasks.each do |task|
30-
final.push(task.toc_info)
31-
end
32-
33-
final
34-
end
3517
end
3618
end

0 commit comments

Comments
 (0)