mirror of
https://github.com/SaphireLattice/quest-reader.git
synced 2026-11-12 18:40:36 -07:00
Get parsing working, and HTML generation from resulting objects
This commit is contained in:
9 files changed
+171
-146
No files matched your search
+30
-30
@@ -9,12 +9,16 @@ public class PostsSource
|
||||
{
|
||||
public List<ThreadPost> Posts { get; set; }
|
||||
|
||||
public List<ThreadPost> Accepted { get; set; }
|
||||
public HashSet<ThreadPost> Accepted { get; set; }
|
||||
|
||||
public Metadata Metadata { get; set; }
|
||||
|
||||
public PostsSource(string questName, string basePath)
|
||||
public string BasePath { get; set; }
|
||||
|
||||
public PostsSource(string questName)
|
||||
{
|
||||
BasePath = $"quests/{questName}";
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
@@ -22,45 +26,41 @@ public class PostsSource
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
using var fileStream = File.OpenRead(Path.Combine(basePath, "metadata.json"));
|
||||
using var fileStream = File.OpenRead(Path.Combine(BasePath, "metadata.json"));
|
||||
Metadata = JsonSerializer.Deserialize<Metadata>(fileStream, options)
|
||||
?? throw new InvalidDataException("Empty deserialisation result for quest metadata");
|
||||
fileStream.Dispose();
|
||||
|
||||
Console.Out.WriteLine($"Loaded metadata: {Metadata}");
|
||||
Posts = Metadata.Threads.SelectMany(tId =>
|
||||
{
|
||||
using var fileStream = File.OpenRead(Path.Combine(basePath, $"thread_{tId}.json"));
|
||||
var threadData = JsonSerializer.Deserialize<List<ThreadPost>>(fileStream, options)
|
||||
?? throw new InvalidDataException("Empty deserialisation result for thread data");
|
||||
fileStream.Dispose();
|
||||
Posts = Metadata.Threads
|
||||
.SelectMany(tId => QuestdenParse.GetThread(tId, BasePath).Result)
|
||||
.ToList();
|
||||
|
||||
return threadData;
|
||||
}).ToList();
|
||||
|
||||
using var postsListStream = File.OpenRead(Path.Combine(basePath, "accepted.json"));
|
||||
using var postsListStream = File.OpenRead(Path.Combine(BasePath, "accepted.json"));
|
||||
var ids = JsonSerializer.Deserialize<List<int>>(postsListStream, options)
|
||||
?? throw new InvalidDataException("Empty deserialisation result for quest metadata");
|
||||
Accepted = Posts.Where(p => ids.Contains(p.Id)).ToList();
|
||||
Accepted = Posts.Where(p => ids.Contains(p.Id)).ToHashSet();
|
||||
|
||||
foreach (var chapter in Metadata.Chapters)
|
||||
{
|
||||
var post = Accepted.Single(p => p.Id == (chapter.Announce ?? chapter.Start));
|
||||
post.IsChapterAnnounce = true;
|
||||
post.Chapter = chapter;
|
||||
}
|
||||
|
||||
Console.Out.WriteLine($"Loaded a list of {Accepted.Count} posts, referencing {Accepted.Where(a => a.File is not null).Count()} files");
|
||||
|
||||
var rx = new Regex(@"data-post-ref=""(\d+)""",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
foreach (var post in Posts)
|
||||
foreach (var post in Accepted)
|
||||
{
|
||||
var matches = rx.Matches(post.RawHtml);
|
||||
if (!matches.Any())
|
||||
continue;
|
||||
|
||||
post.RepliesTo = new List<int>();
|
||||
foreach (Match match in matches)
|
||||
{
|
||||
var replyId = int.Parse(match.Groups[1].Value);
|
||||
var found = Posts.FirstOrDefault(p => p.Id == replyId);
|
||||
if (found is null)
|
||||
continue;
|
||||
post.RepliesTo.Add(replyId);
|
||||
}
|
||||
post.AuthorPost = true;
|
||||
if (post.ParsedContent is null || post.ParsedContent.Version < QuestdenParse.LatestCompatibleVersion)
|
||||
throw new NotImplementedException("Repairing missing post content or updating it is not implemented yet");
|
||||
}
|
||||
|
||||
var referenced = Accepted.SelectMany(p => p.ParsedContent!.GetReferences());
|
||||
Accepted.UnionWith(Posts.Where(p => referenced.Contains(p.Id)));
|
||||
Accepted = Accepted.OrderBy(p => p.Id).ToHashSet();
|
||||
|
||||
Console.Out.WriteLine($"Done loading with {Accepted.Count} posts, referencing {Accepted.Where(a => a.File is not null).Count()} files");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user