Skip to content

Commit dbf2c55

Browse files
committed
(#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 e7e4444 commit dbf2c55

12 files changed

+243
-273
lines changed

Diff for: 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

Diff for: lib/puppet-strings/markdown/base.rb

-13
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,4 @@ def clean_link(input)
200200
input.tr('^a-zA-Z0-9_-', '-')
201201
end
202202
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
216203
end

Diff for: lib/puppet-strings/markdown/data_types.rb

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,19 @@
33
require_relative 'data_type'
44

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Data Types.
6+
# Adapter to get information about data types
77
module DataTypes
8+
def self.name
9+
'Data types'
10+
end
11+
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-
return if in_dtypes.nil?
19-
in_dtypes.find { |type| type.private? }.nil? ? false : true
20-
end
21-
22-
def self.render
23-
final = !in_dtypes.empty? ? "## Data types\n\n" : ''
24-
in_dtypes.each do |type|
25-
final += type.render unless type.private?
26-
end
27-
final
28-
end
29-
30-
def self.toc_info
31-
final = ['Data types']
32-
33-
in_dtypes.each do |type|
34-
final.push(type.toc_info)
35-
end
36-
37-
final
38-
end
3920
end
4021
end

Diff for: lib/puppet-strings/markdown/defined_types.rb

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,16 @@
33
require_relative 'defined_type'
44

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Defined Types.
6+
# Adapter to get information about defined types
77
module DefinedTypes
8+
def self.name
9+
'Defined types'
10+
end
11+
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-
return if in_dtypes.nil?
16-
in_dtypes.find { |type| type.private? }.nil? ? false : true
17-
end
18-
19-
def self.render
20-
final = !in_dtypes.empty? ? "## Defined types\n\n" : ''
21-
in_dtypes.each do |type|
22-
final += type.render unless type.private?
23-
end
24-
final
25-
end
26-
27-
def self.toc_info
28-
final = ['Defined types']
29-
30-
in_dtypes.each do |type|
31-
final.push(type.toc_info)
32-
end
33-
34-
final
35-
end
3617
end
3718
end

Diff for: lib/puppet-strings/markdown/functions.rb

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,16 @@
33
require_relative 'function'
44

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Functions.
6+
# Adapter to get information about functions
77
module Functions
8+
def self.name
9+
'Functions'
10+
end
11+
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-
return if in_functions.nil?
16-
in_functions.find { |func| func.private? }.nil? ? false : true
17-
end
18-
19-
def self.render
20-
final = !in_functions.empty? ? "## Functions\n\n" : ''
21-
in_functions.each do |func|
22-
final += func.render unless func.private?
23-
end
24-
final
25-
end
26-
27-
def self.toc_info
28-
final = ['Functions']
29-
30-
in_functions.each do |func|
31-
final.push(func.toc_info)
32-
end
33-
34-
final
35-
end
3617
end
3718
end

Diff for: lib/puppet-strings/markdown/puppet_classes.rb

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,16 @@
33
require_relative 'puppet_class'
44

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Classes.
6+
# Adapter to get information about classes
77
module PuppetClasses
8+
def self.name
9+
'Classes'
10+
end
11+
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-
return if in_classes.nil?
16-
in_classes.find { |klass| klass.private? }.nil? ? false : true
17-
end
18-
19-
def self.render
20-
final = !in_classes.empty? ? "## Classes\n\n" : ''
21-
in_classes.each do |klass|
22-
final += klass.render unless klass.private?
23-
end
24-
final
25-
end
26-
27-
def self.toc_info
28-
final = ['Classes']
29-
30-
in_classes.each do |klass|
31-
final.push(klass.toc_info)
32-
end
33-
34-
final
35-
end
3617
end
3718
end

Diff for: lib/puppet-strings/markdown/puppet_plans.rb

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,16 @@
33
require_relative 'puppet_plan'
44

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Plans.
6+
# Adapter to get information about plans
77
module PuppetPlans
8+
def self.name
9+
'Plans'
10+
end
11+
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-
return if in_plans.nil?
16-
in_plans.find { |plan| plan.private? }.nil? ? false : true
17-
end
18-
19-
def self.render
20-
final = !in_plans.empty? ? "## Plans\n\n" : ''
21-
in_plans.each do |plan|
22-
final += plan.render unless plan.private?
23-
end
24-
final
25-
end
26-
27-
def self.toc_info
28-
final = ['Plans']
29-
30-
in_plans.each do |plan|
31-
final.push(plan.toc_info)
32-
end
33-
34-
final
35-
end
3617
end
3718
end

Diff for: lib/puppet-strings/markdown/puppet_tasks.rb

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

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Tasks.
6+
# Adapter to get information about tasks
77
module PuppetTasks
8+
def self.name
9+
'Tasks'
10+
end
11+
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.empty? ? "## 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

Diff for: lib/puppet-strings/markdown/resource_types.rb

+6-25
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,16 @@
33
require_relative 'resource_type'
44

55
module PuppetStrings::Markdown
6-
# Generates Markdown for Puppet Resource Types.
6+
# Adapter to get information about resource types
77
module ResourceTypes
8+
def self.name
9+
'Resource types'
10+
end
11+
812
# @return [Array] list of resource types
9-
def self.in_rtypes
13+
def self.items
1014
arr = YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash)
1115
arr.map! { |a| PuppetStrings::Markdown::ResourceType.new(a) }
1216
end
13-
14-
def self.contains_private?
15-
return if in_rtypes.nil?
16-
in_rtypes.find { |type| type.private? }.nil? ? false : true
17-
end
18-
19-
def self.render
20-
final = !in_rtypes.empty? ? "## Resource types\n\n" : ''
21-
in_rtypes.each do |type|
22-
final += type.render unless type.private?
23-
end
24-
final
25-
end
26-
27-
def self.toc_info
28-
final = ['Resource types']
29-
30-
in_rtypes.each do |type|
31-
final.push(type.toc_info)
32-
end
33-
34-
final
35-
end
3617
end
3718
end

0 commit comments

Comments
 (0)