feat: introduce new composable data providers and increment version
- Added `AnchorDataProvider`, `AnchorCollectionDataProvider`, `ContentsDataProvider`, `ContentsArrayDataProvider`, `DropDownDataProvider`, `ListContentDataProvider`, and `ParagraphedContentDataProvider` for enhanced data extraction flexibility. - Updated project version to 2.5.0.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using HtmlAgilityPack;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.Marshalling;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Beam.Abstractions;
|
||||
|
||||
namespace Beam.Dynamic;
|
||||
|
||||
public class DropDownDataProvider : IComposableDataProvider<string>, IComposableDataProvider<string[]> {
|
||||
public IBinding? Content { get; set; }
|
||||
public Uri? RelativeTo { get; set; }
|
||||
|
||||
public string[] Get(HtmlDocument document) {
|
||||
if (Content is null)
|
||||
return [];
|
||||
var node = Select(document);
|
||||
if (node is null)
|
||||
return [];
|
||||
return Get(node);
|
||||
}
|
||||
|
||||
string IDataProvider<string>.Get(HtmlDocument document) {
|
||||
var node = Select(document);
|
||||
return node is null ? "" : (this as IComposableDataProvider<string>).Get(node);
|
||||
}
|
||||
|
||||
public string[] Get(HtmlNode node) {
|
||||
List<string> links = [];
|
||||
foreach (var child in node.ChildNodes.Where(x => x.Name == "option")) {
|
||||
var childValue = child.GetAttributeValue("value", null);
|
||||
if (!Uri.TryCreate(RelativeTo, childValue, out var uri))
|
||||
continue;
|
||||
links.Add(uri.AbsoluteUri);
|
||||
}
|
||||
|
||||
return links.ToArray();
|
||||
}
|
||||
|
||||
string IComposableDataProvider<string>.Get(HtmlNode node) {
|
||||
return JsonSerializer.Serialize(Get(node));
|
||||
}
|
||||
|
||||
public HtmlNode? Select(HtmlDocument doc) {
|
||||
return Content?.Select(doc);
|
||||
}
|
||||
|
||||
HtmlNode? IComposableDataProvider<string[]>.Select(HtmlNode node) {
|
||||
return node;
|
||||
}
|
||||
|
||||
HtmlNode? IComposableDataProvider<string>.Select(HtmlNode node) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user