commit 8640ef4190be1a1aabc738275cc159719c8870dc
Author: spesk1 <spesk@pm.me>
Date: Sat Jun 22 19:21:36 2019 +0000
Added coloring for diffs. A few issues but basics are there
diff --git a/README.md b/README.md
index f29117d..3c43c5b 100644
--- a/README.md
+++ b/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
diff --git a/lib/Gsg/Html.pm b/lib/Gsg/Html.pm
index 8e64209..ffad1d1 100644
--- a/lib/Gsg/Html.pm
+++ b/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);