public BlockClosingAction RenderSilentEval(ViewSourceReader viewSourceReader, TemplateClassBuilder builder)
        {
            var code = viewSourceReader.CurrentInputLine.NormalizedText;

            builder.AppendSilentCode(code, false);

            if( viewSourceReader.IsBlock )
            {
                builder.BeginCodeBlock();

                if( !viewSourceReader.CurrentInputLine.NormalizedText.Trim().Split( ' ' )[0].ToUpperInvariant().Equals( "CASE" ) )
                {
                    return () =>
                      {
                          if( (viewSourceReader.CurrentInputLine.Text.TrimStart().StartsWith(SilentEvalMarkupRule.SignifierChar)) &&
                            MidBlockKeywords.Contains( viewSourceReader.CurrentInputLine.NormalizedText.Trim().Split( ' ' )[0].ToUpperInvariant() ) )
                          {
                              builder.Unindent();
                          }
                          else
                          {
                              builder.EndCodeBlock();
                          }
                      };
                }
            }

            return MarkupRule.EmptyClosingAction;
        }
        public BlockClosingAction RenderSilentEval(ViewSourceReader viewSourceReader, TemplateClassBuilder builder)
        {
            var code = viewSourceReader.CurrentInputLine.NormalizedText;

            var lambdaMatch = LambdaRegex.Match( code );

            var templateClassBuilder = (BooTemplateClassBuilder)builder;
            if( !lambdaMatch.Success )
            {
                templateClassBuilder.AppendSilentCode(code, !viewSourceReader.IsBlock);

                if( viewSourceReader.IsBlock )
                {
                    templateClassBuilder.BeginCodeBlock();

                    return templateClassBuilder.EndCodeBlock;
                }

                return MarkupRule.EmptyClosingAction;
            }

            var depth = viewSourceReader.CurrentInputLine.IndentCount;

            templateClassBuilder.AppendChangeOutputDepth( depth, true );
            templateClassBuilder.AppendSilentCode( code, false );
            templateClassBuilder.BeginCodeBlock();

            return () =>
              {
                  templateClassBuilder.AppendChangeOutputDepth( depth, false );
                  templateClassBuilder.EndCodeBlock();
              };
        }
Beispiel #3
0
        protected static void AppendText(string text, TemplateClassBuilder builder, TemplateOptions options)
        {
            var encodeHtml = options.EncodeHtml;

            if (text.StartsWith("!"))
            {
                text = text.Substring(1, text.Length - 1);
                text.TrimStart(' ');
                encodeHtml = false;
            }
            if (text.StartsWith("&"))
            {
                text = text.Substring(1, text.Length - 1);
                text.TrimStart(' ');
                encodeHtml = true;
            }

            var parser = new ExpressionStringParser(text);

            parser.Parse();
            foreach (var expressionStringToken in parser.ExpressionStringTokens)
            {
                if (expressionStringToken.IsExpression)
                {
                    builder.AppendCode(expressionStringToken.Value, encodeHtml);
                }
                else
                {
                    builder.AppendOutput(expressionStringToken.Value);
                }
            }
        }
Beispiel #4
0
        public TemplateParser(TemplateOptions options, TemplateClassBuilder templateClassBuilder, IList <IViewSource> viewSources)
        {
            BlockClosingActions = new Stack <BlockClosingAction>();

            viewSourceReader = new ViewSourceReader(options, viewSources);
            Options          = options;
            builder          = templateClassBuilder;
        }
Beispiel #5
0
        private static void ParseAndRenderAttributes(TemplateClassBuilder builder, Match tagMatch)
        {
            var idAndClasses   = tagMatch.Groups[2].Value;
            var attributesHash = tagMatch.Groups[4].Value.Trim();

            var match = _idClassesRegex.Match(idAndClasses);

            var classes = new List <string>();

            foreach (Capture capture in match.Groups[2].Captures)
            {
                classes.Add(capture.Value);
            }

            if (classes.Count > 0)
            {
                attributesHash = PrependAttribute(attributesHash, Class, string.Join(" ", classes.ToArray()));
            }

            string id = null;

            foreach (Capture capture in match.Groups[1].Captures)
            {
                id = capture.Value;
                break;
            }

            if (!string.IsNullOrEmpty(id))
            {
                attributesHash = PrependAttribute(attributesHash, Id, id);
            }

            if (string.IsNullOrEmpty(attributesHash))
            {
                return;
            }

            var attributeParser = new AttributeParser(attributesHash);

            attributeParser.Parse();
            foreach (var attribute in attributeParser.Attributes)
            {
                if (attribute.Type == ParsedAttributeType.String)
                {
                    var expressionStringParser = new ExpressionStringParser(attribute.Value);

                    expressionStringParser.Parse();

                    builder.AppendAttributeTokens(attribute.Schema, attribute.Name, expressionStringParser.ExpressionStringTokens);
                }
                else
                {
                    var token = new ExpressionStringToken(attribute.Value, true);

                    builder.AppendAttributeTokens(attribute.Schema, attribute.Name, new[] { token });
                }
            }
        }
Beispiel #6
0
        private static void BeforeCompile(TemplateClassBuilder templateClassBuilder, object context)
        {
            var templateBuilderContext = (TemplateBuilderContext)context;
            var codeDomClassBuilder    = (CodeDomClassBuilder)templateClassBuilder;

            foreach (var pair in templateBuilderContext.Helpers)
            {
                var property = new CodeMemberField
                {
                    Name       = pair.Key,
                    Type       = new CodeTypeReference(pair.Value),
                    Attributes = MemberAttributes.Public,
                };
                codeDomClassBuilder.Members.Add(property);
            }
        }
        public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var templateSource = builder.Build(options.Usings);

            var typeBuilder = new BooTemplateTypeBuilder(options);

            var templateType = typeBuilder.Build(templateSource, builder.ClassName);

            if( templateType == null )
            {
                var path = ListExtensions.Last(viewSourceReader.ViewSources).Path;
                TemplateCompilationException.Throw( typeBuilder.CompilerResults,typeBuilder.Source, path );
            }

            return new TemplateFactory( templateType );
        }
        public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var ruby = new StringBuilder();

            foreach (var reference in options.References)
            {
                ruby.AppendLine( string.Format("require '{0}'", reference) );
            }

            ruby.Append(builder.Build(options.Usings));

            var templateSource = ruby.ToString();
            _scriptEngine.Execute( templateSource );

            return CreateTemplateFactory( _scriptEngine, builder.ClassName );
        }
Beispiel #9
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var content = viewSourceReader.CurrentInputLine.NormalizedText.Trim().Replace("\"", "\"\"");

            var indexOfEquals = content.IndexOf('=');
            var key           = content.Substring(0, indexOfEquals).Trim();
            var value         = content.Substring(indexOfEquals + 1, content.Length - indexOfEquals - 1).Trim();

            builder.Meta[key] = value;

            return(EmptyClosingAction);
        }
Beispiel #10
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var currentInputLine = viewSourceReader.CurrentInputLine;
            var match            = _commentRegex.Match(currentInputLine.NormalizedText);

            if (!match.Success)
            {
                SyntaxException.Throw(currentInputLine, ErrorParsingTag, currentInputLine);
            }

            var ieBlock = match.Groups[1].Value;
            var content = match.Groups[2].Value;

            var openingTag = new StringBuilder(currentInputLine.Indent);

            openingTag.Append("<!--");
            var closingTag = new StringBuilder("-->");

            if (!string.IsNullOrEmpty(ieBlock))
            {
                openingTag.AppendFormat("{0}>", ieBlock);
                closingTag.Insert(0, "<![endif]");
            }

            if (string.IsNullOrEmpty(content))
            {
                builder.AppendOutput(openingTag.ToString());
                builder.AppendOutputLine();
                closingTag.Insert(0, currentInputLine.Indent);
            }
            else
            {
                if (content.Length > 50)
                {
                    builder.AppendOutput(openingTag.ToString());
                    builder.AppendOutputLine();

                    builder.AppendOutput(viewSourceReader.NextIndent);

                    builder.AppendOutput(content);

                    builder.AppendOutputLine();
                }
                else
                {
                    builder.AppendOutput(openingTag.ToString());
                    builder.AppendOutput(content);
                    closingTag.Insert(0, ' ');
                }
            }

            return(() =>
            {
                builder.AppendOutput(closingTag.ToString());
                builder.AppendOutputLine();
            });
        }
Beispiel #11
0
        public BlockClosingAction RenderSilentEval(ViewSourceReader viewSourceReader, TemplateClassBuilder builder)
        {
            var code = viewSourceReader.CurrentInputLine.NormalizedText;

            builder.AppendSilentCode(code, false);

            if (viewSourceReader.IsBlock)
            {
                builder.BeginCodeBlock();

                if (!viewSourceReader.CurrentInputLine.NormalizedText.Trim().Split(' ')[0].ToUpperInvariant().Equals("CASE"))
                {
                    return(() =>
                    {
                        if ((viewSourceReader.CurrentInputLine.Text.TrimStart().StartsWith(SilentEvalMarkupRule.SignifierChar)) &&
                            MidBlockKeywords.Contains(viewSourceReader.CurrentInputLine.NormalizedText.Trim().Split(' ')[0].ToUpperInvariant()))
                        {
                            builder.Unindent();
                        }
                        else
                        {
                            builder.EndCodeBlock();
                        }
                    });
                }
            }

            return(MarkupRule.EmptyClosingAction);
        }
Beispiel #12
0
        public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var ruby = new StringBuilder();

            foreach (var reference in options.References)
            {
                ruby.AppendLine(string.Format("require '{0}'", reference));
            }

            ruby.Append(builder.Build(options.Usings));

            var templateSource = ruby.ToString();

            _scriptEngine.Execute(templateSource);

            return(CreateTemplateFactory(_scriptEngine, builder.ClassName));
        }
Beispiel #13
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var currentInputLine = viewSourceReader.CurrentInputLine;
            var content          = currentInputLine.NormalizedText.Trim().ToLowerInvariant();

            if (string.IsNullOrEmpty(content))
            {
                builder.AppendOutput(@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">");
            }
            else if (string.Equals(content, "1.1"))
            {
                builder.AppendOutput(@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">");
            }
            else if (string.Equals(content, "strict"))
            {
                builder.AppendOutput(@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">");
            }
            else if (string.Equals(content, "frameset"))
            {
                builder.AppendOutput(@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Frameset//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"">");
            }
            else if (string.Equals(content, "html"))
            {
                builder.AppendOutput(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">");
            }
            else if (string.Equals(content, "html strict"))
            {
                builder.AppendOutput(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01//EN"" ""http://www.w3.org/TR/html4/strict.dtd"">");
            }
            else if (string.Equals(content, "html frameset"))
            {
                builder.AppendOutput(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Frameset//EN"" ""http://www.w3.org/TR/html4/frameset.dtd"">");
            }
            else
            {
                var parts = content.Split(' ');

                if (string.Equals(parts[0], "xml"))
                {
                    var encoding = "utf-8";

                    if (parts.Length == 2)
                    {
                        encoding = parts[1];
                    }

                    var invariant = Utility.FormatInvariant(@"<?xml version=""1.0"" encoding=""{0}"" ?>", encoding);
                    builder.AppendOutput(invariant);
                }
                else
                {
                    SyntaxException.Throw(currentInputLine, ErrorParsingTag, currentInputLine);
                }
            }

            builder.AppendOutputLine();

            return(EmptyClosingAction);
        }
Beispiel #14
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var text         = viewSourceReader.CurrentInputLine.NormalizedText.TrimStart();
            var indexOfSpace = text.IndexOf(' ');
            var sectionName  = text.Substring(indexOfSpace + 1, text.Length - indexOfSpace - 1);
            var code         = string.Format("{0}).AddSection(\"{1}\", x =>", viewSourceReader.CurrentInputLine.Indent, sectionName);

            builder.EndCodeBlock();
            builder.Indent();

            builder.AppendSilentCode(code, false);
            builder.BeginCodeBlock();


            return(builder.Unindent);
        }
Beispiel #15
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            builder.AppendOutput(viewSourceReader.CurrentInputLine.Indent);
            builder.AppendOutput(viewSourceReader.CurrentInputLine.NormalizedText);
            builder.AppendOutputLine();

            return(EmptyClosingAction);
        }
Beispiel #16
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var currentInputLine = viewSourceReader.CurrentInputLine;
            var input            = PreprocessLine(currentInputLine);
            var match            = _tagRegex.Match(input);

            if (!match.Success)
            {
                SyntaxException.Throw(currentInputLine, ErrorParsingTag, currentInputLine);
            }

            var groups  = match.Groups;
            var tagName = groups[1].Value.Replace("\\", string.Empty);

            var isWhitespaceSensitive = _whitespaceSensitiveTags.Contains(tagName);
            var openingTag            = string.Format("{0}<{1}", currentInputLine.Indent, tagName);
            var closingTag            = string.Format("</{0}>", tagName);

            builder.AppendOutput(openingTag);

            ParseAndRenderAttributes(builder, match);

            var action = groups[6].Value.Trim();

            if (string.Equals("/", action) || options.IsAutoClosingTag(tagName))
            {
                builder.AppendOutput(" />");
                builder.AppendOutputLine();
                return(EmptyClosingAction);
            }

            var content = groups[7].Value.Trim();

            if (string.IsNullOrEmpty(content))
            {
                builder.AppendOutput(">");
                builder.AppendOutputLine();
                closingTag = currentInputLine.Indent + closingTag;
            }
            else
            {
                if ((content.Length > 50) || ("=" == action) || ("&=" == action) || ("!=" == action))
                {
                    builder.AppendOutput(">");

                    if (!isWhitespaceSensitive)
                    {
                        builder.AppendOutputLine();
                        builder.AppendOutput(viewSourceReader.NextIndent);
                    }

                    if (string.Equals("=", action))
                    {
                        builder.AppendCode(content, options.EncodeHtml);
                    }
                    else if (string.Equals("&=", action))
                    {
                        builder.AppendCode(content, true);
                    }
                    else if (string.Equals("!=", action))
                    {
                        builder.AppendCode(content, false);
                    }
                    else
                    {
                        AppendText(content, builder, options);
                    }

                    if (!isWhitespaceSensitive)
                    {
                        builder.AppendOutputLine();
                        closingTag = currentInputLine.Indent + closingTag;
                    }
                }
                else
                {
                    builder.AppendOutput(">");
                    AppendText(content, builder, options);
                    if ((currentInputLine.IndentCount + 1) == viewSourceReader.NextInputLine.IndentCount)
                    {
                        builder.AppendOutputLine();
                        closingTag = currentInputLine.Indent + closingTag;
                    }
                }
            }

            return(() =>
            {
                builder.AppendOutput(closingTag);
                builder.AppendOutputLine();
            });
        }
Beispiel #17
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var code = viewSourceReader.CurrentInputLine.NormalizedText.Trim();

            if (!string.IsNullOrEmpty(code))
            {
                builder.AppendPreambleCode(code);
            }

            return(EmptyClosingAction);
        }
Beispiel #18
0
 public abstract BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder);
Beispiel #19
0
        public BlockClosingAction RenderSilentEval(ViewSourceReader viewSourceReader, TemplateClassBuilder builder)
        {
            var code = viewSourceReader.CurrentInputLine.NormalizedText;

            var lambdaMatch = LambdaRegex.Match(code);

            var templateClassBuilder = (BooTemplateClassBuilder)builder;

            if (!lambdaMatch.Success)
            {
                templateClassBuilder.AppendSilentCode(code, !viewSourceReader.IsBlock);

                if (viewSourceReader.IsBlock)
                {
                    templateClassBuilder.BeginCodeBlock();

                    return(templateClassBuilder.EndCodeBlock);
                }

                return(MarkupRule.EmptyClosingAction);
            }

            var depth = viewSourceReader.CurrentInputLine.IndentCount;

            templateClassBuilder.AppendChangeOutputDepth(depth, true);
            templateClassBuilder.AppendSilentCode(code, false);
            templateClassBuilder.BeginCodeBlock();

            return(() =>
            {
                templateClassBuilder.AppendChangeOutputDepth(depth, false);
                templateClassBuilder.EndCodeBlock();
            });
        }
