Beispiel #1
0
        /// <summary>
        /// Builds a text generator based on the given format.
        /// </summary>
        /// <param name="format">The format to parse.</param>
        /// <returns>The text generator.</returns>
        public MustacheGenerator Compile(string format)
        {
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            CompoundMustacheGenerator   generator = new CompoundMustacheGenerator(_masterDefinition, new ArgumentCollection());
            Dictionary <string, string> partials  = new Dictionary <string, string>(_partialLookup);
            List <Context> context = new List <Context>()
            {
                new Context(_masterDefinition.Name, new ContextParameter[0])
            };
            int    formatIndex = buildCompoundGenerator(_masterDefinition, partials, context, generator, format, 0);
            string trailing    = format.Substring(formatIndex);

            if (!trailing.Equals(string.Empty))
            {
                generator.AddGenerator(new StaticMustacheGenerator(trailing, RemoveNewLines));
            }
            return(new MustacheGenerator(generator));
        }
Beispiel #2
0
        private int buildCompoundGenerator(
            TagDefinition tagDefinition,
            Dictionary <string, string> partials,
            List <Context> context,
            CompoundMustacheGenerator generator,
            string format, int formatIndex)
        {
            while (true)
            {
                Match match = findNextTag(tagDefinition, format, formatIndex);

                if (!match.Success)
                {
                    if (tagDefinition.ClosingTags.Any())
                    {
                        string message = String.Format(Resources.MissingClosingTag, tagDefinition.Name);
                        throw new FormatException(message);
                    }
                    break;
                }

                string leading = format.Substring(formatIndex, match.Index - formatIndex);

                if (match.Groups["key"].Success)
                {
                    if (!leading.Equals(string.Empty))
                    {
                        generator.AddGenerator(new StaticMustacheGenerator(leading, RemoveNewLines));
                    }
                    formatIndex = match.Index + match.Length;
                    bool   isExtension = match.Groups["extension"].Success;
                    string key         = match.Groups["key"].Value;
                    string alignment   = match.Groups["alignment"].Value;
                    string formatting  = match.Groups["format"].Value;
                    if (key.StartsWith("@"))
                    {
                        VariableFoundEventArgs args = new VariableFoundEventArgs(key.Substring(1), alignment, formatting, isExtension, context.ToArray());
                        if (VariableFound != null)
                        {
                            VariableFound(this, args);
                            key         = "@" + args.Name;
                            alignment   = args.Alignment;
                            formatting  = args.Formatting;
                            isExtension = args.IsExtension;
                        }
                    }
                    else
                    {
                        PlaceholderFoundEventArgs args = new PlaceholderFoundEventArgs(key, alignment, formatting, isExtension, context.ToArray());
                        if (PlaceholderFound != null)
                        {
                            PlaceholderFound(this, args);
                            key         = args.Key;
                            alignment   = args.Alignment;
                            formatting  = args.Formatting;
                            isExtension = args.IsExtension;
                        }
                    }
                    KeyMustacheGenerator keyGenerator = new KeyMustacheGenerator(key, alignment, formatting, isExtension);
                    generator.AddGenerator(keyGenerator);
                }
                // if we come across a partial template definition
                else if (match.Groups["define"].Success)
                {
                    formatIndex = match.Index + match.Length;

                    // add the template definition to the lookup
                    partials.Add(match.Groups["name"].Value, match.Groups["definition"].Value);
                }
                // if we come across a call for a partial template
                else if (match.Groups["call"].Success)
                {
                    formatIndex = match.Index + match.Length;

                    // include the substring since the last match
                    if (!leading.Equals(string.Empty))
                    {
                        generator.AddGenerator(new StaticMustacheGenerator(leading, RemoveNewLines));
                    }

                    var partialTag = new PartialCallTagDefinition();

                    // retrieve the arguments from the regex
                    ArgumentCollection arguments = getArguments(partialTag, match, context);

                    string name = match.Groups["name"].Value;

                    string partialTemplate;
                    if (partials.TryGetValue(name, out partialTemplate))
                    {
                        bool hasContext = match.Groups["context"].Success;
                        if (hasContext)
                        {
                            // if a special context is to be provided, do it
                            var contextString = match.Groups["context"].Value;
                            var param         = new ContextParameter("context", contextString);
                            context.Add(new Context(partialTag.Name, param));
                        }

                        // include a fully compiled copy of the template
                        CompoundMustacheGenerator partialGenerator = new CompoundMustacheGenerator(partialTag, arguments);
                        int trailingIndex = buildCompoundGenerator(partialTag, partials, context, partialGenerator, partialTemplate, 0);
                        generator.AddGenerator(partialGenerator);

                        // and the part of the template after the last match
                        string trailing = partialTemplate.Substring(trailingIndex);
                        if (!trailing.Equals(string.Empty))
                        {
                            generator.AddGenerator(new StaticMustacheGenerator(trailing, RemoveNewLines));
                        }

                        if (hasContext)
                        {
                            // undo the context change
                            context.RemoveAt(context.Count - 1);
                        }
                    }
                    else
                    {
                        string message = String.Format(Resources.PartialNotDefined, name);
                        throw new FormatException(message);
                    }
                }
                else if (match.Groups["open"].Success)
                {
                    formatIndex = match.Index + match.Length;
                    string        tagName        = match.Groups["name"].Value;
                    TagDefinition nextDefinition = _tagLookup[tagName];
                    if (nextDefinition == null)
                    {
                        string message = String.Format(Resources.UnknownTag, tagName);
                        throw new FormatException(message);
                    }

                    if (!leading.Equals(string.Empty))
                    {
                        generator.AddGenerator(new StaticMustacheGenerator(leading, RemoveNewLines));
                    }
                    ArgumentCollection arguments = getArguments(nextDefinition, match, context);

                    if (nextDefinition.HasContent)
                    {
                        CompoundMustacheGenerator  compoundGenerator = new CompoundMustacheGenerator(nextDefinition, arguments);
                        IEnumerable <TagParameter> contextParameters = nextDefinition.GetChildContextParameters();
                        bool hasContext = contextParameters.Any();
                        if (hasContext)
                        {
                            ContextParameter[] parameters = contextParameters.Select(p => new ContextParameter(p.Name, arguments.GetKey(p))).ToArray();
                            context.Add(new Context(nextDefinition.Name, parameters));
                        }
                        formatIndex = buildCompoundGenerator(nextDefinition, partials, context, compoundGenerator, format, formatIndex);
                        generator.AddGenerator(nextDefinition, compoundGenerator);
                        if (hasContext)
                        {
                            context.RemoveAt(context.Count - 1);
                        }
                    }
                    else
                    {
                        InlineMustacheGenerator inlineGenerator = new InlineMustacheGenerator(nextDefinition, arguments);
                        generator.AddGenerator(inlineGenerator);
                    }
                }
                else if (match.Groups["close"].Success)
                {
                    if (!leading.Equals(string.Empty))
                    {
                        generator.AddGenerator(new StaticMustacheGenerator(leading, RemoveNewLines));
                    }
                    string        tagName        = match.Groups["name"].Value;
                    TagDefinition nextDefinition = _tagLookup[tagName];
                    formatIndex = match.Index;
                    if (tagName == tagDefinition.Name)
                    {
                        formatIndex += match.Length;
                    }
                    break;
                }
                else if (match.Groups["comment"].Success)
                {
                    if (!leading.Equals(string.Empty))
                    {
                        generator.AddGenerator(new StaticMustacheGenerator(leading, RemoveNewLines));
                    }
                    formatIndex = match.Index + match.Length;
                }
                else if (match.Groups["unknown"].Success)
                {
                    string tagName = match.Value;
                    string message = String.Format(Resources.UnknownTag, tagName);
                    throw new FormatException(message);
                }
            }
            return(formatIndex);
        }