gout

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

refs.c (4484B)


      1 #include "writer/html/refs.h"
      2 
      3 #include <assert.h>
      4 #include <err.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 
      8 #include "format.h"
      9 #include "git/commit.h"
     10 #include "utils.h"
     11 #include "writer/html/page.h"
     12 
     13 typedef struct {
     14   char* title;
     15   char* id;
     16   FILE* out;
     17 } HtmlRefsTable;
     18 
     19 struct HtmlRefs {
     20   const GitRepo* repo;
     21   const FileSystem* fs;
     22   FILE* out;
     23   HtmlPage* page;
     24   HtmlRefsTable* branches;
     25   HtmlRefsTable* tags;
     26 };
     27 
     28 static HtmlRefsTable* html_refstable_create(const char* title,
     29                                             const char* id,
     30                                             FILE* out);
     31 static void html_refstable_free(HtmlRefsTable* table);
     32 static void html_refstable_begin(HtmlRefsTable* table);
     33 static void html_refstable_add_ref(HtmlRefsTable* table,
     34                                    const GitReference* ref);
     35 static void html_refstable_end(HtmlRefsTable* table);
     36 
     37 static HtmlRefsTable* html_refstable_create(const char* title,
     38                                             const char* id,
     39                                             FILE* out) {
     40   assert(title != NULL);
     41   assert(id != NULL);
     42   assert(out != NULL);
     43   HtmlRefsTable* table = ecalloc(1, sizeof(HtmlRefsTable));
     44   table->title = estrdup(title);
     45   table->id = estrdup(id);
     46   table->out = out;
     47   return table;
     48 }
     49 
     50 static void html_refstable_free(HtmlRefsTable* table) {
     51   if (!table) {
     52     return;
     53   }
     54   free(table->title);
     55   table->title = NULL;
     56   free(table->id);
     57   table->id = NULL;
     58   free(table);
     59 }
     60 
     61 static void html_refstable_begin(HtmlRefsTable* table) {
     62   assert(table != NULL);
     63   fprintf(table->out, "<h2>%s</h2>", table->title);
     64   fprintf(table->out, "<table id=\"%s\">", table->id);
     65   fprintf(table->out,
     66           "<thead>\n"
     67           "<tr>"
     68           "<td><b>Name</b></td>"
     69           "<td><b>Last commit date</b></td>"
     70           "<td><b>Author</b></td>\n"
     71           "</tr>\n"
     72           "</thead><tbody>\n");
     73 }
     74 
     75 static void html_refstable_add_ref(HtmlRefsTable* table,
     76                                    const GitReference* ref) {
     77   assert(table != NULL);
     78   assert(ref != NULL);
     79   GitCommit* commit = ref->commit;
     80   fprintf(table->out, "<tr><td>");
     81   print_xml_encoded(table->out, ref->shorthand);
     82   fprintf(table->out, "</td><td>");
     83   print_time_short(table->out, commit->author_time);
     84   fprintf(table->out, "</td><td>");
     85   print_xml_encoded(table->out, commit->author_name);
     86   fprintf(table->out, "</td></tr>\n");
     87 }
     88 
     89 static void html_refstable_end(HtmlRefsTable* table) {
     90   assert(table != NULL);
     91   fprintf(table->out, "</tbody></table><br/>\n");
     92 }
     93 
     94 HtmlRefs* html_refs_create(const GitRepo* repo, const FileSystem* fs) {
     95   assert(repo != NULL);
     96   assert(fs != NULL);
     97   HtmlRefs* refs = ecalloc(1, sizeof(HtmlRefs));
     98   refs->repo = repo;
     99   refs->fs = fs;
    100   refs->out = fs->fopen("refs.html", "w");
    101   if (!refs->out) {
    102     err(1, "fopen: refs.html");
    103   }
    104   refs->page = html_page_create(refs->out, repo, fs, "Refs", "");
    105   return refs;
    106 }
    107 
    108 void html_refs_free(HtmlRefs* refs) {
    109   if (!refs) {
    110     return;
    111   }
    112   refs->fs->fclose(refs->out);
    113   refs->out = NULL;
    114   html_page_free(refs->page);
    115   refs->page = NULL;
    116   html_refstable_free(refs->branches);
    117   refs->branches = NULL;
    118   html_refstable_free(refs->tags);
    119   refs->tags = NULL;
    120   free(refs);
    121 }
    122 
    123 void html_refs_begin(HtmlRefs* refs) {
    124   assert(refs != NULL);
    125   html_page_begin(refs->page);
    126 }
    127 
    128 void html_refs_add_ref(HtmlRefs* refs, const GitReference* ref) {
    129   assert(refs != NULL);
    130   assert(ref != NULL);
    131   switch (ref->type) {
    132     case kReftypeBranch:
    133       if (!refs->branches) {
    134         refs->branches =
    135             html_refstable_create("Branches", "branches", refs->out);
    136         html_refstable_begin(refs->branches);
    137       }
    138       html_refstable_add_ref(refs->branches, ref);
    139       break;
    140     case kReftypeTag:
    141       if (refs->branches) {
    142         html_refstable_end(refs->branches);
    143         html_refstable_free(refs->branches);
    144         refs->branches = NULL;
    145       }
    146       if (!refs->tags) {
    147         refs->tags = html_refstable_create("Tags", "tags", refs->out);
    148         html_refstable_begin(refs->tags);
    149       }
    150       html_refstable_add_ref(refs->tags, ref);
    151       break;
    152   }
    153 }
    154 
    155 void html_refs_end(HtmlRefs* refs) {
    156   assert(refs != NULL);
    157   if (refs->branches) {
    158     html_refstable_end(refs->branches);
    159     html_refstable_free(refs->branches);
    160     refs->branches = NULL;
    161   }
    162   if (refs->tags) {
    163     html_refstable_end(refs->tags);
    164     html_refstable_free(refs->tags);
    165     refs->tags = NULL;
    166   }
    167   html_page_end(refs->page);
    168 }