Browse Source

Rewriting header

mj-deploy
Standa Lukeš 4 years ago
parent
commit
5b96b8fb75
  1. 6
      server/Ksp.WebServer/Controllers/GrafikPageController.cs
  2. 30
      server/Ksp.WebServer/KspPageRewriter.cs

6
server/Ksp.WebServer/Controllers/GrafikPageController.cs

@ -21,14 +21,17 @@ namespace Ksp.WebServer.Controllers
{
private readonly ILogger<TasksController> logger;
private readonly IWebHostEnvironment env;
private readonly KspPageRewriter pageRewriter;
private readonly KspProxyConfig kspProxyConfig;
public GrafikPageController(
ILogger<TasksController> logger,
IWebHostEnvironment env,
IOptions<KspProxyConfig> kspProxyConfig)
IOptions<KspProxyConfig> kspProxyConfig,
KspPageRewriter pageRewriter)
{
this.env = env;
this.pageRewriter = pageRewriter;
this.kspProxyConfig = kspProxyConfig.Value;
this.logger = logger;
}
@ -56,6 +59,7 @@ namespace Ksp.WebServer.Controllers
var grafik = p.ParseDocument(grafikPage);
var kspTemplate = p.ParseDocument(await FetchBlankPage());
pageRewriter.ModifyTree(kspTemplate, "grafik");
var innerBody = grafik.Body;
innerBody.Replace(kspTemplate.Body);

30
server/Ksp.WebServer/KspPageRewriter.cs

@ -1,6 +1,8 @@
using System.IO;
using AngleSharp.Html;
using Microsoft.AspNetCore.Http;
using System.Linq;
using AngleSharp.Html.Dom;
namespace Ksp.WebServer
{
@ -11,6 +13,15 @@ namespace Ksp.WebServer
var p = new AngleSharp.Html.Parser.HtmlParser();
var document = p.ParseDocument(source);
ModifyTree(document, context.Request.Path.Value.Trim('/'));
var outputHtml = new StringWriter();
document.ToHtml(outputHtml, new PrettyMarkupFormatter() { Indentation = "\t", NewLine = "\n" });
return outputHtml.ToString();
}
public void ModifyTree(IHtmlDocument document, string path)
{
foreach (var form in document.QuerySelectorAll("form"))
{
if (form.QuerySelector("input[type=password]") is null)
@ -22,11 +33,22 @@ namespace Ksp.WebServer
form.Prepend(warning);
}
var encyklopedie = document.QuerySelectorAll("#menu ul li:not(.active) a").FirstOrDefault(x => x.TextContent.Trim() == "Encyklopedie");
if (encyklopedie is object)
{
encyklopedie.TextContent = "Kurzy";
encyklopedie.SetAttribute("href", "/grafik");
if ("grafik" == path)
{
encyklopedie.ParentElement.ClassList.Add("active");
}
}
var outputHtml = new StringWriter();
document.ToHtml(outputHtml, new PrettyMarkupFormatter() { Indentation = "\t", NewLine = "\n" });
return outputHtml.ToString();
var logo = document.QuerySelector("#logo > h1:nth-child(1) > a:nth-child(1)");
if (logo is object)
{
logo.TextContent = "KSP Hacked Edition";
}
}
}
}