1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-27 00:43:47 +00:00

Add default_pastie_lang() method and language_selected mustache.

This commit is contained in:
King_DuckZ 2017-06-18 15:08:44 +01:00
parent 00e27a774a
commit 9deaa8a0b8
6 changed files with 34 additions and 6 deletions

View file

@ -65,8 +65,11 @@ namespace kamokan {
assert(not token_invalid()); assert(not token_invalid());
assert(not pastie_not_found()); 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["self_destructed"] = m_pastie_info.self_destructed;
parContext["pastie_token"] = get_search_token(cgi_env()); 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); this->on_general_mustache_prepare(std::move(*m_pastie_info.pastie), parContext);
} }
@ -79,4 +82,11 @@ namespace kamokan {
bool GeneralPastieResponse::pastie_not_found() const { bool GeneralPastieResponse::pastie_not_found() const {
return not m_pastie_info.pastie; 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 } //namespace kamokan

View file

@ -38,6 +38,7 @@ namespace kamokan {
virtual void on_mustache_prepare (mstch::map& parContext) override final; virtual void on_mustache_prepare (mstch::map& parContext) override final;
virtual tawashi::HttpHeader on_general_pastie_process() = 0; virtual tawashi::HttpHeader on_general_pastie_process() = 0;
virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) = 0; virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) = 0;
std::string default_pastie_lang();
Storage::RetrievedPastie m_pastie_info; Storage::RetrievedPastie m_pastie_info;
}; };

View file

@ -77,11 +77,15 @@ namespace kamokan {
return boost::make_optional(buffer.str()); 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; mstch::array retval;
for (auto&& lang : list_highlight_langs(parSettings)) { 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; return retval;
} }
@ -173,13 +177,13 @@ namespace kamokan {
statuslog->info("Sending response"); statuslog->info("Sending response");
SPDLOG_TRACE(statuslog, "Preparing mustache dictionary"); SPDLOG_TRACE(statuslog, "Preparing mustache dictionary");
const bool is_submit_page = this->is_submit_page();
mstch::map mustache_context { 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)}}, {"version", boost::string_view{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}},
{"tawashi_version", tawashi::version()}, {"tawashi_version", tawashi::version()},
{"base_uri", base_uri()}, {"base_uri", base_uri()},
{"host_path", make_host_path(this->settings())}, {"host_path", make_host_path(this->settings())}
{"languages", make_mstch_langmap(*m_settings)}
}; };
m_storage.finalize_connection(); m_storage.finalize_connection();
@ -188,6 +192,11 @@ namespace kamokan {
tawashi::HttpHeader http_header = this->on_process(); tawashi::HttpHeader http_header = this->on_process();
*m_stream_out << http_header; *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()) { if (http_header.body_required()) {
using std::chrono::steady_clock; using std::chrono::steady_clock;
using std::chrono::milliseconds; using std::chrono::milliseconds;
@ -275,4 +284,8 @@ namespace kamokan {
assert(parTime <= m_time0); assert(parTime <= m_time0);
m_time0 = parTime; m_time0 = parTime;
} }
std::string Response::default_pastie_lang() {
return std::string();
}
} //namespace kamokan } //namespace kamokan

View file

@ -69,6 +69,7 @@ namespace kamokan {
virtual tawashi::HttpHeader on_process(); virtual tawashi::HttpHeader on_process();
virtual void on_mustache_prepare (mstch::map& parContext); virtual void on_mustache_prepare (mstch::map& parContext);
virtual std::string on_mustache_retrieve(); virtual std::string on_mustache_retrieve();
virtual std::string default_pastie_lang();
virtual bool is_submit_page() { return false; } virtual bool is_submit_page() { return false; }
Storage m_storage; Storage m_storage;

View file

@ -82,6 +82,7 @@ namespace kamokan {
Storage::RetrievedPastie::RetrievedPastie() : Storage::RetrievedPastie::RetrievedPastie() :
pastie(), pastie(),
lang(),
self_destructed(false), self_destructed(false),
valid_token(false) valid_token(false)
{ {
@ -175,9 +176,10 @@ namespace kamokan {
if (not retval.valid_token) if (not retval.valid_token)
return retval; 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()); 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()); 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<bool>(*selfdes)) { if (selfdes and string_conv<bool>(*selfdes)) {
const bool deleted = m_redis->del(parToken); const bool deleted = m_redis->del(parToken);

View file

@ -45,6 +45,7 @@ namespace kamokan {
~RetrievedPastie() = default; ~RetrievedPastie() = default;
boost::optional<std::string> pastie; boost::optional<std::string> pastie;
boost::optional<std::string> lang;
bool self_destructed; bool self_destructed;
bool valid_token; bool valid_token;
}; };