Skip to content

Commit 2811c0c

Browse files
committed
(puppetlabs#223) Use code blocks as appropriate in Markdown
Sometimes data types, default values, and other code outputted in Markdown documentation is multiline. When that’s the case, it’s cleaner to use a code block (below) rather and inline code (`code`). ``` example code block with two lines ``` This updates the Markdown template code to use code blocks in many places if the code in question contains a newline. Thanks to @crazymind1337 for the example of a multiline type alias!
1 parent 897b13f commit 2811c0c

File tree

9 files changed

+116
-13
lines changed

9 files changed

+116
-13
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'puppet-strings'
44
require 'puppet-strings/json'
55
require 'puppet-strings/yard'
6+
require 'puppet-strings/markdown/helpers'
67

78
# Implements classes that make elements in a YARD::Registry hash easily accessible for template.
89
module PuppetStrings::Markdown
@@ -50,6 +51,9 @@ module PuppetStrings::Markdown
5051
# ") inherits foo::bar {\n" +
5152
# "}"}
5253
class Base
54+
# Helpers for ERB templates
55+
include PuppetStrings::Markdown::Helpers
56+
5357
# Set or return the name of the group
5458
#
5559
# @param [Optional[String]] Name of the group to set

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# Helpers for rendering Markdown
4+
module PuppetStrings::Markdown::Helpers
5+
# Formats code as either inline or a block.
6+
#
7+
# Note that this does not do any escaping even if the code contains ` or ```.
8+
#
9+
# @param [String] code The code to format.
10+
# @param [Symbol] type The type of the code, e.g. :text, :puppet, or :ruby.
11+
# @param [String] block_prefix String to insert before if it’s a block.
12+
# @param [String] inline_prefix String to insert before if it’s inline.
13+
# @returns [String] Markdown
14+
def code_maybe_block(code, type: :puppet, block_prefix: "\n\n", inline_prefix: ' ')
15+
if code.include?("\n")
16+
"#{block_prefix}```#{type}\n#{code}\n```"
17+
else
18+
"#{inline_prefix}`#{code}`"
19+
end
20+
end
21+
end

Diff for: lib/puppet-strings/markdown/templates/classes_and_defines.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The following parameters are available in the `<%= name %>` <%= @type %>:
5757
##### <a name="<%= param[:link] %>"></a>`<%= param[:name] %>`
5858

5959
<% if param[:types] -%>
60-
Data type: `<%= param[:types].join(', ') -%>`
60+
Data type:<%= code_maybe_block(param[:types].join(', ')) %>
6161

6262
<% end -%>
6363
<%= param[:text] %>
@@ -79,7 +79,7 @@ Options:
7979

8080
<% end -%>
8181
<% if defaults && defaults[param[:name]] -%>
82-
Default value: `<%= defaults[param[:name]] %>`
82+
Default value:<%= code_maybe_block(defaults[param[:name]]) %>
8383

8484
<% end -%>
8585
<% end -%>

Diff for: lib/puppet-strings/markdown/templates/data_type.erb

+3-7
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@
4545
<% end -%>
4646
<% end -%>
4747
<% if alias_of -%>
48-
Alias of
49-
50-
```puppet
51-
<%= alias_of %>
52-
```
48+
Alias of<%= code_maybe_block(alias_of) %>
5349

5450
<% end -%>
5551
<% if params -%>
@@ -65,7 +61,7 @@ The following parameters are available in the `<%= name %>` <%= @type %>:
6561
##### <a name="<%= param[:link] %>"></a>`<%= param[:name] %>`
6662

6763
<% if param[:types] -%>
68-
Data type: `<%= param[:types].join(', ') -%>`
64+
Data type:<%= code_maybe_block(param[:types].join(', ')) %>
6965

7066
<% end -%>
7167
<%= param[:text] %>
@@ -87,7 +83,7 @@ Options:
8783

8884
<% end -%>
8985
<% if defaults && defaults[param[:name]] -%>
90-
Default value: `<%= defaults[param[:name]] %>`
86+
Default value:<%= code_maybe_block(defaults[param[:name]]) %>
9187

9288
<% end -%>
9389
<% end -%>

Diff for: lib/puppet-strings/markdown/templates/data_type_function.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Raises:
4343
<% params.each do |param| -%>
4444
##### `<%= param[:name] %>`
4545

46-
Data type: `<%= param[:types][0] %>`
46+
Data type:<%= code_maybe_block(param[:types][0]) %>
4747

4848
<%= param[:text] %>
4949

Diff for: lib/puppet-strings/markdown/templates/function.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Raises:
7777
<% sig.params.each do |param| -%>
7878
##### `<%= param[:name] %>`
7979

80-
Data type: `<%= param[:types][0] %>`
80+
Data type:<%= code_maybe_block(param[:types][0]) %>
8181

8282
<%= param[:text] %>
8383

Diff for: lib/puppet-strings/markdown/templates/puppet_task.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
##### `<%= param[:name] %>`
2020

2121
<% if param[:types] -%>
22-
Data type: `<%= param[:types].join(', ') -%>`
22+
Data type:<%= code_maybe_block(param[:types].join(', ')) %>
2323

2424
<% end -%>
2525
<%= param[:text] %>

Diff for: lib/puppet-strings/markdown/templates/resource_type.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Options:
8787

8888
<% end -%>
8989
<% if prop[:default] -%>
90-
Default value: `<%= prop[:default] %>`
90+
Default value:<%= code_maybe_block(prop[:default], type: :ruby) %>
9191

9292
<% end -%>
9393
<% end -%>

Diff for: spec/unit/puppet-strings/markdown_spec.rb

+82
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,86 @@ def parse_data_type_content
258258
259259
MARKDOWN
260260
end
261+
262+
it 'renders single-line data types with inline code' do
263+
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
264+
# @summary it’s for testing
265+
type MyEnum = Enum[a, b]
266+
PUPPET
267+
268+
expect(described_class.generate).to match(%r{^Alias of `Enum\[a, b\]`$})
269+
end
270+
271+
it 'renders multi-line data types with inline code' do
272+
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
273+
# summary Test Type
274+
#
275+
type Test_module::Test_type = Hash[
276+
Pattern[/^[a-z][a-z0-9_-]*$/],
277+
Struct[
278+
{
279+
param1 => String[1],
280+
param2 => Stdlib::Absolutepath,
281+
paramX => Boolean,
282+
}
283+
]
284+
]
285+
PUPPET
286+
287+
expect(described_class.generate).to include(<<~'MARKDOWN')
288+
Alias of
289+
290+
```puppet
291+
Hash[Pattern[/^[a-z][a-z0-9_-]*$/], Struct[
292+
{
293+
param1 => String[1],
294+
param2 => Stdlib::Absolutepath,
295+
paramX => Boolean,
296+
}
297+
]]
298+
```
299+
MARKDOWN
300+
end
301+
302+
it 'renders single-line default values with inline code' do
303+
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
304+
# @summary Test
305+
class myclass (
306+
String $os = 'linux',
307+
) {
308+
}
309+
PUPPET
310+
311+
expect(described_class.generate).to include(<<~'MARKDOWN')
312+
Default value: `'linux'`
313+
MARKDOWN
314+
end
315+
316+
it 'renders multi-line default values with a code block' do
317+
skip('Broken by https://tickets.puppetlabs.com/browse/PUP-11632')
318+
319+
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
320+
# @summary Test
321+
class myclass (
322+
String $os = $facts['kernel'] ? {
323+
'Linux' => 'linux',
324+
'Darwin' => 'darwin',
325+
default => $facts['kernel'],
326+
},
327+
) {
328+
}
329+
PUPPET
330+
331+
expect(described_class.generate).to include(<<~'MARKDOWN')
332+
Default value:
333+
334+
```puppet
335+
$facts['kernel'] ? {
336+
'Linux' => 'linux',
337+
'Darwin' => 'darwin',
338+
default => $facts['kernel']
339+
}
340+
```
341+
MARKDOWN
342+
end
261343
end

0 commit comments

Comments
 (0)