1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-02-13 09:33:56 +00:00

Add an array of lines and line numbers to the mustache context.

This commit is contained in:
King_DuckZ 2017-06-07 21:52:55 +01:00
parent a56014bed8
commit c3609e1768

View file

@ -26,6 +26,10 @@
#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 tawashi {
namespace {
@ -41,6 +45,29 @@ namespace tawashi {
assert(it_found - parRequest.begin() <= parRequest.size());
return parRequest.substr(0, it_found - parRequest.begin());
}
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
PastieResponse::PastieResponse (
@ -126,6 +153,9 @@ namespace tawashi {
}
parContext["pastie"] = std::move(processed_pastie);
parContext["pastie_lines"] = pastie_to_numbered_lines(
boost::get<std::string>(parContext["pastie"])
);
}
std::string PastieResponse::on_mustache_retrieve() {