page.c (2206B)
1 #include "writer/gemini/page.h" 2 3 #include <assert.h> 4 #include <stdlib.h> 5 6 #include "format.h" 7 #include "utils.h" 8 9 struct GeminiPage { 10 FILE* out; 11 const GitRepo* repo; 12 const FileSystem* fs; 13 char* title; 14 char* relpath; 15 }; 16 17 GeminiPage* gemini_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 GeminiPage* page = ecalloc(1, sizeof(GeminiPage)); 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 gemini_page_free(GeminiPage* page) { 37 if (!page) { 38 return; 39 } 40 free(page->title); 41 free(page->relpath); 42 free(page); 43 } 44 45 void gemini_page_begin(GeminiPage* page) { 46 assert(page != NULL); 47 FILE* out = page->out; 48 fprintf(out, "# "); 49 if (page->title[0] != '\0') { 50 fprintf(out, "%s", page->title); 51 const char* short_name = page->repo->short_name; 52 if (short_name && short_name[0] != '\0') { 53 fprintf(out, " - %s", short_name); 54 } 55 } else if (page->repo->short_name) { 56 fprintf(out, "%s", page->repo->short_name); 57 } 58 fprintf(out, "\n\n"); 59 60 const char* description = page->repo->description; 61 if (description && description[0] != '\0') { 62 fprintf(out, "> %s\n\n", description); 63 } 64 65 const char* clone_url = page->repo->clone_url; 66 if (clone_url && clone_url[0] != '\0') { 67 fprintf(out, "git clone %s\n\n", clone_url); 68 } 69 70 fprintf(out, "=> %slog.gmi Log\n", page->relpath); 71 fprintf(out, "=> %sfiles.gmi Files\n", page->relpath); 72 fprintf(out, "=> %srefs.gmi Refs\n", page->relpath); 73 74 for (size_t i = 0; i < page->repo->special_files_len; i++) { 75 RepoSpecialFile* sf = &page->repo->special_files[i]; 76 fprintf(out, "=> %sfile/", page->relpath); 77 print_percent_encoded(out, sf->path); 78 fprintf(out, ".gmi %s\n", sf->label); 79 } 80 fprintf(out, "\n---\n\n"); 81 } 82 83 void gemini_page_end(GeminiPage* page) { 84 assert(page != NULL); 85 (void)page; 86 }