Skip to content

Commit 87dba00

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 9e453e0 commit 87dba00

File tree

9 files changed

+116
-13
lines changed

9 files changed

+116
-13
lines changed

lib/puppet-strings/markdown/base.rb

Lines changed: 4 additions & 0 deletions
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
module PuppetStrings::Markdown
89
# This class makes elements in a YARD::Registry hash easily accessible for templates.
@@ -49,6 +50,9 @@ module PuppetStrings::Markdown
4950
# ") inherits foo::bar {\n" +
5051
# "}"}
5152
class Base
53+
# Helpers for ERB templates
54+
include PuppetStrings::Markdown::Helpers
55+
5256
def initialize(registry, component_type)
5357
@type = component_type
5458
@registry = registry
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module PuppetStrings::Markdown::Helpers
4+
# Formats code as either inline or a block.
5+
#
6+
# Note that this does not do any escaping even if the code contains ` or ```.
7+
#
8+
# @param [String] code The code to format.
9+
# @param [Symbol] type The type of the code, e.g. :text, :puppet, or :ruby.
10+
# @param [String] block_prefix String to insert before if it’s a block.
11+
# @param [String] inline_prefix String to insert before if it’s inline.
12+
# @returns [String] Markdown
13+
def code_maybe_block(code, type: :puppet, block_prefix: "\n\n", inline_prefix: " ")
14+
if code.include?("\n")
15+
"#{block_prefix}```#{type.to_s}\n#{code}\n```"
16+
else
17+
"#{inline_prefix}`#{code}`"
18+
end
19+
end
20+
end
21+

lib/puppet-strings/markdown/templates/classes_and_defines.erb

Lines changed: 2 additions & 2 deletions
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 -%>

lib/puppet-strings/markdown/templates/data_type.erb

Lines changed: 3 additions & 7 deletions
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 -%>

lib/puppet-strings/markdown/templates/data_type_function.erb

Lines changed: 1 addition & 1 deletion
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

lib/puppet-strings/markdown/templates/function.erb

Lines changed: 1 addition & 1 deletion
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

lib/puppet-strings/markdown/templates/puppet_task.erb

Lines changed: 1 addition & 1 deletion
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] %>

lib/puppet-strings/markdown/templates/resource_type.erb

Lines changed: 1 addition & 1 deletion
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 -%>

spec/unit/puppet-strings/markdown_spec.rb

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

0 commit comments

Comments
 (0)