Browse Source

Final fix for escaping HTML contained in source files

spesk1 4 years ago
parent
commit
1afd193eda
1 changed files with 43 additions and 6 deletions
  1. 43 6
      lib/Gsg/Html.pm

+ 43 - 6
lib/Gsg/Html.pm

@@ -54,6 +54,36 @@ sub write_root_index($$$$) {
 
 }
 
+sub check_for_html($) {
+
+	# Expects line from gen_line_nums or gen_diff_colors
+	# Will change < and > chars to &lt; or &gt;
+	# This adds tons of overhead, but should work better
+	# than <xmp> and will solve the rendering problems
+	my $line = shift;
+	my $new_line;
+	open my $fh, '>>', \$new_line or die "Can't open variable: $!";
+	foreach my $char ( split("",$line) ) {
+		if ( ! defined $char || $char eq "" ) {
+			print "Empty char\n";
+			next;
+		}
+
+		if ( $char eq "<" ) {
+			print $fh "&lt;";
+		} elsif ( $char eq ">" ) {
+			print $fh "&gt;";
+		} else {
+			print $fh "$char";
+		}
+	}
+
+	close $fh;
+	return $new_line;
+
+}
+
+
 sub gen_line_nums($$) {
 
 	my $raw_file = shift;
@@ -66,6 +96,9 @@ sub gen_line_nums($$) {
 	print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
 	my $line_counter = 1;
 	foreach my $line ( split("\n", $raw_file) ) {
+		if ( $line ne "" ) {
+			$line = check_for_html($line);
+		}
 		print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a>\t$line</br>";
 		$line_counter++;
 	}
@@ -90,18 +123,22 @@ sub gen_diff_colors($$) {
 	print $fh "<!DOCTYPE html><html><div id=\"content\"><pre>";
 	my $line_counter = 1;
 	foreach my $line ( split("\n", $raw_diff) ) {
+		if ( $line ne "" ) {
+			$line = check_for_html($line);
+		}
+
 		if ( $line =~ m/^\+/ ) {
-			print $fh "<font color=\"green\"><xmp>$line</xmp></font>";
+			print $fh "<font color=\"green\">$line</font></br>";
 		} elsif ( $line =~ m/^\-/ ) {
-			print $fh "<font color=\"red\"><xmp>$line</xmp></font>";
+			print $fh "<font color=\"red\">$line</font></br>";
 		} elsif ( $line =~ m/^@@/ ) {
-			print $fh "<font color=\"blue\"><xmp>$line</xmp></font>";
+			print $fh "<font color=\"blue\">$line</font></br>";
 		} elsif ( $line =~ m/^diff/ ) {
-			print $fh "<font color=\"purple\"><xmp>$line</xmp></font>";
+			print $fh "<font color=\"purple\">$line</font></br>";
 		} elsif ( $line =~ m/^commit/ ) {
-			print $fh "<font color=\"purple\"><b><xmp>$line</xmp></b></font>";
+			print $fh "<font color=\"purple\"><b>$line</b></font></br>";
 		} else {
-			print $fh "<xmp>$line</xmp>";
+			print $fh "$line</br>";
 		}
 		$line_counter++;
 	}