Html.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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);
  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/>",$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>",$index);
  42. append_file("<tr><td><a href=\"projects/$indexPath\">$project</a></td>",$index);
  43. shellex("$mkdirCmd -p $web_projects_dir_path$project",$logger);
  44. }
  45. append_file("</tr></tbody></table></div></body></html>",$index);
  46. $logger->info("Wrote root index at $index");
  47. }
  48. sub gen_line_nums($$) {
  49. my $raw_file = shift;
  50. my $logger = shift;
  51. my $html_file;
  52. # Might be a better way to do this? TODO
  53. open my $fh, '>>', \$html_file or die "Can't open variable: $!";
  54. print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
  55. my $line_counter = 1;
  56. foreach my $line ( split("\n", $raw_file) ) {
  57. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a>\t$line</br>";
  58. $line_counter++;
  59. }
  60. print $fh "</pre></div><html>";
  61. close $fh;
  62. $logger->info("Generated line numbers for file");
  63. return $html_file;
  64. }
  65. sub gen_diff_colors($$) {
  66. my $raw_diff = shift;
  67. my $logger = shift;
  68. my $html_diff;
  69. open my $fh, '>>', \$html_diff or die "Can't open variable: $!";
  70. print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
  71. my $line_counter = 1;
  72. foreach my $line ( split("\n", $raw_diff) ) {
  73. if ( $line =~ m/^\+/ ) {
  74. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a><font color=\"green\">\t$line</font></br>";
  75. } elsif ( $line =~ m/^\-/ ) {
  76. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a><font color=\"red\">\t$line</font></br>";
  77. } elsif ( $line =~ m/^@@/ ) {
  78. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a><font color=\"blue\">\t$line</font></br>";
  79. } else {
  80. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a>\t$line</br>";
  81. }
  82. $line_counter++;
  83. }
  84. print $fh "</pre></div></html>";
  85. close $fh;
  86. return $html_diff;
  87. }
  88. # Main sub for generating project page
  89. # Might make more sense to split into more subs?
  90. sub write_project_content($$$$) {
  91. my $project_dirs_ref = shift;
  92. my $trimmed_project_dirs_ref = shift;
  93. my $web_projects_dir = shift;
  94. my $logger = shift;
  95. # Make these array's easier to work with in a hash
  96. # Key is path to actual git dir, val is path to associated web dir
  97. my %projects_map;
  98. @projects_map{@$project_dirs_ref} = @$trimmed_project_dirs_ref;
  99. $logger->info("Assembling data structures of git info");
  100. # Write files part of project index
  101. foreach my $project_path ( keys %projects_map ) {
  102. my $spec_web_dir = $web_projects_dir . $projects_map{$project_path};
  103. my $project_index = $spec_web_dir . "index.html";
  104. write_file("",$project_index);
  105. append_file("<html><a href=\"../../index.html\">Return to index</a></b><hr/>",$project_index);
  106. # Get all project data structures/info
  107. my ( $file_tree_ref, $file_content_ref, $commits_ref, $commit_ids_ref ) = get_file_tree($project_path,$logger);
  108. # Handle README
  109. if ( grep /^README.md$/, keys %$file_content_ref ) {
  110. $logger->info("$projects_map{$project_path} contains a README");
  111. my $readme_html = render_readme(${$file_content_ref}{'README.md'},$logger);
  112. append_file("$readme_html",$project_index);
  113. }
  114. append_file("<b>Files for $projects_map{$project_path}</b><br>",$project_index);
  115. append_file("<hr/>",$project_index);
  116. ## Write files ##
  117. 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);
  118. foreach my $filename ( sort keys %$file_content_ref ) {
  119. my $browserCompat = $filename . ".html";
  120. # Rewrite dir paths so we can save on disk without producing actual dir structure
  121. if ( $filename =~ m/\// ) {
  122. my $copy = $filename;
  123. $copy =~ s/\//_/g;
  124. $browserCompat = $copy . ".html";
  125. }
  126. append_file("<tr><td><a href=\"$browserCompat\">$filename</a></td><td>${$file_tree_ref}{$filename}</td>",$project_index);
  127. my $html_file = gen_line_nums(${$file_content_ref}{$filename},$logger);
  128. write_file("$html_file",$spec_web_dir . $browserCompat);
  129. }
  130. append_file("</tr></tbody></table></div></body>",$project_index);
  131. append_file("<br>", $project_index);
  132. append_file("<html><b>Logs for $projects_map{$project_path}</b><br>",$project_index);
  133. append_file("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$project_index);
  134. append_file("<hr/>",$project_index);
  135. # iterate over array to keep ordering
  136. foreach my $commit_id ( @$commit_ids_ref ) {
  137. my $filename = $commit_id . ".html";
  138. append_file("<tr><td><a href=\"$filename\">$filename</a></td>",$project_index);
  139. my $html_diff = gen_diff_colors(${$commits_ref}{$commit_id},$logger);
  140. write_file($html_diff,$spec_web_dir . $filename);
  141. }
  142. append_file("</tr></tbody></table></div></body>",$project_index);
  143. append_file("</html>",$project_index);
  144. }
  145. $logger->info("Done writing files");
  146. }
  147. # Not used currently, need to do more trimming/etc
  148. # TODO
  149. # Work around is rm -rf the webroot manually and then just rerun gsg
  150. sub clean_web_root($$$) {
  151. my $web_projects_dir_path = shift;
  152. my $git_projects_ref = shift;
  153. my $logger = shift;
  154. my $lsCmd = findBin("ls",$logger);
  155. my $rmCmd = findBin("rm",$logger);
  156. foreach my $dir ( split("\n", shellex("$lsCmd -d $web_projects_dir_path/*/",$logger)) ) {
  157. if ( ! grep( /^$dir$/, @$git_projects_ref ) ) {
  158. $logger->info("Found $dir in webroot but not in git root, removing...");
  159. my $rmdir = $web_projects_dir_path . $dir;
  160. $logger->info("Would remove $rmdir");
  161. # Does this need to be safer? TODO
  162. #shellex("$rmCmd $rmdir/*",$logger);
  163. #shellex("$rmCmd -d $rmdir",$logger);
  164. }
  165. }
  166. }
  167. 1;