Skip to content

Commit

Permalink
(puppetlabs#302) Fix warnings generated by ERB.new
Browse files Browse the repository at this point in the history
This has to special case Ruby <2.6.0 since those versions will interpret
the keyword argument as a hash passed as the second argument.

Without this change running this code in Ruby 2.6+ will generate
warnings about the parameters passed to `ERB.new`.
  • Loading branch information
danielparks committed Sep 25, 2022
1 parent dba16ed commit fa4b232
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 15 additions & 2 deletions lib/puppet-strings/markdown/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ def word_wrap(text, line_width: 120, break_sequence: "\n")
# @return [String] full markdown rendering of a component
def render(template)
begin
file = File.join(File.dirname(__FILE__),"templates/#{template}")
ERB.new(File.read(file), nil, '-').result(binding)
PuppetStrings::Markdown.erb(template).result(binding)
rescue StandardError => e
fail "Processing #{@registry[:file]}:#{@registry[:line]} with #{file} => #{e}"
end
Expand All @@ -196,4 +195,18 @@ def select_tags(name)
tags.empty? ? nil : tags
end
end

# Helper function to load an ERB template
#
# @param [String] template The template name, including the .erb suffix.
# @return [ERB] Template
def self.erb(template)
template_path = File.join(File.dirname(__FILE__), 'templates', template)
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
ERB.new(File.read(template_path), trim_mode: '-')
else
# This works in Ruby 2.6+, but it outputs warnings.
ERB.new(File.read(template_path), nil, '-')
end
end
end
3 changes: 1 addition & 2 deletions lib/puppet-strings/markdown/table_of_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def self.render
group = toc
priv = r.contains_private?

template = File.join(File.dirname(__FILE__),"templates/table_of_contents.erb")
final += ERB.new(File.read(template), nil, '-').result(binding)
final += PuppetStrings::Markdown.erb('table_of_contents.erb').result(binding)
end
final
end
Expand Down

0 comments on commit fa4b232

Please sign in to comment.