Html.pm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 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 $logger = shift;
  75. my $html_file;
  76. # Might be a better way to do this? TODO
  77. open my $fh, '>>', \$html_file or die "Can't open variable: $!";
  78. print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
  79. my $line_counter = 1;
  80. foreach my $line ( split("\n", $raw_file) ) {
  81. if ( $line ne "" ) {
  82. $line = check_for_html($line);
  83. }
  84. print $fh "<a href=\"\#l$line_counter\" class=\"line\" id=\"l$line_counter\">$line_counter</a>\t$line</br>";
  85. $line_counter++;
  86. }
  87. print $fh "</pre></div><html>";
  88. close $fh;
  89. $logger->info("Generated line numbers for file");
  90. return $html_file;
  91. }
  92. sub gen_diff_colors($$) {
  93. my $raw_diff = shift;
  94. my $logger = shift;
  95. my $html_diff;
  96. open my $fh, '>>', \$html_diff or die "Can't open variable: $!";
  97. #print $fh "<!DOCTYPE html><html><div id=\"content\"><pre id=\"blob\">";
  98. print $fh "<!DOCTYPE html><html><div id=\"content\"><pre>";
  99. my $line_counter = 1;
  100. foreach my $line ( split("\n", $raw_diff) ) {
  101. if ( $line ne "" ) {
  102. $line = check_for_html($line);
  103. }
  104. if ( $line =~ m/^\+/ ) {
  105. print $fh "<font color=\"green\">$line</font></br>";
  106. } elsif ( $line =~ m/^\-/ ) {
  107. print $fh "<font color=\"red\">$line</font></br>";
  108. } elsif ( $line =~ m/^@@/ ) {
  109. print $fh "<font color=\"blue\">$line</font></br>";
  110. } elsif ( $line =~ m/^diff/ ) {
  111. print $fh "<font color=\"purple\">$line</font></br>";
  112. } elsif ( $line =~ m/^commit/ ) {
  113. print $fh "<font color=\"purple\"><b>$line</b></font></br>";
  114. } else {
  115. print $fh "$line</br>";
  116. }
  117. $line_counter++;
  118. }
  119. print $fh "</pre></div></html>";
  120. close $fh;
  121. return $html_diff;
  122. }
  123. # Main sub for generating project page
  124. # Might make more sense to split into more subs?
  125. sub write_project_content($$$$) {
  126. my $project_dirs_ref = shift;
  127. my $trimmed_project_dirs_ref = shift;
  128. my $web_projects_dir = shift;
  129. my $logger = shift;
  130. # Make these array's easier to work with in a hash
  131. # Key is path to actual git dir, val is path to associated web dir
  132. my %projects_map;
  133. @projects_map{@$project_dirs_ref} = @$trimmed_project_dirs_ref;
  134. $logger->info("Assembling data structures of git info");
  135. # Write files part of project index
  136. foreach my $project_path ( keys %projects_map ) {
  137. my $spec_web_dir = $web_projects_dir . $projects_map{$project_path};
  138. my $project_index = $spec_web_dir . "index.html";
  139. write_file("",$project_index);
  140. append_file("<html><a href=\"../../index.html\">Return to index</a></b><hr/>",$project_index);
  141. # Get all project data structures/info
  142. my ( $file_tree_ref, $file_content_ref, $commits_ref, $commit_ids_ref ) = get_file_tree($project_path,$logger);
  143. # Handle README
  144. if ( grep /^README.md$/, keys %$file_content_ref ) {
  145. $logger->info("$projects_map{$project_path} contains a README");
  146. my $readme_html = render_readme(${$file_content_ref}{'README.md'},$logger);
  147. append_file("$readme_html",$project_index);
  148. }
  149. append_file("<b>Files for $projects_map{$project_path}</b><br>",$project_index);
  150. append_file("<hr/>",$project_index);
  151. ## Write files ##
  152. 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);
  153. foreach my $filename ( sort keys %$file_content_ref ) {
  154. my $browserCompat = $filename . ".html";
  155. # Rewrite dir paths so we can save on disk without producing actual dir structure
  156. if ( $filename =~ m/\// ) {
  157. my $copy = $filename;
  158. $copy =~ s/\//_/g;
  159. $browserCompat = $copy . ".html";
  160. }
  161. append_file("<tr><td><a href=\"$browserCompat\">$filename</a></td><td>${$file_tree_ref}{$filename}</td>",$project_index);
  162. my $html_file = gen_line_nums(${$file_content_ref}{$filename},$logger);
  163. write_file("$html_file",$spec_web_dir . $browserCompat);
  164. }
  165. append_file("</tr></tbody></table></div></body>",$project_index);
  166. append_file("<br>", $project_index);
  167. append_file("<html><b>Logs for $projects_map{$project_path}</b><br>",$project_index);
  168. append_file("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$project_index);
  169. append_file("<hr/>",$project_index);
  170. # iterate over array to keep ordering
  171. foreach my $commit_id ( @$commit_ids_ref ) {
  172. my $filename = $commit_id . ".html";
  173. append_file("<tr><td><a href=\"$filename\">$filename</a></td>",$project_index);
  174. my $html_diff = gen_diff_colors(${$commits_ref}{$commit_id},$logger);
  175. write_file($html_diff,$spec_web_dir . $filename);
  176. }
  177. append_file("</tr></tbody></table></div></body>",$project_index);
  178. append_file("</html>",$project_index);
  179. }
  180. $logger->info("Done writing files");
  181. }
  182. # Not used currently, need to do more trimming/etc
  183. # TODO
  184. # Work around is rm -rf the webroot manually and then just rerun gsg
  185. sub clean_web_root($$$) {
  186. my $web_projects_dir_path = shift;
  187. my $git_projects_ref = shift;
  188. my $logger = shift;
  189. my $lsCmd = findBin("ls",$logger);
  190. my $rmCmd = findBin("rm",$logger);
  191. foreach my $dir ( split("\n", shellex("$lsCmd -d $web_projects_dir_path/*/",$logger)) ) {
  192. if ( ! grep( /^$dir$/, @$git_projects_ref ) ) {
  193. $logger->info("Found $dir in webroot but not in git root, removing...");
  194. my $rmdir = $web_projects_dir_path . $dir;
  195. $logger->info("Would remove $rmdir");
  196. # Does this need to be safer? TODO
  197. #shellex("$rmCmd $rmdir/*",$logger);
  198. #shellex("$rmCmd -d $rmdir",$logger);
  199. }
  200. }
  201. }
  202. 1;