Last active
January 2, 2016 02:49
-
-
Save jcsjcs/8239632 to your computer and use it in GitHub Desktop.
Syntax highlight selected code for pasting in emails. Requires xclip.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Format code with syntax highlighting for pasting in emails. | |
# To use: | |
# 1) Highlight the code. | |
# 2) fmail ruby/sql/java_script etc... | |
# 3) Middle-click in email to paste html code. | |
require 'coderay' | |
require 'open3' | |
is_irb = false | |
begin | |
str = `xclip -o` # Requires xclip | |
rescue Errno::ENOENT | |
puts "\"xclip\" is not installed." | |
exit 1 | |
end | |
opts = [:sql, :html, :ruby, :c, :css, :debug, :delphi, | |
:diff, :groovy, :html, :java, :javascript, :js, | |
:json, :php, :python, :rhtml, :ruby, :scheme, | |
:sql, :text, :xhtml, :xml, :yaml] | |
opt = ARGV[0] | |
if opt.nil? | |
puts "Supply the syntax type as an argument - one of:\n\n\t#{opts.sort.join("\n\t")}" | |
exit 1 | |
end | |
fmt = case opt.to_sym | |
when *opts | |
opt.to_sym | |
when :irb | |
is_irb = true | |
:ruby | |
else | |
puts "Unknown syntax: \"#{opt}\"" #:ruby | |
exit 1 | |
end | |
fmt = :java_script if fmt == :javascript || fmt == :js | |
if is_irb | |
new = [] | |
arb4 = str.split("\n") | |
arb4.each do |a| | |
b = a.match(/^.+?>/)[0] | |
c = b =~ /.*=>/ ? '<span style="background-color:#ffff00;color:#00f">=></span>' : '' | |
new << (c << CodeRay.encode(a.sub(b, ''), fmt, :html, :css => :style) << "<br />") | |
end | |
output = "<pre>#{new.join("\n")}</pre>" | |
else | |
output = "<pre>#{CodeRay.encode(str, fmt, :html, :css => :style).gsub("\n", "<br />")}</pre>" | |
end | |
# Pipe output to xclip... | |
Open3.popen2e("xclip") {|i,o,t| | |
i << output | |
i.close | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment