Files
Beam/Beam.Exports/HtmlExporter.cs
T
qwsdcvghyu89 7ed05abdb8 refactor: modularize Beam into new projects and interfaces
- Introduced modularity by splitting Beam into new projects: Beam.Abstractions, Beam.Models, and Beam.Downloaders.
- Refactored existing classes into appropriate namespaces and projects.
- Replaced specific implementations with abstractions (e.g., SourceLinkBuilder to LinkBuilder, State to IState, etc.).
- Updated interfaces: added ITemplate, IArticleData, IDownloadReport, and others for improved extensibility.
- Removed deprecated classes like SourceLinkBuilder and StateChangerFactory.
- Enhanced link handling in downloaders by refactoring to use `string` over `SourceLink`.
- Consolidated shared logic under Beam.Abstractions.
2025-09-22 01:51:46 +10:00

45 lines
1.6 KiB
C#

using System.Text;
using Beam.Abstractions;
using Beam.Models;
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());
}
}
}