All pastes #426267 Raw Edit

Bitty

public ruby v1 · immutable
·published 2007-04-06 00:46 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 :'(### state tracking variablesbracedepth	= 0	 		### nested {incomment	= 0			### in multi-line commentinstring	= 0			### in "..." string### 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=\"bg-color:#{colbr[bracedepth]};\">"closespan	= "</span>"closefont	= "</font>"html        	= ""### this is the tricky part### priority: preprocessor statements (handled in main loop),### comments, strings, braces### problem : unexpected kEND, remove them and get error : expecting kENDdef scanline	i = 0	while i < line.length		### could mess up if line[0] is a star and line[-1] is /		if line[i]=="*" and line[i-1]=="/" and incomment+instring== 0 			line = line[0,i-2] + comment + line[i-1,-1]			incomment = 1			i++		end		if line[i] == "*" and line[i+1] == "/" and incomment == 1			line = line[0,i+1] + closefont + line[i+2,-1]			incomment = 0			i++		end		i++	endend	mifile		= ARGV.shift# thanks to mr. hdm for some way cool code hackshtml << %q^<html><head><title>#{mifile} - BitRot</title></head><body><style>	body {	background-color:lightgray;	font-face:monaco,courier;	font-size:10px; }	</style><pre>^### main loop### TMP TEST that just looks for lines starting with # and colors themFile.readlines(mifile).each do |line|	line = line.gsub('<', '&lt;').gsub('>', '&gt;').chomp		if line =~ /^#/		if(incomment + instring == 0)			line = preprocess + line + "\n" + closefont		end	### calls to scanline go here	end		html << lineendhtml << %q^</body></html>	^#outp = ARGV.shift#fd   = outp ? File.open(outp, "w") : $stdoutprint htmlend