Fix the code so it builds & runs.
But I'm not sure the result is correct, and some implementations are still missing.
This commit is contained in:
parent
79ac7534f2
commit
84a599e771
2 changed files with 92 additions and 23 deletions
|
@ -52,6 +52,7 @@ namespace duck { namespace sl {
|
|||
using MustacheEntryMap = std::map<std::string, MustacheEntry>;
|
||||
|
||||
struct EntryNode {
|
||||
EntryNode (const EntryNode&) = default;
|
||||
explicit EntryNode (const std::string& parName) :
|
||||
name(parName)
|
||||
{
|
||||
|
@ -65,6 +66,7 @@ namespace duck { namespace sl {
|
|||
};
|
||||
|
||||
struct ApplyEntry {
|
||||
ApplyEntry (const ApplyEntry&) = default;
|
||||
ApplyEntry (const SourceInfo* parAppTo, std::string_view parMstchName) :
|
||||
apply_to(parAppTo),
|
||||
content(""),
|
||||
|
@ -102,6 +104,12 @@ namespace duck { namespace sl {
|
|||
EntryNode& m_root;
|
||||
};
|
||||
|
||||
mstch::map to_mustache_dict_recursive (
|
||||
const EntryNode& parNode,
|
||||
std::string_view parSrc,
|
||||
XPathRunner& parRunner
|
||||
);
|
||||
|
||||
void store_entry_subtree (
|
||||
const std::vector<StructItem>& parXPaths,
|
||||
EntryNode& parCurrList
|
||||
|
@ -164,6 +172,7 @@ namespace duck { namespace sl {
|
|||
|
||||
const EntryNodeList& global_entries() const { return m_global_entries; }
|
||||
const MustacheEntryMap& mustache_entries() const { return m_mustaches; }
|
||||
const std::vector<ApplyEntry>& apply_entries() const { return m_apply_entries; }
|
||||
|
||||
private:
|
||||
EntryNodeList m_global_entries;
|
||||
|
@ -180,6 +189,46 @@ namespace duck { namespace sl {
|
|||
std::size_t operator()(const std::vector<std::string>& parItem) const { return parItem.size(); }
|
||||
};
|
||||
|
||||
class ArraysToStructArrayVisitor : public boost::static_visitor<> {
|
||||
public:
|
||||
explicit ArraysToStructArrayVisitor (std::size_t parExpectedSize) :
|
||||
m_expected_size(parExpectedSize)
|
||||
{
|
||||
m_array.resize(m_expected_size, mstch::map());
|
||||
}
|
||||
|
||||
void operator()(const std::string& parName, const mstch::array& parItem) {
|
||||
for (std::size_t z = 0; z < parItem.size(); ++z) {
|
||||
auto& curr_map = boost::get<mstch::map>(m_array[z]);
|
||||
curr_map[parName] = parItem[z];
|
||||
}
|
||||
}
|
||||
|
||||
void operator()(const std::string& parName, const std::vector<std::string>& parItem) {
|
||||
for (std::size_t z = 0; z < parItem.size(); ++z) {
|
||||
auto& curr_map = boost::get<mstch::map>(m_array[z]);
|
||||
curr_map[parName] = parItem[z];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void operator()(const std::string& parName, const T& parItem) {
|
||||
auto& curr_map = boost::get<mstch::map>(m_array[0]);
|
||||
curr_map[parName] = parItem;
|
||||
}
|
||||
|
||||
mstch::node steal_struct() {
|
||||
if (1 == m_expected_size)
|
||||
return mstch::node(std::move(m_array[0]));
|
||||
else
|
||||
return mstch::node(std::move(m_array));
|
||||
}
|
||||
|
||||
private:
|
||||
mstch::array m_array;
|
||||
const std::size_t m_expected_size;
|
||||
};
|
||||
|
||||
std::size_t largest_array_size_in (mstch::map& parMap) {
|
||||
typedef ItemCountingVisitor ITC;
|
||||
using boost::apply_visitor;
|
||||
|
@ -195,48 +244,65 @@ namespace duck { namespace sl {
|
|||
);
|
||||
}
|
||||
|
||||
mstch::map to_mustache_dict_recursive (
|
||||
void fill_with_xpaths (
|
||||
mstch::map& parOut,
|
||||
const EntryNode& parNode,
|
||||
std::string_view parSrc,
|
||||
XPathRunner& parRunner,
|
||||
bool parMakeVecsSameSize
|
||||
XPathRunner& parRunner
|
||||
) {
|
||||
mstch::map retval;
|
||||
|
||||
for (const XPathElement* xpath : parNode.xpaths) {
|
||||
assert(xpath);
|
||||
std::cout << "Running query for \"" << xpath->name << "\"\n";
|
||||
auto results = parRunner.query(parSrc, xpath->xpath);
|
||||
if (results.size() == 1) {
|
||||
retval[xpath->name] = results.front();
|
||||
parOut[xpath->name] = results.front();
|
||||
}
|
||||
else if (results.size() > 1) {
|
||||
mstch::array values;
|
||||
values.reserve(results.size());
|
||||
std::copy(results.begin(), results.end(), std::back_inserter(values));
|
||||
retval[xpath->name] = std::move(values);
|
||||
parOut[xpath->name] = std::move(values);
|
||||
}
|
||||
else if (xpath->def_val) {
|
||||
retval[xpath->name] = *xpath->def_val;
|
||||
parOut[xpath->name] = *xpath->def_val;
|
||||
}
|
||||
else {
|
||||
retval[xpath->name] = std::string();
|
||||
parOut[xpath->name] = std::string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fill_with_structs (
|
||||
mstch::map& parOut,
|
||||
const EntryNode& parNode,
|
||||
std::string_view parSrc,
|
||||
XPathRunner& parRunner
|
||||
) {
|
||||
for (auto& curr_struct : parNode.structs) {
|
||||
assert(not curr_struct.name.empty());
|
||||
|
||||
mstch::array extracted_struct;
|
||||
auto new_struct = to_mustache_dict_recursive(curr_struct, parSrc, parRunner, false);
|
||||
auto new_struct = to_mustache_dict_recursive(curr_struct, parSrc, parRunner);
|
||||
const std::size_t extracted_struct_size = largest_array_size_in(new_struct);
|
||||
std::cout << "Largest array size in \"" << curr_struct.name << "\" = " << extracted_struct_size << '\n';
|
||||
for (auto&& itm : new_struct) {
|
||||
|
||||
ArraysToStructArrayVisitor fix_visitor(extracted_struct_size);
|
||||
for (auto&& itm : new_struct) {
|
||||
auto visitor = [&fix_visitor,&name=itm.first](const auto& var) { fix_visitor(name, var); };
|
||||
boost::apply_visitor(visitor, itm.second);
|
||||
}
|
||||
|
||||
// retval[std::string(curr_struct.name)] =
|
||||
parOut[std::string(curr_struct.name)] = std::move(fix_visitor.steal_struct());
|
||||
}
|
||||
}
|
||||
|
||||
mstch::map to_mustache_dict_recursive (
|
||||
const EntryNode& parNode,
|
||||
std::string_view parSrc,
|
||||
XPathRunner& parRunner
|
||||
) {
|
||||
mstch::map retval;
|
||||
|
||||
fill_with_xpaths(retval, parNode, parSrc, parRunner);
|
||||
fill_with_structs(retval, parNode, parSrc, parRunner);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
@ -256,7 +322,7 @@ namespace duck { namespace sl {
|
|||
assert(false); //not implemented
|
||||
}
|
||||
|
||||
mstch::map curr_entry_map = to_mustache_dict_recursive(entry.second, src_url, parRunner, false);
|
||||
mstch::map curr_entry_map = to_mustache_dict_recursive(entry.second, src_url, parRunner);
|
||||
curr_entry_map.merge(std::move(retval));
|
||||
retval.swap(curr_entry_map);
|
||||
}
|
||||
|
@ -277,18 +343,21 @@ namespace duck { namespace sl {
|
|||
std::vector<std::string> retval;
|
||||
const EntryNodeList& global_entries = dict_builder.global_entries();
|
||||
const MustacheEntryMap& mustaches = dict_builder.mustache_entries();
|
||||
retval.reserve(mustaches.size());
|
||||
const std::vector<ApplyEntry> apply_entries = dict_builder.apply_entries();
|
||||
retval.reserve(apply_entries.size());
|
||||
|
||||
std::cout << "-------------- visiting done ----------------\n";
|
||||
XPathRunner xpath_runner(html_pool);
|
||||
mstch::map mustache_ctx = to_mustache_map(global_entries, xpath_runner);
|
||||
for (auto& must : mustaches) {
|
||||
}
|
||||
|
||||
//for (auto& itm : dict_builder.global_entries()) {
|
||||
// std::cout << "item: \"" << itm.first << "\", \"" <<
|
||||
// itm.second->xpath << "\"\n";
|
||||
//}
|
||||
for (auto& apply_entry : apply_entries) {
|
||||
EntryNodeList entry_node {std::make_pair(apply_entry.apply_to, apply_entry.content)};
|
||||
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::cout << mstch::render(mustaches.at(name).text, entry_ctx) << std::endl;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ namespace duck { namespace sl {
|
|||
std::string_view parSrc,
|
||||
std::string_view parQuery
|
||||
) {
|
||||
static std::vector<std::string> deleme;
|
||||
static std::vector<std::string> deleme {"hello", "world"};
|
||||
return deleme;
|
||||
}
|
||||
}} //namespace duck::sl
|
||||
|
|
Loading…
Add table
Reference in a new issue