Exemplo n.º 1
0
        public static DocumentToken Transform(DocumentToken document, List<DocumentToken> documents)
        {
            var transform = DocumentTransform.TransformSections(document, documents);

            transform = DocumentTransform.TransformRender(transform, documents);

            return transform;
        }
Exemplo n.º 2
0
        public static DocumentToken Transform(DocumentToken document, List <DocumentToken> documents)
        {
            var transform = DocumentTransform.TransformSections(document, documents);

            transform = DocumentTransform.TransformRender(transform, documents);

            return(transform);
        }
Exemplo n.º 3
0
        private static DocumentToken TransformRender(DocumentToken document, List <DocumentToken> documents)
        {
            var transform = new DocumentToken(document.Name, document.Content);

            foreach (var token in document.Tokens)
            {
                TransformRender(transform, token, documents);
            }

            return(transform);
        }
Exemplo n.º 4
0
        private static void TransformRender(DocumentToken transform, Token token, List <DocumentToken> documents)
        {
            if (token is RenderToken)
            {
                var render = token as RenderToken;

                renderdepth += 1;

                if (renderdepth > 32)
                {
                    renderdepth -= 1;

                    string message = string.Format("error: exceeded maximum render depth for ", render.Name);

                    transform.Tokens.Add(new ContentToken(message, 0, message.Length));

                    return;
                }

                var subdocument = GetDocument(render.Name, documents);

                if (subdocument != null)
                {
                    subdocument = Transform(subdocument, documents);

                    foreach (var _token in subdocument.Tokens)
                    {
                        TransformRender(transform, _token, documents);
                    }
                }

                renderdepth -= 1;

                return;
            }

            if (token is ContentToken)
            {
                transform.Tokens.Add(token);
            }

            foreach (var _token in token.Tokens)
            {
                TransformRender(transform, _token, documents);
            }
        }
Exemplo n.º 5
0
        private static DocumentToken TransformSections(DocumentToken document, List <DocumentToken> documents)
        {
            var transform = new DocumentToken(document.Name, document.Content);

            var chain = CreateDocumentChain(document, documents);

            var root = chain[0];

            var decendants = new List <DocumentToken>();

            for (int i = 1; i < chain.Count; i++)
            {
                decendants.Add(chain[i]);
            }

            foreach (var token in root.Tokens)
            {
                if (token is SectionToken)
                {
                    var section = token as SectionToken;

                    foreach (var decendant in decendants)
                    {
                        var subsection = decendant.GetSection(section.Name);

                        if (subsection != null)
                        {
                            section = subsection;
                        }
                        else
                        {
                            break;
                        }
                    }

                    transform.Tokens.Add(section);
                }
                else
                {
                    transform.Tokens.Add(token);
                }
            }

            return(transform);
        }
Exemplo n.º 6
0
        private DocumentToken LoadDocument(string name, string filename)
        {
            if (System.IO.File.Exists(filename))
            {
                try
                {
                    var document = new DocumentToken(name, System.IO.File.ReadAllText(filename));

                    return(Lexer.Scan(document));
                }
                catch
                {
                }
            }

            var error = new DocumentToken(name, string.Format("error: unable to load template for {0}", filename));

            error.Tokens.Add(new ContentToken(error.Content, 0, error.Content.Length));

            return(error);
        }
Exemplo n.º 7
0
        private static List <DocumentToken> CreateDocumentChain(DocumentToken document, List <DocumentToken> documents)
        {
            var chain = new List <DocumentToken>();

            chain.Add(document);

            while (document != null)
            {
                var import = document.GetImport();

                if (import == null)
                {
                    document = null;

                    break;
                }

                if (GetDocument(import.Name, chain) != null)
                {
                    var message = string.Format("error: cyclic imports detected for ", import.Name);

                    var error = new DocumentToken(document.Name, message);

                    error.Tokens.Add(new ContentToken(error.Content, 0, error.Content.Length));

                    return(new List <DocumentToken>(new DocumentToken[] { error }));
                }

                document = GetDocument(import.Name, documents);

                if (document != null)
                {
                    chain.Add(document);
                }
            }

            chain.Reverse();

            return(chain);
        }
