3 Commits
6 changed files with 42 additions and 22 deletions
+6 -7
View File
@@ -9,14 +9,13 @@ Json::JToken Html2JToken(std::shared_ptr<Html::HtmlNode> node) {
} else { } else {
Json::JObject obj; Json::JObject obj;
obj.SetValue("Tag", node->text_or_tag); obj.SetValue("Tag", node->text_or_tag);
Json::JArray attrs; Json::JObject attrs;
for (auto &item : node->attributes) { for (auto &item : node->attributes) {
if (item.second) if (item.second) {
attrs.Add(Json::JObject( attrs.SetValue(item.first, *item.second);
{{"Key", item.first}, {"Value", item.second.value()}})); } else {
else attrs.SetValue(item.first, nullptr);
attrs.Add( }
Json::JObject({{"Key", item.first}, {"Value", nullptr}}));
} }
obj.SetValue("Attributes", attrs); obj.SetValue("Attributes", attrs);
+7 -7
View File
@@ -58,13 +58,13 @@ int main(int argc, char **argv) {
html->FindNodes("meta", metaTags); html->FindNodes("meta", metaTags);
for (auto &meta : metaTags) { for (auto &meta : metaTags) {
std::optional<std::string> prop, content; auto prop_off = meta->attributes.find("property");
for (auto &[k, v] : meta->attributes) { auto content_off = meta->attributes.find("content");
if (k == "property" && v) if (!(prop_off != meta->attributes.end() &&
prop = *v; content_off != meta->attributes.end()))
if (k == "content" && v) continue;
content = *v; auto &prop = prop_off->second;
} auto &content = content_off->second;
if (prop && content && prop->find("og:") == 0) { if (prop && content && prop->find("og:") == 0) {
Tesses::Framework::Console::WriteView(prop.value()); Tesses::Framework::Console::WriteView(prop.value());
+3
View File
@@ -1,5 +1,8 @@
# Changelog # Changelog
## 0.0.9
Make html parser use case insensitive unordered_map for attributes
## 0.0.8 ## 0.0.8
Fixed a stoll bug that was there since day one, html parser, fix pkgconfig Fixed a stoll bug that was there since day one, html parser, fix pkgconfig
+1 -1
View File
@@ -1,3 +1,3 @@
set(TESSESFRAMEWORK_MAJOR_VERSION 0) set(TESSESFRAMEWORK_MAJOR_VERSION 0)
set(TESSESFRAMEWORK_MINOR_VERSION 0) set(TESSESFRAMEWORK_MINOR_VERSION 0)
set(TESSESFRAMEWORK_PATCH_VERSION 8) set(TESSESFRAMEWORK_PATCH_VERSION 9)
+21 -2
View File
@@ -21,13 +21,31 @@
#pragma once #pragma once
#include "../TextStreams/TextReader.hpp" #include "../TextStreams/TextReader.hpp"
#include <algorithm>
namespace Tesses::Framework::Serialization::Html { namespace Tesses::Framework::Serialization::Html {
struct CaseInsensitiveHash {
size_t operator()(const std::string &s) const {
std::string lower = s;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
return std::hash<std::string>{}(lower);
}
};
struct CaseInsensitiveEqual {
bool operator()(const std::string &a, const std::string &b) const {
return strcasecmp(a.c_str(), b.c_str()) == 0;
}
};
struct HtmlNode; struct HtmlNode;
struct HtmlNode : public std::enable_shared_from_this<HtmlNode> { struct HtmlNode : public std::enable_shared_from_this<HtmlNode> {
std::string text_or_tag; std::string text_or_tag;
std::vector<std::pair<std::string, std::optional<std::string>>> attributes; std::unordered_map<std::string, std::optional<std::string>,
CaseInsensitiveHash, CaseInsensitiveEqual>
attributes;
std::vector<std::shared_ptr<HtmlNode>> children; std::vector<std::shared_ptr<HtmlNode>> children;
bool isText = false; bool isText = false;
@@ -60,7 +78,8 @@ struct HtmlNode : public std::enable_shared_from_this<HtmlNode> {
auto node = std::make_shared<HtmlNode>(); auto node = std::make_shared<HtmlNode>();
node->isText = false; node->isText = false;
node->text_or_tag = std::string{name}; node->text_or_tag = std::string{name};
node->attributes.insert(node->attributes.end(), attrBegin, attrEnd); for (auto it = attrBegin; it != attrEnd; ++it)
node->attributes[it->first] = it->second;
node->children.insert(node->children.end(), nodesBegin, nodesEnd); node->children.insert(node->children.end(), nodesBegin, nodesEnd);
return node; return node;
} }
+4 -5
View File
@@ -360,16 +360,15 @@ struct HtmlParser {
if (v.type == if (v.type ==
HtmlLexTokenType::STRING) { HtmlLexTokenType::STRING) {
Read(); Read();
newNode->attributes.emplace_back( newNode->attributes[k.text] =
k.text,
Http::HttpUtils::HtmlDecode( Http::HttpUtils::HtmlDecode(
v.text)); v.text);
continue; continue;
} }
} }
} }
newNode->attributes.emplace_back(k.text, newNode->attributes[k.text] = std::nullopt;
std::nullopt);
continue; continue;
} }