Lots of crap but it works. I'll improve code as I go.
This commit is contained in:
parent
a6916f6179
commit
b028e8c492
7 changed files with 55 additions and 19 deletions
|
@ -45,7 +45,7 @@ namespace duck {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isHttps (const std::string& parUrl) {
|
bool isHttps (const std::string_view& parUrl) {
|
||||||
const char protocol[] = "https://";
|
const char protocol[] = "https://";
|
||||||
const size_t protocolLen = sizeof(protocol) / sizeof(protocol[0]) - 1;
|
const size_t protocolLen = sizeof(protocol) / sizeof(protocol[0]) - 1;
|
||||||
if (parUrl.size() < protocolLen)
|
if (parUrl.size() < protocolLen)
|
||||||
|
@ -103,7 +103,7 @@ namespace duck {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string fetch_html (const std::string& parSource, std::string parUserAgent, bool parSslVerifyPeer, bool parSslVerifyHost) {
|
std::string fetch_html (const std::string_view& parSource, std::string parUserAgent, bool parSslVerifyPeer, bool parSslVerifyHost) {
|
||||||
using curl::curl_easy;
|
using curl::curl_easy;
|
||||||
using curl::curl_pair;
|
using curl::curl_pair;
|
||||||
using curl::curl_ios;
|
using curl::curl_ios;
|
||||||
|
@ -111,7 +111,7 @@ namespace duck {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
curl_ios<std::ostream> wr(oss);
|
curl_ios<std::ostream> wr(oss);
|
||||||
curl_easy easy(wr);
|
curl_easy easy(wr);
|
||||||
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_URL, parSource));
|
easy.add(curl_pair<CURLoption, std::string>(CURLOPT_URL, std::string(parSource)));
|
||||||
if (isHttps(parSource)) {
|
if (isHttps(parSource)) {
|
||||||
easy.add(curl_pair<CURLoption, bool>(CURLOPT_SSL_VERIFYPEER, parSslVerifyPeer));
|
easy.add(curl_pair<CURLoption, bool>(CURLOPT_SSL_VERIFYPEER, parSslVerifyPeer));
|
||||||
easy.add(curl_pair<CURLoption, bool>(CURLOPT_SSL_VERIFYHOST, parSslVerifyHost));
|
easy.add(curl_pair<CURLoption, bool>(CURLOPT_SSL_VERIFYHOST, parSslVerifyHost));
|
||||||
|
|
|
@ -20,9 +20,10 @@
|
||||||
#define idC6776D903059465191FFB64FCFD6B86A
|
#define idC6776D903059465191FFB64FCFD6B86A
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
std::string fetch_html ( const std::string& parSource, std::string parUserAgent, bool parSslVerifyPeer, bool parSslVerifyHost );
|
std::string fetch_html ( const std::string_view& parSource, std::string parUserAgent, bool parSslVerifyPeer, bool parSslVerifyHost );
|
||||||
std::string clean_html ( std::string&& html );
|
std::string clean_html ( std::string&& html );
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
||||||
|
|
|
@ -72,6 +72,8 @@ namespace duck { namespace sl {
|
||||||
content(""),
|
content(""),
|
||||||
mustache_name(parMstchName)
|
mustache_name(parMstchName)
|
||||||
{
|
{
|
||||||
|
assert(apply_to);
|
||||||
|
assert(not apply_to->value.empty());
|
||||||
}
|
}
|
||||||
ApplyEntry (ApplyEntry&&) = default;
|
ApplyEntry (ApplyEntry&&) = default;
|
||||||
ApplyEntry& operator=(ApplyEntry&&) = default;
|
ApplyEntry& operator=(ApplyEntry&&) = default;
|
||||||
|
@ -150,6 +152,7 @@ namespace duck { namespace sl {
|
||||||
#if defined(APPLY_VERBOSE)
|
#if defined(APPLY_VERBOSE)
|
||||||
std::cout << parVal << '\n';
|
std::cout << parVal << '\n';
|
||||||
#endif
|
#endif
|
||||||
|
assert(not parVal.source.value.empty());
|
||||||
m_apply_entries.emplace_back(&parVal.source, parVal.mustache_model);
|
m_apply_entries.emplace_back(&parVal.source, parVal.mustache_model);
|
||||||
store_entry_subtree(parVal.xpaths, m_apply_entries.back().content);
|
store_entry_subtree(parVal.xpaths, m_apply_entries.back().content);
|
||||||
}
|
}
|
||||||
|
@ -353,9 +356,15 @@ namespace duck { namespace sl {
|
||||||
for (auto& apply_entry : apply_entries) {
|
for (auto& apply_entry : apply_entries) {
|
||||||
EntryNodeList entry_node {std::make_pair(apply_entry.apply_to, apply_entry.content)};
|
EntryNodeList entry_node {std::make_pair(apply_entry.apply_to, apply_entry.content)};
|
||||||
mstch::map entry_ctx = to_mustache_map(entry_node, xpath_runner);
|
mstch::map entry_ctx = to_mustache_map(entry_node, xpath_runner);
|
||||||
//std::cout << "Raw mustache for \"" << must.first << "\":\n" <<
|
|
||||||
// must.second.text << "\nRendered mustache:\n";
|
|
||||||
std::string name(apply_entry.mustache_name);
|
std::string name(apply_entry.mustache_name);
|
||||||
|
|
||||||
|
std::cout << "context size: " << entry_ctx.size() << '\n';
|
||||||
|
for (auto& ctx_itm : entry_ctx) {
|
||||||
|
std::cout << '\t' << ctx_itm.first << '\n';
|
||||||
|
}
|
||||||
|
std::cout << "Raw mustache for \"" << name << "\":\n" <<
|
||||||
|
mustaches.at(name).text << "\nRendered mustache:\n";
|
||||||
|
|
||||||
std::cout << mstch::render(mustaches.at(name).text, entry_ctx) << std::endl;
|
std::cout << mstch::render(mustaches.at(name).text, entry_ctx) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,11 @@
|
||||||
|
|
||||||
#include "implem/ResourcePool.hpp"
|
#include "implem/ResourcePool.hpp"
|
||||||
#include "kakoune/safe_ptr.hh"
|
#include "kakoune/safe_ptr.hh"
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace duck { namespace sl {
|
namespace duck { namespace sl {
|
||||||
namespace implem {
|
namespace implem {
|
||||||
typedef duckutil::ResourcePool<std::string, std::string> HtmlPoolBase;
|
typedef duckutil::ResourcePool<std::string, std::string_view> HtmlPoolBase;
|
||||||
} //namespace implem
|
} //namespace implem
|
||||||
|
|
||||||
class HtmlPoolBase : public implem::HtmlPoolBase, public Kakoune::SafeCountable {
|
class HtmlPoolBase : public implem::HtmlPoolBase, public Kakoune::SafeCountable {
|
||||||
|
|
|
@ -17,13 +17,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "xpath_runner.hpp"
|
#include "xpath_runner.hpp"
|
||||||
|
#include "xpath.hpp"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace duck { namespace sl {
|
namespace duck { namespace sl {
|
||||||
struct XPathRunner::XPathKey {
|
struct XPathRunner::XPathKey {
|
||||||
XPathKey (const std::string& parSrc, const std::string& parQuery) :
|
XPathKey (const std::string_view& parSrc, const std::string_view& parQuery) :
|
||||||
source_address(parSrc),
|
source_address(std::string(parSrc)),
|
||||||
xpath_query(parQuery)
|
xpath_query(std::string(parQuery))
|
||||||
{
|
{
|
||||||
assert(not source_address.empty());
|
assert(not source_address.empty());
|
||||||
}
|
}
|
||||||
|
@ -51,7 +53,24 @@ namespace duck { namespace sl {
|
||||||
std::string_view parSrc,
|
std::string_view parSrc,
|
||||||
std::string_view parQuery
|
std::string_view parQuery
|
||||||
) {
|
) {
|
||||||
static std::vector<std::string> deleme {"hello", "world"};
|
std::cout << "XPathRunner::query()\n";
|
||||||
return deleme;
|
auto ins_retval = m_cached_results.insert(std::make_pair(XPathKey(parSrc, parQuery), std::vector<std::string>()));
|
||||||
|
const bool inserted = ins_retval.second;
|
||||||
|
assert(ins_retval.first != m_cached_results.end());
|
||||||
|
std::vector<std::string>& curr_vec = ins_retval.first->second;
|
||||||
|
|
||||||
|
if (inserted) {
|
||||||
|
const auto id = m_pool->AddResource(parSrc);
|
||||||
|
const std::string* html = m_pool->GetByID(id);
|
||||||
|
|
||||||
|
curr_vec = xpath_query(*html, std::string(parQuery));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "returning " << curr_vec.size() << " items: ";
|
||||||
|
for (auto& i : curr_vec) {
|
||||||
|
std:: cout << '"' << i << "\", ";
|
||||||
|
}
|
||||||
|
std::cout << '\n';
|
||||||
|
return curr_vec;
|
||||||
}
|
}
|
||||||
}} //namespace duck::sl
|
}} //namespace duck::sl
|
||||||
|
|
|
@ -76,12 +76,18 @@ namespace duck {
|
||||||
return std::move(retval);
|
return std::move(retval);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string xpath_query (const std::string& parXML, const std::string& parQuery) {
|
std::vector<std::string> xpath_query (const std::string& parXML, const std::string& parQuery) {
|
||||||
auto retval = xpath_query(parXML, std::vector<std::string>{parQuery});
|
auto query_res = xpath_query(parXML, std::vector<std::string>{parQuery});
|
||||||
if (retval.empty() or retval.front().empty())
|
if (query_res.empty() or query_res.front().empty()) {
|
||||||
return std::string();
|
return std::vector<std::string>();
|
||||||
else
|
}
|
||||||
return retval.front().front().second;
|
else {
|
||||||
|
std::vector<std::string> retval;
|
||||||
|
const std::vector<std::pair<std::string, std::string>>& src = query_res.front();
|
||||||
|
retval.reserve(src.size());
|
||||||
|
std::transform(src.begin(), src.end(), std::back_inserter(retval), [](const auto& pair) { return pair.second; });
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ParseError::ParseError (int parLine, int parColumn, std::string parMessage) {
|
ParseError::ParseError (int parLine, int parColumn, std::string parMessage) {
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace duck {
|
||||||
};
|
};
|
||||||
|
|
||||||
XPathBatchResults xpath_query ( const std::string& parXML, const std::vector<std::string>& parQueries );
|
XPathBatchResults xpath_query ( const std::string& parXML, const std::vector<std::string>& parQueries );
|
||||||
std::string xpath_query ( const std::string& parXML, const std::string& parQuery );
|
std::vector<std::string> xpath_query ( const std::string& parXML, const std::string& parQuery );
|
||||||
} //namespace duck
|
} //namespace duck
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue