lib/chef_attrdoc.rb in chef_attrdoc-1.0.2 vs lib/chef_attrdoc.rb in chef_attrdoc-1.0.3
- old
+ new
@@ -137,19 +137,26 @@
def self.write_readme(readme, parsed)
File.open(readme, File::RDWR) do |f|
# TODO find a cleaner way and do this in one step
content = f.read
if content =~ /\nAttributes\s*=+\s*\n/
- updated = content.gsub(/(?<before>.*\nAttributes\s*=+\s*?\n)\n*(?m:.+?)(?<after>\n.+\s*\n=+.*)/,
+ updated = content.sub(/(?<before>.*\nAttributes\s*=+\s*?\n)\n*(?m:.+?)(?<after>\n.+\s*\n=+.*)/,
'\k<before>CHEF_ATTRDOC_UPDATING_TEMPLATE\k<after>')
elsif content =~ /\n\#+\s*Attributes\s*\n/
- updated = content.gsub(/(?<before>.*\n\#+\s*Attributes\s*?\n)\n*(.+?)(?<after>\n\#+.*)/m,
+ updated = content.sub(/(?<before>.*\n\#+\s*Attributes\s*?\n)\n*(.+?)(?<after>\n\#+.*)/m,
'\k<before>CHEF_ATTRDOC_UPDATING_TEMPLATE\k<after>')
else
- raise StandardError, "Could not find Attributes heading in #{readme}. Please make sure your README file has proper markdown formatting and includes an Attributes heading."
+ raise StandardError, "Could not find Attributes heading in #{readme}. "+
+ "Please make sure your README file has proper markdown formatting "+
+ "and includes an Attributes heading."
end
- updated.sub! 'CHEF_ATTRDOC_UPDATING_TEMPLATE', "\n" + parsed
+ updated.sub!(
+ 'CHEF_ATTRDOC_UPDATING_TEMPLATE',
+ # need to sanitize the second parameter, otherwise it can repeat
+ # parts of the original README
+ "\n" + parsed.gsub(/\\/, "\\\\\\"))
+
f.rewind
f.write(updated)
f.flush
f.truncate(f.pos)
end