Created
March 30, 2010 14:41
-
-
Save ebot/349153 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
require 'rubygems' | |
require 'coderay' | |
require 'win32/clipboard' | |
require 'htmlclipboard.rb' | |
include Win32 | |
# gather the file information | |
code_file_name = ARGV[0] | |
code_file_name = 'filing_rules.sql' if code_file_name.nil? | |
code_file = File.new( code_file_name, 'r' ) | |
extension = File.extname( code_file_name ).gsub( /\./, '' ) | |
out_file_name = File.basename( code_file_name, ".#{extension}" ) << ".html" | |
# Create the html | |
code = CodeRay.scan( code_file.read, extension ) | |
Clipboard.set_html_data( code.html( :line_numbers => :inline, :wrap => :page ) ) |
This file contains hidden or 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
# | |
# htmlclipboard.rb | |
# by John Allen | |
# http://johnallen.us/?p=289 | |
# | |
module Win32 | |
class Clipboard | |
@cfHTML = nil | |
def self.registerHTML | |
if not @cfHTML | |
@cfHTML = Win32::Clipboard.RegisterClipboardFormat("HTML Format") | |
end | |
end | |
=begin | |
zeroFmt(nbr) | |
-- Takes a number and formats with zero's in front. | |
1234 => "0000001234" | |
23 => "0000000023" | |
=end | |
def self.zeroFmt(nbr) | |
zeros = "0"*10 | |
len = nbr.to_s.length # this is the number of digits in the number 2784 => 4 | |
return zeros.slice(0..(zeros.length - len - 1)) + nbr.to_s | |
end | |
def self.set_html_data(clip_data) | |
sDataStart = "<HTML><BODY>\n<!--StartFragment -->" | |
sDataEnd = "<!--EndFragment -->\n</BODY></HTML>" | |
sData = "" | |
# | |
# Clipboard Header: looks like -> | |
# Version: 1.0 | |
# StartHTML:0000000000 | |
# EndHTML:0000000000 | |
# StartFragment:0000000000 | |
# EndFragment:0000000000 | |
sDataHdr = "Version:1.0\r\nStartHTML:aaaaaaaaaa\r\nEndHTML:bbbbbbbbbb\r\n" | |
sDataHdr += "StartFragment:cccccccccc\r\nEndFragment:dddddddddd\r\n" | |
sData = sDataHdr + sDataStart + clip_data + sDataEnd | |
sData = sData.gsub(/aaaaaaaaaa/) { zeroFmt(sDataHdr.length) } | |
sData = sData.gsub(/bbbbbbbbbb/) { zeroFmt(sData.length) } | |
sData = sData.gsub(/cccccccccc/) { zeroFmt(sDataHdr.length + sDataStart.length) } | |
sData = sData.gsub(/dddddddddd/) { zeroFmt(sDataHdr.length + sDataStart.length + clip_data.length) } | |
if not @cfHTML | |
self.registerHTML | |
end | |
self.open | |
EmptyClipboard() | |
hmem = GlobalAlloc(GHND, sData.length + 10) | |
mem = GlobalLock(hmem) | |
memcpy(mem, sData, sData.length) | |
begin | |
if SetClipboardData(@cfHTML, hmem) == 0 | |
raise Error, "SetClipboardData() failed; " + get_last_error | |
end | |
ensure | |
GlobalFree(hmem) | |
self.close | |
end | |
self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment