Ejemplo n.º 1
0
        public Template Parse(string content, string name)
        {
            Stopwatch stopwatch = null;

            if (log.IsDebugEnabled())
            {
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }

            var template = new Template(name, content);

            log.Verbose("Start: Parsing Template: {0}", template.Name);

            var preTemplate = new PreTokenParser().Parse(content, Options);

            template.Options = preTemplate.Options;

            if (string.IsNullOrWhiteSpace(preTemplate.Name) == false)
            {
                template.Name = preTemplate.Name;
            }

            foreach (var hint in preTemplate.Hints)
            {
                if (template.Hints.Any(t => t == hint) == false)
                {
                    template.Hints.Add(hint);
                }
            }

            foreach (var tag in preTemplate.Tags)
            {
                if (template.Tags.Any(t => t == tag) == false)
                {
                    template.Tags.Add(tag);
                }
            }

            foreach (var preToken in preTemplate.Tokens)
            {
                var token = new Token(preToken.Content);

                if (Options.TrimLeadingWhitespaceInTokenPreamble)
                {
                    if (preToken.Preamble.IsOnlySpaces())
                    {
                        token.Preamble = preToken.Preamble;
                    }
                    else if (string.IsNullOrWhiteSpace(preToken.Preamble))
                    {
                        token.Preamble = preToken.Preamble.TrimLeadingSpaces();
                    }
                    else
                    {
                        token.Preamble = preToken.Preamble.TrimStart();
                    }
                }
                else
                {
                    token.Preamble = preToken.Preamble;
                }

                token.Name               = preToken.Name;
                token.Optional           = preToken.Optional;
                token.Repeating          = preToken.Repeating;
                token.TerminateOnNewLine = preToken.TerminateOnNewline;
                token.Required           = preToken.Required;
                token.Id                 = preToken.Id;
                token.DependsOnId        = preToken.DependsOnId;
                token.IsFrontMatterToken = preToken.IsFrontMatterToken;
                token.IsNull             = preToken.IsNull;
                token.Location           = preToken.Location;
                token.ConsiderOnce       = preToken.ConsiderOnce;

                // All tokens optional if out-of-order enabled
                if (template.Options.OutOfOrderTokens)
                {
                    token.Optional = true;
                }

                ParseTokenDecorators(preToken, token);

                template.AddToken(token);

                if (string.IsNullOrEmpty(token.Name) == false)
                {
                    log.Verbose("  -> Token[{0:000}]: {1}", token.Id, token);
                }
            }

            log.Verbose("Parsed '{0}' - {1:###,###,###,##0} byte(s) in {2}", template.Name, content.Length, stopwatch?.Elapsed.ToString("g"));

            return(template);
        }
Ejemplo n.º 2
0
 public void SetUp()
 {
     parser = new PreTokenParser();
 }