From 9b6c72d9d56b6271fb7cbf6e0bf578a025b4381b Mon Sep 17 00:00:00 2001 From: SpikeHimself Date: Sun, 2 Apr 2023 19:25:53 +0200 Subject: [PATCH] Add Alignment.Unspecified and default to it --- ManyFormats/Enums.cs | 1 + ManyFormats/Format.cs | 13 ++++++++++++- ManyFormats/Formats/Bbcode.cs | 19 ++++++++----------- ManyFormats/Formats/Markdown.cs | 14 +++----------- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/ManyFormats/Enums.cs b/ManyFormats/Enums.cs index b2d5aad..847cef1 100644 --- a/ManyFormats/Enums.cs +++ b/ManyFormats/Enums.cs @@ -54,6 +54,7 @@ public enum ListBullets public enum Alignment { + Unspecified, Left, Centre, Right, diff --git a/ManyFormats/Format.cs b/ManyFormats/Format.cs index a1a9a48..1ab4ee6 100644 --- a/ManyFormats/Format.cs +++ b/ManyFormats/Format.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace ManyFormats { @@ -7,8 +8,18 @@ public abstract class Format public virtual string Name { get; set; } public FormatPreferences Preferences { get; set; } + protected readonly Dictionary AlignmentMap; + protected Format(string name, FormatPreferences prefs) { + AlignmentMap = new Dictionary() + { + [Alignment.Unspecified] = string.Empty, + [Alignment.Left] = "left", + [Alignment.Centre] = "center", + [Alignment.Right] = "right", + }; + Name = name; Preferences = prefs; } @@ -111,7 +122,7 @@ public virtual string Font(string text, FontName font) return NotImplemented(text, nameof(Font)); } - public virtual string Align(string text, Alignment alignment) + public virtual string Align(string text, Alignment align = Alignment.Unspecified) { return NotImplemented(text, nameof(Align)); } diff --git a/ManyFormats/Formats/Bbcode.cs b/ManyFormats/Formats/Bbcode.cs index 7df8abb..60fcc47 100644 --- a/ManyFormats/Formats/Bbcode.cs +++ b/ManyFormats/Formats/Bbcode.cs @@ -6,7 +6,6 @@ public class Bbcode : Format { private readonly Dictionary headingToFontSizeMap; private readonly Dictionary fontsToFontNameMap; - private readonly Dictionary alignmentMap; public Bbcode() : this("BBCode") { } @@ -33,13 +32,6 @@ public Bbcode(string name) : base(name) [FontName.TrebuchetMs] = "Trebuchet MS", [FontName.Verdana] = "Verdana", }; - - alignmentMap = new Dictionary() - { - [Alignment.Left] = "left", - [Alignment.Centre] = "center", - [Alignment.Right] = "right", - }; } public override string Size(string text, FontSize size = FontSize.Two) @@ -73,7 +65,7 @@ public override string Colour(string text, string colour) return $"[color={colour}]{text}[/color]"; } - public override string Image(string uri, int height = -1, int width = -1, Alignment align = Alignment.Left) + public override string Image(string uri, int height = -1, int width = -1, Alignment align = Alignment.Unspecified) { return Align($"[img]{uri}[/img]", align); } @@ -139,9 +131,14 @@ public string Font(string text, string fontName) return $"[font={fontName}]{text}[/font]"; } - public override string Align(string text, Alignment align = Alignment.Left) + public override string Align(string text, Alignment align = Alignment.Unspecified) { - var strAlign = alignmentMap[align]; + if (align == Alignment.Unspecified) + { + return text; + } + + var strAlign = AlignmentMap[align]; return $"[{strAlign}]{text}[/{strAlign}]"; } } diff --git a/ManyFormats/Formats/Markdown.cs b/ManyFormats/Formats/Markdown.cs index baefa73..ce53dd5 100644 --- a/ManyFormats/Formats/Markdown.cs +++ b/ManyFormats/Formats/Markdown.cs @@ -6,7 +6,6 @@ public class Markdown : Format { private readonly Dictionary headingSizePrefixes; private readonly Dictionary listBulletsPrefixes; - private readonly Dictionary alignmentMap; public Markdown() : this("Markdown") { } @@ -27,13 +26,6 @@ public Markdown(string name) : base(name) [ListBullets.Asterisk] = "* ", [ListBullets.Plus] = "+ ", }; - - alignmentMap = new Dictionary() - { - [Alignment.Left] = "left", - [Alignment.Centre] = "center", - [Alignment.Right] = "right", - }; } public override string Bold(string text) @@ -61,11 +53,11 @@ public override string Heading(string text, HeadingSize size = HeadingSize.Large public override string Image(string link, int height = -1, int width = -1, Alignment align = Alignment.Left) { - var strAlign = alignmentMap[align]; + var strAlign = AlignmentMap[align]; return $" -1 ? $" height=\"{height}\"" : "") + (width > -1 ? $" width=\"{width}\"" : "") - + $" align=\"{strAlign}\"" + + (align == Alignment.Unspecified ? string.Empty : $" align=\"{strAlign}\"") + $" />"; } @@ -137,7 +129,7 @@ public override string Task(string text, bool complete = false) public override string Align(string text, Alignment align) { - var strAlign = alignmentMap[align]; + var strAlign = AlignmentMap[align]; return $"

{text}

"; } }