Skip to content

Commit b2115bb

Browse files
committed
(PUP-12058) Add task to generate man (as markdown) reference
bundle exec rake references:man This task generates an overview page that lists all of the puppet commands. Next, we run the gen_manpages rake task that generates roff formatted man pages in the man directory. Then we convert roff to markdown using pandoc. We unfortunately can't generate markdown directly as legacy apps are hardcoded to generate ronn, such as when running `puppet agent --help`. The task requires the pandoc-ruby gem, so add it to the documentation bundler group. Since the group is optional, check to see if the gem is present before loading it, similar to how we handle ronn. The task also requires the pandoc executable.
1 parent 98bd5dd commit b2115bb

26 files changed

+3507
-0
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ group(:documentation, optional: true) do
7676
gem 'gettext-setup', '~> 1.0', require: false, platforms: [:ruby]
7777
gem 'ronn', '~> 0.7.3', require: false, platforms: [:ruby]
7878
gem 'puppet-strings', require: false, platforms: [:ruby]
79+
gem 'pandoc-ruby', require: false, platforms: [:ruby]
7980
end
8081

8182
if File.exist? "#{__FILE__}.local"

rakelib/generate_references.rake

+99
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'tempfile'
22

33
OUTPUT_DIR = 'references'
4+
MANDIR = File.join(OUTPUT_DIR, 'man')
5+
46
CONFIGURATION_ERB = File.join(__dir__, 'references/configuration.erb')
57
CONFIGURATION_MD = File.join(OUTPUT_DIR, 'configuration.md')
68
METAPARAMETER_ERB = File.join(__dir__, 'references/metaparameter.erb')
@@ -10,6 +12,9 @@ REPORT_MD = File.join(OUTPUT_DIR, 'report.md')
1012
FUNCTIONS_TEMPLATE_ERB = File.join(__dir__, 'references/functions_template.erb')
1113
FUNCTION_ERB = File.join(__dir__, 'references/function.erb')
1214
FUNCTION_MD = File.join(OUTPUT_DIR, 'function.md')
15+
MAN_OVERVIEW_ERB = File.join(__dir__, 'references/man/overview.erb')
16+
MAN_OVERVIEW_MD = File.join(MANDIR, "overview.md")
17+
MAN_ERB = File.join(__dir__, 'references/man.erb')
1318

1419
def render_erb(erb_file, variables)
1520
# Create a binding so only the variables we specify will be visible
@@ -100,4 +105,98 @@ namespace :references do
100105
# renders the preamble and list of functions
101106
generate_reference('function', FUNCTION_ERB, body, FUNCTION_MD)
102107
end
108+
109+
desc "Generate man as markdown references"
110+
task :man do
111+
FileUtils.mkdir_p(MANDIR)
112+
113+
begin
114+
require 'pandoc-ruby'
115+
rescue LoadError
116+
abort("Run `bundle config set with documentation` and `bundle update` to install the `pandoc-ruby` gem.")
117+
end
118+
119+
begin
120+
puts %x{pandoc --version}
121+
rescue Errno::ENOENT => e
122+
abort("Please install the `pandoc` package.")
123+
end
124+
125+
sha = %x{git rev-parse HEAD}.chomp
126+
now = Time.now
127+
128+
# This is based on https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/man.rb#L24-L108
129+
core_apps = %w(
130+
agent
131+
apply
132+
lookup
133+
module
134+
resource
135+
)
136+
occasional_apps = %w(
137+
config
138+
describe
139+
device
140+
doc
141+
epp
142+
generate
143+
help
144+
node
145+
parser
146+
plugin
147+
script
148+
ssl
149+
)
150+
weird_apps = %w(
151+
catalog
152+
facts
153+
filebucket
154+
report
155+
)
156+
157+
variables = {
158+
sha: sha,
159+
now: now,
160+
title: 'Puppet Man Pages',
161+
core_apps: core_apps,
162+
occasional_apps: occasional_apps,
163+
weird_apps: weird_apps
164+
}
165+
166+
content = render_erb(MAN_OVERVIEW_ERB, variables)
167+
File.write(MAN_OVERVIEW_MD, content)
168+
puts "Generated #{MAN_OVERVIEW_MD}"
169+
170+
# Generate manpages in roff
171+
Rake::Task[:gen_manpages].invoke
172+
173+
# Convert the roff formatted man pages to markdown, based on
174+
# https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/man.rb#L119-L128
175+
files = Pathname.glob(File.join(__dir__, '../man/man8/*.8'))
176+
files.each do |f|
177+
next if File.basename(f) == "puppet.8"
178+
179+
app = File.basename(f).delete_prefix('puppet-').delete_suffix(".8")
180+
181+
body =
182+
PandocRuby.convert([f], from: :man, to: :markdown)
183+
.gsub(/#(.*?)\n/, '##\1')
184+
.gsub(/:\s\s\s\n\n```\{=html\}\n<!--\s-->\n```/, '')
185+
.gsub(/\n:\s\s\s\s/, '')
186+
.chomp
187+
188+
variables = {
189+
sha: sha,
190+
now: now,
191+
title: "Man Page: puppet #{app}",
192+
canonical: "/puppet/latest/man/#{app}.html",
193+
body: body
194+
}
195+
196+
content = render_erb(MAN_ERB, variables)
197+
output = File.join(MANDIR, "#{app}.md")
198+
File.write(output, content)
199+
puts "Generated #{output}"
200+
end
201+
end
103202
end

rakelib/references/man.erb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
layout: default
3+
built_from_commit: <%= sha %>
4+
title: '<%= title %>'
5+
canonical: "<%= canonical %>"
6+
---
7+
8+
# <%= title %>
9+
10+
> **NOTE:** This page was generated from the Puppet source code on <%= now %>
11+
12+
<%= body %>

rakelib/references/man/overview.erb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: default
3+
built_from_commit: <%= sha %>
4+
title: <%= title %>
5+
canonical: "/puppet/latest/man/overview.html"
6+
---
7+
8+
# <%= title %>
9+
10+
> **NOTE:** This page was generated from the Puppet source code on <%= now %>
11+
12+
13+
14+
Puppet's command line tools consist of a single `puppet` binary with many subcommands. The following subcommands are available in this version of Puppet:
15+
16+
Core Tools
17+
-----
18+
19+
These subcommands form the core of Puppet's tool set, and every user should understand what they do.
20+
21+
<% core_apps.each do |app| -%>
22+
- [puppet <%= app %>](<%= app %>.md)
23+
<% end -%>
24+
25+
26+
> Note: The `puppet cert` command is available only in Puppet versions prior to 6.0. For 6.0 and later, use the [`puppetserver cert`command](https://puppet.com/docs/puppet/6/puppet_server_ca_cli.html).
27+
28+
Secondary subcommands
29+
-----
30+
31+
Many or most users need to use these subcommands at some point, but they aren't needed for daily use the way the core tools are.
32+
33+
<% occasional_apps.each do |app| -%>
34+
- [puppet <%= app %>](<%= app %>.md)
35+
<% end -%>
36+
37+
38+
Niche subcommands
39+
-----
40+
41+
Most users can ignore these subcommands. They're only useful for certain niche workflows, and most of them are interfaces to Puppet's internal subsystems.
42+
43+
<% weird_apps.each do |app| -%>
44+
- [puppet <%= app %>](<%= app %>.md)
45+
<% end -%>
46+
47+

0 commit comments

Comments
 (0)