From 73c3d2f04dd97fcf27db4da733f0bde13fdb8444 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 21 Apr 2017 19:01:13 +0100 Subject: [PATCH] Use partials in html. --- html/head.mustache | 13 +++++++++++++ html/index.html.mstch | 32 +++++------------------------- html/paste.html.mstch | 27 +++----------------------- html/text.html | 14 +------------- html/topbar.mustache | 10 ++++++++++ src/response.cpp | 45 +++++++++++++++++++++++++++++++------------ 6 files changed, 65 insertions(+), 76 deletions(-) create mode 100644 html/head.mustache create mode 100644 html/topbar.mustache diff --git a/html/head.mustache b/html/head.mustache new file mode 100644 index 0000000..3431e0a --- /dev/null +++ b/html/head.mustache @@ -0,0 +1,13 @@ + + + + + Tawashi + + + + + + + + diff --git a/html/index.html.mstch b/html/index.html.mstch index 1ae66b6..f654765 100644 --- a/html/index.html.mstch +++ b/html/index.html.mstch @@ -1,29 +1,8 @@ - - - - - Tawashi - - - - - - - - +{{> head}}
-
-

tawashi {{version}}

- tawashi icon - - new icon - duplicate and edit icon - text icon - c++ icon - github icon -
+ {{> topbar}}
@@ -33,10 +12,9 @@

Syntax Highlighting:

Paste Expiration:

diff --git a/html/paste.html.mstch b/html/paste.html.mstch index 073c6cc..d3a6027 100644 --- a/html/paste.html.mstch +++ b/html/paste.html.mstch @@ -1,31 +1,10 @@ - - - - - Tawashi - - - - - - - - +{{> head}} -
-

tawashi 1.0.0

- tawashi icon - - new icon - duplicate and edit icon - text icon - c++ icon - github icon -
+ {{> topbar}}
-

+

{{pastie}}

diff --git a/html/text.html b/html/text.html index 073c6cc..5c276bf 100644 --- a/html/text.html +++ b/html/text.html @@ -1,16 +1,4 @@ - - - - - Tawashi - - - - - - - - +{{>head}}
diff --git a/html/topbar.mustache b/html/topbar.mustache new file mode 100644 index 0000000..419bedc --- /dev/null +++ b/html/topbar.mustache @@ -0,0 +1,10 @@ +
+

tawashi {{version}}

+ tawashi icon + + new icon + duplicate and edit icon + text icon + c++ icon + github icon +
diff --git a/src/response.cpp b/src/response.cpp index c431d68..c7367dc 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include namespace tawashi { namespace { @@ -67,6 +69,24 @@ namespace tawashi { throw std::runtime_error("Unknown setting for \"redis_mode\", valid settings are \"inet\" or \"sock\""); } } + + boost::optional load_whole_file (const std::string& parWebsiteRoot, const char* parSuffix, const std::string& parName, bool parThrow) { + std::ostringstream oss; + oss << parWebsiteRoot << parName << parSuffix; + std::cerr << "Trying to load \"" << oss.str() << "\"\n"; + std::ifstream if_mstch(oss.str(), std::ios::binary | std::ios::in); + + if (not if_mstch) { + if (parThrow) + throw std::runtime_error(std::string("File \"") + oss.str() + "\" not found"); + else + return boost::optional(); + } + + std::ostringstream buffer; + buffer << if_mstch.rdbuf(); + return boost::make_optional(buffer.str()); + } } //unnamed namespace Response::Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const IniFile& parIni, bool parWantRedis) : @@ -121,7 +141,17 @@ namespace tawashi { std::ostringstream stream_out; if (ContentType == m_resp_type) this->on_send(stream_out); - std::cout << mstch::render(stream_out.str(), mustache_context); + std::cout << mstch::render( + stream_out.str(), + mustache_context, + std::bind( + &load_whole_file, + std::cref(m_website_root), + ".mustache", + std::placeholders::_1, + false + ) + ); std::cout.flush(); } @@ -145,17 +175,8 @@ namespace tawashi { } std::string Response::load_mustache() const { - std::ostringstream oss; - oss << m_website_root << page_basename() << ".html.mstch"; - std::cerr << "Trying to load \"" << oss.str() << "\"\n"; - std::ifstream if_mstch(oss.str(), std::ios::binary | std::ios::in); - - if (!if_mstch) - throw std::runtime_error(std::string("File \"") + oss.str() + "\" not found"); - - std::ostringstream buffer; - buffer << if_mstch.rdbuf(); - return buffer.str(); + boost::optional content = load_whole_file(m_website_root, ".html.mstch", page_basename(), true); + return *content; } redis::IncRedis& Response::redis() const {