Html.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package Gsg::Html;
  2. use strict;
  3. use warnings;
  4. use Log::Log4perl qw(:easy);
  5. use lib "/usr/local/lib";
  6. use Shellex::Shellex qw(shellex findBin);
  7. use Gsg::Gather qw(get_file_tree get_diff_stat);
  8. use Gsg::MdParse qw (render_readme);
  9. use Exporter qw(import);
  10. our @EXPORT_OK = qw(
  11. write_file append_file write_root_index clean_web_root
  12. write_project_content
  13. );
  14. # These subs might belong in shellex
  15. # Add logger for write opts TODO
  16. sub write_file($$) {
  17. my $content = shift;
  18. my $path = shift;
  19. open(my $fh, ">", $path) or die "Couldnt open $path\n";
  20. print $fh "$content";
  21. close $fh;
  22. }
  23. sub append_file($$) {
  24. my $content = shift;
  25. my $path = shift;
  26. open(my $fh, ">>", $path) or die "Couldnt open $path\n";
  27. print $fh "$content";
  28. close $fh;
  29. }
  30. sub write_root_index($$$$) {
  31. my $index = shift;
  32. my $project_dirs_ref = shift;
  33. my $web_projects_dir_path = shift;
  34. my $logger = shift;
  35. write_file("", $index);
  36. append_file("<html><body><b>Git Projects</b><br><head><META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\"></head>\n",$index);
  37. append_file("<small><i>Statically generated web root for browsing this git server</i></small><br><hr/>\n",$index);
  38. my $mkdirCmd = findBin("mkdir",$logger);
  39. foreach my $project ( @$project_dirs_ref ) {
  40. my $indexPath = $project . "index.html";
  41. append_file("<table><div id=\"cotent\"><table id=\"index\"><tbody>\n",$index);
  42. append_file("<tr><td><a href=\"projects/$indexPath\">$project</a></td>\n",$index);
  43. shellex("$mkdirCmd -p $web_projects_dir_path$project",$logger);
  44. }
  45. append_file("</tr></tbody></table></div></body></html>\n",$index);
  46. $logger->info("Wrote root index at $index");
  47. }
  48. sub check_for_html($) {
  49. # Expects line from gen_line_nums or gen_diff_colors
  50. # Will change < and > chars to &lt; or &gt;
  51. # This adds tons of overhead, but should work better
  52. # than <xmp> and will solve the rendering problems
  53. my $line = shift;
  54. my $new_line;
  55. open my $fh, '>>', \$new_line or die "Can't open variable: $!";
  56. foreach my $char ( split("",$line) ) {
  57. if ( ! defined $char || $char eq "" ) {
  58. print "Empty char\n";
  59. next;
  60. }
  61. if ( $char eq "<" ) {
  62. print $fh "&lt;";
  63. } elsif ( $char eq ">" ) {
  64. print $fh "&gt;";
  65. } else {
  66. print $fh "$char";
  67. }
  68. }
  69. close $fh;
  70. return $new_line;
  71. }
  72. sub gen_line_nums($$$) {
  73. my $raw_file = shift;
  74. my $filename = shift;
  75. my $logger = shift;
  76. my $html_file;
  77. # Might be a better way to do this? TODO
  78. open my $fh, '>>', \$html_file or die "Can't open variable: $!";
  79. print $fh "<!DOCTYPE html><html><b>$filename</b><hr/><div id=\"content\"><pre id=\"blob\">\n";
  80. my $line_counter = 1;
  81. foreach my $line ( split("\n", $raw_file) ) {
  82. if ( $line ne "" ) {
  83. $line = check_for_html($line);
  84. }
  85. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a>\t$line<br>\n";
  86. $line_counter++;
  87. }
  88. print $fh "</pre></div><html>\n";
  89. close $fh;
  90. $logger->info("Generated line numbers for $filename");
  91. return $html_file;
  92. }
  93. sub gen_diff_colors($$$) {
  94. my $raw_diff = shift;
  95. my $id = shift;
  96. my $logger = shift;
  97. my $html_diff;
  98. open my $fh, '>>', \$html_diff or die "Can't open variable: $!";
  99. #print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
  100. print $fh "<!DOCTYPE html><html><b>$id</b><hr/><div id=\"content\"><pre>";
  101. my $line_counter = 1;
  102. foreach my $line ( split("\n", $raw_diff) ) {
  103. if ( $line ne "" ) {
  104. $line = check_for_html($line);
  105. }
  106. if ( $line =~ m/^\+/ ) {
  107. print $fh "<font color=\"green\">$line</font><br>";
  108. } elsif ( $line =~ m/^\-/ ) {
  109. print $fh "<font color=\"red\">$line</font><br>";
  110. } elsif ( $line =~ m/^@@/ ) {
  111. print $fh "<font color=\"blue\">$line</font><br>";
  112. } elsif ( $line =~ m/^diff/ ) {
  113. print $fh "<font color=\"purple\">$line</font><br>";
  114. } elsif ( $line =~ m/^commit/ ) {
  115. print $fh "<font color=\"purple\"><b>$line</b></font><br>";
  116. } else {
  117. print $fh "$line<br>";
  118. }
  119. $line_counter++;
  120. }
  121. print $fh "</pre></div></html>";
  122. close $fh;
  123. $logger->info("Generated colored diff for $id");
  124. return $html_diff;
  125. }
  126. # Main sub for generating project page
  127. # Might make more sense to split into more subs?
  128. sub write_project_content($$$$$) {
  129. my $project_dirs_ref = shift;
  130. my $trimmed_project_dirs_ref = shift;
  131. my $web_projects_dir = shift;
  132. my $clone_path = shift;
  133. my $logger = shift;
  134. # Make these array's easier to work with in a hash
  135. # Key is path to actual git dir, val is path to associated web dir
  136. my %projects_map;
  137. @projects_map{@$project_dirs_ref} = @$trimmed_project_dirs_ref;
  138. $logger->info("Assembling data structures of git info");
  139. # Write files part of project index
  140. foreach my $project_path ( keys %projects_map ) {
  141. my $spec_web_dir = $web_projects_dir . $projects_map{$project_path};
  142. my $project_index = $spec_web_dir . "index.html";
  143. write_file("",$project_index);
  144. append_file("<html><a href=\"../../index.html\"><b>Return to index</a></b><br>",$project_index);
  145. my $uniq_clone_path = "Disabled";
  146. if ( $clone_path ne "Disabled" ) {
  147. $uniq_clone_path = $clone_path . $projects_map{$project_path};
  148. }
  149. append_file("<b><pre>Clone:<font color=\"green\"> git clone $uniq_clone_path</font></pre></b><hr/>",$project_index);
  150. # Get all project data structures/info
  151. my ( $file_tree_ref, $file_content_ref, $commits_ref, $commit_ids_ref ) = get_file_tree($project_path,$logger);
  152. # Handle README
  153. if ( grep /^README.md$/, keys %$file_content_ref ) {
  154. $logger->info("$projects_map{$project_path} contains a README");
  155. my $readme_html = render_readme(${$file_content_ref}{'README.md'},$logger);
  156. append_file("$readme_html",$project_index);
  157. }
  158. append_file("<b>Files for $projects_map{$project_path}</b><br>",$project_index);
  159. append_file("<hr/>",$project_index);
  160. ## Write files ##
  161. append_file("<table><div id=\"cotent\"><table id=\"index\"><thead><tr><td><b>File</b></td><td><b>Commit</b></td></tr></thead><tbody>",$project_index);
  162. foreach my $filename ( sort keys %$file_content_ref ) {
  163. my $browserCompat = $filename . ".html";
  164. # Rewrite dir paths so we can save on disk without producing actual dir structure
  165. if ( $filename =~ m/\// ) {
  166. my $copy = $filename;
  167. $copy =~ s/\//_/g;
  168. $browserCompat = $copy . ".html";
  169. }
  170. append_file("<tr><td><a href=\"$browserCompat\">$filename</a></td><td>${$file_tree_ref}{$filename}</td>",$project_index);
  171. my $html_file = gen_line_nums(${$file_content_ref}{$filename},$filename,$logger);
  172. write_file("$html_file",$spec_web_dir . $browserCompat);
  173. }
  174. append_file("</tr></tbody></table></div></body>",$project_index);
  175. append_file("<br>", $project_index);
  176. append_file("<html><b>Logs for $projects_map{$project_path}</b><br>",$project_index);
  177. append_file("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$project_index);
  178. append_file("<hr/>",$project_index);
  179. # iterate over array to keep ordering
  180. foreach my $commit_id ( @$commit_ids_ref ) {
  181. my $filename = $commit_id . ".html";
  182. append_file("<tr><td><a href=\"$filename\">$filename</a></td>",$project_index);
  183. my $html_diff = gen_diff_colors(${$commits_ref}{$commit_id},$commit_id,$logger);
  184. write_file($html_diff,$spec_web_dir . $filename);
  185. }
  186. append_file("</tr></tbody></table></div></body>",$project_index);
  187. append_file("</html>",$project_index);
  188. }
  189. $logger->info("Done writing files");
  190. }
  191. # Not used currently, need to do more trimming/etc
  192. # TODO
  193. # Work around is rm -rf the webroot manually and then just rerun gsg
  194. sub clean_web_root($$$) {
  195. my $web_projects_dir_path = shift;
  196. my $git_projects_ref = shift;
  197. my $logger = shift;
  198. my $lsCmd = findBin("ls",$logger);
  199. my $rmCmd = findBin("rm",$logger);
  200. foreach my $dir ( split("\n", shellex("$lsCmd -d $web_projects_dir_path/*/",$logger)) ) {
  201. if ( ! grep( /^$dir$/, @$git_projects_ref ) ) {
  202. $logger->info("Found $dir in webroot but not in git root, removing...");
  203. my $rmdir = $web_projects_dir_path . $dir;
  204. $logger->info("Would remove $rmdir");
  205. # Does this need to be safer? TODO
  206. #shellex("$rmCmd $rmdir/*",$logger);
  207. #shellex("$rmCmd -d $rmdir",$logger);
  208. }
  209. }
  210. }
  211. 1;