Skip to content

Commit

Permalink
Add a function to generate config file headers
Browse files Browse the repository at this point in the history
  • Loading branch information
wbclark committed Jul 22, 2021
1 parent 5d482cb commit dfd7744
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/puppet/functions/pulpcore/generate_header_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Puppet::Functions.create_function(:'pulpcore::generate_header_content') do
dispatch :generate_header_content do
param 'Variant[String, Array[String]]', :raw_content
param 'Integer', :line_width
param 'String', :comment_glyph
return_type 'Array[String]'
end

def generate_header_content(raw_content, line_width, comment_glyph)
raw_content = [raw_content] unless raw_content.is_a?(Array)
buffer = comment_glyph * (line_width / comment_glyph.length.to_f.ceil)
reduced_width = line_width - (2*(1+comment_glyph.length))

raw_content.map do |text|
text.scan(/\S.{0,#{reduced_width}}\S(?=\s|$)|\S+/).map do |line|
"#{comment_glyph} " + line + (" " * (reduced_width - line.length)) + " #{comment_glyph}"
end.append(buffer)
end.flatten.prepend(buffer)
end
end

0 comments on commit dfd7744

Please sign in to comment.