Add project files.

This commit is contained in:
2025-04-19 20:47:58 +03:00
parent 9e14d137ae
commit bfdcdb1f3b
66 changed files with 2394 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Beam\Beam.csproj" />
</ItemGroup>
</Project>
+37
View File
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Beam.Exports {
public class PlainTextExporter : IExporter, IAsyncExporter {
public PlainTextExporter(IDocument document) {
Document = document;
}
public IDocument Document { get; }
protected virtual string Convert() {
return Document.ToString();
}
protected virtual Task<string> ConvertAsync() {
return Task.FromResult(Document.ToString());
}
public virtual void Write(string path) {
var text = Convert();
if (!Directory.Exists(Path.GetDirectoryName(path)))
throw new ArgumentException(S.M.FileDirectoryDoesNotExist, nameof(path));
File.WriteAllText(path, text, Encoding.Unicode);
}
public virtual async Task WriteAsync(string path) {
var text = await ConvertAsync();
if (!Directory.Exists(path))
throw new ArgumentException(S.M.FileDirectoryDoesNotExist, nameof(path));
await File.WriteAllTextAsync(path, text);
}
}
}
+42
View File
@@ -0,0 +1,42 @@
using System.Text;
namespace Beam.Exports {
public class HtmlExporter : PlainTextExporter {
public HtmlExporter(IDocument document,
ArticleData? meta = null,
Dictionary<string, string>? linkButtons = null,
string? eofHtml = null) : base(document) {
Meta = meta;
LinkButtons = linkButtons;
EofHtml = eofHtml;
}
public ArticleData? Meta { get; }
public Dictionary<string, string>? LinkButtons { get; }
public string? EofHtml { get; }
protected override string Convert() {
var text = Document.ToString();
// Convert newlines to <p></p> tags
text = "<p>" + text.Replace("\n", "</p><p>") + "</p>";
if (Meta is null)
return text;
text = $"<h1>{Meta.Name}</h1>" + text;
if (LinkButtons is null || LinkButtons.Count == 0)
return text;
StringBuilder buttons = new();
foreach(var (btnText, btnLink) in LinkButtons.Select((x) => (x.Key, x.Value))) {
buttons.AppendLine($"<a href=\"{btnLink}\">{btnText}</a>");
}
var buttonsDiv = $"<div class=\"controls\">{buttons}</div>";
text = buttonsDiv + text + buttonsDiv;
text += EofHtml ?? "";
text = "<!DOCTYPE html>\n<html>" + text + "</html>";
return text;
}
protected override Task<string> ConvertAsync() {
return Task.FromResult(Convert());
}
}
}
+10
View File
@@ -0,0 +1,10 @@
namespace Beam.Exports {
public interface IAsyncExporter : IExporter {
/// <summary>
/// Asynchronously writes the object to the desired path, creating it if it does not exist.
/// </summary>
/// <param name="path">The path of the exported object</param>
/// <returns></returns>
public Task WriteAsync(string path);
}
}
+19
View File
@@ -0,0 +1,19 @@
namespace Beam.Exports {
public interface IExporter {
/// <summary>
/// Synchronously writes the object to the desired path, creating it if it does not exist.
/// </summary>
/// <param name="path">The path of the exported object</param>
public void Write(string path);
protected void EnsurePathExists(string path) {
if (File.Exists(path)) {
File.Delete(path);
return;
}
else if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace Beam.Exports {
public interface IStreamExporter : IAsyncExporter {
/// <summary>
/// Asynchronously writes the object to the desired path in many parts, returning the path
/// of each written file as a stream
/// </summary>
/// <param name="path">The path of the exported object</param>
/// <returns>The async enumerator of each written file</returns>
public IAsyncEnumerator<string> WriteAsyncStream(string path);
}
}
+13
View File
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Beam.Exports {
internal static class S {
internal static class M {
internal const string FileDirectoryDoesNotExist = "Part of the path supplied does not exist.";
}
}
}