ln.templates/ln.templates/html/TemplateReader.cs

71 lines
1.9 KiB
C#

// /**
// * File: TemplateReader.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.IO;
namespace ln.templates.html
{
public class TemplateReader : ElementReader
{
public virtual TemplateDocument TemplateDocument => Document as TemplateDocument;
public TemplateReader()
{
}
public override DocumentElement CreateDocument() => new TemplateDocument();
public override Element CreateElement(string tagName) => new TemplateElement(tagName);
public override void Text(string text)
{
if (text.Contains("{{"))
{
int pOpen = 0;
int pClose = 0;
while (pOpen < text.Length)
{
pOpen = text.IndexOf("{{", pClose);
if (pOpen == -1)
pOpen = text.Length;
string preText = text.Substring(pClose, pOpen - pClose);
if (preText.Length > 0)
{
CurrentElement.AppendChild(CreateTextElement(preText));
}
if (pOpen == text.Length)
break;
pOpen += 2;
pClose = text.IndexOf("}}", pOpen);
if (pClose == -1)
throw new FormatException("missing }}");
string expr = text.Substring(pOpen, pClose - pOpen - 1);
pClose += 2;
CurrentElement.AppendChild(new ExpressionElement(expr));
}
} else
{
CurrentElement.AppendChild(
CreateTextElement(text)
);
}
}
}
}