gitout

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

page.c (4004B)


      1 #include "writer/html/page.h"
      2 
      3 #include <stdlib.h>
      4 
      5 #include "format.h"
      6 #include "utils.h"
      7 
      8 struct HtmlPage {
      9   FILE* out;
     10   const GitRepo* repo;
     11   char* title;
     12   char* relpath;
     13 };
     14 
     15 HtmlPage* html_page_create(FILE* out,
     16                            const GitRepo* repo,
     17                            const char* title,
     18                            const char* relpath) {
     19   HtmlPage* page = ecalloc(1, sizeof(HtmlPage));
     20   page->out = out;
     21   page->repo = repo;
     22   page->title = estrdup(title);
     23   page->relpath = estrdup(relpath);
     24   return page;
     25 }
     26 
     27 void html_page_free(HtmlPage* page) {
     28   if (!page) {
     29     return;
     30   }
     31   free(page->title);
     32   page->title = NULL;
     33   free(page->relpath);
     34   page->relpath = NULL;
     35   free(page);
     36 }
     37 
     38 void html_page_begin(HtmlPage* page) {
     39   FILE* out = page->out;
     40   fprintf(
     41       out,
     42       "<!DOCTYPE html>\n"
     43       "<html>\n"
     44       "<head>\n"
     45       "<meta "
     46       "http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
     47       "<meta "
     48       "name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n");
     49   fprintf(out, "<title>");
     50 
     51   print_xml_encoded(out, page->title);
     52   const char* short_name = gitrepo_short_name(page->repo);
     53   if (page->title[0] != '\0' && short_name[0] != '\0') {
     54     fprintf(out, " - ");
     55     print_xml_encoded(out, short_name);
     56   }
     57   const char* description = gitrepo_description(page->repo);
     58   if (page->title[0] != '\0' && description[0] != '\0') {
     59     fprintf(out, " - ");
     60     print_xml_encoded(out, description);
     61   }
     62 
     63   const char* relpath = page->relpath;
     64   fprintf(out, "</title>\n");
     65   fprintf(out,
     66           "<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n",
     67           relpath);
     68   fprintf(out,
     69           "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"");
     70   print_xml_encoded(out, gitrepo_name(page->repo));
     71   fprintf(out, " Atom Feed\" href=\"%satom.xml\" />\n", relpath);
     72   fprintf(out,
     73           "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"");
     74   print_xml_encoded(out, gitrepo_name(page->repo));
     75   fprintf(out, " Atom Feed (tags)\" href=\"%stags.xml\" />\n", relpath);
     76   fprintf(out,
     77           "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />",
     78           relpath);
     79   fprintf(out, "\n</head>\n<body>\n<table><tr><td>");
     80   fprintf(out, "<a href=\"../%s\">", relpath);
     81   fprintf(out, "<img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" />",
     82           relpath);
     83   fprintf(out, "</a></td><td><h1>");
     84   print_xml_encoded(out, gitrepo_short_name(page->repo));
     85   fprintf(out, "</h1><span class=\"desc\">");
     86   print_xml_encoded(out, gitrepo_description(page->repo));
     87   fprintf(out, "</span></td></tr>");
     88 
     89   const char* clone_url = gitrepo_clone_url(page->repo);
     90   if (clone_url[0] != '\0') {
     91     fprintf(out, "<tr class=\"url\"><td></td><td>git clone <a href=\"");
     92     print_xml_encoded(out, clone_url);
     93     fprintf(out, "\">");
     94     print_xml_encoded(out, clone_url);
     95     fprintf(out, "</a></td></tr>");
     96   }
     97   fprintf(out, "<tr><td></td><td>\n");
     98   fprintf(out, "<a href=\"%slog.html\">Log</a> | ", relpath);
     99   fprintf(out, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
    100   fprintf(out, "<a href=\"%srefs.html\">Refs</a>", relpath);
    101 
    102   const char* submodules = gitrepo_submodules(page->repo);
    103   if (submodules[0] != '\0') {
    104     fprintf(out, " | <a href=\"%sfile/", relpath);
    105     print_xml_encoded(out, submodules);
    106     fprintf(out, ".html\">Submodules</a>");
    107   }
    108   const char* readme = gitrepo_readme(page->repo);
    109   if (readme[0] != '\0') {
    110     fprintf(out, " | <a href=\"%sfile/", relpath);
    111     print_xml_encoded(out, readme);
    112     fprintf(out, ".html\">README</a>");
    113   }
    114   const char* license = gitrepo_license(page->repo);
    115   if (license[0] != '\0') {
    116     fprintf(out, " | <a href=\"%sfile/", relpath);
    117     print_xml_encoded(out, license);
    118     fprintf(out, ".html\">LICENSE</a>");
    119   }
    120   fprintf(out, "</td></tr></table>\n<hr/>\n<div id=\"content\">\n");
    121 }
    122 
    123 void html_page_end(HtmlPage* page) {
    124   fprintf(page->out, "</div>\n</body>\n</html>\n");
    125 }