Skip to content

Commit

Permalink
Stah
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 20, 2023
1 parent 5af1f91 commit 935f7ab
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 30 deletions.
11 changes: 11 additions & 0 deletions API/Protocol/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public enum ContentType
/// </summary>
TextCss,

/// <summary>
/// A markdown file.
/// </summary>
TextMarkdown,

/// <summary>
/// A scriban file.
/// </summary>
TextScriban,

/// <summary>
/// A JavaScript source file.
/// </summary>
Expand Down Expand Up @@ -245,6 +255,7 @@ public class FlexibleContentType
{
{ ContentType.TextHtml, "text/html" },
{ ContentType.TextCss, "text/css" },
{ ContentType.TextMarkdown, "text/markdown" },
{ ContentType.ApplicationJavaScript, "application/javascript" },
{ ContentType.ImageIcon, "image/x-icon" },
{ ContentType.ImageGif, "image/gif" },
Expand Down
4 changes: 4 additions & 0 deletions Modules/AutoLayout/GenHTTP.Modules.AutoLayout.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@

<ProjectReference Include="..\Basics\GenHTTP.Modules.Basics.csproj" />
<ProjectReference Include="..\IO\GenHTTP.Modules.IO.csproj" />

<ProjectReference Include="..\Layouting\GenHTTP.Modules.Layouting.csproj" />

<ProjectReference Include="..\Markdown\GenHTTP.Modules.Markdown.csproj" />
<ProjectReference Include="..\Scriban\GenHTTP.Modules.Scriban.csproj" />

</ItemGroup>

Expand Down
17 changes: 17 additions & 0 deletions Modules/AutoLayout/IResourceHandlerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;
using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;

namespace GenHTTP.Modules.AutoLayout
{

public interface IResourceHandlerProvider
{

public bool Supports(IResource resource);

ValueTask<IHandlerBuilder> GetHandlerAsync(IResource resource);

}

}
18 changes: 18 additions & 0 deletions Modules/AutoLayout/Provider/DownloadProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Threading.Tasks;
using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;
using GenHTTP.Modules.IO;

namespace GenHTTP.Modules.AutoLayout.Provider
{

public class DownloadProvider : IResourceHandlerProvider
{

public bool Supports(IResource resource) => true;

public ValueTask<IHandlerBuilder> GetHandlerAsync(IResource resource) => new(Content.From(resource));

}

}
24 changes: 24 additions & 0 deletions Modules/AutoLayout/Provider/MarkdownProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Threading.Tasks;
using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;
using GenHTTP.Api.Protocol;
using GenHTTP.Modules.Basics;
using GenHTTP.Modules.Markdown;

namespace GenHTTP.Modules.AutoLayout.Provider
{

public class MarkdownProvider : IResourceHandlerProvider
{

public bool Supports(IResource resource) => (resource.ContentType?.KnownType ?? resource.Name?.GuessContentType()) == ContentType.TextMarkdown;

public ValueTask<IHandlerBuilder> GetHandlerAsync(IResource resource)
{
return new(ModMarkdown.Page(resource));
}

}

}
25 changes: 25 additions & 0 deletions Modules/AutoLayout/Provider/ScribanProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading.Tasks;

using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;
using GenHTTP.Api.Protocol;

using GenHTTP.Modules.Basics;
using GenHTTP.Modules.Scriban;

namespace GenHTTP.Modules.AutoLayout.Provider
{

public class ScribanProvider : IResourceHandlerProvider
{

public bool Supports(IResource resource) => (resource.ContentType?.KnownType ?? resource.Name?.GuessContentType()) == ContentType.TextScriban;

public ValueTask<IHandlerBuilder> GetHandlerAsync(IResource resource)
{
return new(ModScriban.Page(resource));
}

}

}
21 changes: 21 additions & 0 deletions Modules/AutoLayout/Resolvers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GenHTTP.Modules.AutoLayout.Provider;
using GenHTTP.Modules.AutoLayout.Scanning;

namespace GenHTTP.Modules.AutoLayout
{

public static class Resolvers
{

public static HandlerRegistryBuilder Default()
{
return new HandlerRegistryBuilder().Fallback(new DownloadProvider())
.Add(new MarkdownProvider())
.Add(new ScribanProvider());
}

public static HandlerRegistryBuilder Empty() => new();

}

}
56 changes: 56 additions & 0 deletions Modules/AutoLayout/Scanning/HandlerRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using GenHTTP.Api.Content;
using GenHTTP.Api.Content.IO;

namespace GenHTTP.Modules.AutoLayout.Scanning
{

public class HandlerRegistry
{

#region Get-/Setters

public List<IResourceHandlerProvider> Providers { get; }

public IResourceHandlerProvider Fallback { get; }

#endregion

#region Initialization

public HandlerRegistry(List<IResourceHandlerProvider> providers, IResourceHandlerProvider fallback)
{
Providers = providers;
Fallback = fallback;
}

#endregion

#region Functionality

public async ValueTask<IHandlerBuilder> ResolveAsync(IResource resource)
{
foreach (var provider in Providers)
{
if (provider.Supports(resource))
{
return await provider.GetHandlerAsync(resource);
}
}

if (!Fallback.Supports(resource))
{
throw new InvalidOperationException($"Fallback cannot handle resource '{resource.Name}'");
}

return await Fallback.GetHandlerAsync(resource);
}

#endregion

}

}
37 changes: 37 additions & 0 deletions Modules/AutoLayout/Scanning/HandlerRegistryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;

using GenHTTP.Api.Infrastructure;

namespace GenHTTP.Modules.AutoLayout.Scanning
{

public class HandlerRegistryBuilder : IBuilder<HandlerRegistry>
{
private readonly List<IResourceHandlerProvider> _Providers = new();

private IResourceHandlerProvider? _Fallback;

#region Functionality

public HandlerRegistryBuilder Add(IResourceHandlerProvider provider)
{
_Providers.Add(provider);
return this;
}

public HandlerRegistryBuilder Fallback(IResourceHandlerProvider provider)
{
_Fallback = provider;
return this;
}

public HandlerRegistry Build()
{
return new HandlerRegistry(_Providers, _Fallback ?? throw new BuilderMissingPropertyException("fallback"));
}

#endregion

}

}
26 changes: 0 additions & 26 deletions Modules/AutoLayout/Scanning/HandlerResolver.cs

This file was deleted.

11 changes: 7 additions & 4 deletions Modules/AutoLayout/Scanning/TreeScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ namespace GenHTTP.Modules.AutoLayout.Scanning
public static class TreeScanner
{

public static ValueTask<LayoutBuilder> ScanAsync(IResourceTree tree, params string[] indexNames) => ScanContainerAsync(tree, indexNames);
public static ValueTask<LayoutBuilder> ScanAsync(IResourceTree tree, HandlerRegistry registry, params string[] indexNames)
{
return ScanContainerAsync(tree, registry, indexNames);
}

private static async ValueTask<LayoutBuilder> ScanContainerAsync(IResourceContainer container, params string[] indexNames)
private static async ValueTask<LayoutBuilder> ScanContainerAsync(IResourceContainer container, HandlerRegistry registry, params string[] indexNames)
{
var layout = Layout.Create();

await foreach (var node in container.GetNodes())
{
layout.Add(node.Name, await ScanContainerAsync(node));
layout.Add(node.Name, await ScanContainerAsync(node, registry));
}

await foreach (var resource in container.GetResources())
{
if (resource.Name is not null)
{
var handler = HandlerResolver.Resolve(resource);
var handler = await registry.ResolveAsync(resource);

var fileName = Path.GetFileNameWithoutExtension(resource.Name).ToLowerInvariant();

Expand Down
9 changes: 9 additions & 0 deletions Modules/Basics/CoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public static bool HasType(this IRequest request, params RequestMethod[] methods
{ "cfg", ContentType.TextPlain },
{ "conf", ContentType.TextPlain },
{ "config", ContentType.TextPlain },
// Markdown
{ "md", ContentType.TextMarkdown },
// Scriban
{ "scriban-html", ContentType.TextScriban },
{ "criban-htm", ContentType.TextScriban },
{ "sbn-html", ContentType.TextScriban },
{ "sbn-htm", ContentType.TextScriban },
{ "sbnhtml", ContentType.TextScriban },
{ "sbnhtm", ContentType.TextScriban },
// Fonts
{ "eot", ContentType.FontEmbeddedOpenTypeFont },
{ "ttf", ContentType.FontTrueTypeFont },
Expand Down

0 comments on commit 935f7ab

Please sign in to comment.