Код компиляции документа
This commit is contained in:
@@ -16,6 +16,8 @@ namespace mROA.CodegenTools
|
|||||||
}
|
}
|
||||||
|
|
||||||
public string Tag => _tag;
|
public string Tag => _tag;
|
||||||
|
public int TargetLength => StoredText.Length;
|
||||||
|
|
||||||
public string Bake()
|
public string Bake()
|
||||||
{
|
{
|
||||||
return String.Empty;
|
return String.Empty;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ namespace mROA.CodegenTools
|
|||||||
public interface ITemplatePart
|
public interface ITemplatePart
|
||||||
{
|
{
|
||||||
TemplateDocument Context { get; set; }
|
TemplateDocument Context { get; set; }
|
||||||
|
int TargetLength { get; }
|
||||||
string Bake();
|
string Bake();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ namespace mROA.CodegenTools
|
|||||||
{
|
{
|
||||||
public class InsertTemplatePart : ITemplatePart, ITagged
|
public class InsertTemplatePart : ITemplatePart, ITagged
|
||||||
{
|
{
|
||||||
private string _insertedText;
|
private object _insertedValue;
|
||||||
private ITemplatePart _insertedPart;
|
|
||||||
private string _tag;
|
private string _tag;
|
||||||
public string Tag => _tag;
|
public string Tag => _tag;
|
||||||
public TemplateDocument Context { get; set; }
|
public TemplateDocument Context { get; set; }
|
||||||
@@ -21,9 +20,6 @@ namespace mROA.CodegenTools
|
|||||||
|
|
||||||
private bool IsInserted(object value)
|
private bool IsInserted(object value)
|
||||||
{
|
{
|
||||||
if (!(value is string) && !(value is ITemplatePart) )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (Parameters.IndexOf("R") == 0) return false;
|
if (Parameters.IndexOf("R") == 0) return false;
|
||||||
|
|
||||||
ProduceInserted(value);
|
ProduceInserted(value);
|
||||||
@@ -37,38 +33,36 @@ namespace mROA.CodegenTools
|
|||||||
var clone = (InsertTemplatePart)MemberwiseClone();
|
var clone = (InsertTemplatePart)MemberwiseClone();
|
||||||
clone._tag += "+";
|
clone._tag += "+";
|
||||||
|
|
||||||
if (value is string s)
|
clone._insertedValue = value;
|
||||||
{
|
|
||||||
clone.Setup(s);
|
|
||||||
}else
|
|
||||||
clone.Setup((ITemplatePart)value);
|
|
||||||
|
|
||||||
Context.Parts.Insert(currentIndex, clone);
|
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;
|
return;
|
||||||
_insertedText = inside;
|
if (IsInserted(value))
|
||||||
|
return;
|
||||||
|
|
||||||
|
_insertedValue = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Setup(ITemplatePart part)
|
|
||||||
{
|
public int TargetLength => 0;
|
||||||
if (IsInserted(part))
|
|
||||||
return;
|
|
||||||
_insertedPart = part;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Bake()
|
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 string LinkedTag => _linkedTag;
|
||||||
|
|
||||||
|
public int TargetLength { get; }
|
||||||
|
|
||||||
public string Bake()
|
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 TemplateDocument Context { get; set; }
|
||||||
|
public int TargetLength { get; }
|
||||||
|
|
||||||
public string Bake()
|
public string Bake()
|
||||||
{
|
{
|
||||||
return _internalText;
|
return _internalText;
|
||||||
|
|||||||
@@ -1,9 +1,35 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace mROA.CodegenTools
|
namespace mROA.CodegenTools
|
||||||
{
|
{
|
||||||
public class TemplateDocument
|
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