improve wiki parsing (#532)

This commit is contained in:
Jesse Plamondon-Willard 2018-05-26 15:21:07 -04:00
parent 03860cca86
commit 738c0ce386
2 changed files with 29 additions and 3 deletions

View File

@ -91,19 +91,25 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki
} }
// parse other fields // parse other fields
string name = node.Descendants("td").FirstOrDefault()?.InnerText?.Trim();
string[] ids = this.GetAttribute(node, "data-id")?.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray() ?? new string[0];
int? nexusID = this.GetNullableIntAttribute(node, "data-nexus-id"); int? nexusID = this.GetNullableIntAttribute(node, "data-nexus-id");
int? chucklefishID = this.GetNullableIntAttribute(node, "data-chucklefish-id"); int? chucklefishID = this.GetNullableIntAttribute(node, "data-chucklefish-id");
string githubRepo = node.GetAttributeValue("data-github", null); string githubRepo = this.GetAttribute(node, "data-github");
string customSourceUrl = node.GetAttributeValue("data-custom-source", null); string customSourceUrl = this.GetAttribute(node, "data-custom-source");
string customUrl = this.GetAttribute(node, "data-custom-url");
// yield model // yield model
yield return new WikiCompatibilityEntry yield return new WikiCompatibilityEntry
{ {
ID = ids,
Name = name,
Status = status, Status = status,
NexusID = nexusID, NexusID = nexusID,
ChucklefishID = chucklefishID, ChucklefishID = chucklefishID,
GitHubRepo = githubRepo, GitHubRepo = githubRepo,
CustomSourceUrl = customSourceUrl, CustomSourceUrl = customSourceUrl,
CustomUrl = customUrl,
UnofficialVersion = unofficialVersion UnofficialVersion = unofficialVersion
}; };
} }
@ -114,12 +120,23 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki
/// <param name="attributeName">The attribute name.</param> /// <param name="attributeName">The attribute name.</param>
private int? GetNullableIntAttribute(HtmlNode node, string attributeName) private int? GetNullableIntAttribute(HtmlNode node, string attributeName)
{ {
string raw = node.GetAttributeValue(attributeName, null); string raw = this.GetAttribute(node, attributeName);
if (raw != null && int.TryParse(raw, out int value)) if (raw != null && int.TryParse(raw, out int value))
return value; return value;
return null; return null;
} }
/// <summary>Get a strings attribute value.</summary>
/// <param name="node">The HTML node.</param>
/// <param name="attributeName">The attribute name.</param>
private string GetAttribute(HtmlNode node, string attributeName)
{
string raw = node.GetAttributeValue(attributeName, null);
if (raw != null)
raw = HtmlEntity.DeEntitize(raw);
return raw;
}
/// <summary>The response model for the MediaWiki parse API.</summary> /// <summary>The response model for the MediaWiki parse API.</summary>
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")] [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")] [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]

View File

@ -3,6 +3,12 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki
/// <summary>An entry in the mod compatibility list.</summary> /// <summary>An entry in the mod compatibility list.</summary>
public class WikiCompatibilityEntry public class WikiCompatibilityEntry
{ {
/// <summary>The mod's unique ID. A mod may have multiple current IDs in rare cases (e.g. due to parallel releases or unofficial updates).</summary>
public string[] ID { get; set; }
/// <summary>The mod's display name.</summary>
public string Name { get; set; }
/// <summary>The mod ID on Nexus.</summary> /// <summary>The mod ID on Nexus.</summary>
public int? NexusID { get; set; } public int? NexusID { get; set; }
@ -15,6 +21,9 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki
/// <summary>The URL to a non-GitHub source repo.</summary> /// <summary>The URL to a non-GitHub source repo.</summary>
public string CustomSourceUrl { get; set; } public string CustomSourceUrl { get; set; }
/// <summary>The custom mod page URL (if applicable).</summary>
public string CustomUrl { get; set; }
/// <summary>The version of the latest unofficial update, if applicable.</summary> /// <summary>The version of the latest unofficial update, if applicable.</summary>
public ISemanticVersion UnofficialVersion { get; set; } public ISemanticVersion UnofficialVersion { get; set; }