mirror of
https://github.com/SaphireLattice/quest-reader.git
synced 2026-11-12 18:40:36 -07:00
Rewrite generator pipeline
This commit is contained in:
8 files changed
+219
-420
No files matched your search
@@ -0,0 +1,65 @@
|
||||
namespace QuestReader.Services;
|
||||
|
||||
using System.Reflection;
|
||||
using RazorEngineCore;
|
||||
|
||||
public class Generator
|
||||
{
|
||||
public IRazorEngineCompiledTemplate<HtmlSafeTemplate<TemplateModel>> RazorTemplate { get; set; }
|
||||
|
||||
public string QuestName { get; set; }
|
||||
|
||||
public PostsSource PostsSource { get; set; }
|
||||
|
||||
public string QuestPath { get; set; }
|
||||
|
||||
public Generator(string questName)
|
||||
{
|
||||
QuestPath = $"quests/{questName}";
|
||||
|
||||
QuestName = questName;
|
||||
PostsSource = new PostsSource(questName, QuestPath);
|
||||
|
||||
var chapterAnnounces = PostsSource.Metadata.Chapters.Select(c => c.Announce ?? c.Start);
|
||||
|
||||
PostsSource.Accepted.Where(p => chapterAnnounces.Contains(p.Id)).ToList().ForEach(p => {
|
||||
p.IsChapterAnnounce = true;
|
||||
p.Chapter = PostsSource.Metadata.Chapters.Single(c => (c.Announce ?? c.Start) == p.Id);
|
||||
});
|
||||
|
||||
var razorEngine = new RazorEngine();
|
||||
var templateFile = "page_template.cshtml";
|
||||
var baseUrl = "";
|
||||
RazorTemplate = razorEngine.Compile<HtmlSafeTemplate<TemplateModel>>(
|
||||
File.ReadAllText("page_template.cshtml"),
|
||||
option => {
|
||||
option.AddAssemblyReferenceByName("System.Collections");
|
||||
option.AddAssemblyReferenceByName("System.Private.Uri");
|
||||
}
|
||||
);
|
||||
|
||||
Console.WriteLine($"Using \"{templateFile}\" with base URL {baseUrl}");
|
||||
}
|
||||
|
||||
public string Run()
|
||||
{
|
||||
string result = RazorTemplate.Run(instance =>
|
||||
{
|
||||
instance.Model = new TemplateModel
|
||||
{
|
||||
Metadata = PostsSource.Metadata,
|
||||
Posts = PostsSource.Accepted,
|
||||
Now = @DateTime.UtcNow,
|
||||
//BaseUrl = "assets"
|
||||
BaseUrl = $"/static/{QuestName}",
|
||||
ToolVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "unknown"
|
||||
};
|
||||
});
|
||||
|
||||
Console.WriteLine($"Template output {result.Length} bytes");
|
||||
var outputPath = Path.Join(QuestPath, "output.html");
|
||||
File.WriteAllText(outputPath, result);
|
||||
Console.WriteLine($"Wrote output to {outputPath}");
|
||||
return outputPath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
namespace QuestReader.Services;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class PostsSource
|
||||
{
|
||||
public List<ThreadPost> Posts { get; set; }
|
||||
|
||||
public List<ThreadPost> Accepted { get; set; }
|
||||
|
||||
public Metadata Metadata { get; set; }
|
||||
|
||||
public PostsSource(string questName, string basePath)
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
return threadData;
|
||||
}).ToList();
|
||||
|
||||
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();
|
||||
Console.Out.WriteLine($"Loaded a list of {Accepted.Count} posts, referencing {Accepted.Where(a => a.File is not null).Count()} files");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user