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