1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 22:45:44 +00:00

Abandon du HTML Agility Pack ( pour l'instant ;) )

This commit is contained in:
Yan Maniez 2018-06-05 00:00:53 +02:00
parent 2ba11fc99f
commit 20b1366e92
15 changed files with 1275 additions and 1273 deletions

View file

@ -45,7 +45,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.8.4" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="1.60.1" />
<PackageReference Include="Xamarin.Forms" Version="3.0.0.482510" />
</ItemGroup>

View file

@ -1,7 +1,7 @@
using AideDeJeuLib.Spells;
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace AideDeJeuLib.Cards
{
@ -66,14 +66,15 @@ namespace AideDeJeuLib.Cards
return texts.ToArray();
}
public static CardContent[] ToContents(HtmlNode description)
public static CardContent[] ToContents(XmlNode description)
{
var contents = new List<CardContent>();
string currentText = "";
foreach (var content in description.ChildNodes)
foreach (var contentt in description.ChildNodes)
{
var content = contentt as XmlNode;
//Debug.WriteLine(content.NodeType + " " + content.Name + " " + content.InnerText);
if ((content.NodeType == HtmlNodeType.Element || content.NodeType == HtmlNodeType.Document) && content.Name == "strong")
if ((content.NodeType == XmlNodeType.Element || content.NodeType == XmlNodeType.Document) && content.Name == "strong")
{
if (currentText.Length > 0)
{
@ -82,11 +83,11 @@ namespace AideDeJeuLib.Cards
}
contents.Add(new SectionCardContent(content.InnerText));
}
else if ((content.NodeType == HtmlNodeType.Element || content.NodeType == HtmlNodeType.Document) && content.Name == "em")
else if ((content.NodeType == XmlNodeType.Element || content.NodeType == XmlNodeType.Document) && content.Name == "em")
{
currentText += "<em>" + content.InnerText + "</em>";
}
else if (content.NodeType == HtmlNodeType.Text)
else if (content.NodeType == XmlNodeType.Text)
{
var texts = SplitText(content.InnerText);
for (int i = 0; i < texts.Length - 1; i++)
@ -143,7 +144,7 @@ namespace AideDeJeuLib.Cards
//new FillCardContent(1),
//new TextCardContent(spell.DescriptionText),
});
var description = ToContents(spell.DescriptionDiv.Element("div"));
var description = ToContents(spell.DescriptionDiv.SelectSingleNode("div"));
foreach (var line in description)
{
int size = contents.Sum(cc => cc.Height);

View file

@ -1,5 +1,5 @@
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Xml;
namespace AideDeJeuLib
{
@ -13,23 +13,23 @@ namespace AideDeJeuLib
public string NamePHB { get; set; }
public string Html { get; set; }
public static IEnumerable<string> NodeListToStringList(IEnumerable<HtmlNode> nodes)
public static IEnumerable<string> NodeListToStringList(IEnumerable<XmlNode> nodes)
{
if (nodes == null) return null;
var strings = new List<string>();
foreach (var node in nodes)
{
strings.Add(node.OuterHtml);
strings.Add(node.OuterXml);
}
return strings;
}
public static HtmlNode StringToNode(string str)
public static XmlNode StringToNode(string str)
{
if (str == null) return null;
var doc = new HtmlDocument();
doc.LoadHtml(str);
return doc.DocumentNode;
var doc = new XmlDocument();
doc.LoadXml(str);
return doc.DocumentElement;
}
//public static IEnumerable<HtmlNode> StringListToNodeList(IEnumerable<string> strings)

View file

@ -1,8 +1,8 @@
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Xml;
namespace AideDeJeuLib.Monsters
{
@ -52,7 +52,7 @@ namespace AideDeJeuLib.Monsters
public IEnumerable<string> SpecialFeatures { get; set; }
[IgnoreDataMember]
public IEnumerable<HtmlNode> SpecialFeaturesNodes
public IEnumerable<XmlNode> SpecialFeaturesNodes
{
set
{
@ -62,7 +62,7 @@ namespace AideDeJeuLib.Monsters
public IEnumerable<string> Actions { get; set; }
[IgnoreDataMember]
public IEnumerable<HtmlNode> ActionsNodes
public IEnumerable<XmlNode> ActionsNodes
{
set
{
@ -72,7 +72,7 @@ namespace AideDeJeuLib.Monsters
public IEnumerable<string> LegendaryActions { get; set; }
[IgnoreDataMember]
public IEnumerable<HtmlNode> LegendaryActionsNodes
public IEnumerable<XmlNode> LegendaryActionsNodes
{
set
{
@ -83,15 +83,15 @@ namespace AideDeJeuLib.Monsters
public void ParseHtml()
{
var pack = new HtmlDocument();
pack.LoadHtml(this.Html);
var divSpell = pack.DocumentNode.SelectNodes("//div[contains(@class,'bloc')]").FirstOrDefault();
var pack = new XmlDocument();
pack.LoadXml(this.Html);
var divSpell = pack.DocumentElement.SelectSingleNode("//div[contains(@class,'bloc')]");
ParseNode(divSpell);
}
//public static Monster FromHtml(HtmlNode divBloc)
//{
public void ParseNode(HtmlNode divBloc)
public void ParseNode(XmlNode divBloc)
{
//monster.Html = divBloc.OuterHtml;
var divMonster = divBloc?.SelectSingleNode("div[contains(@class,'monstre')]");
@ -99,7 +99,7 @@ namespace AideDeJeuLib.Monsters
var divTrad = divMonster.SelectSingleNode("div[@class='trad']");
var linkVO = divTrad.SelectSingleNode("a").GetAttributeValue("href", "");
var linkVO = divTrad.SelectSingleNode("a").Attributes["href"].InnerText;
var matchIdVF = new Regex(@"\?vf=(?<idvf>.*)").Match(linkVO);
this.IdVF = matchIdVF?.Groups["idvf"]?.Value;
var matchIdVO = new Regex(@"\?vo=(?<idvo>.*)").Match(linkVO);
@ -160,24 +160,24 @@ namespace AideDeJeuLib.Monsters
this.Languages = divRed?.SelectSingleNode("strong[contains(text(),'Langues') or contains(text(),'Languages')]")?.NextSibling?.InnerText;
this.Challenge = divRed?.SelectSingleNode("strong[contains(text(),'Puissance') or contains(text(),'Challenge')]")?.NextSibling?.InnerText;
List<HtmlNode> nodes = new List<HtmlNode>();
List<HtmlNode> specialFeatures = null;
List<HtmlNode> actions = null;
List<HtmlNode> legendaryActions = null;
List<XmlNode> nodes = new List<XmlNode>();
List<XmlNode> specialFeatures = null;
List<XmlNode> actions = null;
List<XmlNode> legendaryActions = null;
var node = divSansSerif.SelectSingleNode("p");
while(node != null)
{
if(node.NodeType == HtmlNodeType.Element && node.Name == "div")
if(node.NodeType == XmlNodeType.Element && node.Name == "div")
{
if(node.InnerText == "ACTIONS")
{
specialFeatures = nodes;
nodes = new List<HtmlNode>();
nodes = new List<XmlNode>();
}
else if (node.InnerText == "ACTIONS LÉGENDAIRES" || node.InnerText == "LEGENDARY ACTIONS")
{
actions = nodes;
nodes = new List<HtmlNode>();
nodes = new List<XmlNode>();
}
}
else
@ -213,13 +213,13 @@ namespace AideDeJeuLib.Monsters
this.Source = divSource?.InnerText;
var img = divBloc?.SelectSingleNode("div[contains(@class,'center')]/img[contains(@class,'picture')]");
this.Picture = img?.GetAttributeValue("src", null);
this.Picture = img?.Attributes["src"].InnerText;
}
public static Monster FromHtml(HtmlNode node)
public static Monster FromHtml(XmlNode node)
{
var monster = new Monster();
monster.Html = node.OuterHtml;
monster.Html = node.OuterXml;
monster.ParseNode(node);
return monster;
}

View file

@ -1,11 +1,11 @@
using HtmlAgilityPack;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
namespace AideDeJeuLib.Monsters
{
@ -78,22 +78,23 @@ namespace AideDeJeuLib.Monsters
var url = string.Format($"https://www.aidedd.org/dnd-filters/monstres.php?c={category}&t={type}&min={minPower}&max={maxPower}&sz={size}&lg={legendary}&s={source}", category, type, minPower, maxPower, size, legendary, source);
html = await client.GetStringAsync(url);
var pack = new HtmlDocument();
pack.LoadHtml(html);
var pack = new XmlDocument();
pack.LoadXml(html);
//var trs = pack.GetElementbyId("liste").Element("table").Elements("tr").ToList();
var trs = pack.DocumentNode.SelectSingleNode("//table[contains(@class,'liste')]").Elements("tr").ToList();
var trs = pack.DocumentElement.SelectSingleNode("//table[contains(@class,'liste')]").SelectNodes("tr");
var monsters = new List<Monster>();
foreach (var tr in trs)
foreach (var tro in trs)
{
var tds = tr.Elements("td").ToArray();
if (tds.Length > 0)
var tr = tro as XmlNode;
var tds = tr.SelectNodes("td");
if (tds.Count > 0)
{
var monster = new Monster();
var aname = tds[1].Element("a");
var spanname = aname.Element("span");
var aname = tds[1].SelectSingleNode("a");
var spanname = aname.SelectSingleNode("span");
if (spanname != null)
{
monster.NamePHB = spanname.GetAttributeValue("title", "");
monster.NamePHB = spanname.Attributes["title"].InnerText;
monster.Name = spanname.InnerText;
}
else
@ -103,7 +104,7 @@ namespace AideDeJeuLib.Monsters
}
//monster.Name = tds[0].InnerText;
var href = aname.GetAttributeValue("href", "");
var href = aname.Attributes["href"].InnerText;
var regex = new Regex("vf=(?<id>.*)");
monster.Id = regex.Match(href).Groups["id"].Value;
monster.Power = tds[2].InnerText;
@ -125,9 +126,9 @@ namespace AideDeJeuLib.Monsters
html = await client.GetStringAsync(string.Format($"https://www.aidedd.org/dnd/monstres.php?vf={id}", id));
var pack = new HtmlDocument();
pack.LoadHtml(html);
var divBloc = pack.DocumentNode.SelectNodes("//div[contains(@class,'bloc')]").FirstOrDefault();
var pack = new XmlDocument();
pack.LoadXml(html);
var divBloc = pack.DocumentElement.SelectSingleNode("//div[contains(@class,'bloc')]");
var monster = Monster.FromHtml(divBloc);
monster.Id = id;
return monster;

View file

@ -1,5 +1,4 @@
using HtmlAgilityPack;
using System;
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
@ -28,24 +27,24 @@ namespace AideDeJeuLib.Spells
}
}
[IgnoreDataMember]
public HtmlNode DescriptionDiv
public XmlNode DescriptionDiv
{
get
{
if(DescriptionHtml != null)
{
//XmlDocument xdoc = new XmlDocument();
//xdoc.LoadXml(DescriptionHtml);
//return xdoc.DocumentElement;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(DescriptionHtml);
return doc.DocumentNode;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(DescriptionHtml);
return xdoc.DocumentElement;
//HtmlDocument doc = new HtmlDocument() { OptionOutputAsXml = true };
//doc.LoadHtml(DescriptionHtml);
//return doc.DocumentNode;
}
return null;
}
set
{
DescriptionHtml = value?.OuterHtml;
DescriptionHtml = value?.OuterXml;
}
}
@ -55,18 +54,18 @@ namespace AideDeJeuLib.Spells
public void ParseHtml()
{
var pack = new HtmlDocument();
pack.LoadHtml(this.Html);
var divSpell = pack.DocumentNode.SelectNodes("//div[contains(@class,'bloc')]").FirstOrDefault();
var pack = new XmlDocument();
pack.LoadXml(this.Html);
var divSpell = pack.DocumentElement.SelectSingleNode("//div[contains(@class,'bloc')]");
ParseNode(divSpell);
}
public void ParseNode(HtmlNode nodeSpell)
public void ParseNode(XmlNode nodeSpell)
{
this.Name = nodeSpell.SelectSingleNode("h1").InnerText;
var divTrad = nodeSpell.SelectSingleNode("div[@class='trad']");
var linkVO = divTrad.SelectSingleNode("a").GetAttributeValue("href", "");
var linkVO = divTrad.SelectSingleNode("a").Attributes["href"].InnerText;
var matchIdVF = new Regex(@"\?vf=(?<idvf>.*)").Match(linkVO);
this.IdVF = matchIdVF?.Groups["idvf"]?.Value;
var matchIdVO = new Regex(@"\?vo=(?<idvo>.*)").Match(linkVO);
@ -95,10 +94,10 @@ namespace AideDeJeuLib.Spells
this.Source = nodeSpell.SelectSingleNode("div[@class='source']").InnerText;
}
public static Spell FromHtml(HtmlNode nodeSpell)
public static Spell FromHtml(XmlNode nodeSpell)
{
var spell = new Spell();
spell.Html = nodeSpell.OuterHtml;
spell.Html = nodeSpell.OuterXml;
spell.ParseNode(nodeSpell);
return spell;
}

View file

@ -1,10 +1,11 @@
using HtmlAgilityPack;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace AideDeJeuLib.Spells
{
@ -76,22 +77,23 @@ namespace AideDeJeuLib.Spells
var url = string.Format("https://www.aidedd.org/dnd-filters/sorts.php?c={0}&min={1}&max={2}&e={3}&r={4}&s={5}", classe, niveauMin, niveauMax, ecole, rituel, source);
html = await client.GetStringAsync(url);
var pack = new HtmlDocument();
pack.LoadHtml(html);
var pack = new XmlDocument();
pack.LoadXml(html);
//var tdssort = pack.GetElementbyId("liste").Element("table").Elements("tr").ToList();
var tdssort = pack.DocumentNode.SelectSingleNode("//table[contains(@class,'liste')]").Elements("tr").ToList();
var tdssort = pack.DocumentElement.SelectSingleNode("//table[contains(@class,'liste')]").SelectNodes("tr");
var spells = new List<Spell>();
foreach (var tdsort in tdssort)
foreach (var tdsortt in tdssort)
{
var thssort = tdsort.Elements("td").ToArray();
if (thssort.Length > 0)
var tdsort = tdsortt as XmlNode;
var thssort = tdsort.SelectNodes("td");
if (thssort.Count > 0)
{
Spell spell = new Spell();
var aname = thssort[1].Element("a");
var spanname = aname.Element("span");
var aname = thssort[1].SelectSingleNode("a");
var spanname = aname.SelectSingleNode("span");
if(spanname != null)
{
spell.NamePHB = spanname.GetAttributeValue("title", "");
spell.NamePHB = spanname.Attributes["title"].InnerText;
spell.Name = spanname.InnerText;
}
else
@ -99,7 +101,7 @@ namespace AideDeJeuLib.Spells
spell.NamePHB = aname.InnerText;
spell.Name = aname.InnerText;
}
var href = aname.GetAttributeValue("href", "");
var href = aname.Attributes["href"].InnerText;
var regex = new Regex("vf=(?<id>.*)");
spell.Id = regex.Match(href).Groups["id"].Value;
@ -123,9 +125,9 @@ namespace AideDeJeuLib.Spells
html = await client.GetStringAsync(string.Format("https://www.aidedd.org/dnd/sorts.php?vf={0}", id));
var pack = new HtmlDocument();
pack.LoadHtml(html);
var divSpell = pack.DocumentNode.SelectNodes("//div[contains(@class,'bloc')]").FirstOrDefault();
var pack = new XmlDocument();
pack.LoadXml(html);
var divSpell = pack.DocumentElement.SelectSingleNode("//div[contains(@class,'bloc')]");
var spell = Spell.FromHtml(divSpell);
spell.Id = id;
return spell;
@ -141,9 +143,9 @@ namespace AideDeJeuLib.Spells
html = await client.GetStringAsync(string.Format("https://www.aidedd.org/adj/livre-sorts/?c={0}&min={1}&max={2}", classe, niveauMin, niveauMax));
var pack = new HtmlDocument();
pack.LoadHtml(html);
return pack.DocumentNode.SelectNodes("//input[@name='select_sorts[]']").Select(node => node.GetAttributeValue("value", ""));
var pack = new XmlDocument();
pack.LoadXml(html);
return pack.DocumentElement.SelectNodes("//input[@name='select_sorts[]']").Cast<XmlNode>().Select(node => node.Attributes["value"].InnerText);
}
public async Task<IEnumerable<Spell>> GetSpells(IEnumerable<string> spellIds)
@ -159,14 +161,14 @@ namespace AideDeJeuLib.Spells
var response = await client.PostAsync("http://www.aidedd.org/dnd/sorts.php", content);
html = await response.Content.ReadAsStringAsync();
var pack = new HtmlDocument();
pack.LoadHtml(html);
var pack = new XmlDocument();
pack.LoadXml(html);
var newSpells = new List<Spell>();
var spells = pack.DocumentNode.SelectNodes("//div[contains(@class,'blocCarte')]").ToList();
var spells = pack.DocumentElement.SelectNodes("//div[contains(@class,'blocCarte')]");
foreach (var spell in spells)
{
//var newSpell = new Spell();
var newSpell = Spell.FromHtml(spell);
var newSpell = Spell.FromHtml(spell as XmlNode);
//newSpell.Name = spell.SelectSingleNode("h1").InnerText;
//newSpell.NameVO = spell.SelectSingleNode("div[@class='trad']").InnerText;
//newSpell.LevelType = spell.SelectSingleNode("h2/em").InnerText;

View file

@ -1,9 +1,9 @@
using AideDeJeu.ViewModels;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
using Xamarin.Forms;
namespace AideDeJeu.Tools
@ -41,11 +41,11 @@ namespace AideDeJeu.Tools
var str = value as string;
if (str != null)
{
var doc = new HtmlDocument();
doc.LoadHtml(str);
var doc = new XmlDocument();
doc.LoadXml("<div>" + str + "</div>");
var fs = new FormattedString();
FormatedTextHelpers.HtmlNodeToFormatedString(doc.DocumentNode, fs);
FormatedTextHelpers.HtmlNodeToFormatedString(doc.DocumentElement, fs);
return fs;
}
else
@ -70,10 +70,10 @@ namespace AideDeJeu.Tools
var fs = new FormattedString();
foreach (var str in strings)
{
var doc = new HtmlDocument();
doc.LoadHtml(str);
var doc = new XmlDocument();
doc.LoadXml("<div>" + str + "</div>");
FormatedTextHelpers.HtmlNodeToFormatedString(doc.DocumentNode, fs);
FormatedTextHelpers.HtmlNodeToFormatedString(doc.DocumentElement, fs);
fs.Spans.Add(new Span() { Text = "\r\n" });
}
return fs;

View file

@ -1,8 +1,8 @@
using HtmlAgilityPack;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
@ -10,20 +10,21 @@ namespace AideDeJeu.Tools
{
public static class FormatedTextHelpers
{
public static void HtmlNodesToFormatedString(HtmlNodeCollection nodes, FormattedString fs, FontAttributes attributes = FontAttributes.None)
public static void HtmlNodesToFormatedString(XmlNodeList nodes, FormattedString fs, FontAttributes attributes = FontAttributes.None)
{
if (nodes != null)
{
foreach (var node in nodes)
foreach (var nodee in nodes)
{
var node = nodee as XmlNode;
HtmlNodeToFormatedString(node, fs, attributes);
}
}
}
public static void HtmlNodeToFormatedString(HtmlNode node, FormattedString fs, FontAttributes attributes = FontAttributes.None)
public static void HtmlNodeToFormatedString(XmlNode node, FormattedString fs, FontAttributes attributes = FontAttributes.None)
{
if (node.NodeType == HtmlNodeType.Text)
if (node.NodeType == XmlNodeType.Text)
{
var resname = "content";
if (attributes.HasFlag(FontAttributes.Bold))
@ -37,23 +38,23 @@ namespace AideDeJeu.Tools
var fd = FontData.FromResource(resname);
fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText });
}
else if (node.NodeType == HtmlNodeType.Element && node.Name == "br")
else if (node.NodeType == XmlNodeType.Element && node.Name == "br")
{
fs.Spans.Add(new Span() { Text = "\r\n" });
}
else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong")
else if (node.NodeType == XmlNodeType.Element && node.Name == "strong")
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Bold);
}
else if (node.NodeType == HtmlNodeType.Element && node.Name == "em")
else if (node.NodeType == XmlNodeType.Element && node.Name == "em")
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes | FontAttributes.Italic);
}
else if (node.NodeType == HtmlNodeType.Element)
else if (node.NodeType == XmlNodeType.Element)
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
}
else if (node.NodeType == HtmlNodeType.Document)
else if (node.NodeType == XmlNodeType.Document)
{
HtmlNodesToFormatedString(node.ChildNodes, fs, attributes);
}

View file

@ -1,7 +1,6 @@
using AideDeJeu.Services;
using AideDeJeuLib.Monsters;
using AideDeJeuLib.Spells;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Globalization;
@ -74,7 +73,7 @@ namespace AideDeJeuCmd
spellsVF.ForEach(sp => sp.Html = null);
spellsVO.ForEach(sp => sp.Html = null);
spellsVF.ForEach(sp => sp.DescriptionDiv = sp.DescriptionDiv);
spellsVO.ForEach(sp => sp.Html = null);
spellsVO.ForEach(sp => sp.DescriptionDiv = sp.DescriptionDiv);
monstersVF.ForEach(it => it.Html = null);
monstersVO.ForEach(it => it.Html = null);

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long