mirror of
https://git.tesses.org/tesses50/tessesframework.git
synced 2026-09-30 08:44:13 +00:00
Compare commits
3
Commits
95442f8acc
...
e4ca34aa59
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4ca34aa59 | ||
|
|
9ddbfefa41 | ||
|
|
15f08f474f |
+6
-7
@@ -9,14 +9,13 @@ Json::JToken Html2JToken(std::shared_ptr<Html::HtmlNode> node) {
|
||||
} else {
|
||||
Json::JObject obj;
|
||||
obj.SetValue("Tag", node->text_or_tag);
|
||||
Json::JArray attrs;
|
||||
Json::JObject attrs;
|
||||
for (auto &item : node->attributes) {
|
||||
if (item.second)
|
||||
attrs.Add(Json::JObject(
|
||||
{{"Key", item.first}, {"Value", item.second.value()}}));
|
||||
else
|
||||
attrs.Add(
|
||||
Json::JObject({{"Key", item.first}, {"Value", nullptr}}));
|
||||
if (item.second) {
|
||||
attrs.SetValue(item.first, *item.second);
|
||||
} else {
|
||||
attrs.SetValue(item.first, nullptr);
|
||||
}
|
||||
}
|
||||
obj.SetValue("Attributes", attrs);
|
||||
|
||||
|
||||
+7
-7
@@ -58,13 +58,13 @@ int main(int argc, char **argv) {
|
||||
html->FindNodes("meta", metaTags);
|
||||
|
||||
for (auto &meta : metaTags) {
|
||||
std::optional<std::string> prop, content;
|
||||
for (auto &[k, v] : meta->attributes) {
|
||||
if (k == "property" && v)
|
||||
prop = *v;
|
||||
if (k == "content" && v)
|
||||
content = *v;
|
||||
}
|
||||
auto prop_off = meta->attributes.find("property");
|
||||
auto content_off = meta->attributes.find("content");
|
||||
if (!(prop_off != meta->attributes.end() &&
|
||||
content_off != meta->attributes.end()))
|
||||
continue;
|
||||
auto &prop = prop_off->second;
|
||||
auto &content = content_off->second;
|
||||
|
||||
if (prop && content && prop->find("og:") == 0) {
|
||||
Tesses::Framework::Console::WriteView(prop.value());
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## 0.0.9
|
||||
Make html parser use case insensitive unordered_map for attributes
|
||||
|
||||
## 0.0.8
|
||||
Fixed a stoll bug that was there since day one, html parser, fix pkgconfig
|
||||
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
set(TESSESFRAMEWORK_MAJOR_VERSION 0)
|
||||
set(TESSESFRAMEWORK_MINOR_VERSION 0)
|
||||
set(TESSESFRAMEWORK_PATCH_VERSION 8)
|
||||
set(TESSESFRAMEWORK_PATCH_VERSION 9)
|
||||
|
||||
@@ -21,13 +21,31 @@
|
||||
|
||||
#pragma once
|
||||
#include "../TextStreams/TextReader.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
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 : public std::enable_shared_from_this<HtmlNode> {
|
||||
|
||||
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;
|
||||
bool isText = false;
|
||||
|
||||
@@ -60,7 +78,8 @@ struct HtmlNode : public std::enable_shared_from_this<HtmlNode> {
|
||||
auto node = std::make_shared<HtmlNode>();
|
||||
node->isText = false;
|
||||
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);
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -360,16 +360,15 @@ struct HtmlParser {
|
||||
if (v.type ==
|
||||
HtmlLexTokenType::STRING) {
|
||||
Read();
|
||||
newNode->attributes.emplace_back(
|
||||
k.text,
|
||||
newNode->attributes[k.text] =
|
||||
Http::HttpUtils::HtmlDecode(
|
||||
v.text));
|
||||
v.text);
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
newNode->attributes.emplace_back(k.text,
|
||||
std::nullopt);
|
||||
newNode->attributes[k.text] = std::nullopt;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user