Skip to content

Instantly share code, notes, and snippets.

@tormaroe
Last active December 10, 2015 05:18
Show Gist options
  • Select an option

  • Save tormaroe/4386546 to your computer and use it in GitHub Desktop.

Select an option

Save tormaroe/4386546 to your computer and use it in GitHub Desktop.
Small script that modifies the content of the clipboard by HTML encoding dangerous characters and wraps i in a <pre>-tag. Used when blogging source code.
=begin
TO INSTALL DEPENDENCIES
-----------------------
gem install ffi <-- For Windows users
gem install clipboard
=end
HTML_CODES = {
"&" => "&amp;",
">" => "&gt;",
"<" => "&lt;",
"\\" => "&#92;",
"\t" => " "
}
def html_wrap tag, content
"<#{tag}>#{content}</#{tag}>"
end
require "clipboard"
source = Clipboard.paste
HTML_CODES.each {|c,s| source.gsub! c,s }
Clipboard.copy html_wrap("pre", source)
hr = "-" * 60
puts "Copied to clipboard:"
puts hr
puts Clipboard.paste
puts hr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment