gout

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

refs.c (4354B)


      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   free(table->id);
     56   free(table);
     57 }
     58 
     59 static void html_refstable_begin(HtmlRefsTable* table) {
     60   assert(table != NULL);
     61   fprintf(table->out, "<h2>%s</h2>", table->title);
     62   fprintf(table->out, "<table id=\"%s\">", table->id);
     63   fprintf(table->out,
     64           "<thead>\n"
     65           "<tr>"
     66           "<td><b>Name</b></td>"
     67           "<td><b>Last commit date</b></td>"
     68           "<td><b>Author</b></td>\n"
     69           "</tr>\n"
     70           "</thead><tbody>\n");
     71 }
     72 
     73 static void html_refstable_add_ref(HtmlRefsTable* table,
     74                                    const GitReference* ref) {
     75   assert(table != NULL);
     76   assert(ref != NULL);
     77   GitCommit* commit = ref->commit;
     78   fprintf(table->out, "<tr><td>");
     79   print_xml_encoded(table->out, ref->shorthand);
     80   fprintf(table->out, "</td><td>");
     81   print_time_short(table->out, commit->author_time);
     82   fprintf(table->out, "</td><td>");
     83   print_xml_encoded(table->out, commit->author_name);
     84   fprintf(table->out, "</td></tr>\n");
     85 }
     86 
     87 static void html_refstable_end(HtmlRefsTable* table) {
     88   assert(table != NULL);
     89   fprintf(table->out, "</tbody></table><br/>\n");
     90 }
     91 
     92 HtmlRefs* html_refs_create(const GitRepo* repo, const FileSystem* fs) {
     93   assert(repo != NULL);
     94   assert(fs != NULL);
     95   HtmlRefs* refs = ecalloc(1, sizeof(HtmlRefs));
     96   refs->repo = repo;
     97   refs->fs = fs;
     98   refs->out = fs->fopen("refs.html", "w");
     99   if (!refs->out) {
    100     err(1, "fopen: refs.html");
    101   }
    102   refs->page = html_page_create(refs->out, repo, fs, "Refs", "");
    103   return refs;
    104 }
    105 
    106 void html_refs_free(HtmlRefs* refs) {
    107   if (!refs) {
    108     return;
    109   }
    110   refs->fs->fclose(refs->out);
    111   html_page_free(refs->page);
    112   html_refstable_free(refs->branches);
    113   html_refstable_free(refs->tags);
    114   free(refs);
    115 }
    116 
    117 void html_refs_begin(HtmlRefs* refs) {
    118   assert(refs != NULL);
    119   html_page_begin(refs->page);
    120 }
    121 
    122 void html_refs_add_ref(HtmlRefs* refs, const GitReference* ref) {
    123   assert(refs != NULL);
    124   assert(ref != NULL);
    125   switch (ref->type) {
    126     case kReftypeBranch:
    127       if (!refs->branches) {
    128         refs->branches =
    129             html_refstable_create("Branches", "branches", refs->out);
    130         html_refstable_begin(refs->branches);
    131       }
    132       html_refstable_add_ref(refs->branches, ref);
    133       break;
    134     case kReftypeTag:
    135       if (refs->branches) {
    136         html_refstable_end(refs->branches);
    137         html_refstable_free(refs->branches);
    138         refs->branches = NULL;
    139       }
    140       if (!refs->tags) {
    141         refs->tags = html_refstable_create("Tags", "tags", refs->out);
    142         html_refstable_begin(refs->tags);
    143       }
    144       html_refstable_add_ref(refs->tags, ref);
    145       break;
    146   }
    147 }
    148 
    149 void html_refs_end(HtmlRefs* refs) {
    150   assert(refs != NULL);
    151   if (refs->branches) {
    152     html_refstable_end(refs->branches);
    153     html_refstable_free(refs->branches);
    154     refs->branches = NULL;
    155   }
    156   if (refs->tags) {
    157     html_refstable_end(refs->tags);
    158     html_refstable_free(refs->tags);
    159     refs->tags = NULL;
    160   }
    161   html_page_end(refs->page);
    162 }