Exemplo n.º 1
0
        /// <summary>
        /// Loads the template.
        /// </summary>
        /// <param name="reader">The object to read the template from.</param>
        /// <remarks>
        /// The <paramref name="reader" /> is read until it ends, but is not
        /// closed or disposed.
        /// </remarks>
        /// <exception cref="NustacheException">
        /// Thrown when the template contains a syntax error.
        /// </exception>
        public void Load(TextReader reader)
        {
            string template = reader.ReadToEnd();

            var scanner = new Scanner();
            var parser = new Parser();

            parser.Parse(this, scanner.Scan(template));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the template.
        /// </summary>
        /// <param name="reader">The object to read the template from.</param>
        /// <remarks>
        /// The <paramref name="reader" /> is read until it ends, but is not
        /// closed or disposed.
        /// </remarks>
        /// <exception cref="NustacheException">
        /// Thrown when the template contains a syntax error.
        /// </exception>
        public void Load(TextReader reader)
        {
            string template = reader.ReadToEnd();

            var scanner = new Scanner(StartDelimiter, EndDelimiter);
            var parser = new Parser();
            parser.Parse(this, scanner.Scan(template));
            // After load get the last state of the delimiters to save in the context.
            StartDelimiter = scanner.StartDelimiter;
            EndDelimiter = scanner.EndDelimiter;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads a Template from a TextReader
        /// </summary>
        /// <param name="reader">The TextReader containing the Template</param>
        public new void Load(TextReader reader)
        {
            // Get the contents of the template from the TextReader
            var template = reader.ReadToEnd();

            // Replace any Mustache variables with the values in the Pattern Parameters
            foreach (var parameter in _parameters)
            {
                var value = parameter.Value.ToString();
                var key = parameter.Key;

                if (value.Equals(bool.TrueString, StringComparison.InvariantCultureIgnoreCase))
                {
                    // If 'true' strip out {{# }} sections and set {{^ }} sections to empty
                    template = Regex.Replace(template, @"{{#\s?" + key + @"\s?}}(.*?)?{{/\s?" + key + @"\s?}}", @"$1", RegexOptions.Singleline);
                    template = Regex.Replace(template, @"{{\^\s?" + key + @"\s?}}(.*?)?{{/\s?" + key + @"\s?}}", string.Empty, RegexOptions.Singleline);
                }
                else if (value.Equals(bool.FalseString, StringComparison.InvariantCultureIgnoreCase))
                {
                    // If 'true' strip out {{^ }} sections and set {{# }} sections to empty
                    template = Regex.Replace(template, @"{{\^\s?" + key + @"\s?}}(.*?)?{{/\s?" + key + @"\s?}}", @"$1", RegexOptions.Singleline);
                    template = Regex.Replace(template, @"{{#\s?" + key + @"\s?}}(.*?)?{{/\s?" + key + @"\s?}}", string.Empty, RegexOptions.Singleline);
                }
                else
                {
                    // Replace variables with value
                    template = Regex.Replace(template, @"{{\s?" + key + @"\s?}}", value.Replace(PatternProvider.IdentifierParameterString.ToString(), string.Empty));
                }
            }

            // Scan and parse the template with the Pattern Lab specific instance of Nustache classes
            var scanner = new MustacheScanner();
            var parser = new Parser();

            parser.Parse(this, scanner.Scan(template));
        }