Skip to content

(#223) Use code blocks as appropriate in Markdown #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/puppet-strings/markdown/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'puppet-strings'
require 'puppet-strings/json'
require 'puppet-strings/yard'
require 'puppet-strings/markdown/helpers'

# Implements classes that make elements in a YARD::Registry hash easily accessible for template.
module PuppetStrings::Markdown
Expand Down Expand Up @@ -50,6 +51,9 @@ module PuppetStrings::Markdown
# ") inherits foo::bar {\n" +
# "}"}
class Base
# Helpers for ERB templates
include PuppetStrings::Markdown::Helpers

# Set or return the name of the group
#
# @param [Optional[String]] Name of the group to set
Expand Down
21 changes: 21 additions & 0 deletions lib/puppet-strings/markdown/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

# Helpers for rendering Markdown
module PuppetStrings::Markdown::Helpers
# Formats code as either inline or a block.
#
# Note that this does not do any escaping even if the code contains ` or ```.
#
# @param [String] code The code to format.
# @param [Symbol] type The type of the code, e.g. :text, :puppet, or :ruby.
# @param [String] block_prefix String to insert before if it’s a block.
# @param [String] inline_prefix String to insert before if it’s inline.
# @returns [String] Markdown
def code_maybe_block(code, type: :puppet, block_prefix: "\n\n", inline_prefix: ' ')
if code.include?("\n")
"#{block_prefix}```#{type}\n#{code}\n```"
else
"#{inline_prefix}`#{code}`"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The following parameters are available in the `<%= name %>` <%= @type %>:
##### <a name="<%= param[:link] %>"></a>`<%= param[:name] %>`

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

<% end -%>
<%= param[:text] %>
Expand All @@ -79,7 +79,7 @@ Options:

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

<% end -%>
<% end -%>
Expand Down
10 changes: 3 additions & 7 deletions lib/puppet-strings/markdown/templates/data_type.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@
<% end -%>
<% end -%>
<% if alias_of -%>
Alias of

```puppet
<%= alias_of %>
```
Alias of<%= code_maybe_block(alias_of) %>

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

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

<% end -%>
<%= param[:text] %>
Expand All @@ -87,7 +83,7 @@ Options:

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

<% end -%>
<% end -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Raises:
<% params.each do |param| -%>
##### `<%= param[:name] %>`

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

<%= param[:text] %>

Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-strings/markdown/templates/function.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Raises:
<% sig.params.each do |param| -%>
##### `<%= param[:name] %>`

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

<%= param[:text] %>

Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-strings/markdown/templates/puppet_task.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##### `<%= param[:name] %>`

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

<% end -%>
<%= param[:text] %>
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet-strings/markdown/templates/resource_type.erb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Options:

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

<% end -%>
<% end -%>
Expand Down
82 changes: 82 additions & 0 deletions spec/unit/puppet-strings/markdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,86 @@ def parse_data_type_content

MARKDOWN
end

it 'renders single-line data types with inline code' do
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
# @summary it’s for testing
type MyEnum = Enum[a, b]
PUPPET

expect(described_class.generate).to match(%r{^Alias of `Enum\[a, b\]`$})
end

it 'renders multi-line data types with inline code' do
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
# summary Test Type
#
type Test_module::Test_type = Hash[
Pattern[/^[a-z][a-z0-9_-]*$/],
Struct[
{
param1 => String[1],
param2 => Stdlib::Absolutepath,
paramX => Boolean,
}
]
]
PUPPET

expect(described_class.generate).to include(<<~'MARKDOWN')
Alias of

```puppet
Hash[Pattern[/^[a-z][a-z0-9_-]*$/], Struct[
{
param1 => String[1],
param2 => Stdlib::Absolutepath,
paramX => Boolean,
}
]]
```
MARKDOWN
end

it 'renders single-line default values with inline code' do
expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
# @summary Test
class myclass (
String $os = 'linux',
) {
}
PUPPET

expect(described_class.generate).to include(<<~'MARKDOWN')
Default value: `'linux'`
MARKDOWN
end

it 'renders multi-line default values with a code block' do
skip('Broken by https://tickets.puppetlabs.com/browse/PUP-11632')

expect(YARD::Parser::SourceParser.parse_string(<<~'PUPPET', :puppet).enumerator.length).to eq(1)
# @summary Test
class myclass (
String $os = $facts['kernel'] ? {
'Linux' => 'linux',
'Darwin' => 'darwin',
default => $facts['kernel'],
},
) {
}
PUPPET

expect(described_class.generate).to include(<<~'MARKDOWN')
Default value:

```puppet
$facts['kernel'] ? {
'Linux' => 'linux',
'Darwin' => 'darwin',
default => $facts['kernel']
}
```
MARKDOWN
end
end