mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-02-09 09:23:56 +00:00
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.
This commit is contained in:
parent
1adff30ffa
commit
00c62d4ac6
6 changed files with 82 additions and 45 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -21,37 +21,10 @@
|
|||
#include "error_reasons.hpp"
|
||||
#include <cassert>
|
||||
#include <ciso646>
|
||||
#include <boost/algorithm/string/find_iterator.hpp>
|
||||
#include <boost/range/iterator_range_core.hpp>
|
||||
#include <boost/algorithm/string/finder.hpp>
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
#include <utility>
|
||||
|
||||
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<string_view_iterator>;
|
||||
|
||||
int line_num = 1;
|
||||
return boost::copy_range<mstch::array>(
|
||||
boost::make_iterator_range(
|
||||
split_iterator<string_view_iterator>(parPastie, token_finder([](char c){return '\n'==c;})),
|
||||
split_iterator<string_view_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<std::string>(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 {
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -27,21 +27,81 @@
|
|||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <boost/algorithm/string/find_iterator.hpp>
|
||||
#include <boost/range/iterator_range_core.hpp>
|
||||
#include <boost/algorithm/string/finder.hpp>
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
|
||||
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<std::string>("highlight_css");
|
||||
}
|
||||
|
||||
std::string strip_tags_from_highlighted (const std::string& parPastie) {
|
||||
boost::string_view pastie(parPastie);
|
||||
auto beg_stripped = pastie.substr(pastie.find("<tt>") + 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:
|
||||
// <!-- Generator: GNU source-highlight 3.1.8
|
||||
// by Lorenzo Bettini
|
||||
// http://www.lorenzobettini.it
|
||||
// http://www.gnu.org/software/src-highlite -->
|
||||
|
||||
SplitHighlightedPastie retval;
|
||||
{
|
||||
const auto comment_start = parPastie.find("<!--");
|
||||
if (parPastie.npos != comment_start) {
|
||||
const auto comment_len = 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("<pre><tt>");
|
||||
if (parPastie.npos != pre_start) {
|
||||
parPastie.erase(pre_start, 9);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const auto pre_cl_start = parPastie.find("</tt></pre>");
|
||||
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<string_view_iterator>;
|
||||
|
||||
int line_num = 1;
|
||||
return boost::copy_range<mstch::array>(
|
||||
boost::make_iterator_range(
|
||||
split_iterator<string_view_iterator>(parPastie, token_finder([](char c){return '\n'==c;})),
|
||||
split_iterator<string_view_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<std::string>("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<std::string>(parContext["pastie"])
|
||||
);
|
||||
parContext["highlight_comment"] = std::move(highlight_comment);
|
||||
}
|
||||
|
||||
std::string PastieResponse::on_mustache_retrieve() {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue