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