page.c (2399B)
1 #include "writer/gopher/page.h" 2 3 #include <assert.h> 4 #include <stdlib.h> 5 6 #include "format.h" 7 #include "utils.h" 8 9 struct GopherPage { 10 FILE* out; 11 const GitRepo* repo; 12 const FileSystem* fs; 13 char* title; 14 char* relpath; 15 }; 16 17 GopherPage* gopher_page_create(FILE* out, 18 const GitRepo* repo, 19 const FileSystem* fs, 20 const char* title, 21 const char* relpath) { 22 assert(out != NULL); 23 assert(repo != NULL); 24 assert(fs != NULL); 25 assert(title != NULL); 26 assert(relpath != NULL); 27 GopherPage* page = ecalloc(1, sizeof(GopherPage)); 28 page->out = out; 29 page->repo = repo; 30 page->fs = fs; 31 page->title = estrdup(title); 32 page->relpath = estrdup(relpath); 33 return page; 34 } 35 36 void gopher_page_free(GopherPage* page) { 37 if (!page) { 38 return; 39 } 40 free(page->title); 41 free(page->relpath); 42 free(page); 43 } 44 45 void gopher_page_begin(GopherPage* page) { 46 assert(page != NULL); 47 FILE* out = page->out; 48 print_gopher_text(out, page->title, false); 49 const char* short_name = page->repo->short_name; 50 if (page->title[0] != '\0' && short_name && short_name[0] != '\0') { 51 fprintf(out, " - "); 52 print_gopher_text(out, short_name, false); 53 } 54 const char* description = page->repo->description; 55 if (page->title[0] != '\0' && description && description[0] != '\0') { 56 fprintf(out, " - "); 57 print_gopher_text(out, description, false); 58 } 59 fprintf(out, "\n"); 60 const char* clone_url = page->repo->clone_url; 61 if (clone_url && clone_url[0] != '\0') { 62 fprintf(out, "[h|git clone "); 63 print_gopher_link(out, clone_url); 64 fprintf(out, "|URL:"); 65 print_gopher_link(out, clone_url); 66 fprintf(out, "|server|port]\n"); 67 } 68 fprintf(out, "[1|Log|%slog.gph|server|port]\n", page->relpath); 69 fprintf(out, "[1|Files|%sfiles.gph|server|port]\n", page->relpath); 70 fprintf(out, "[1|Refs|%srefs.gph|server|port]\n", page->relpath); 71 72 for (size_t i = 0; i < page->repo->special_files_len; i++) { 73 RepoSpecialFile* sf = &page->repo->special_files[i]; 74 fprintf(out, "[1|"); 75 print_gopher_text(out, sf->label, false); 76 fprintf(out, "|%sfile/", page->relpath); 77 print_gopher_link(out, sf->path); 78 fprintf(out, ".gph|server|port]\n"); 79 } 80 fprintf(out, "---\n"); 81 } 82 83 void gopher_page_end(GopherPage* page) { 84 assert(page != NULL); 85 (void)page; 86 }