1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-06-07 00:51:41 +00:00

Add plain text mode

This commit is contained in:
King_DuckZ 2017-04-27 09:33:47 +01:00
parent 457127aa04
commit 73323c9ee7
3 changed files with 28 additions and 11 deletions

View file

@ -11,7 +11,7 @@
<div> <div>
<p class="title">Syntax Highlighting:</p> <p class="title">Syntax Highlighting:</p>
<select name="lang" class="selectBox"> <select name="lang" class="selectBox">
<option value="" selected="selected">None </option> <option value="plaintext" selected="selected">None </option>
{{#languages}} {{#languages}}
<option value="{{language_name}}">{{language_name}}</option> <option value="{{language_name}}">{{language_name}}</option>
{{/languages}} {{/languages}}

View file

@ -18,30 +18,40 @@
#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>
#include <sstream> #include <sstream>
namespace tawashi { namespace tawashi {
namespace {
const char g_nolang_token[] = "plaintext";
} //unnamed namespace
PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) : PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
Response(Response::ContentType, "text/html", "text", 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),
m_syntax_highlight(true)
{ {
} }
void PastieResponse::on_process() { void PastieResponse::on_process() {
auto env = cgi_env().query_string_split(); auto env = cgi_env().query_string_split();
if (env["m"] == "plain" or cgi_env().query_string().empty()) { const std::string& query_str(cgi_env().query_string());
if (env["m"] == "plain" or query_str.empty()) {
this->change_type(Response::ContentType, "text/plain; charset=utf-8"); this->change_type(Response::ContentType, "text/plain; charset=utf-8");
m_plain_text = true; m_plain_text = true;
} }
else if (query_str == g_nolang_token) {
m_syntax_highlight = false;
}
else { else {
srchilite::LangMap lang_map(m_langmap_dir, "lang.map"); srchilite::LangMap lang_map(m_langmap_dir, "lang.map");
lang_map.open(); lang_map.open();
if (not cgi_env().query_string().empty()) if (not query_str.empty())
m_lang_file = lang_map.getFileName(cgi_env().query_string()); m_lang_file = lang_map.getFileName(query_str);
else else
m_lang_file = "default.lang"; m_lang_file = "default.lang";
} }
@ -61,18 +71,24 @@ namespace tawashi {
highlighter.setGenerateEntireDoc(false); highlighter.setGenerateEntireDoc(false);
highlighter.setGenerateLineNumbers(true); highlighter.setGenerateLineNumbers(true);
const auto lang = m_lang_file; const auto lang = m_lang_file;
//Escapist houdini;
//std::istringstream iss(houdini.escape_html(*pastie));
if (m_plain_text) { std::string processed_pastie;
parContext["pastie"] = *pastie; if (m_syntax_highlight) {
processed_pastie = std::move(*pastie);
} }
else { else {
std::istringstream iss(*pastie); Escapist houdini;
processed_pastie = houdini.escape_html(*pastie);
}
if (not m_plain_text and m_syntax_highlight) {
std::istringstream iss(std::move(processed_pastie));
std::ostringstream oss; std::ostringstream oss;
highlighter.highlight(iss, oss, lang); highlighter.highlight(iss, oss, lang);
parContext["pastie"] = oss.str(); processed_pastie = oss.str();
} }
parContext["pastie"] = std::move(processed_pastie);
} }
std::string PastieResponse::on_mustache_retrieve() { std::string PastieResponse::on_mustache_retrieve() {

View file

@ -34,5 +34,6 @@ namespace tawashi {
std::string m_lang_file; std::string m_lang_file;
std::string m_langmap_dir; std::string m_langmap_dir;
bool m_plain_text; bool m_plain_text;
bool m_syntax_highlight;
}; };
} //namespace tawashi } //namespace tawashi