Reinclude tools
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public class DefineSectionReader : LeadingTextSectionReader
|
||||
{
|
||||
public DefineSectionReader() : base("<!D")
|
||||
{
|
||||
}
|
||||
|
||||
public override ITemplateSection ExtractSection(ref int index, TemplateDocument document)
|
||||
{
|
||||
var tagEnd = TemplateText.IndexOf(">", index, StringComparison.Ordinal);
|
||||
var sectionName = GetTagValue(index, tagEnd);
|
||||
tagEnd += 1;
|
||||
var endIndex = TemplateText.IndexOf("<!D>", tagEnd, StringComparison.Ordinal);
|
||||
var storedText = TemplateText.Substring(tagEnd, endIndex - tagEnd);
|
||||
index = endIndex + 4;
|
||||
PassCaretToCloseSymbol();
|
||||
PassCaretToCloseSymbol();
|
||||
return new DefineTemplateSection(storedText, sectionName, document);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public interface ISectionReader
|
||||
{
|
||||
string TemplateText { get; set; }
|
||||
int GetNextSectionIndex(int currentIndex);
|
||||
ITemplateSection ExtractSection(ref int index, TemplateDocument document);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public class InnerTemplateSectionReader : LeadingTextSectionReader
|
||||
{
|
||||
public InnerTemplateSectionReader()
|
||||
: base("<!T")
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public override ITemplateSection ExtractSection(ref int index, TemplateDocument document)
|
||||
{
|
||||
var end = FindEnd(index + 4);
|
||||
var from = index;
|
||||
index = PassCaretToCloseSymbol();
|
||||
var to = index - 1;
|
||||
|
||||
var tagName = GetTagValue(from, to);
|
||||
|
||||
var innerDocText = TemplateText.Substring(index, end - index);
|
||||
var innerDoc = TemplateReader.Parse(innerDocText);
|
||||
_currentCaretPosition = end + 4;
|
||||
index = _currentCaretPosition;
|
||||
return new InnerTemplateSection(tagName, innerDoc, document);
|
||||
}
|
||||
|
||||
private int FindEnd(int start)
|
||||
{
|
||||
var endSymbol = TemplateText.IndexOf("<!T>", start, StringComparison.Ordinal);
|
||||
var innerStartSymbol = TemplateText.IndexOf("<!T ", start, StringComparison.Ordinal);
|
||||
if (endSymbol < innerStartSymbol || innerStartSymbol == -1)
|
||||
return endSymbol;
|
||||
|
||||
return FindEnd(endSymbol + 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public class InsertSectionReader : LeadingTextSectionReader
|
||||
{
|
||||
public InsertSectionReader() : base("<!I")
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public override ITemplateSection ExtractSection(ref int index, TemplateDocument document)
|
||||
{
|
||||
var from = index;
|
||||
index = PassCaretToCloseSymbol();
|
||||
var to = index - 1;
|
||||
|
||||
var tagText = GetTagValue(from, to);
|
||||
var parts = tagText.Split(' ');
|
||||
|
||||
return new InsertTemplateSection(parts[0], document, parts.Skip(1).ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public abstract class LeadingTextSectionReader : ISectionReader
|
||||
{
|
||||
private readonly string _tagLeading;
|
||||
protected int _currentCaretPosition;
|
||||
|
||||
protected LeadingTextSectionReader(string tagLeading)
|
||||
{
|
||||
_tagLeading = tagLeading + " ";
|
||||
}
|
||||
|
||||
public string TemplateText { get; set; }
|
||||
|
||||
public int GetNextSectionIndex(int currentIndex)
|
||||
{
|
||||
_currentCaretPosition = Math.Max(currentIndex, _currentCaretPosition);
|
||||
var index = TemplateText.IndexOf(_tagLeading, _currentCaretPosition, StringComparison.Ordinal);
|
||||
_currentCaretPosition = index;
|
||||
return index;
|
||||
}
|
||||
|
||||
public abstract ITemplateSection ExtractSection(ref int index, TemplateDocument document);
|
||||
|
||||
protected int PassCaretToCloseSymbol()
|
||||
{
|
||||
_currentCaretPosition = TemplateText.IndexOf(">", _currentCaretPosition, StringComparison.Ordinal) + 1;
|
||||
return _currentCaretPosition;
|
||||
}
|
||||
|
||||
protected string GetTagValue(int from, int to)
|
||||
{
|
||||
var realStart = from + 4;
|
||||
var span = TemplateText.Substring(realStart, to - realStart);
|
||||
return span;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public class LinkSectionReader : LeadingTextSectionReader
|
||||
{
|
||||
public LinkSectionReader() : base("<!L")
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public override ITemplateSection ExtractSection(ref int index, TemplateDocument document)
|
||||
{
|
||||
var from = index;
|
||||
index = PassCaretToCloseSymbol();
|
||||
var to = index - 1;
|
||||
|
||||
var tagText = GetTagValue(from, to);
|
||||
return new LinkTemplateSection(tagText, document);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace mROA.CodegenTools.Reading
|
||||
{
|
||||
public static class TemplateReader
|
||||
{
|
||||
private static readonly List<Type> ReaderTypes = new()
|
||||
{
|
||||
typeof(DefineSectionReader),
|
||||
typeof(InsertSectionReader),
|
||||
typeof(LinkSectionReader),
|
||||
typeof(InnerTemplateSectionReader)
|
||||
};
|
||||
|
||||
public static TemplateDocument Parse(string templateText)
|
||||
{
|
||||
var currentIndex = 0;
|
||||
var activeReaders = new List<ISectionReader>();
|
||||
|
||||
foreach (var readerType in ReaderTypes)
|
||||
{
|
||||
var reader = Activator.CreateInstance(readerType) as ISectionReader;
|
||||
reader.TemplateText = templateText;
|
||||
activeReaders.Add(reader);
|
||||
}
|
||||
|
||||
var doc = new TemplateDocument();
|
||||
|
||||
while (currentIndex < templateText.Length)
|
||||
{
|
||||
List<(ISectionReader reader, int index)> indices = activeReaders
|
||||
.Select(i => ((ISectionReader Readers, int index))(i, i.GetNextSectionIndex(currentIndex)))
|
||||
.Where(i => i.index > -1).ToList();
|
||||
var minIndex = templateText.Length;
|
||||
ISectionReader minReader = null;
|
||||
if (indices.Count != 0)
|
||||
{
|
||||
minIndex = indices.Min(i => i.index);
|
||||
minReader = indices.FirstOrDefault(i => i.index == minIndex).reader;
|
||||
|
||||
if (indices.Count != activeReaders.Count) activeReaders = indices.Select(i => i.reader).ToList();
|
||||
}
|
||||
|
||||
|
||||
if (currentIndex != minIndex)
|
||||
{
|
||||
var literalText = templateText.Substring(currentIndex, minIndex - currentIndex);
|
||||
var literal =
|
||||
new LiteralTemplateSection(literalText, doc);
|
||||
doc.Parts.Add(literal);
|
||||
currentIndex = minIndex;
|
||||
}
|
||||
|
||||
if (indices.Count != 0)
|
||||
{
|
||||
var section = minReader.ExtractSection(ref currentIndex, doc);
|
||||
doc.Parts.Add(section);
|
||||
}
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
public static TemplateDocument FromEmbeddedResource(string resourceName)
|
||||
{
|
||||
return FromEmbeddedResource(resourceName, Assembly.GetCallingAssembly());
|
||||
}
|
||||
|
||||
public static TemplateDocument FromEmbeddedResource(string resourceName, Assembly assembly)
|
||||
{
|
||||
var fileName = assembly.GetManifestResourceNames().First(n => n.EndsWith(resourceName));
|
||||
var res = assembly.GetManifestResourceStream(fileName);
|
||||
var reader = new StreamReader(res);
|
||||
var templateText = reader.ReadToEnd();
|
||||
return Parse(templateText);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user