Skip to content

Commit 76a4570

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 eb9eee1 commit 76a4570

26 files changed

+3505
-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

+98
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,97 @@ 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+
pandoc = %x{which pandoc}.chomp
120+
unless File.executable?(pandoc)
121+
abort("Please install the `pandoc` package.")
122+
end
123+
124+
sha = %x{git rev-parse HEAD}.chomp
125+
now = Time.now
126+
127+
# This is based on https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/man.rb#L24-L108
128+
core_apps = %w(
129+
agent
130+
apply
131+
lookup
132+
module
133+
resource
134+
)
135+
occasional_apps = %w(
136+
config
137+
describe
138+
device
139+
doc
140+
epp
141+
generate
142+
help
143+
node
144+
parser
145+
plugin
146+
script
147+
ssl
148+
)
149+
weird_apps = %w(
150+
catalog
151+
facts
152+
filebucket
153+
report
154+
)
155+
156+
variables = {
157+
sha: sha,
158+
now: now,
159+
title: 'Puppet Man Pages',
160+
core_apps: core_apps,
161+
occasional_apps: occasional_apps,
162+
weird_apps: weird_apps
163+
}
164+
165+
content = render_erb(MAN_OVERVIEW_ERB, variables)
166+
File.write(MAN_OVERVIEW_MD, content)
167+
puts "Generated #{MAN_OVERVIEW_MD}"
168+
169+
# Generate manpages in roff
170+
Rake::Task[:gen_manpages].invoke
171+
172+
# Convert the roff formatted man pages to markdown, based on
173+
# https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/man.rb#L119-L128
174+
files = Pathname.glob(File.join(__dir__, '../man/man8/*.8'))
175+
files.each do |f|
176+
next if File.basename(f) == "puppet.8"
177+
178+
app = File.basename(f).delete_prefix('puppet-').delete_suffix(".8")
179+
180+
body =
181+
PandocRuby.convert([f], from: :man, to: :markdown)
182+
.gsub(/#(.*?)\n/, '##\1')
183+
.gsub(/:\s\s\s\n\n```\{=html\}\n<!--\s-->\n```/, '')
184+
.gsub(/\n:\s\s\s\s/, '')
185+
.chomp
186+
187+
variables = {
188+
sha: sha,
189+
now: now,
190+
title: "Man Page: puppet #{app}",
191+
canonical: "/puppet/latest/man/#{app}.html",
192+
body: body
193+
}
194+
195+
content = render_erb(MAN_ERB, variables)
196+
output = File.join(MANDIR, "#{app}.md")
197+
File.write(output, content)
198+
puts "Generated #{output}"
199+
end
200+
end
103201
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)