1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-07-02 14:04:16 +00:00

Return pasties into a proper html page.

I don't think html escaping is needed, since pasties
go through the colorizer first.

I don't really like the workaround to not call on_send() when
I don't want the html output, it's a bit omg what's going on
there... I'll have to rewrite that bit.
This commit is contained in:
King_DuckZ 2017-04-25 22:56:19 +01:00
parent 54e737c171
commit 06920f8d84
6 changed files with 55 additions and 20 deletions

View file

@ -13,7 +13,7 @@
</div> </div>
<div id="content"> <div id="content">
<p></p> <p>{{pastie}}</p>
</div> </div>
</body> </body>

View file

@ -18,7 +18,6 @@
#include "pastie_response.hpp" #include "pastie_response.hpp"
#include "incredis/incredis.hpp" #include "incredis/incredis.hpp"
#include "settings_bag.hpp" #include "settings_bag.hpp"
#include "escapist.hpp"
#include <ciso646> #include <ciso646>
#include <srchilite/sourcehighlight.h> #include <srchilite/sourcehighlight.h>
#include <srchilite/langmap.h> #include <srchilite/langmap.h>
@ -26,7 +25,7 @@
namespace tawashi { namespace tawashi {
PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) : PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
Response(Response::ContentType, "text/html", "", parSettings, true), Response(Response::ContentType, "text/html", "text", parSettings, true),
m_langmap_dir(parSettings->as<std::string>("langmap_dir")), m_langmap_dir(parSettings->as<std::string>("langmap_dir")),
m_plain_text(false) m_plain_text(false)
{ {
@ -48,6 +47,34 @@ namespace tawashi {
} }
} }
void PastieResponse::on_mustache_prepare (mstch::map& parContext) {
using opt_string = redis::IncRedis::opt_string;
using opt_string_list = redis::IncRedis::opt_string_list;
if (not m_plain_text) {
call_on_send(false);
auto token = boost::string_ref(cgi_env().path_info()).substr(1);
auto& redis = this->redis();
opt_string_list pastie_reply = redis.hmget(token, "pastie");
opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string());
srchilite::SourceHighlight highlighter;
highlighter.setDataDir(settings().as<std::string>("langmap_dir"));
highlighter.setGenerateEntireDoc(false);
highlighter.setGenerateLineNumbers(true);
const auto lang = m_lang_file;
//Escapist houdini;
//std::istringstream iss(houdini.escape_html(*pastie));
std::istringstream iss(*pastie);
std::ostringstream oss;
highlighter.highlight(iss, oss, lang);
parContext["pastie"] = oss.str();
}
}
void PastieResponse::on_send (std::ostream& parStream) { void PastieResponse::on_send (std::ostream& parStream) {
using opt_string = redis::IncRedis::opt_string; using opt_string = redis::IncRedis::opt_string;
using opt_string_list = redis::IncRedis::opt_string_list; using opt_string_list = redis::IncRedis::opt_string_list;
@ -56,6 +83,8 @@ namespace tawashi {
return; return;
} }
assert(m_plain_text);
auto token = boost::string_ref(cgi_env().path_info()).substr(1); auto token = boost::string_ref(cgi_env().path_info()).substr(1);
auto& redis = this->redis(); auto& redis = this->redis();
opt_string_list pastie_reply = redis.hmget(token, "pastie"); opt_string_list pastie_reply = redis.hmget(token, "pastie");
@ -65,19 +94,6 @@ namespace tawashi {
assert(false); assert(false);
} }
if (m_plain_text) {
parStream << *pastie; parStream << *pastie;
} }
else {
srchilite::SourceHighlight highlighter;
highlighter.setDataDir(settings().as<std::string>("langmap_dir"));
highlighter.setGenerateEntireDoc(false);
highlighter.setGenerateLineNumbers(true);
const auto lang = m_lang_file;
Escapist houdini;
std::istringstream iss(houdini.escape_html(*pastie));
highlighter.highlight(iss, parStream, lang);
}
}
} //namespace tawashi } //namespace tawashi

View file

@ -29,6 +29,7 @@ namespace tawashi {
private: private:
virtual void on_process() override; virtual void on_process() override;
virtual void on_send (std::ostream& parStream) override; virtual void on_send (std::ostream& parStream) override;
virtual void on_mustache_prepare (mstch::map& parContext) override;
std::string m_lang_file; std::string m_lang_file;
std::string m_langmap_dir; std::string m_langmap_dir;

View file

@ -97,6 +97,10 @@ namespace tawashi {
} }
return retval; return retval;
} }
std::string disable_mstch_escaping (const std::string& parStr) {
return parStr;
};
} //unnamed namespace } //unnamed namespace
Response::Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const Kakoune::SafePtr<SettingsBag>& parSettings, bool parWantRedis) : Response::Response (Types parRespType, std::string&& parValue, std::string&& parPageBaseName, const Kakoune::SafePtr<SettingsBag>& parSettings, bool parWantRedis) :
@ -106,12 +110,15 @@ namespace tawashi {
m_website_root(make_root_path(*parSettings)), m_website_root(make_root_path(*parSettings)),
m_page_basename(std::move(parPageBaseName)), m_page_basename(std::move(parPageBaseName)),
m_resp_type(parRespType), m_resp_type(parRespType),
m_header_sent(false) m_header_sent(false),
m_call_derived_on_send(true)
{ {
if (parWantRedis) { if (parWantRedis) {
m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings)); m_redis = std::make_unique<redis::IncRedis>(make_incredis(*parSettings));
m_redis->connect(); m_redis->connect();
} }
mstch::config::escape = &disable_mstch_escaping;
} }
Response::~Response() noexcept = default; Response::~Response() noexcept = default;
@ -155,8 +162,13 @@ namespace tawashi {
} }
std::ostringstream stream_out; std::ostringstream stream_out;
if (ContentType == m_resp_type) if (ContentType == m_resp_type) {
if (m_call_derived_on_send)
this->on_send(stream_out); this->on_send(stream_out);
else
Response::on_send(stream_out);
}
std::cout << mstch::render( std::cout << mstch::render(
stream_out.str(), stream_out.str(),
mustache_context, mustache_context,
@ -204,4 +216,8 @@ namespace tawashi {
assert(m_settings); assert(m_settings);
return *m_settings; return *m_settings;
} }
void Response::call_on_send (bool parCall) {
m_call_derived_on_send = parCall;
}
} //namespace tawashi } //namespace tawashi

View file

@ -52,6 +52,7 @@ namespace tawashi {
std::string load_mustache() const; std::string load_mustache() const;
redis::IncRedis& redis() const; redis::IncRedis& redis() const;
const SettingsBag& settings() const; const SettingsBag& settings() const;
void call_on_send (bool parCall);
private: private:
virtual void on_process(); virtual void on_process();
@ -66,5 +67,6 @@ namespace tawashi {
Types m_resp_type; Types m_resp_type;
std::unique_ptr<redis::IncRedis> m_redis; std::unique_ptr<redis::IncRedis> m_redis;
bool m_header_sent; bool m_header_sent;
bool m_call_derived_on_send;
}; };
} //namespace tawashi } //namespace tawashi