files.c (1919B)
1 #include "writer/html/files.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <sys/types.h> 6 7 #include "format.h" 8 #include "utils.h" 9 #include "writer/html/page.h" 10 11 struct HtmlFiles { 12 const GitRepo* repo; 13 FILE* out; 14 HtmlPage* page; 15 }; 16 17 HtmlFiles* html_files_create(const GitRepo* repo) { 18 HtmlFiles* files = ecalloc(1, sizeof(HtmlFiles)); 19 files->repo = repo; 20 files->out = efopen("files.html", "w"); 21 files->page = html_page_create(files->out, repo, "Files", ""); 22 return files; 23 } 24 25 void html_files_free(HtmlFiles* files) { 26 if (!files) { 27 return; 28 } 29 fclose(files->out); 30 files->out = NULL; 31 html_page_free(files->page); 32 files->page = NULL; 33 free(files); 34 } 35 36 void html_files_begin(HtmlFiles* files) { 37 html_page_begin(files->page); 38 fprintf(files->out, 39 "<table id=\"files\"><thead>\n" 40 "<tr>" 41 "<td><b>Mode</b></td>" 42 "<td><b>Name</b></td>" 43 "<td class=\"num\" align=\"right\"><b>Size</b></td>" 44 "</tr>\n" 45 "</thead><tbody>\n"); 46 } 47 48 void html_files_add_file(HtmlFiles* files, const GitFile* file) { 49 FILE* out = files->out; 50 51 fprintf(out, "<tr><td>%s</td>", gitfile_mode(file)); 52 fprintf(out, "<td><a href=\"file/"); 53 print_percent_encoded(out, gitfile_repo_path(file)); 54 fprintf(out, ".html\">"); 55 print_xml_encoded(out, gitfile_display_path(file)); 56 fprintf(out, "</a>"); 57 const char* oid = gitfile_commit_oid(file); 58 if (oid[0] != '\0') { 59 fprintf(out, " @ %s", oid); 60 } 61 fprintf(out, "</td><td class=\"num\" align=\"right\">"); 62 63 ssize_t size_lines = gitfile_size_lines(file); 64 ssize_t size_bytes = gitfile_size_lines(file); 65 if (size_lines >= 0) { 66 fprintf(out, "%zdL", size_lines); 67 } else if (size_bytes >= 0) { 68 fprintf(out, "%zdB", size_bytes); 69 } 70 fprintf(out, "</td></tr>\n"); 71 } 72 73 void html_files_end(HtmlFiles* files) { 74 fprintf(files->out, "</tbody></table>"); 75 html_page_end(files->page); 76 }