Beispiel #20
0
        public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var templateSource = builder.Build(options.Usings);

            var typeBuilder = new BooTemplateTypeBuilder(options);

            var templateType = typeBuilder.Build(templateSource, builder.ClassName);

            if (templateType == null)
            {
                var path = ListExtensions.Last(viewSourceReader.ViewSources).Path;
                TemplateCompilationException.Throw(typeBuilder.CompilerResults, typeBuilder.Source, path);
            }

            return(new TemplateFactory(templateType));
        }
Beispiel #21
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var partialName = viewSourceReader.CurrentInputLine.NormalizedText.Trim();

            if (string.IsNullOrEmpty(partialName))
            {
                if (viewSourceReader.ViewSourceQueue.Count == 0)
                {
                    throw new InvalidOperationException(NoPartialName);
                }
                var templatePath = viewSourceReader.ViewSourceQueue.Dequeue();
                viewSourceReader.MergeTemplate(templatePath, true);
            }
            else
            {
                partialName = partialName.Insert(partialName.LastIndexOf(@"\", StringComparison.OrdinalIgnoreCase) + 1, "_");
                var viewSource = options.TemplateContentProvider.GetViewSource(partialName, viewSourceReader.ViewSources);
                viewSourceReader.ViewSourceModifiedChecks.Add(() => viewSource.IsModified);
                viewSourceReader.MergeTemplate(viewSource, true);
            }

            return(EmptyClosingAction);
        }
Beispiel #22
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var dictionary = string.Empty;

            var    text         = viewSourceReader.CurrentInputLine.NormalizedText.Trim();
            var    indexOfSpace = text.IndexOf(' ');
            string componentName;

            if (indexOfSpace == -1)
            {
                componentName = text.Trim();
            }
            else
            {
                dictionary = text.Substring(indexOfSpace, text.Length - indexOfSpace);
                dictionary = dictionary.Trim();
                Debug.Assert(dictionary.StartsWith("{"), "dictionary must start with '{'");
                Debug.Assert(dictionary.EndsWith("}"), "dictionary must start with '}'");
                dictionary    = dictionary.Substring(1, dictionary.Length - 2);
                componentName = text.Substring(0, indexOfSpace);
            }


            var codeDomClassBuilder = (CodeDomClassBuilder)builder;

            var dictionaryLocalVariable = AppendCreateDictionaryLocalVariable(dictionary, codeDomClassBuilder);

            codeDomClassBuilder.CurrentTextWriterVariableName = "x";
            var code = string.Format("{0}Component(\"{1}\", {2}, (x) =>",
                                     viewSourceReader.CurrentInputLine.Indent, componentName, dictionaryLocalVariable);

            codeDomClassBuilder.AppendSilentCode(code, false);
            codeDomClassBuilder.BeginCodeBlock();
            return(() =>
            {
                codeDomClassBuilder.EndCodeBlock();
                codeDomClassBuilder.AppendSilentCode(").Render();", false);
                codeDomClassBuilder.CurrentTextWriterVariableName = TemplateClassBuilder.DefaultTextWriterVariableName;
            });
        }
Beispiel #23
0
 public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
 {
     return(options.TemplateCompiler.RenderSilentEval(viewSourceReader, builder));
 }
Beispiel #24
0
 public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
 {
     return(EmptyClosingAction);
 }
Beispiel #25
0
        public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder)
        {
            var inputLine = viewSourceReader.CurrentInputLine;

            builder.AppendOutput(inputLine.Indent);
            builder.AppendCode(inputLine.NormalizedText.Trim(), options.EncodeHtml);

            builder.AppendOutputLine();
            return(EmptyClosingAction);
        }