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); } } }
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, "_"); IViewSource viewSource; try { viewSource = options.TemplateContentProvider.GetViewSource(partialName, viewSourceReader.ViewSources); } catch (FileNotFoundException) { //ToDo: render content behind the ? tag viewSource = null; } viewSourceReader.ViewSourceModifiedChecks.Add(() => viewSource.IsModified); viewSourceReader.MergeTemplate(viewSource, true); } return EmptyClosingAction; }
public BlockClosingAction RenderSilentEval(ViewSourceReader viewSourceReader, TemplateClassBuilder builder) { var code = viewSourceReader.CurrentInputLine.NormalizedText; var lambdaMatch = lambdaRegex.Match( code ); if( !lambdaMatch.Success ) { builder.AppendSilentCode(code, !viewSourceReader.IsBlock); if( viewSourceReader.IsBlock ) { builder.BeginCodeBlock(); return builder.EndCodeBlock; } return MarkupRule.EmptyClosingAction; } var depth = viewSourceReader.CurrentInputLine.IndentCount; code = TranslateLambda( code, lambdaMatch ); builder.AppendChangeOutputDepth(depth); builder.AppendSilentCode(code, true); return () => { builder.AppendChangeOutputDepth(depth); builder.AppendSilentCode("})", true); }; }
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; }
public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder) { builder.AppendOutput(viewSourceReader.CurrentInputLine.Indent); builder.AppendOutput(viewSourceReader.CurrentInputLine.NormalizedText); builder.AppendOutputLine(); return EmptyClosingAction; }
public TemplateParser(TemplateOptions options, TemplateClassBuilder templateClassBuilder, IList<IViewSource> viewSources) { BlockClosingActions = new Stack<BlockClosingAction>(); viewSourceReader = new ViewSourceReader(options, viewSources); Options = options; builder = templateClassBuilder; }
public override BlockClosingAction Render(HamlNode node, TemplateOptions options, TemplateClassBuilder builder) { //var inputLine = viewSourceReader.CurrentInputLine; //builder.AppendOutput( inputLine.Indent ); //builder.AppendCode( inputLine.NormalizedText.Trim(), false ); //builder.AppendOutputLine(); return EmptyClosingAction; }
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(); }; }
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; }
public override BlockClosingAction Render(HamlNode node, TemplateOptions options, TemplateClassBuilder builder) { //int currentLineIndentCount = viewSourceReader.CurrentInputLine.IndentCount; //while ((viewSourceReader.NextInputLine != null) // && (viewSourceReader.NextInputLine.IndentCount > currentLineIndentCount)) //{ // viewSourceReader.MoveNext(); //} return null; }
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; }
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; }
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; }
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 typeBuilder = CreateTemplateTypeBuilder(options); //TODO: leaky abstraction var classBuilder = (CodeDomClassBuilder) builder; var provider = GetCodeDomProvider(typeBuilder.ProviderOptions); classBuilder.CodeDomProvider = provider; typeBuilder.CodeDomProvider = provider; var templateSource = classBuilder.Build(options.Usings); var templateType = typeBuilder.Build( templateSource, classBuilder.ClassName ); if( templateType == null ) { var viewSources = viewSourceReader.ViewSources; TemplateCompilationException.Throw(typeBuilder.CompilerResults, typeBuilder.Source, ListExtensions.Last(viewSources).Path); } return new TemplateFactory( templateType ); }
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; }; }
public override BlockClosingAction Render(HamlNode node, 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; }
public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder) { return options.TemplateCompiler.RenderSilentEval(viewSourceReader, builder); }
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 }); } } }
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(); }; }
public override BlockClosingAction Render(HamlNode hamlNode, TemplateOptions options, TemplateClassBuilder builder) { //return options.TemplateCompiler.RenderSilentEval(viewSourceReader, builder); return null; }
public abstract BlockClosingAction Render(HamlNode node, TemplateOptions options, TemplateClassBuilder builder);
public BlockClosingAction RenderSilentEval(ViewSourceReader viewSourceReader, TemplateClassBuilder builder) { var code = viewSourceReader.CurrentInputLine.NormalizedText; var lambdaMatch = lambdaRegex.Match(code); if (!lambdaMatch.Success) { builder.AppendSilentCode(code, !viewSourceReader.IsBlock); if (viewSourceReader.IsBlock) { builder.BeginCodeBlock(); return(builder.EndCodeBlock); } return(MarkupRule.EmptyClosingAction); } var depth = viewSourceReader.CurrentInputLine.IndentCount; code = TranslateLambda(code, lambdaMatch); builder.AppendChangeOutputDepth(depth); builder.AppendSilentCode(code, true); return(() => { builder.AppendChangeOutputDepth(depth); builder.AppendSilentCode("})", true); }); }
public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder) { var typeBuilder = CreateTemplateTypeBuilder(options); //TODO: leaky abstraction var classBuilder = (CodeDomClassBuilder)builder; var provider = GetCodeDomProvider(typeBuilder.ProviderOptions); classBuilder.CodeDomProvider = provider; typeBuilder.CodeDomProvider = provider; var templateSource = classBuilder.Build(options.Usings); var templateType = typeBuilder.Build(templateSource, classBuilder.ClassName); if (templateType == null) { var viewSources = viewSourceReader.ViewSources; TemplateCompilationException.Throw(typeBuilder.CompilerResults, typeBuilder.Source, ListExtensions.Last(viewSources).Path); } return(new TemplateFactory(templateType)); }
public override BlockClosingAction Render(HamlNode node, 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 if (string.Equals(content, "mobile")) //{ // builder.AppendOutput("<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\">"); //} //else if (string.Equals(content, "basic")) //{ // builder.AppendOutput("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.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; }
public override BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder) { return EmptyClosingAction; }
public abstract BlockClosingAction Render(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder);