gitout

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

refs.c (3977B)


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