Html.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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>",$index);
  38. append_file("<small><i>This is a read-only site and does not provide a merge/clone interface</i></small><hr/>",$index);
  39. my $mkdirCmd = findBin("mkdir",$logger);
  40. foreach my $project ( @$project_dirs_ref ) {
  41. my $indexPath = $project . "index.html";
  42. append_file("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$index);
  43. append_file("<tr><td><a href=\"projects/$indexPath\">$project</a></td>",$index);
  44. shellex("$mkdirCmd -p $web_projects_dir_path$project",$logger);
  45. }
  46. append_file("</tr></tbody></table></div></body></html>",$index);
  47. $logger->info("Wrote root index at $index");
  48. }
  49. # Main sub for generating project page
  50. # Might make more sense to split into more subs?
  51. sub write_project_content($$$$) {
  52. my $project_dirs_ref = shift;
  53. my $trimmed_project_dirs_ref = shift;
  54. my $web_projects_dir = shift;
  55. my $logger = shift;
  56. # Make these array's easier to work with in a hash
  57. # Key is path to actual git dir, val is path to associated web dir
  58. my %projects_map;
  59. @projects_map{@$project_dirs_ref} = @$trimmed_project_dirs_ref;
  60. $logger->info("Assembling data structures of git info");
  61. # Write files part of project index
  62. foreach my $project_path ( keys %projects_map ) {
  63. my $spec_web_dir = $web_projects_dir . $projects_map{$project_path};
  64. my $project_index = $spec_web_dir . "index.html";
  65. write_file("",$project_index);
  66. append_file("<html><a href=\"../../index.html\">Return to index</a></b><hr/>",$project_index);
  67. # Get all project data structures/info
  68. my ( $file_tree_ref, $file_content_ref, $commits_ref, $commit_ids_ref ) = get_file_tree($project_path,$logger);
  69. # Handle README
  70. if ( grep /^README.md$/, keys %$file_content_ref ) {
  71. $logger->info("$projects_map{$project_path} contains a README");
  72. my $readme_html = render_readme(${$file_content_ref}{'README.md'},$logger);
  73. append_file("$readme_html",$project_index);
  74. }
  75. append_file("<b>Files for $projects_map{$project_path}</b><br>",$project_index);
  76. append_file("<hr/>",$project_index);
  77. ## Write files ##
  78. 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);
  79. foreach my $filename ( sort keys %$file_content_ref ) {
  80. my $browserCompat = $filename . ".txt";
  81. # Rewrite dir paths so we can save on disk without producing actual dir structure
  82. if ( $filename =~ m/\// ) {
  83. my $copy = $filename;
  84. $copy =~ s/\//_/g;
  85. $browserCompat = $copy . ".txt";
  86. }
  87. append_file("<tr><td><a href=\"$browserCompat\">$filename</a></td><td>${$file_tree_ref}{$filename}</td>",$project_index);
  88. write_file("${$file_content_ref}{$filename}",$spec_web_dir . $browserCompat);
  89. }
  90. append_file("</tr></tbody></table></div></body>",$project_index);
  91. append_file("<br>", $project_index);
  92. append_file("<html><b>Logs for $projects_map{$project_path}</b><br>",$project_index);
  93. append_file("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$project_index);
  94. append_file("<hr/>",$project_index);
  95. # iterate over array to keep ordering
  96. foreach my $commit_id ( @$commit_ids_ref ) {
  97. my $filename = $commit_id . ".txt";
  98. append_file("<tr><td><a href=\"$filename\">$filename</a></td>",$project_index);
  99. write_file(${$commits_ref}{$commit_id},$spec_web_dir . $filename);
  100. }
  101. append_file("</tr></tbody></table></div></body>",$project_index);
  102. append_file("</html>",$project_index);
  103. }
  104. $logger->info("Done writing files");
  105. }
  106. # Not used currently, need to do more trimming/etc
  107. # TODO
  108. # Work around is rm -rf the webroot manually and then just rerun gsg
  109. sub clean_web_root($$$) {
  110. my $web_projects_dir_path = shift;
  111. my $git_projects_ref = shift;
  112. my $logger = shift;
  113. my $lsCmd = findBin("ls",$logger);
  114. my $rmCmd = findBin("rm",$logger);
  115. foreach my $dir ( split("\n", shellex("$lsCmd -d $web_projects_dir_path/*/",$logger)) ) {
  116. if ( ! grep( /^$dir$/, @$git_projects_ref ) ) {
  117. $logger->info("Found $dir in webroot but not in git root, removing...");
  118. my $rmdir = $web_projects_dir_path . $dir;
  119. $logger->info("Would remove $rmdir");
  120. # Does this need to be safer? TODO
  121. #shellex("$rmCmd $rmdir/*",$logger);
  122. #shellex("$rmCmd -d $rmdir",$logger);
  123. }
  124. }
  125. }
  126. 1;