Код компиляции документа
This commit is contained in:
@@ -16,6 +16,8 @@ namespace mROA.CodegenTools
|
||||
}
|
||||
|
||||
public string Tag => _tag;
|
||||
public int TargetLength => StoredText.Length;
|
||||
|
||||
public string Bake()
|
||||
{
|
||||
return String.Empty;
|
||||
|
||||
@@ -3,6 +3,7 @@ namespace mROA.CodegenTools
|
||||
public interface ITemplatePart
|
||||
{
|
||||
TemplateDocument Context { get; set; }
|
||||
int TargetLength { get; }
|
||||
string Bake();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,7 @@ namespace mROA.CodegenTools
|
||||
{
|
||||
public class InsertTemplatePart : ITemplatePart, ITagged
|
||||
{
|
||||
private string _insertedText;
|
||||
private ITemplatePart _insertedPart;
|
||||
private object _insertedValue;
|
||||
private string _tag;
|
||||
public string Tag => _tag;
|
||||
public TemplateDocument Context { get; set; }
|
||||
@@ -21,9 +20,6 @@ namespace mROA.CodegenTools
|
||||
|
||||
private bool IsInserted(object value)
|
||||
{
|
||||
if (!(value is string) && !(value is ITemplatePart) )
|
||||
return false;
|
||||
|
||||
if (Parameters.IndexOf("R") == 0) return false;
|
||||
|
||||
ProduceInserted(value);
|
||||
@@ -37,38 +33,36 @@ namespace mROA.CodegenTools
|
||||
var clone = (InsertTemplatePart)MemberwiseClone();
|
||||
clone._tag += "+";
|
||||
|
||||
if (value is string s)
|
||||
{
|
||||
clone.Setup(s);
|
||||
}else
|
||||
clone.Setup((ITemplatePart)value);
|
||||
clone._insertedValue = value;
|
||||
|
||||
Context.Parts.Insert(currentIndex, clone);
|
||||
}
|
||||
|
||||
|
||||
public void Setup(string inside)
|
||||
public void Setup(object value)
|
||||
{
|
||||
if (IsInserted(inside))
|
||||
if (!(value is string) && !(value is ITemplatePart) )
|
||||
return;
|
||||
_insertedText = inside;
|
||||
if (IsInserted(value))
|
||||
return;
|
||||
|
||||
_insertedValue = value;
|
||||
}
|
||||
|
||||
public void Setup(ITemplatePart part)
|
||||
{
|
||||
if (IsInserted(part))
|
||||
return;
|
||||
_insertedPart = part;
|
||||
}
|
||||
|
||||
public int TargetLength => 0;
|
||||
|
||||
public string Bake()
|
||||
{
|
||||
if (_insertedText != null)
|
||||
switch (_insertedValue)
|
||||
{
|
||||
return _insertedText;
|
||||
case null:
|
||||
return string.Empty;
|
||||
case string s:
|
||||
return s;
|
||||
default:
|
||||
return ((ITemplatePart)_insertedValue).Bake();
|
||||
}
|
||||
|
||||
return _insertedPart.Bake();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,11 @@ namespace mROA.CodegenTools
|
||||
|
||||
public string LinkedTag => _linkedTag;
|
||||
|
||||
public int TargetLength { get; }
|
||||
|
||||
public string Bake()
|
||||
{
|
||||
return Context.Parts.OfType<ITagged>().FirstOrDefault(i => i.Tag == _linkedTag)?.Bake();
|
||||
return Context[_linkedTag]?.Bake();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ namespace mROA.CodegenTools
|
||||
}
|
||||
|
||||
public TemplateDocument Context { get; set; }
|
||||
public int TargetLength { get; }
|
||||
|
||||
public string Bake()
|
||||
{
|
||||
return _internalText;
|
||||
|
||||
@@ -1,9 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace mROA.CodegenTools
|
||||
{
|
||||
public class TemplateDocument
|
||||
{
|
||||
public List<ITemplatePart> Parts { get; set; }
|
||||
public List<ITemplatePart> Parts { get; }
|
||||
public ITagged this[string tag] => Parts.OfType<ITagged>().FirstOrDefault(i => i.Tag == tag);
|
||||
public void Insert(string tag, object value)
|
||||
{
|
||||
var selectedPart = this[tag];
|
||||
if (selectedPart is InsertTemplatePart itp)
|
||||
{
|
||||
itp.Setup(value);
|
||||
}
|
||||
}
|
||||
|
||||
public string Compile()
|
||||
{
|
||||
var approximatelyLength = Parts.Sum(i => i.TargetLength);
|
||||
var stringBuilder = new StringBuilder(approximatelyLength);
|
||||
|
||||
for (int i = 0; i < Parts.Count; i++)
|
||||
{
|
||||
var part = Parts[i];
|
||||
var baked = part.Bake();
|
||||
stringBuilder.Append(baked);
|
||||
}
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user