Skip to content

Commit a2989a1

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 1b8df5a commit a2989a1

File tree

10 files changed

+119
-13
lines changed

10 files changed

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

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

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# frozen_string_literal: true
22

3+
require 'puppet-strings/markdown/helpers'
4+
35
module PuppetStrings::Markdown
46
module TableOfContents
7+
# Helpers for ERB templates
8+
include PuppetStrings::Markdown::Helpers
9+
510
def self.render
611
template = PuppetStrings::Markdown.erb(File.join(__dir__, 'templates', 'table_of_contents.erb'))
712
renders = PuppetStrings::Markdown.groups.map do |group|

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

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

0 commit comments

Comments
 (0)