mirror of
https://github.com/SaphireLattice/quest-reader.git
synced 2026-11-12 18:40:36 -07:00
Use completely standalone Razor template and compiler
This commit is contained in:
8 files changed
+299
-90
No files matched your search
+18
-23
@@ -1,11 +1,10 @@
|
||||
namespace QuestReader.Services;
|
||||
|
||||
using System.Reflection;
|
||||
using RazorEngineCore;
|
||||
|
||||
public class Generator
|
||||
{
|
||||
public IRazorEngineCompiledTemplate<HtmlSafeTemplate<TemplateModel>> RazorTemplate { get; set; }
|
||||
public StandaloneTemplate<TemplateModel> RazorTemplate { get; set; }
|
||||
|
||||
public string QuestName { get; set; }
|
||||
|
||||
@@ -27,38 +26,34 @@ public class Generator
|
||||
p.Chapter = PostsSource.Metadata.Chapters.Single(c => (c.Announce ?? c.Start) == p.Id);
|
||||
});
|
||||
|
||||
var razorEngine = new RazorEngine();
|
||||
var razorEngine = new RazorStandalone<StandaloneTemplate<TemplateModel>>("QuestReader");
|
||||
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");
|
||||
}
|
||||
);
|
||||
RazorTemplate = razorEngine.Compile(
|
||||
"page_template.cshtml"
|
||||
) ?? throw new Exception("No template");
|
||||
|
||||
Console.WriteLine($"Using \"{templateFile}\" with base URL {baseUrl}");
|
||||
}
|
||||
|
||||
public string Run()
|
||||
{
|
||||
string result = RazorTemplate.Run(instance =>
|
||||
RazorTemplate.Model = new TemplateModel
|
||||
{
|
||||
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"
|
||||
};
|
||||
});
|
||||
Metadata = PostsSource.Metadata,
|
||||
Posts = PostsSource.Accepted,
|
||||
AllPosts = PostsSource.Posts,
|
||||
Now = @DateTime.UtcNow,
|
||||
BaseUrl = $"/static/{QuestName}",
|
||||
ToolVersion = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "unknown"
|
||||
};
|
||||
|
||||
var outputStream = new MemoryStream();
|
||||
RazorTemplate.ExecuteAsync(outputStream).Wait();
|
||||
|
||||
Console.WriteLine($"Template output {result.Length} bytes");
|
||||
var outputPath = Path.Join(QuestPath, "output.html");
|
||||
File.WriteAllText(outputPath, result);
|
||||
Console.WriteLine($"Template output {outputStream.Length} bytes");
|
||||
File.WriteAllBytes(outputPath, outputStream.ToArray());
|
||||
Console.WriteLine($"Wrote output to {outputPath}");
|
||||
return outputPath;
|
||||
}
|
||||
|
||||
+22
-1
@@ -2,6 +2,7 @@ namespace QuestReader.Services;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
public class PostsSource
|
||||
{
|
||||
@@ -16,7 +17,8 @@ public class PostsSource
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
using var fileStream = File.OpenRead(Path.Combine(basePath, "metadata.json"));
|
||||
@@ -39,5 +41,24 @@ public class PostsSource
|
||||
?? 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");
|
||||
|
||||
var rx = new Regex(@"data-post-ref=""(\d+)""",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
foreach (var post in Posts)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user