Skip to content

Instantly share code, notes, and snippets.

@melborne
Created July 23, 2008 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melborne/1451 to your computer and use it in GitHub Desktop.
Save melborne/1451 to your computer and use it in GitHub Desktop.
make a html file with syntax highlighting
#!/usr/bin/env ruby
require "rubygems"
require "uv"
class Hilite
BGCOLORS = {"all_hallows_eve" => {:bg => '#000000', :fg => '#ffffff'},
"amy" => {:bg => '#200420', :fg => '#d1d0ff'},
"blackboard" => {:bg => '#0d1021', :fg => '#f8f8f8'},
"brilliance_black" => {:bg => '#0d0d0d', :fg => '#cccccc'},
"brilliance_dull" => {:bg => '#0a0a0a', :fg => '#cdcdcd'},
"cobalt" => {:bg => '#002444', :fg => '#e6e1dc'},
"dawn" => {:bg => '#f9f9f9', :fg => '#080808'},
"espresso_libre" => {:bg => '#2a211c', :fg => '#bcae9d'},
"idle" => {:bg => '#ffffff', :fg => '#000000'},
"iplastic" => {:bg => '#efefef', :fg => '#000000'},
"lazy" => {:bg => '#ffffff', :fg => '#000000'},
"mac_classic" => {:bg => '#ffffff', :fg => '#000000'},
"magicwb_amiga" => {:bg => '#969696', :fg => '#000000'},
"pastels_on_dark" => {:bg => '#211e1e', :fg => '#dadada'},
"slush_poppies" => {:bg => '#f1f1f1', :fg => '#000000'},
"spacecadet" => {:bg => '#0d0d0d', :fg => '#dde6cf'},
"sunburst" => {:bg => '#000000', :fg => '#f8f8f8'},
"twilight" => {:bg => '#141414', :fg => '#f8f8f8'},
"zenburnesque" => {:bg => '#404040', :fg => '#dedede'}
}
FILE_TYPES = {'rb' => 'ruby', 'rjs' => 'ruby', 'rxml' => 'ruby_on_rails',
'rhtml' => 'ruby_on_rails', 'pl' => 'perl', 'pm' => 'perl',
'html' => 'html', 'applescript' => 'applescript',
'py' => 'python', 'js' => 'javascript', 'as' => 'actionscript',
'php' => 'php', 'c' => 'c', 'h' => 'c', 'java' => 'java',
'markdown' => 'markdown', 'sh' => 'shell-unix-generic',
'bashrc' => 'shell-unix-generic', 'sql' => 'sql',
'txt' => 'plain_text', 'textile' => 'textile',
'xml' => 'xml', 'tld' => 'xml', 'jsp' => 'xml',
'rss' => 'xml', 'yaml' => 'yaml', 'lisp' => 'lisp',
'hs' => 'haskell'
}
def initialize(file)
@extention = File.extname(file).delete('.')
@syntax = FILE_TYPES[@extention]
@file = file
end
Uv.themes.each do |theme|
define_method("parse_with_#{theme}") do
parse(theme, true)
end
end
def parse(theme, numbering)
begin
File.open(@file) do |f|
html = Uv.parse( f.read, "xhtml", @syntax, numbering, theme )
css_file = File.join(Uv.path, %w(render xhtml files css), "#{theme}.css")
styles = {}
IO.read(css_file).gsub(/pre\.#{theme}\s+\.(\w+)\s*\{(.+?)\}/m) do
styles[$1] = $2.gsub(/\s/, "")
end
styles.each do |key, value|
html.gsub!(/class="#{key}"/i, %Q{style="#{value}"})
end
html.gsub!(/class="#{theme}"/) { |match| match +
%Q{ style="background-color:#{BGCOLORS[theme][:bg]};color:#{BGCOLORS[theme][:fg]}"} }
end
rescue Exception => e
puts e
end
end
end
if __FILE__ == $0
themes = Hilite::BGCOLORS.keys
# ARGV.each do |file|
# theme = themes[rand(themes.length)]
# colored_code = Hilite.new(file).parse(theme, true)
# puts "<p>#{file} with #{theme}</p>" + colored_code
# end
ARGV.each do |file|
colored_code = Hilite.new(file).parse_with_cobalt
puts colored_code #"<p>#{file} with cobalt</p>" + colored_code
end
end
Shoes.setup do
gem 'textpow'
gem 'ultraviolet'
end
require "hilite"
@file, @theme = nil, nil
Shoes.app :width => 500, :height => 370, :resizable => false do
background "#eed"
background "#082444", :height => 40
flow :height => 40, :width => self.width-100, :margin => 5 do
caption "Highlighting Your Code", :weight => "bold", :stroke => rgb(0.3, 0.8, 0.8)
end
flow :height => 45, :width => 100, :margin => 5 do
button("Create!") do
if @file.nil?
alert "Select your File"
elsif @theme.nil?
alert "Select a Theme"
else
@edit.text = Hilite.new(@file).parse(@theme, @numbering.checked?)
end
end
end
flow :height => 36 do
button("Select a file") do
@file = ask_open_file
@disp.replace @file
end
@disp = para "No file selected"
end
flow :height => 36, :margin => 3 do
list_box :items => Hilite::BGCOLORS.keys do |item|
@theme = item.text
end
@numbering = check
@numbering.checked = true
para "Numbering"
end
stack :margin => 10 do
@edit = edit_box :width => width - 20, :height => 200
end
flow do
button "Save" do
open(ask_save_file, 'w') do |f|
f.puts @edit.text
end
end
button "Copy" do
self.clipboard = @edit.text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment