Html.pm 5.1 KB

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