diff --git a/mROA.CodegenTools/DefineTemplatePart.cs b/mROA.CodegenTools/DefineTemplatePart.cs new file mode 100644 index 0000000..76df73c --- /dev/null +++ b/mROA.CodegenTools/DefineTemplatePart.cs @@ -0,0 +1,27 @@ +using System; + +namespace mROA.CodegenTools +{ + public class DefineTemplatePart : ITemplatePart, ITagged + { + public TemplateDocument Context { get; set; } + public string StoredText { get; } + private string _tag; + + public DefineTemplatePart(string storedText) + { + StoredText = storedText; + } + + public string Tag => _tag; + public string Bake() + { + return String.Empty; + } + + public override string ToString() + { + return StoredText; + } + } +} \ No newline at end of file diff --git a/mROA.CodegenTools/ITemplatePart.cs b/mROA.CodegenTools/ITemplatePart.cs new file mode 100644 index 0000000..d9a588c --- /dev/null +++ b/mROA.CodegenTools/ITemplatePart.cs @@ -0,0 +1,13 @@ +namespace mROA.CodegenTools +{ + public interface ITemplatePart + { + TemplateDocument Context { get; set; } + string Bake(); + } + + public interface ITagged : ITemplatePart + { + string Tag { get; } + } +} \ No newline at end of file diff --git a/mROA.CodegenTools/InsertTemplatePart.cs b/mROA.CodegenTools/InsertTemplatePart.cs new file mode 100644 index 0000000..b1aa582 --- /dev/null +++ b/mROA.CodegenTools/InsertTemplatePart.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; + +namespace mROA.CodegenTools +{ + public class InsertTemplatePart : ITemplatePart, ITagged + { + private string _insertedText = null; + private ITemplatePart _insertedPart = null; + private string _tag; + public string Tag => _tag; + public TemplateDocument Context { get; set; } + public List Parameters; + + private bool IsInserted(object value) + { + if (!(value is string) && !(value is ITemplatePart) ) + return false; + + if (Parameters.IndexOf("R") == 0) return false; + + ProduceInserted(value); + + return true; + } + + private void ProduceInserted(object value) + { + var currentIndex = Context.Parts.IndexOf(this); + var clone = (InsertTemplatePart)MemberwiseClone(); + clone._tag += "+"; + + if (value is string s) + { + clone.Setup(s); + }else + clone.Setup((ITemplatePart)value); + + Context.Parts.Insert(currentIndex, clone); + } + + + public void Setup(string inside) + { + if (IsInserted(inside)) + return; + _insertedText = inside; + } + + public void Setup(ITemplatePart part) + { + if (IsInserted(part)) + return; + _insertedPart = part; + } + + public string Bake() + { + if (_insertedText != null) + { + return _insertedText; + } + + return _insertedPart.Bake(); + } + } +} \ No newline at end of file diff --git a/mROA.CodegenTools/LinkTemplatePart.cs b/mROA.CodegenTools/LinkTemplatePart.cs new file mode 100644 index 0000000..586228e --- /dev/null +++ b/mROA.CodegenTools/LinkTemplatePart.cs @@ -0,0 +1,17 @@ +using System.Linq; + +namespace mROA.CodegenTools +{ + public class LinkTemplatePart : ITemplatePart + { + private readonly string _linkedTag; + public TemplateDocument Context { get; set; } + + public string LinkedTag => _linkedTag; + + public string Bake() + { + return Context.Parts.OfType().FirstOrDefault(i => i.Tag == _linkedTag)?.Bake(); + } + } +} \ No newline at end of file diff --git a/mROA.CodegenTools/LiteralTemplatePart.cs b/mROA.CodegenTools/LiteralTemplatePart.cs new file mode 100644 index 0000000..e5fcb3a --- /dev/null +++ b/mROA.CodegenTools/LiteralTemplatePart.cs @@ -0,0 +1,17 @@ +namespace mROA.CodegenTools +{ + public class LiteralTemplatePart : ITemplatePart + { + private string _internalText; + public TemplateDocument Context { get; set; } + public string Bake() + { + return _internalText; + } + + public override string ToString() + { + return _internalText; + } + } +} \ No newline at end of file diff --git a/mROA.CodegenTools/TemplateDocument.cs b/mROA.CodegenTools/TemplateDocument.cs new file mode 100644 index 0000000..41ed3e3 --- /dev/null +++ b/mROA.CodegenTools/TemplateDocument.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace mROA.CodegenTools +{ + public class TemplateDocument + { + public List Parts { get; set; } + } +} \ No newline at end of file