diff --git a/src/kamokan_impl/general_pastie_response.cpp b/src/kamokan_impl/general_pastie_response.cpp index 4ca6206..b6204f8 100644 --- a/src/kamokan_impl/general_pastie_response.cpp +++ b/src/kamokan_impl/general_pastie_response.cpp @@ -65,8 +65,11 @@ namespace kamokan { assert(not token_invalid()); assert(not pastie_not_found()); + auto pastie_lang = (m_pastie_info.lang ? boost::string_view(*m_pastie_info.lang) : boost::string_view()); parContext["self_destructed"] = m_pastie_info.self_destructed; parContext["pastie_token"] = get_search_token(cgi_env()); + parContext["pastie_lang"] = pastie_lang; + parContext["colourless"] = pastie_lang.empty() or pastie_lang == "colourless"; this->on_general_mustache_prepare(std::move(*m_pastie_info.pastie), parContext); } @@ -79,4 +82,11 @@ namespace kamokan { bool GeneralPastieResponse::pastie_not_found() const { return not m_pastie_info.pastie; } + + std::string GeneralPastieResponse::default_pastie_lang() { + if (m_pastie_info.lang) + return *m_pastie_info.lang; + else + return std::string(); + } } //namespace kamokan diff --git a/src/kamokan_impl/general_pastie_response.hpp b/src/kamokan_impl/general_pastie_response.hpp index dbf8a39..9e4fc0c 100644 --- a/src/kamokan_impl/general_pastie_response.hpp +++ b/src/kamokan_impl/general_pastie_response.hpp @@ -38,6 +38,7 @@ namespace kamokan { virtual void on_mustache_prepare (mstch::map& parContext) override final; virtual tawashi::HttpHeader on_general_pastie_process() = 0; virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) = 0; + std::string default_pastie_lang(); Storage::RetrievedPastie m_pastie_info; }; diff --git a/src/kamokan_impl/response.cpp b/src/kamokan_impl/response.cpp index 140529a..1215d66 100644 --- a/src/kamokan_impl/response.cpp +++ b/src/kamokan_impl/response.cpp @@ -77,11 +77,15 @@ namespace kamokan { return boost::make_optional(buffer.str()); } - mstch::array make_mstch_langmap (const SettingsBag& parSettings) { + mstch::array make_mstch_langmap (const SettingsBag& parSettings, const std::string& parSelected) { mstch::array retval; for (auto&& lang : list_highlight_langs(parSettings)) { - retval.push_back(mstch::map{{"language_name", std::move(lang)}}); + const bool selected = (parSelected == lang); + retval.push_back(mstch::map{ + {"language_name", std::move(lang)}, + {"language_selected", selected} + }); } return retval; } @@ -173,13 +177,13 @@ namespace kamokan { statuslog->info("Sending response"); SPDLOG_TRACE(statuslog, "Preparing mustache dictionary"); + const bool is_submit_page = this->is_submit_page(); mstch::map mustache_context { - {"submit_page", this->is_submit_page()}, + {"submit_page", is_submit_page}, {"version", boost::string_view{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}}, {"tawashi_version", tawashi::version()}, {"base_uri", base_uri()}, - {"host_path", make_host_path(this->settings())}, - {"languages", make_mstch_langmap(*m_settings)} + {"host_path", make_host_path(this->settings())} }; m_storage.finalize_connection(); @@ -188,6 +192,11 @@ namespace kamokan { tawashi::HttpHeader http_header = this->on_process(); *m_stream_out << http_header; + if (is_submit_page) { + SPDLOG_TRACE(statuslog, "Adding language list to mustache context"); + mustache_context["languages"] = make_mstch_langmap(*m_settings, this->default_pastie_lang()); + } + if (http_header.body_required()) { using std::chrono::steady_clock; using std::chrono::milliseconds; @@ -275,4 +284,8 @@ namespace kamokan { assert(parTime <= m_time0); m_time0 = parTime; } + + std::string Response::default_pastie_lang() { + return std::string(); + } } //namespace kamokan diff --git a/src/kamokan_impl/response.hpp b/src/kamokan_impl/response.hpp index 3183d3b..a92696f 100644 --- a/src/kamokan_impl/response.hpp +++ b/src/kamokan_impl/response.hpp @@ -69,6 +69,7 @@ namespace kamokan { virtual tawashi::HttpHeader on_process(); virtual void on_mustache_prepare (mstch::map& parContext); virtual std::string on_mustache_retrieve(); + virtual std::string default_pastie_lang(); virtual bool is_submit_page() { return false; } Storage m_storage; diff --git a/src/kamokan_impl/storage.cpp b/src/kamokan_impl/storage.cpp index 1dfa16d..9193b77 100644 --- a/src/kamokan_impl/storage.cpp +++ b/src/kamokan_impl/storage.cpp @@ -82,6 +82,7 @@ namespace kamokan { Storage::RetrievedPastie::RetrievedPastie() : pastie(), + lang(), self_destructed(false), valid_token(false) { @@ -175,9 +176,10 @@ namespace kamokan { if (not retval.valid_token) return retval; - opt_string_list pastie_reply = m_redis->hmget(parToken, "pastie", "selfdes"); + opt_string_list pastie_reply = m_redis->hmget(parToken, "pastie", "selfdes", "lang"); retval.pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string()); opt_string selfdes = (pastie_reply and pastie_reply->size() > 1 ? (*pastie_reply)[1] : opt_string()); + retval.lang = (pastie_reply and pastie_reply->size() > 2 ? (*pastie_reply)[2] : opt_string()); if (selfdes and string_conv(*selfdes)) { const bool deleted = m_redis->del(parToken); diff --git a/src/kamokan_impl/storage.hpp b/src/kamokan_impl/storage.hpp index 4d6594d..cd1ee2c 100644 --- a/src/kamokan_impl/storage.hpp +++ b/src/kamokan_impl/storage.hpp @@ -45,6 +45,7 @@ namespace kamokan { ~RetrievedPastie() = default; boost::optional pastie; + boost::optional lang; bool self_destructed; bool valid_token; };