From 00c62d4ac6c17aecf642f440f4f5601c9fe376f1 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 18 Jun 2017 02:23:07 +0100 Subject: [PATCH] Improve code that strips unwanted tags from the highlighted html. Provide a highlight_comment mustache tag with the comment that I stripped from the highlighted pastie. --- src/kamokan_impl/edit_response.cpp | 4 +- src/kamokan_impl/edit_response.hpp | 2 +- src/kamokan_impl/general_pastie_response.cpp | 34 +------- src/kamokan_impl/general_pastie_response.hpp | 2 +- src/kamokan_impl/pastie_response.cpp | 83 ++++++++++++++++++-- src/kamokan_impl/pastie_response.hpp | 2 +- 6 files changed, 82 insertions(+), 45 deletions(-) diff --git a/src/kamokan_impl/edit_response.cpp b/src/kamokan_impl/edit_response.cpp index af92119..6ea343c 100644 --- a/src/kamokan_impl/edit_response.cpp +++ b/src/kamokan_impl/edit_response.cpp @@ -32,8 +32,8 @@ namespace kamokan { return tawashi::make_header_type_html(); } - std::string EditResponse::on_pastie_prepare (std::string&& parPastie) { + void EditResponse::on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) { tawashi::Escapist houdini; - return houdini.escape_html(parPastie); + parContext["pastie"] = houdini.escape_html(parPastie); } } //namespace kamokan diff --git a/src/kamokan_impl/edit_response.hpp b/src/kamokan_impl/edit_response.hpp index ebd98ec..0ef32f5 100644 --- a/src/kamokan_impl/edit_response.hpp +++ b/src/kamokan_impl/edit_response.hpp @@ -34,7 +34,7 @@ namespace kamokan { } private: - virtual std::string on_pastie_prepare (std::string&& parPastie) override; + virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) override; virtual tawashi::HttpHeader on_general_pastie_process() override; }; } //namespace kamokan diff --git a/src/kamokan_impl/general_pastie_response.cpp b/src/kamokan_impl/general_pastie_response.cpp index 4cb34a7..2857527 100644 --- a/src/kamokan_impl/general_pastie_response.cpp +++ b/src/kamokan_impl/general_pastie_response.cpp @@ -21,37 +21,10 @@ #include "error_reasons.hpp" #include #include -#include -#include -#include -#include #include namespace kamokan { namespace { - mstch::array pastie_to_numbered_lines (boost::string_view parPastie) { - using boost::string_view; - using string_view_iterator = string_view::const_iterator; - using boost::split_iterator; - using boost::token_finder; - using boost::adaptors::transformed; - using MatchRange = boost::iterator_range; - - int line_num = 1; - return boost::copy_range( - boost::make_iterator_range( - split_iterator(parPastie, token_finder([](char c){return '\n'==c;})), - split_iterator() - ) | - transformed([&line_num,parPastie](const MatchRange& r) { - return mstch::map { - {"number", line_num++}, - {"line", parPastie.substr(r.begin() - parPastie.begin(), r.size())} - }; - }) - ); - } - boost::string_view get_search_token (const cgi::Env& parCgiEnv) { return cgi::drop_arguments(parCgiEnv.request_uri_relative()); } @@ -92,14 +65,11 @@ namespace kamokan { assert(not token_invalid()); assert(not pastie_not_found()); - parContext["pastie"] = - this->on_pastie_prepare(std::move(*m_pastie_info.pastie)); - parContext["pastie_lines"] = pastie_to_numbered_lines( - boost::get(parContext["pastie"]) - ); parContext["self_destructed"] = m_pastie_info.self_destructed; parContext["pastie_token"] = get_search_token(cgi_env()); parContext["pastie_page"] = true; + + this->on_general_mustache_prepare(std::move(*m_pastie_info.pastie), parContext); } bool GeneralPastieResponse::token_invalid() const { diff --git a/src/kamokan_impl/general_pastie_response.hpp b/src/kamokan_impl/general_pastie_response.hpp index d4f99cd..dbf8a39 100644 --- a/src/kamokan_impl/general_pastie_response.hpp +++ b/src/kamokan_impl/general_pastie_response.hpp @@ -36,8 +36,8 @@ namespace kamokan { private: virtual tawashi::HttpHeader on_process() override final; virtual void on_mustache_prepare (mstch::map& parContext) override final; - virtual std::string on_pastie_prepare (std::string&& parPastie) = 0; virtual tawashi::HttpHeader on_general_pastie_process() = 0; + virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) = 0; Storage::RetrievedPastie m_pastie_info; }; diff --git a/src/kamokan_impl/pastie_response.cpp b/src/kamokan_impl/pastie_response.cpp index b74f1d0..20b8ab7 100644 --- a/src/kamokan_impl/pastie_response.cpp +++ b/src/kamokan_impl/pastie_response.cpp @@ -27,21 +27,81 @@ #include #include #include +#include +#include +#include +#include namespace kamokan { namespace { const char g_nolang_token[] = "colourless"; + struct SplitHighlightedPastie { + std::string comment; + std::string text; + }; + std::string highlight_css_path (const SettingsBag& parSettings) { //TODO: make sure the file exists or throw or do something return parSettings.as("highlight_css"); } - std::string strip_tags_from_highlighted (const std::string& parPastie) { - boost::string_view pastie(parPastie); - auto beg_stripped = pastie.substr(pastie.find("") + 4); - auto end_stripped = beg_stripped.substr(0, beg_stripped.size() - 12); - return std::string(end_stripped); + SplitHighlightedPastie strip_tags_from_highlighted (std::string parPastie) { + //I'm expecting some comment at the beginning, like: + // + + SplitHighlightedPastie retval; + { + const auto comment_start = parPastie.find("") - comment_start + 3; + retval.comment = parPastie.substr(comment_start, comment_len); + parPastie.erase(comment_start, comment_len); + } + } + + { + const auto pre_start = parPastie.find("
");
+				if (parPastie.npos != pre_start) {
+					parPastie.erase(pre_start, 9);
+				}
+			}
+
+			{
+				const auto pre_cl_start = parPastie.find("
"); + if (parPastie.npos != pre_cl_start) { + parPastie.erase(pre_cl_start, 11); + } + } + + retval.text = std::move(parPastie); + return retval; + } + + mstch::array pastie_to_numbered_lines (boost::string_view parPastie) { + using boost::string_view; + using string_view_iterator = string_view::const_iterator; + using boost::split_iterator; + using boost::token_finder; + using boost::adaptors::transformed; + using MatchRange = boost::iterator_range; + + int line_num = 1; + return boost::copy_range( + boost::make_iterator_range( + split_iterator(parPastie, token_finder([](char c){return '\n'==c;})), + split_iterator() + ) | + transformed([&line_num,parPastie](const MatchRange& r) { + return mstch::map { + {"number", line_num++}, + {"line", parPastie.substr(r.begin() - parPastie.begin(), r.size())} + }; + }) + ); } } //unnamed namespace @@ -79,7 +139,7 @@ namespace kamokan { return tawashi::make_header_type_html(); } - std::string PastieResponse::on_pastie_prepare (std::string&& parPastie) { + void PastieResponse::on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) { srchilite::SourceHighlight highlighter; highlighter.setDataDir(settings().as("langmap_dir")); highlighter.setGenerateEntireDoc(false); @@ -95,6 +155,7 @@ namespace kamokan { highlighter.setGenerateLineNumbers(false); std::string processed_pastie; + std::string highlight_comment; if (m_syntax_highlight) { processed_pastie = std::move(parPastie); } @@ -107,10 +168,16 @@ namespace kamokan { std::istringstream iss(std::move(processed_pastie)); std::ostringstream oss; highlighter.highlight(iss, oss, m_lang_file); - processed_pastie = strip_tags_from_highlighted(oss.str()); + SplitHighlightedPastie split = strip_tags_from_highlighted(oss.str()); + processed_pastie = std::move(split.text); + highlight_comment = std::move(split.comment); } - return processed_pastie; + parContext["pastie"] = std::move(processed_pastie); + parContext["pastie_lines"] = pastie_to_numbered_lines( + boost::get(parContext["pastie"]) + ); + parContext["highlight_comment"] = std::move(highlight_comment); } std::string PastieResponse::on_mustache_retrieve() { diff --git a/src/kamokan_impl/pastie_response.hpp b/src/kamokan_impl/pastie_response.hpp index 03e5a86..7a6b1f5 100644 --- a/src/kamokan_impl/pastie_response.hpp +++ b/src/kamokan_impl/pastie_response.hpp @@ -35,8 +35,8 @@ namespace kamokan { private: virtual std::string on_mustache_retrieve() override; - virtual std::string on_pastie_prepare (std::string&& parPastie) override; virtual tawashi::HttpHeader on_general_pastie_process() override; + virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) override; std::string m_lang_file; std::string m_langmap_dir;