Skip to content

Commit 83ef641

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 13f2953 commit 83ef641

File tree

11 files changed

+217
-254
lines changed

11 files changed

+217
-254
lines changed

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

+33-12
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,42 @@ module PuppetStrings::Markdown
1313
require_relative 'markdown/puppet_plans'
1414
require_relative 'markdown/table_of_contents'
1515

16+
# Get modules that handle collecting and rendering each section.
17+
#
18+
# Does not include the special TableOfContents module.
19+
# @return [Array[module]] The modules
20+
def self.groups
21+
[
22+
PuppetStrings::Markdown::PuppetClasses,
23+
PuppetStrings::Markdown::DefinedTypes,
24+
PuppetStrings::Markdown::ResourceTypes,
25+
PuppetStrings::Markdown::Functions,
26+
PuppetStrings::Markdown::DataTypes,
27+
PuppetStrings::Markdown::PuppetTasks,
28+
PuppetStrings::Markdown::PuppetPlans,
29+
]
30+
end
31+
1632
# generates markdown documentation
1733
# @return [String] markdown doc
1834
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
35+
final = [
36+
"# Reference\n\n",
37+
"<!-- DO NOT EDIT: This document was generated by Puppet Strings -->\n\n",
38+
PuppetStrings::Markdown::TableOfContents.render,
39+
]
40+
41+
final += self.groups.map do |group|
42+
renders = group.items.map { |item| item.render unless item.private? }.compact
43+
44+
if renders.empty?
45+
''
46+
else
47+
"## #{group.name}\n\n" + renders.join('')
48+
end
49+
end
50+
51+
final.join('')
3152
end
3253

3354
# mimicks the behavior of the json render, although path will never be nil

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

+4-27
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,17 @@
44

55
module PuppetStrings::Markdown
66
module DataTypes
7+
def self.name
8+
'Data types'
9+
end
710

811
# @return [Array] list of data types
9-
def self.in_dtypes
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_data_type).map!(&:to_hash)
1114
arr.concat(YARD::Registry.all(:puppet_data_type_alias).map!(&:to_hash))
1215

1316
arr.sort! { |a,b| a[:name] <=> b[:name] }
1417
arr.map! { |a| PuppetStrings::Markdown::DataType.new(a) }
1518
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
4219
end
4320
end

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

+4-26
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,14 @@
44

55
module PuppetStrings::Markdown
66
module DefinedTypes
7+
def self.name
8+
'Defined types'
9+
end
710

811
# @return [Array] list of defined types
9-
def self.in_dtypes
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash)
1114
arr.map! { |a| PuppetStrings::Markdown::DefinedType.new(a) }
1215
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
3816
end
3917
end

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

+4-26
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,15 @@
44

55
module PuppetStrings::Markdown
66
module Functions
7+
def self.name
8+
'Functions'
9+
end
710

811
# @return [Array] list of functions
9-
def self.in_functions
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash)
1114
arr.map! { |a| PuppetStrings::Markdown::Function.new(a) }
1215
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
3816
end
3917
end
4018

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

+4-26
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,14 @@
44

55
module PuppetStrings::Markdown
66
module PuppetClasses
7+
def self.name
8+
'Classes'
9+
end
710

811
# @return [Array] list of classes
9-
def self.in_classes
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash)
1114
arr.map! { |a| PuppetStrings::Markdown::PuppetClass.new(a) }
1215
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
3816
end
3917
end

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

+4-26
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,14 @@
44

55
module PuppetStrings::Markdown
66
module PuppetPlans
7+
def self.name
8+
'Plans'
9+
end
710

811
# @return [Array] list of classes
9-
def self.in_plans
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_plan).sort_by!(&:name).map!(&:to_hash)
1114
arr.map! { |a| PuppetStrings::Markdown::PuppetPlan.new(a) }
1215
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
3816
end
3917
end

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

+4-23
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@
44

55
module PuppetStrings::Markdown
66
module PuppetTasks
7+
def self.name
8+
'Tasks'
9+
end
710

811
# @return [Array] list of classes
9-
def self.in_tasks
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_task).sort_by!(&:name).map!(&:to_hash)
1114
arr.map! { |a| PuppetStrings::Markdown::PuppetTask.new(a) }
1215
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
3516
end
3617
end

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

+4-26
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,14 @@
44

55
module PuppetStrings::Markdown
66
module ResourceTypes
7+
def self.name
8+
'Resource types'
9+
end
710

811
# @return [Array] list of resource types
9-
def self.in_rtypes
12+
def self.items
1013
arr = YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash)
1114
arr.map! { |a| PuppetStrings::Markdown::ResourceType.new(a) }
1215
end
13-
14-
def self.contains_private?
15-
result = false
16-
unless in_rtypes.nil?
17-
in_rtypes.find { |type| type.private? }.nil? ? false : true
18-
end
19-
end
20-
21-
def self.render
22-
final = in_rtypes.length > 0 ? "## Resource types\n\n" : ""
23-
in_rtypes.each do |type|
24-
final += type.render unless type.private?
25-
end
26-
final
27-
end
28-
29-
def self.toc_info
30-
final = ["Resource types"]
31-
32-
in_rtypes.each do |type|
33-
final.push(type.toc_info)
34-
end
35-
36-
final
37-
end
3816
end
3917
end

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

+9-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
module PuppetStrings::Markdown
44
module TableOfContents
55
def self.render
6-
final = "## Table of Contents\n\n"
6+
template = PuppetStrings::Markdown.erb(File.join(__dir__, 'templates', 'table_of_contents.erb'))
7+
renders = PuppetStrings::Markdown.groups.map do |group|
8+
group_name = group.name
9+
items = group.items.map { |item| item.toc_info }
10+
has_private = items.any? { |item| item[:private] }
11+
has_public = items.any? { |item| !item[:private] }
712

8-
[PuppetStrings::Markdown::PuppetClasses,
9-
PuppetStrings::Markdown::DefinedTypes,
10-
PuppetStrings::Markdown::ResourceTypes,
11-
PuppetStrings::Markdown::Functions,
12-
PuppetStrings::Markdown::DataTypes,
13-
PuppetStrings::Markdown::PuppetTasks,
14-
PuppetStrings::Markdown::PuppetPlans].each do |r|
15-
toc = r.toc_info
16-
group_name = toc.shift
17-
group = toc
18-
priv = r.contains_private?
19-
20-
template = File.join(File.dirname(__FILE__), 'templates/table_of_contents.erb')
21-
final += PuppetStrings::Markdown.erb(template).result(binding)
13+
template.result(binding)
2214
end
23-
final
15+
16+
"## Table of Contents\n\n" + renders.join('')
2417
end
2518
end
2619
end

0 commit comments

Comments
 (0)