Escape code for html <pre> or <code> tag into clipboard: Ruby Version

Alec Jacobson

November 05, 2009

weblog/

I previously posted a bash script to speed up posting code in a <pre> or <code> tag in an html page or blog. The script escapes all less than and greater than symbols (< with &lt; and > with &gt;) in a given file then puts the results in the clipboard to facilitate pasting into a text area or text editor. I have now rewritten this idea in ruby. Now you can either cat in a file to the ruby script using pipe, or enter (paste) the input text directly. Save the following the ruby source in a file called escape-copy.rb:
#!/usr/bin/ruby -w
# Usage:
# cat input_file | ruby escape-copy.rb | pbcopy
# or
# ruby escape-copy.rb | pbcopy
# (enter or paste your text then CTRL-D on its own line as EOF)
print ARGF.read.gsub(/</,"<").gsub(/>/,">")
Note: I of course used escape-copy.rb to make posting the source above super easy!