All pastes #424953 Raw Edit

Miscellany

public ruby v1 · immutable
·published 2007-04-05 03:38 UTC
rendered paste body
#!/usr/bin/ruby# bitty's itty bitty highlighter :D# c/c++ - colors comments, strs, brace blocks# my first ruby program so do not laugh :'(def usage	puts "usage #{$0} <input.c> [output.html]"	exit(0)end### state tracking variablesbracedepth	= 0 		### nested {incomment	= 0			### in multi-line commentinstring	= 0			### in "..." stringbslash		= 0			### prev char is / ### syntax colorscolcc		= "blue"		### compiler instructionscolcom		= "green"		### commentscolstr		= "maroon"		### stringscolbr		= ["antiquewhite","aquamarine","thistle"]### the three brace colors cycle### common html strings### yeah <font> is deprecated but it makes my life easiercomment		= "<font color='#{colcom}'>"string		= "<font color='#{colstr}'>" preprocess	= "<font color='#{colcc}'>"brace		= "<span style=\"color:#{colbr[bracedepth]};\">"closespan	= "</span>"closefont	= "</font>"html        = ""mifile		= ARGV.shift || usage()html << %q^<html><head>	<title>#{mifile} - BitRot</title>	<style> <!--- //		body {			background-color:lightgray;			font-face:monaco,courier;			font-size:10px; 		}	//-->	</style></head><body><pre>^### main loop### TMP TEST that just looks for lines starting with # and colors them### except it does not work :'(File.readlines(mifile).each do |line|	prefix = ''	suffix = ''	if line =~ /^#/		if(incomment + instring == 0)			prefix = preprocess			suffix = closefont		end	end		html << prefix	html << line.gsub('<', '&lt;').gsub('>', '&gt;').chomp 	html << suffix + "\n"endhtml << %q^</body></html>	^outp = ARGV.shiftfd   = outp ? File.open(outp, "w") : $stdoutfd.puts html