-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from askonomm/16-date-formatting-attribute-parser
Implement ExpressionModifierParser and a few default modifiers.
- Loading branch information
Showing
30 changed files
with
1,069 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ obj/ | |
/packages/ | ||
riderModule.iml | ||
/_ReSharper.Caches/\ | ||
/.idea/ | ||
/.idea/ | ||
Htmt.sln.DotSettings.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
using System.Xml; | ||
|
||
namespace Htmt.AttributeParsers; | ||
|
||
/// <summary> | ||
/// Base class for attribute parsers that provides some common functionality. | ||
/// </summary> | ||
public partial class BaseAttributeParser : IAttributeParser | ||
{ | ||
/// <summary> | ||
/// XML Tag selector for finding the relevant nodes. | ||
/// </summary> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public virtual string XTag => throw new NotImplementedException(); | ||
|
||
/// <summary> | ||
/// The entire XML document that is being parsed. | ||
/// </summary> | ||
public XmlDocument Xml { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// Templating data. | ||
/// </summary> | ||
public Dictionary<string, object?> Data { get; set; } = new(); | ||
|
||
/// <summary> | ||
/// List of expression modifiers. | ||
/// </summary> | ||
public IExpressionModifier[] ExpressionModifiers { get; set; } = []; | ||
|
||
[GeneratedRegex(@"(?<name>\{.*?\})")] | ||
private static partial Regex WholeKeyRegex(); | ||
|
||
/// <summary> | ||
/// Parser the expression where it replaces variables with their data, and applies | ||
/// expression modifiers. | ||
/// </summary> | ||
/// <param name="str"></param> | ||
/// <returns>Returns the parsed expression as a string.</returns> | ||
protected string ParseExpression(string str) | ||
{ | ||
var matches = WholeKeyRegex().Matches(str).Select(x => x.Groups["name"].Value).ToArray(); | ||
|
||
foreach (var match in matches) | ||
{ | ||
var strippedName = match[1..^1]; | ||
var expressionModifierParser = new ExpressionModifierParser { Data = Data, ExpressionModifiers = ExpressionModifiers }; | ||
var value = expressionModifierParser.Parse(strippedName); | ||
|
||
if (value != null) | ||
{ | ||
str = value switch | ||
{ | ||
string s => str.Replace(match, s), | ||
int i => str.Replace(match, i.ToString()), | ||
double d => str.Replace(match, d.ToString(CultureInfo.CurrentCulture)), | ||
bool b => str.Replace(match, b.ToString()), | ||
_ => str.Replace(match, value.ToString()), | ||
}; | ||
} | ||
else | ||
{ | ||
str = str.Replace(match, ""); | ||
} | ||
} | ||
|
||
return str; | ||
} | ||
|
||
/// <summary> | ||
/// A method that is called to parse the XML nodes. | ||
/// </summary> | ||
/// <param name="nodes"></param> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public virtual void Parse(XmlNodeList? nodes) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.