Exemplo n.º 1
0
        private string render(String line, TemplateReader reader, String pattern, Dictionary <string, object> parameters, bool renderWithTextBlock = false)
        {
            Match         lineMatch  = Regex.Match(line.Trim(), pattern);
            String        tag        = "div";
            String        text       = "";
            List <string> attributes = new List <string>();
            int           level      = reader.Level;

            if (lineMatch.Groups["tag"].Success)
            {
                tag = lineMatch.Groups["tag"].Value;
            }
            if (lineMatch.Groups["text"].Success)
            {
                text = lineMatch.Groups["text"].Value;
            }
            StringBuilder textBuilder = new StringBuilder();

            if (text.Length > 0)
            {
                textBuilder.Append(TemplateRendererUtils.CreateIndent(renderer.LineIndent + 1));
                textBuilder.Append(text);
                textBuilder.Append("\n");
            }
            renderer.LineIndent++;
            String sourceBlock;

            if (renderWithTextBlock || textBlockTags.Contains(tag))
            {
                sourceBlock = reader.ReadBlockWithIndent(renderer.LineIndent);
                sourceBlock = sourceBlock.TrimEnd('\n');
                if (sourceBlock.Length > 0)
                {
                    textBuilder.Append(sourceBlock);
                    textBuilder.Append("\n");
                }
            }
            else
            {
                sourceBlock = reader.ReadBlock();
                textBuilder.Append(renderer.RenderBlock(sourceBlock));
            }
            renderer.LineIndent--;
            if (lineMatch.Groups["id"].Success)
            {
                attributes.Add("id=\"" + lineMatch.Groups["id"].Value + "\"");
            }
            if (lineMatch.Groups["class"].Success)
            {
                attributes.Add("class=\"" + lineMatch.Groups["class"].Value + "\"");
            }
            foreach (Capture cap in lineMatch.Groups["attr"].Captures)
            {
                attributes.Add(cap.Value.Trim());
            }
            return(generateTag(tag, attributes, textBuilder.ToString()));
        }
        public override string RenderTag(string line, TemplateReader reader, Dictionary <string, object> parameters, int patternIndex)
        {
            Match  lineMatch   = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String condition   = lineMatch.Groups["condition"].Value;
            var    interpreter = new Interpreter();

            foreach (var element in parameters)
            {
                interpreter.SetVariable(element.Key, element.Value, element.Value.GetType());
            }
            var result = interpreter.Eval <bool>(condition);

            if (result)
            {
                return(renderer.RenderBlock(reader.ReadBlock()));
            }
            reader.ReadBlock();
            return("");
        }
Exemplo n.º 3
0
        /// <summary>
        /// If in child template collect blocks
        /// If in parrent render blocks using child blocks
        /// </summary>
        /// <param name="line"></param>
        /// <param name="reader"></param>
        /// <param name="parameters"></param>
        /// <param name="patternIndex"></param>
        /// <returns></returns>
        public override string RenderTag(string line, TemplateReader reader, Dictionary <string, object> parameters, int patternIndex)
        {
            Match  lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]);
            String statment  = lineMatch.Groups["statment"].Value;

            if (!renderer.GetPlugin <ExtendsPlugin>().IsInParrent)
            {
                blocks.Add(statment, reader.ReadBlock());
                return("");
            }
            StringBuilder renderedSource = new StringBuilder();

            renderedSource.Append(renderer.RenderBlock(reader.ReadBlock()));
            if (blocks.ContainsKey(statment))
            {
                renderedSource.Append(renderer.RenderBlock(blocks[statment]));
            }
            return(renderedSource.ToString());
        }
Exemplo n.º 4
0
        private string eachParser(string statment, TemplateReader reader, Dictionary <string, object> parameters)
        {
            string        variable          = @"([a-zA-Z_][a-zA-Z0-9_]*)([\.]([a-zA-Z_][a-zA-Z0-9_]*))*";
            string        indexPattern      = "(,[ ]*(?<index>" + variable + "))";
            string        iteratorPattern   = "(?<iterator>" + variable + ")";
            string        collectionPattern = "(?<collection>" + variable + ")";
            string        optional          = "?";
            string        regex             = iteratorPattern + "[ ]*" + indexPattern + optional + "[ ]+in[ ]+" + collectionPattern;
            Match         m       = Regex.Match(statment, regex);
            StringBuilder builder = new StringBuilder();

            if (m.Success)
            {
                String indexName   = null;
                bool   isWithIndex = m.Groups["index"].Success;
                if (isWithIndex)
                {
                    indexName = m.Groups["index"].Value;
                }
                var    iteratorName   = m.Groups["iterator"].Value;
                var    collectionName = m.Groups["collection"].Value;
                string codeBlock      = reader.ReadBlock();
                if (codeBlock.Length == 0)
                {
                    return("");
                }
                int     index      = 0;
                dynamic collection = parameters[collectionName];
                if (isWithIndex)
                {
                    parameters.Add(indexName, 0);
                }
                foreach (var it in collection)
                {
                    parameters.Add(iteratorName, it);
                    if (isWithIndex)
                    {
                        parameters[indexName] = index;
                    }
                    index++;
                    builder.Append(renderer.RenderBlock(codeBlock));
                    parameters.Remove(iteratorName);
                }
                if (isWithIndex)
                {
                    parameters.Remove(indexName);
                }
            }
            else
            {
                throw new TemplateException("Can't parse each statment.");
            }
            return(builder.ToString());
        }