#!/usr/bin/perl -w # htmlify.pl # # Author: Reto Schaer # # Documentation: # http://www.infocopter.com/perl/modules/util-htmlify.html # # -- $Id: htmlify.pl,v 1.5 2006/01/20 13:43:06 webcms Exp $ $| = 1; use strict; my $VERSION = '0.80.05'; use File::Copy; ##### PROTOTYPING sub bleechQuote ($); sub Error ($); ##### GLOBAL my @BLUE_WORDS = qw(my if else unless next sleep use strict scalar localtime while foreach); my @RED_WORDS = qw(print mkdir open close exit 0 1); my $DIEZAHL = '42'; # -- http://www.42.org/42.html my $IN = $ARGV[0] || ''; my $OUT = "/tmp/zz_trash_out$$\.html"; open(IN , "<$IN" ) or print STDERR $!; open(OUT, ">$OUT") or print STDERR $!; my $shebang = ; chomp $shebang; if ($shebang =~ /^\
', $shebang;

my $LC = 1; # line counter (shebang already processed)

while() {
	$LC++;
	# -- Safe these first:
	s/\&/\&/g; # -- 1. !
	s/__(.+)$DIEZAHL(.*)__/--$DIEZAHL==$1$DIEZAHL$2==$DIEZAHL--/; # -- 2. !
	s/\/__GT42FOO__/g;
	s/\\\#/__SAFE42HASH__/g;
	s/\\"/__SAFE42QUOTE__/g;

	while (/["']/ && $_ !~ /\#/) {
		# -- Found a quote and no comment on line
		$_ = bleechQuote $_ ;
	}

	# -- Colorize keywords:
	foreach my $bw (@BLUE_WORDS) {
		s/\#/__FIRST42HASH__/;
		my ($left, $right) = split /__FIRST42HASH__/; $right ||= '';
		$left =~ s/\b$bw\b/__COLBLUE42START__$bw\__SPAN42END__/g;
		$_ = $left;
		$_ .= '#' . $right if $right;
	}

	foreach my $rw (@RED_WORDS) {
		s/\#/__FIRST42HASH__/;
		my ($left, $right) = split /__FIRST42HASH__/; $right ||= '';
		$left =~ s/\b$rw\b/__COLRED42START__$rw\__SPAN42END__/g;
		$_ = $left;
		$_ .= '#' . $right if $right;
	}

	if (/\#/) {
		s/\#/__FIRST42HASH__/;
		my ($left, $right) = split /__FIRST42HASH__/;
		while ($left =~ /["']/) {
			$left = (bleechQuote $left);
		}
		$_ = $left . '__FIRST42HASH__' . $right;
	}

	# -- "Green" a comment:
	s/color\:\#/__HTML42COLOR__/gi; # -- Safe these hash chars
	s/\#/__FIRST42HASH__/;
	s/(.*)__FIRST42HASH__(.+)/$1__HASH42FOO__$2\<\/span>/;

	# -- Wrap up:

	s/__COLRED42START__/\/g;
	s/__COLBLUE42START__/\/g;
	s/__SPAN42END__/\<\/span>/g;

	s/__QUOT42BEGIN__/\"\/g;
	s/__QUOT42END__/\<\/span>\"/g;

	s/__SQUOT42BEGIN__/'\/g;
	s/__SQUOT42END__/\<\/span>'/g;

	s/__HTML42COLOR__/color\:\#/g;
	s/__STYLE42FOO__/ style="/g;
	s/__STYLE42CLOSE__/\">/;
	s/__HASH42FOO__/\#/g;

	s/__FREE42QUOTE__/"/g;
	s/__FREE42SQUOTE__/'/g;
	s/__FIRST42HASH__/\\#<\/span>/g;

	s/__LT42FOO__/\</g;
	s/__GT42FOO__/\>/g;

	s/__SAFE42HASH__/\\\#/g;
	s/__SAFE42QUOTE__/\\\"/g;
	s/--$DIEZAHL==/__/g;
	s/==$DIEZAHL--/__/g;

	print OUT $_;
}

print OUT '
'; close IN; close OUT; copy $OUT, $IN or print STDERR "$!\n"; unlink $OUT or print STDERR "$!"; print $LC, " lines processed\n"; ########################################## sub bleechQuote ($) { ########################################## my $in = $_[0]; my $quot = '"'; (my $temp = $in) =~ s/$quot/__QUOT42BEGIN__/; if ($temp =~ /$quot/) { $temp =~ s/$quot/__QUOT42END__/; $in = $temp; } else { # Impair quote(s) $in =~ s/$quot/__FREE42QUOTE__/; } # -- Phase 2 $quot = "'"; ($temp = $in) =~ s/$quot/__SQUOT42BEGIN__/; if ($temp =~ /$quot/) { $temp =~ s/$quot/__SQUOT42END__/; $in = $temp; } else { # Impair quote(s) $in =~ s/$quot/__FREE42SQUOTE__/; } $in; } sub Error ($) { unlink $OUT; print "ERROR: $_[0]\n"; exit; }