Exemplo n.º 8
0
        private static List<DocumentToken> CreateDocumentChain(DocumentToken document, List<DocumentToken> documents)
        {
            var chain = new List<DocumentToken>();

            chain.Add(document);

            while (document != null)
            {
                var import = document.GetImport();

                if (import == null)
                {
                    document = null;

                    break;
                }

                if (GetDocument(import.Name, chain) != null)
                {
                    var message = string.Format("error: cyclic imports detected for ", import.Name);

                    var error = new DocumentToken(document.Name, message);

                    error.Tokens.Add(new ContentToken(error.Content, 0, error.Content.Length));

                    return new List<DocumentToken>(new DocumentToken[] {error});
                }

                document = GetDocument(import.Name, documents);

                if (document != null)
                {
                    chain.Add(document);
                }
            }

            chain.Reverse();

            return chain;
        }
Exemplo n.º 9
0
        public Template(string filename)
        {
            var documents = DocumentLoader.Load(filename);

            this.document = DocumentTransform.Transform(documents[0], documents);
        }
Exemplo n.º 10
0
        public Template(string filename)
        {
            var documents = DocumentLoader.Load(filename);

            this.document = DocumentTransform.Transform(documents[0], documents);
        }
Exemplo n.º 11
0
        private static DocumentToken TransformSections(DocumentToken document, List<DocumentToken> documents)
        {
            var transform  = new DocumentToken(document.Name, document.Content);

            var chain      = CreateDocumentChain(document, documents);

            var root       = chain[0];

            var decendants = new List<DocumentToken>();

            for (int i = 1; i < chain.Count; i++) {

                decendants.Add(chain[i]);
            }

            foreach (var token in root.Tokens)
            {
                if (token is SectionToken)
                {
                    var section = token as SectionToken;

                    foreach (var decendant in decendants)
                    {
                        var subsection = decendant.GetSection(section.Name);

                        if (subsection != null)
                        {
                            section = subsection;
                        }
                        else
                        {
                            break;
                        }
                    }

                    transform.Tokens.Add(section);
                }
                else
                {
                    transform.Tokens.Add(token);
                }
            }

            return transform;
        }
Exemplo n.º 12
0
        private static DocumentToken TransformRender(DocumentToken document, List<DocumentToken> documents)
        {
            var transform = new DocumentToken(document.Name, document.Content);

            foreach (var token in document.Tokens)
            {
                TransformRender(transform, token, documents);
            }

            return transform;
        }
Exemplo n.º 13
0
        private static void TransformRender(DocumentToken transform, Token token, List<DocumentToken> documents)
        {
            if (token is RenderToken)
            {
                var render = token as RenderToken;

                renderdepth += 1;

                if(renderdepth > 32)
                {
                    renderdepth -= 1;

                    string message = string.Format("error: exceeded maximum render depth for ", render.Name);

                    transform.Tokens.Add(new ContentToken(message, 0, message.Length));

                    return;
                }

                var subdocument = GetDocument(render.Name, documents);

                if (subdocument != null)
                {
                    subdocument = Transform(subdocument, documents);

                    foreach (var _token in subdocument.Tokens)
                    {
                        TransformRender(transform, _token, documents);
                    }
                }

                renderdepth -= 1;

                return;
            }

            if (token is ContentToken)
            {
                transform.Tokens.Add(token);
            }

            foreach (var _token in token.Tokens)
            {
                TransformRender(transform, _token, documents);
            }
        }
Exemplo n.º 14
0
        private DocumentToken LoadDocument(string name, string filename)
        {
            if(System.IO.File.Exists(filename))
            {
                try
                {
                    var document = new DocumentToken(name, System.IO.File.ReadAllText(filename));

                    return Lexer.Scan(document);
                }
                catch
                {

                }
            }

            var error = new DocumentToken(name, string.Format("error: unable to load template for {0}", filename));

            error.Tokens.Add(new ContentToken(error.Content, 0, error.Content.Length));

            return error;
        }