(no commit message)
[ppastats.git] / src / html.c
index c9cb9d6..07945f6 100644 (file)
 #include "lp_ws.h"
 #include "ppastats.h"
 
-#define HTML_FOOTER \
-" <div id=\"footer\">Generated by \
-<a href='http://wpitchoune.net/ppastats'>ppastats</a></div>\n\
-  </body>\n\
-</html>"
+static char *footer;
 
 #define HTML_PKG_TEMPLATE \
 "      <h1><span id=\"pkg_name\">N/A</span></h1>\n\
@@ -98,8 +94,7 @@ src=\"js/excanvas.js\"></script><![endif]-->\n\
        <em>Distros</em>:\n\
        <ul id=\"distros\"></ul>\n\
       </div>\n\
-    </div>\n\
-%s"
+    </div>\n"
 
 #define HTML_HEADER \
 "<html>\n\
@@ -143,11 +138,13 @@ src=\"js/excanvas.js\"></script><![endif]-->\n\
 
 static char *path_new(const char *dir, const char *file, const char *suffixe)
 {
-       char *path = malloc(strlen(dir)+1+
-                           strlen(file)+
-                           strlen(".html")+
-                           (suffixe ? strlen(suffixe) : 0) +
-                           1);
+       char *path;
+
+       /* [dir]/[file][suffixe] */
+       path = malloc(strlen(dir)+1+
+                     strlen(file)+
+                     (suffixe ? strlen(suffixe) : 0) +
+                     1);
 
        strcpy(path, dir);
        strcat(path, "/");
@@ -157,6 +154,23 @@ static char *path_new(const char *dir, const char *file, const char *suffixe)
        return path;
 }
 
+
+
+static const char *get_footer()
+{
+       const char *path;
+
+       if (!footer) {
+               path = DEFAULT_WWW_DIR"/footer.tpl";
+               footer = file_get_content(path);
+
+               if (!footer)
+                       log_err("Failed to read footer template: %s", path);
+       }
+
+       return footer;
+}
+
 static struct json_object *date_to_json(struct tm *tm)
 {
        json_object *json;
@@ -381,6 +395,7 @@ version_to_html(struct ppa_stats *ppa,
 {
        char *f_name, *path;
        FILE *f;
+       const char *footer;
 
        f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
        sprintf(f_name, "%s_%s", pkg->name, version->version);
@@ -394,8 +409,11 @@ version_to_html(struct ppa_stats *ppa,
        }
 
        fprintf(f, HTML_VERSION_TEMPLATE,
-               version_to_json(ppa, pkg, version),
-               HTML_FOOTER);
+               version_to_json(ppa, pkg, version));
+
+       footer = get_footer();
+       if (footer)
+               fputs(footer, f);
 
        fclose(f);
 
@@ -410,6 +428,7 @@ create_html(const char *path,
            const char *script)
 {
        FILE *f;
+       const char *footer;
 
        f = fopen(path, "w");
 
@@ -420,7 +439,10 @@ create_html(const char *path,
 
        fprintf(f, HTML_HEADER, title, script);
        fputs(body_template, f);
-       fputs(HTML_FOOTER, f);
+
+       footer = get_footer();
+       if (footer)
+               fputs(footer, f);
 
        fclose(f);
 }