#!/usr/bin/perl -w use strict; # sql to html converter # output is filename.java.out.html # overwrites output without asking # very slow # Brian Robb. if (1) { ########################################################### foreach my $cmd (@ARGV) { if ($cmd eq "--help") { print "sqltohtml: Converts sql source files to formatted html...\n"; print "\n"; print "Usage: sqltohtml\n"; print "\n"; print "sqltohtml --help\n"; print "sqltohtml --version\n"; exit(0); } if ($cmd eq "--version") { print "1.0\n"; exit(0); } } ########################################################### my $start_time = time(); my @keywords = qw(begin end declare select while from order by varchar uniqueidentifier alter procedure set nocount on ANSI_NULLS QUOTED_IDENTIFIER as where go top if break create table return int not null is RAISERROR unique ASC DESC CLOSE DEALLOCATE exists print char convert open fetch next into cursor for newid values insert NONCLUSTERED INDEX print char UPDATE MAX MIN and or else); die "Usage: $0 \n" unless (@ARGV == 1); my @filenames = glob($ARGV[0]); foreach my $filename (@filenames) { open(IN, "$filename") || die ($!); open(OUT, ">$filename.out.html") || die ($!); my $keywords_color = "#0033FF"; my $important_words_color = "#FF6600"; my $single_line_comment_color = "#339999"; my $multi_line_comment_color = "#339966"; my $string_color = "#663366"; my $brace_color = "#333333"; my $number_color = "#3399FF"; my $floating_point_number_color = "#6699FF"; my $preprocessor_words_color = "#333333"; my $line_number = 0; my $debug = 0; my $warnings = 1; my @tokens = (); print OUT "\n\n\t$filename\n\n"; while (my $line = ) { $line_number++; if ($warnings) { if (length($line) > 80) { print "Warning, line ", $line_number, " ", $filename, " maybe too long (", length($line), " characters) to print:\n", $line; } chomp($line); @tokens = (); while (length($line) > 0) { if ($line =~ m/^( |\t)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^([#\@a-zA-Z_\[\].]+)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(\d+)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(\()/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(\))/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(,)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(;)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(=)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(\+)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(\>)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(\*)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^('.*?')/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } if ($line =~ m/^(--.*)/) { push (@tokens, $1); $line = ""; } if ($line =~ m/^(-)/) { push (@tokens, $1); $line = $'; # the line now consists of the text to the right of the matching expression } } push (@tokens, "\n"); foreach my $token (@tokens) { if ($token eq " ") { $token = " "; } elsif ($token eq "\n") { $token = "
\n"; } elsif ($token =~ m/^--/) { $token = "$token"; } elsif ($token =~ m/^'/) { $token = "$token"; } elsif ($token =~ m/^(\d+)/) { $token = "$token"; } elsif ($token =~ m/^\(/) { $token = "$token"; } elsif ($token =~ m/^\)/) { $token = "$token"; } elsif ($token =~ m/^\@/) { $token = "$token"; } else { foreach my $word (@keywords) { $word = uc($word); $token =~ s/(\b)$word(\b)/$1$word$2<\/font>/gi; } } print OUT $token; } } } print OUT "\n\n"; close IN || die ($!); close OUT || die ($!); my $total_time = time() - $start_time; print "tadah! ", $filename, " took ", $total_time, " second", ($total_time == 1 ? "" : "s"), "\n"; } }