repo_index.c (2599B)
1 #include "writer/html/repo_index.h" 2 3 #include <stdlib.h> 4 5 #include "format.h" 6 #include "git/commit.h" 7 #include "utils.h" 8 9 struct HtmlRepoIndex { 10 FILE* out; 11 const char* me_url; 12 }; 13 14 HtmlRepoIndex* html_repoindex_create(FILE* out) { 15 HtmlRepoIndex* index = ecalloc(1, sizeof(HtmlRepoIndex)); 16 index->out = out; 17 index->me_url = NULL; 18 return index; 19 } 20 21 void html_repoindex_free(HtmlRepoIndex* index) { 22 if (!index) { 23 return; 24 } 25 fclose(index->out); 26 index->out = NULL; 27 free(index); 28 } 29 30 void html_repoindex_set_me_url(HtmlRepoIndex* index, const char* url) { 31 index->me_url = url; 32 } 33 34 void html_repoindex_begin(HtmlRepoIndex* index) { 35 FILE* out = index->out; 36 fprintf( 37 out, 38 "<!DOCTYPE html>\n" 39 "<html>\n" 40 "<head>\n" 41 "<meta " 42 "http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" 43 "<meta " 44 "name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" 45 "<title>Repositories</title>\n" 46 "<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\" />\n" 47 "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"); 48 if (index->me_url) { 49 fprintf(out, "<link rel=\"me\" href=\"%s\" />\n", index->me_url); 50 } 51 fprintf( 52 out, 53 "</head>\n" 54 "<body>\n" 55 "<table>\n<tr><td>" 56 "<img src=\"logo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n" 57 "<td><span class=\"desc\">Repositories</span></td></tr>" 58 "<tr><td></td><td>\n" 59 "</td></tr>\n" 60 "</table>\n<hr/>\n<div id=\"content\">\n" 61 "<table id=\"index\"><thead>\n" 62 "<tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Owner</b></td>" 63 "<td><b>Last commit</b></td></tr>" 64 "</thead><tbody>\n"); 65 } 66 67 static void print_author_time(const GitCommit* commit, void* user_data) { 68 FILE* out = (FILE*)user_data; 69 print_time_short(out, gitcommit_author_time(commit)); 70 } 71 72 void html_repoindex_add_repo(HtmlRepoIndex* index, GitRepo* repo) { 73 FILE* out = index->out; 74 fprintf(out, "<tr><td><a href=\""); 75 print_percent_encoded(out, gitrepo_short_name(repo)); 76 fprintf(out, "/log.html\">"); 77 print_xml_encoded(out, gitrepo_short_name(repo)); 78 fprintf(out, "</a></td><td>"); 79 print_xml_encoded(out, gitrepo_description(repo)); 80 fprintf(out, "</td><td>"); 81 print_xml_encoded(out, gitrepo_owner(repo)); 82 fprintf(out, "</td><td>"); 83 gitrepo_for_commit(repo, "HEAD", print_author_time, out); 84 fprintf(out, "</td></tr>"); 85 } 86 87 void html_repoindex_end(HtmlRepoIndex* index) { 88 FILE* out = index->out; 89 fprintf(out, "</tbody>\n</table>\n</div>\n</body>\n</html>\n"); 90 }