git repos / oastat_static_web_generator

commit 5cdc350c

sago007 · 2018-12-01 18:58
5cdc350c9045cab74480a4633275e0506c1a3446 patch · browse files
parent 5104d5a09fd91a2ec346a223bd51bcc8b97dbf4f

Add ctemplate system

Changed files

M CMakeLists.txt before
M src/oastat_static_web_generator.cpp before
A templates/index.tpl
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d88ab2..0cf8bf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 2.8.9)
project (oastat_static_web_generator)
find_package(Boost COMPONENTS program_options REQUIRED)
add_executable(oastat_static_web_generator src/oastat_static_web_generator.cpp)
-TARGET_LINK_LIBRARIES( oastat_static_web_generator ${Boost_LIBRARIES} cppdb )
+TARGET_LINK_LIBRARIES( oastat_static_web_generator ${Boost_LIBRARIES} cppdb ctemplate )
diff --git a/src/oastat_static_web_generator.cpp b/src/oastat_static_web_generator.cpp index 76354b9..ee825d6 100644 --- a/src/oastat_static_web_generator.cpp +++ b/src/oastat_static_web_generator.cpp
@@ -3,6 +3,7 @@
#include <cppdb/frontend.h>
#include "command_arguments.hpp"
#include <fstream>
+#include <ctemplate/template.h>
#ifndef VERSIONNUMBER
#define VERSIONNUMBER "0.1.0"
@@ -12,6 +13,16 @@ CommandArguments cmdargs;
const char* const SAGO_CONNECTION_STRING = "SAGO_CONNECTION_STRING";
+std::string timestamp_now_as_string(cppdb::session& database) {
+ cppdb::statement st = database.prepare("SELECT now()");
+ cppdb::result res = st.query();
+ if(res.next()) {
+ std::string value;
+ res >> value;
+ return value;
+ }
+ return "";
+}
static void create_dir_recursive(const std::string& path) {
std::string command = std::string("mkdir -p \"")+path+"\"";
@@ -31,10 +42,13 @@ static void copy_static_files(const std::string& source, const std::string& dest
void write_html_index(cppdb::session& database) {
+ ctemplate::TemplateDictionary index_tpl("templates/index.tpl");
+ index_tpl.SetValue("GENERATION_DATE", timestamp_now_as_string(database));
+ std::string output;
+ ctemplate::ExpandTemplate("templates/index.tpl", ctemplate::DO_NOT_STRIP, &index_tpl, &output);
std::ofstream myfile;
myfile.open (cmdargs.output_dir+"/index.html");
- myfile << "<html>\n";
- myfile << "</html>\n";
+ myfile << output;
myfile.close();
}
@@ -43,6 +57,8 @@ void write_files(cppdb::session& database) {
}
+
+
void do_dbtest(cppdb::session& database) {
cppdb::statement st = database.prepare("SELECT now()");
cppdb::result res = st.query();
diff --git a/templates/index.tpl b/templates/index.tpl new file mode 100644 index 0000000..dceab49 --- /dev/null +++ b/templates/index.tpl
@@ -0,0 +1,22 @@
+{{%AUTOESCAPE context="HTML"}}
+
+<html>
+<HEAD>
+<META http-equiv="Content-Type" content="text/html; charset=utf-8">
+<META http-equiv="Content-Language" content="en">
+<META name="description" content="OpenArena stats by oastats">
+<LINK rel="stylesheet" href="static/css/oastat.css" type="text/css">
+<TITLE>OpenArena Stats</TITLE>
+</HEAD>
+<BODY>
+<DIV id="marco">
+
+
+<DIV class="cuadropresentacion">
+<DIV class="update">Last updated on {{GENERATION_DATE}}</DIV>
+<DIV class="intro">OpenArena Stats</DIV>
+</DIV>
+</DIV>
+</BODY>
+
+</html>