using ReportGeneration.Abstract;
using ReportGeneration.Interface;
using System.IO;
using System.Reflection;
using System.Text;
namespace ReportGeneration.Generators;
public class HtmlDocumentGenerator : DocumentGeneratorBase
{
private int _sectionLevel = 0;
public string Title { get; init; } = string.Empty;
///
public HtmlDocumentGenerator() { }
///
public HtmlDocumentGenerator(string filePath) : base(filePath)
{
Title = Path.GetFileNameWithoutExtension(filePath);
}
///
public HtmlDocumentGenerator(Stream stream) : base(stream) { }
///
public HtmlDocumentGenerator(Stream stream, Encoding encoding) : base(stream, encoding) { }
#region State
///
protected override void OnOpen()
{
base.OnOpen();
// Init html document
Write("");
Write("");
// Header
Write("
");
Write(HtmlTools.Wrap("title", Title));
Write("");
Write("");
Write("");
Write("");
// Init body
Write("");
}
///
protected override void OnClose()
{
base.OnClose();
// End document
Write("");
Write("");
}
#endregion
#region Writing
///
public IDocumentGenerator AppendParagraph(string? text, string? @class) =>
Append(HtmlTools.Wrap("p", text, @class));
///
public override IDocumentGenerator AppendParagraph(string? text = default) =>
AppendParagraph(text, default);
///
public override IDocumentGenerator AppendHeading(int level, string text) =>
Append($"{text}");
///
protected override ITableGenerator MakeTable(int columns, Stream stream) =>
new CollapsibleHtmlTableGenerator(columns, stream);
///
public override string FormatImage(string path, IBounds? bounds = default) =>
FormatImage(path, default, bounds);
///
public string FormatImage(string path, string? @class, IBounds? bounds = default) =>
HtmlTools.FormatImage(path, @class, bounds);
#endregion
#region Resource Management
private static class Resources
{
private static readonly Assembly assembly = Assembly.GetExecutingAssembly();
private static readonly string basePath =
typeof(HtmlDocumentGenerator).Namespace + ".Resources.";
public static Stream Get(string fileName) =>
assembly.GetManifestResourceStream(basePath + fileName) ??
throw new FileNotFoundException("Could not get resource", fileName);
}
#endregion
}