page.c (2273B)
1 #include "writer/gemini/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 GeminiPage { 11 FILE* out; 12 const GitRepo* repo; 13 const FileSystem* fs; 14 char* title; 15 char* relpath; 16 }; 17 18 GeminiPage* gemini_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 GeminiPage* page = ecalloc(1, sizeof(GeminiPage)); 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 gemini_page_free(GeminiPage* 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 gemini_page_begin(GeminiPage* page) { 49 assert(page != NULL); 50 FILE* out = page->out; 51 fprintf(out, "# "); 52 if (page->title[0] != '\0') { 53 fprintf(out, "%s", page->title); 54 const char* short_name = page->repo->short_name; 55 if (short_name && short_name[0] != '\0') { 56 fprintf(out, " - %s", short_name); 57 } 58 } else if (page->repo->short_name) { 59 fprintf(out, "%s", page->repo->short_name); 60 } 61 fprintf(out, "\n\n"); 62 63 const char* description = page->repo->description; 64 if (description && description[0] != '\0') { 65 fprintf(out, "> %s\n\n", description); 66 } 67 68 const char* clone_url = page->repo->clone_url; 69 if (clone_url && clone_url[0] != '\0') { 70 fprintf(out, "git clone %s\n\n", clone_url); 71 } 72 73 fprintf(out, "=> %slog.gmi Log\n", page->relpath); 74 fprintf(out, "=> %sfiles.gmi Files\n", page->relpath); 75 fprintf(out, "=> %srefs.gmi Refs\n", page->relpath); 76 77 for (size_t i = 0; i < page->repo->special_files_len; i++) { 78 RepoSpecialFile* sf = &page->repo->special_files[i]; 79 fprintf(out, "=> %sfile/", page->relpath); 80 print_percent_encoded(out, sf->path); 81 fprintf(out, ".gmi %s\n", sf->label); 82 } 83 fprintf(out, "\n---\n\n"); 84 } 85 86 void gemini_page_end(GeminiPage* page) { 87 assert(page != NULL); 88 (void)page; 89 }