gitout

A static git page generator
git clone https://git.bracken.jp/gitout.git
Log | Files | Refs | Submodules | README | LICENSE

page.c (2421B)


      1 #include "writer/gopher/page.h"
      2 
      3 #include <stdbool.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   char* title;
     13   char* relpath;
     14 };
     15 
     16 GopherPage* gopher_page_create(FILE* out,
     17                                const GitRepo* repo,
     18                                const char* title,
     19                                const char* relpath) {
     20   GopherPage* page = ecalloc(1, sizeof(GopherPage));
     21   page->out = out;
     22   page->repo = repo;
     23   page->title = estrdup(title);
     24   page->relpath = estrdup(relpath);
     25   return page;
     26 }
     27 
     28 void gopher_page_free(GopherPage* page) {
     29   if (!page) {
     30     return;
     31   }
     32   free(page->title);
     33   page->title = NULL;
     34   free(page->relpath);
     35   page->relpath = NULL;
     36   free(page);
     37 }
     38 
     39 void gopher_page_begin(GopherPage* page) {
     40   FILE* out = page->out;
     41   print_gopher_text(out, page->title, false);
     42   const char* short_name = gitrepo_short_name(page->repo);
     43   if (page->title[0] != '\0' && short_name[0] != '\0') {
     44     fprintf(out, " - ");
     45     print_gopher_text(out, short_name, false);
     46   }
     47   const char* description = gitrepo_description(page->repo);
     48   if (page->title[0] != '\0' && description[0] != '\0') {
     49     fprintf(out, " - ");
     50     print_gopher_text(out, description, false);
     51     fprintf(out, "\n");
     52   }
     53   const char* clone_url = gitrepo_clone_url(page->repo);
     54   if (clone_url[0] != '\0') {
     55     fprintf(out, "[h|git clone ");
     56     print_gopher_link(out, clone_url);
     57     fprintf(out, "|URL:");
     58     print_gopher_link(out, clone_url);
     59     fprintf(out, "|server|port]\n");
     60   }
     61   fprintf(out, "[1|Log|%slog.gph|server|port]\n", page->relpath);
     62   fprintf(out, "[1|Files|%sfiles.gph|server|port]\n", page->relpath);
     63   fprintf(out, "[1|Refs|%srefs.gph|server|port]\n", page->relpath);
     64 
     65   const char* submodules = gitrepo_submodules(page->repo);
     66   if (submodules[0] != '\0') {
     67     fprintf(out, "[1|Submodules|%sfile/%s.gph|server|port]\n", page->relpath,
     68             submodules);
     69   }
     70   const char* readme = gitrepo_readme(page->repo);
     71   if (readme[0] != '\0') {
     72     fprintf(out, "[1|README|%sfile/%s.gph|server|port]\n", page->relpath,
     73             readme);
     74   }
     75   const char* license = gitrepo_license(page->repo);
     76   if (license[0] != '\0') {
     77     fprintf(out, "[1|LICENSE|%sfile/%s.gph|server|port]\n", page->relpath,
     78             license);
     79   }
     80   fprintf(out, "---\n");
     81 }
     82 
     83 void gopher_page_end(GopherPage* page) {
     84   if (!page) {
     85     return;
     86   }
     87 }