Browse Source

Added coloring for diffs. A few issues but basics are there

spesk1 4 years ago
parent
commit
8640ef4190
2 changed files with 36 additions and 3 deletions
  1. 2 1
      README.md
  2. 34 2
      lib/Gsg/Html.pm

+ 2 - 1
README.md

@@ -14,12 +14,13 @@ DONE:
 * Generate project specific index with links to git log/diffs and file content of repo
 * Extremely basic markdown parser (only supports headings and non nested bullets
 * Line numbers for file browsing
+* Diff highlighting for log
 
 TODO:
 * Show git:// address for cloning (configurable via config file)
 * Code needs to be more robust / handle failure cases
 * Need to recurse git home, right now will only pull dirs in root of git home
-* Diff highlighting for log
+* Fix issue with diff highlight where if the diff contains HTML it gets rendered in the browser (isolate/sanitize it somehow)
 * README.md markdown renderer/parser / display in project index
 * (Stretch Goal) HTML based syntax highlighting for files
 * Support detection of misconfig of config file

+ 34 - 2
lib/Gsg/Html.pm

@@ -73,10 +73,41 @@ sub gen_line_nums($$) {
 	print $fh "</pre></div><html>";
 	close $fh;
 
+	$logger->info("Generated line numbers for file");
 	return $html_file;
 
 }
 
+sub gen_diff_colors($$) {
+
+	my $raw_diff = shift;
+	my $logger = shift;
+
+	my $html_diff;
+	open my $fh, '>>', \$html_diff or die "Can't open variable: $!";
+
+	print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
+	my $line_counter = 1;
+	foreach my $line ( split("\n", $raw_diff) ) {
+		if ( $line =~ m/^\+/ ) {
+			print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a><font color=\"green\">\t$line</font></br>";
+		} elsif ( $line =~ m/^\-/ ) {
+			print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a><font color=\"red\">\t$line</font></br>";
+		} elsif ( $line =~ m/^@@/ ) {
+			print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a><font color=\"blue\">\t$line</font></br>";
+		} else {
+			print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a>\t$line</br>";
+		}
+		$line_counter++;
+	}
+
+	print $fh "</pre></div></html>";
+	close $fh;
+
+	return $html_diff;
+
+}
+
 # Main sub for generating project page
 # Might make more sense to split into more subs?
 sub write_project_content($$$$) {
@@ -135,9 +166,10 @@ sub write_project_content($$$$) {
 
 	# iterate over array to keep ordering
 	foreach my $commit_id ( @$commit_ids_ref ) {
-		my $filename = $commit_id . ".txt";
+		my $filename = $commit_id . ".html";
 		append_file("<tr><td><a href=\"$filename\">$filename</a></td>",$project_index);
-		write_file(${$commits_ref}{$commit_id},$spec_web_dir . $filename);
+		my $html_diff = gen_diff_colors(${$commits_ref}{$commit_id},$logger);
+		write_file($html_diff,$spec_web_dir . $filename);
         }
         append_file("</tr></tbody></table></div></body>",$project_index);
         append_file("</html>",$project_index);