diff --git a/ExtendedConsole/Parser.cs b/ExtendedConsole/Parser.cs
index 2565311..cafeb0c 100644
--- a/ExtendedConsole/Parser.cs
+++ b/ExtendedConsole/Parser.cs
@@ -1,10 +1,17 @@
-using System;
+using System;
using System.Xml.Linq;
+using System.Linq;
namespace ExtendedConsole
{
internal static class Parser
{
+ ///
+ /// Parse markup and returns an instance of the TextBuilder class,
+ /// containing actions to write the text to the Console.
+ ///
+ /// A string containing the markup to write to Console.
+ /// An instance of the TextBuilder class ready to write the text to the Console.
internal static TextBuilder ParseMarkeup(string markup)
{
var textBuilder = new TextBuilder();
@@ -18,8 +25,18 @@ internal static TextBuilder ParseMarkeup(string markup)
return textBuilder;
}
+ ///
+ /// Recursively parses XML nodes in the markup.
+ ///
+ /// The instance of TextBuilder to hold the parsed information.
+ /// The current XML node to parse.
private static void ParseNode(TextBuilder textBuilder, XNode node)
{
+ if (node is null)
+ {
+ return;
+ }
+
var element = node as XElement;
if (element is null)
{
@@ -30,35 +47,26 @@ private static void ParseNode(TextBuilder textBuilder, XNode node)
ParseElement(textBuilder, element);
}
- if (node.NextNode != null)
- {
- ParseNode(textBuilder, node.NextNode);
- }
+ ParseNode(textBuilder, node.NextNode);
+
}
+ ///
+ /// Parse an XML element into the text builder.
+ /// If the element has children, Pass them to the ParseNode method.
+ ///
+ /// The instance of TextBuilder to hold the parsed information.
+ /// The current XML element to parse.
private static void ParseElement(TextBuilder textBuilder, XElement element)
{
ConsoleColor? foregroundColor = null, backgroundColor = null;
- var isColorElement = element.Name.LocalName == "c";
+ var isColorElement = element.Name.LocalName == "c" &&
+ element.Attributes().Any(a => a.Name == "f" || a.Name == "b");
+
if (isColorElement)
{
- var foreground = element.Attribute("f");
- if (foreground != null)
- {
- if (Enum.TryParse(foreground.Value, true, out ConsoleColor color))
- {
- foregroundColor = textBuilder.SetForegroundColor(color);
- }
- }
-
- var background = element.Attribute("b");
- if (background != null)
- {
- if (Enum.TryParse(background.Value, true, out ConsoleColor color))
- {
- backgroundColor = textBuilder.SetBackgroundColor(color);
- }
- }
+ SetColor(ref foregroundColor, "f", textBuilder.SetForegroundColor);
+ SetColor(ref backgroundColor, "b", textBuilder.SetBackgroundColor);
}
else
{
@@ -79,11 +87,26 @@ private static void ParseElement(TextBuilder textBuilder, XElement element)
{
textBuilder.AddText($"{element.Name}>");
}
+
+ void SetColor(ref ConsoleColor? color, string attributeName, Func setColor)
+ {
+ var attribute = element.Attribute(attributeName);
+ if (attribute != null &&
+ Enum.TryParse(attribute.Value, true, out ConsoleColor newColor))
+ {
+ color = setColor(newColor);
+ }
+ }
}
+ ///
+ /// Adds the XML element to the TextBuilder.
+ ///
+ /// The instance of TextBuilder to hold the parsed information.
+ /// The current XML element to add to the TextBuilder.
private static void WriteElement(TextBuilder textBuilder, XElement element)
{
-
+
textBuilder.AddText($"<{element.Name}");
if (element.HasAttributes)
{
@@ -96,12 +119,18 @@ private static void WriteElement(TextBuilder textBuilder, XElement element)
textBuilder.AddText(">");
}
+ ///
+ /// Adds the attributes of an XML element to the TextBuilder.
+ ///
+ /// The instance of TextBuilder to hold the parsed information.
+ /// The current attribute to add to the TextBuilder.
private static void WriteAttributes(TextBuilder textBuilder, XAttribute attribute)
{
textBuilder.AddText($" {attribute}");
- if (attribute.NextAttribute != null)
+ var nextAttribute = attribute.NextAttribute;
+ if (nextAttribute != null)
{
- WriteAttributes(textBuilder, attribute.NextAttribute);
+ WriteAttributes(textBuilder, nextAttribute);
}
}
}