gitout

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

atom.c (3288B)


      1 #include "atom.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "format.h"
      8 #include "utils.h"
      9 
     10 struct Atom {
     11   const GitRepo* repo;
     12   const char* baseurl;
     13   FILE* out;
     14   size_t remaining_commits;
     15 };
     16 
     17 Atom* atom_create(const GitRepo* repo, AtomType type) {
     18   Atom* atom = ecalloc(1, sizeof(Atom));
     19   atom->repo = repo;
     20   atom->baseurl = "";
     21   const char* filename = (type == kAtomTypeAll) ? "atom.xml" : "tags.xml";
     22   atom->out = efopen(filename, "w");
     23   atom->remaining_commits = 100;
     24   return atom;
     25 }
     26 
     27 void atom_free(Atom* atom) {
     28   fclose(atom->out);
     29   atom->out = NULL;
     30   free(atom);
     31 }
     32 
     33 void atom_set_baseurl(Atom* atom, const char* baseurl) {
     34   atom->baseurl = baseurl;
     35 }
     36 
     37 void atom_begin(Atom* atom) {
     38   fprintf(atom->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
     39   fprintf(atom->out, "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
     40   fprintf(atom->out, "<title>");
     41   print_xml_encoded(atom->out, gitrepo_short_name(atom->repo));
     42   fprintf(atom->out, ", branch HEAD</title>\n");
     43   fprintf(atom->out, "<subtitle>");
     44   print_xml_encoded(atom->out, gitrepo_description(atom->repo));
     45   fprintf(atom->out, "</subtitle>\n");
     46 }
     47 
     48 void atom_add_commit(Atom* atom,
     49                      const GitCommit* commit,
     50                      const char* path,
     51                      const char* content_type,
     52                      const char* tag) {
     53   FILE* out = atom->out;
     54   if (atom->remaining_commits == 0) {
     55     return;
     56   }
     57   atom->remaining_commits--;
     58 
     59   fprintf(out, "<entry>\n");
     60   fprintf(out, "<id>%s</id>\n", gitcommit_oid(commit));
     61 
     62   fprintf(out, "<published>");
     63   print_time_z(out, gitcommit_author_time(commit));
     64   fprintf(out, "</published>\n");
     65 
     66   fprintf(out, "<updated>");
     67   print_time_z(out, gitcommit_commit_time(commit));
     68   fprintf(out, "</updated>\n");
     69 
     70   if (gitcommit_summary(commit)) {
     71     fprintf(out, "<title>");
     72     if (tag && tag[0] != '\0') {
     73       fputc('[', out);
     74       print_xml_encoded(out, tag);
     75       fputc(']', out);
     76     }
     77     print_xml_encoded(out, gitcommit_summary(commit));
     78     fprintf(out, "</title>\n");
     79   }
     80   fprintf(out, "<link rel=\"alternate\" ");
     81   if (strlen(content_type) > 0) {
     82     fprintf(out, "type=\"%s\" ", content_type);
     83   }
     84   fprintf(out, "href=\"%s%s\" />\n", atom->baseurl, path);
     85 
     86   fprintf(out, "<author>\n<name>");
     87   print_xml_encoded(out, gitcommit_author_name(commit));
     88   fprintf(out, "</name>\n<email>");
     89   print_xml_encoded(out, gitcommit_author_email(commit));
     90   fprintf(out, "</email>\n</author>\n");
     91 
     92   fprintf(out, "<content>");
     93   fprintf(out, "commit %s\n", gitcommit_oid(commit));
     94   const char* parentoid = gitcommit_parentoid(commit);
     95   if (parentoid[0]) {
     96     fprintf(out, "parent %s\n", parentoid);
     97   }
     98   fprintf(out, "Author: ");
     99   print_xml_encoded(out, gitcommit_author_name(commit));
    100   fprintf(out, " &lt;");
    101   print_xml_encoded(out, gitcommit_author_email(commit));
    102   fprintf(out, "&gt;\n");
    103   fprintf(out, "Date:   ");
    104   print_time(out, gitcommit_author_time(commit),
    105              gitcommit_author_timezone_offset(commit));
    106   fprintf(out, "\n");
    107   const char* message = gitcommit_message(commit);
    108   if (message) {
    109     fputc('\n', out);
    110     print_xml_encoded(out, message);
    111   }
    112   fprintf(out, "\n</content>\n</entry>\n");
    113 }
    114 
    115 void atom_end(Atom* atom) {
    116   fprintf(atom->out, "</feed>\n");
    117 }