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