From 44b50b46f91d169bbf295a6e6e8bbf22376ab3a0 Mon Sep 17 00:00:00 2001 From: SonjaKhan Date: Tue, 28 Oct 2014 15:42:09 -0700 Subject: [PATCH] updating the page generator and base view to work with razor helper functions --- .../Views/AttributeValue.cs | 40 ++ .../Views/BaseView.cs | 514 ++++++------------ .../Views/HelperResult.cs | 34 ++ src/PageGenerator/Program.cs | 10 + 4 files changed, 241 insertions(+), 357 deletions(-) create mode 100644 src/Microsoft.AspNet.Diagnostics/Views/AttributeValue.cs create mode 100644 src/Microsoft.AspNet.Diagnostics/Views/HelperResult.cs diff --git a/src/Microsoft.AspNet.Diagnostics/Views/AttributeValue.cs b/src/Microsoft.AspNet.Diagnostics/Views/AttributeValue.cs new file mode 100644 index 0000000000..55947d26ea --- /dev/null +++ b/src/Microsoft.AspNet.Diagnostics/Views/AttributeValue.cs @@ -0,0 +1,40 @@ +using System; + +namespace Microsoft.AspNet.Diagnostics.Views +{ + public class AttributeValue + { + public AttributeValue(Tuple prefix, Tuple value, bool literal) + { + Prefix = prefix; + Value = value; + Literal = literal; + } + + public Tuple Prefix { get; } + + public Tuple Value { get; } + + public bool Literal { get; } + + public static AttributeValue FromTuple(Tuple, Tuple, bool> value) + { + return new AttributeValue(value.Item1, value.Item2, value.Item3); + } + + public static AttributeValue FromTuple(Tuple, Tuple, bool> value) + { + return new AttributeValue(value.Item1, new Tuple(value.Item2.Item1, value.Item2.Item2), value.Item3); + } + + public static implicit operator AttributeValue(Tuple, Tuple, bool> value) + { + return FromTuple(value); + } + + public static implicit operator AttributeValue(Tuple, Tuple, bool> value) + { + return FromTuple(value); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs b/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs index ee2c49d477..d0979c1ce3 100644 --- a/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs +++ b/src/Microsoft.AspNet.Diagnostics/Views/BaseView.cs @@ -61,367 +61,100 @@ namespace Microsoft.AspNet.Diagnostics.Views /// protected void WriteLiteral(string value) { - Output.Write(value); + WriteLiteralTo(Output, value); } /// - /// - /// - /// - /// - /// - /// - /// - protected void WriteAttribute( - string name, - Tuple leader, - Tuple trailer, - Tuple, Tuple, bool> part1) - { - if (name == null) - { - throw new ArgumentNullException("name"); - } - if (leader == null) - { - throw new ArgumentNullException("leader"); - } - if (trailer == null) - { - throw new ArgumentNullException("trailer"); - } - if (part1 == null) - { - throw new ArgumentNullException("part1"); - } - WriteLiteral(leader.Item1); - WriteLiteral(part1.Item1.Item1); - Write(part1.Item2.Item1); - WriteLiteral(trailer.Item1); - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - protected void WriteAttribute( - string name, - Tuple leader, - Tuple trailer, - Tuple, Tuple, bool> part1, - Tuple, Tuple, bool> part2) - { - if (name == null) - { - throw new ArgumentNullException("name"); - } - if (leader == null) - { - throw new ArgumentNullException("leader"); - } - if (trailer == null) - { - throw new ArgumentNullException("trailer"); - } - if (part1 == null) - { - throw new ArgumentNullException("part1"); - } - if (part2 == null) - { - throw new ArgumentNullException("part2"); - } - WriteLiteral(leader.Item1); - WriteLiteral(part1.Item1.Item1); - Write(part1.Item2.Item1); - WriteLiteral(part2.Item1.Item1); - Write(part2.Item2.Item1); - WriteLiteral(trailer.Item1); - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - protected void WriteAttribute( - string name, - Tuple leader, - Tuple trailer, - Tuple, Tuple, bool> part1, - Tuple, Tuple, bool> part2, - Tuple, Tuple, bool> part3) - { - if (name == null) - { - throw new ArgumentNullException("name"); - } - if (leader == null) - { - throw new ArgumentNullException("leader"); - } - if (trailer == null) - { - throw new ArgumentNullException("trailer"); - } - if (part1 == null) - { - throw new ArgumentNullException("part1"); - } - if (part2 == null) - { - throw new ArgumentNullException("part2"); - } - if (part3 == null) - { - throw new ArgumentNullException("part3"); - } - WriteLiteral(leader.Item1); - WriteLiteral(part1.Item1.Item1); - Write(part1.Item2.Item1); - WriteLiteral(part2.Item1.Item1); - Write(part2.Item2.Item1); - WriteLiteral(part3.Item1.Item1); - Write(part3.Item2.Item1); - WriteLiteral(trailer.Item1); - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - protected void WriteAttribute( - string name, - Tuple leader, - Tuple trailer, - Tuple, Tuple, bool> part1, - Tuple, Tuple, bool> part2, - Tuple, Tuple, bool> part3, - Tuple, Tuple, bool> part4) - { - if (name == null) - { - throw new ArgumentNullException("name"); - } - if (leader == null) - { - throw new ArgumentNullException("leader"); - } - if (trailer == null) - { - throw new ArgumentNullException("trailer"); - } - if (part1 == null) - { - throw new ArgumentNullException("part1"); - } - if (part2 == null) - { - throw new ArgumentNullException("part2"); - } - if (part3 == null) - { - throw new ArgumentNullException("part3"); - } - if (part4 == null) - { - throw new ArgumentNullException("part4"); - } - WriteLiteral(leader.Item1); - WriteLiteral(part1.Item1.Item1); - Write(part1.Item2.Item1); - WriteLiteral(part2.Item1.Item1); - Write(part2.Item2.Item1); - WriteLiteral(part3.Item1.Item1); - Write(part3.Item2.Item1); - WriteLiteral(part4.Item1.Item1); - Write(part4.Item2.Item1); - WriteLiteral(trailer.Item1); - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - protected void WriteAttribute( - string name, - Tuple leader, - Tuple trailer, - Tuple, Tuple, bool> part1, - Tuple, Tuple, bool> part2, - Tuple, Tuple, bool> part3, - Tuple, Tuple, bool> part4, - Tuple, Tuple, bool> part5) - { - if (name == null) - { - throw new ArgumentNullException("name"); - } - if (leader == null) - { - throw new ArgumentNullException("leader"); - } - if (trailer == null) - { - throw new ArgumentNullException("trailer"); - } - if (part1 == null) - { - throw new ArgumentNullException("part1"); - } - if (part2 == null) - { - throw new ArgumentNullException("part2"); - } - if (part3 == null) - { - throw new ArgumentNullException("part3"); - } - if (part4 == null) - { - throw new ArgumentNullException("part4"); - } - if (part5 == null) - { - throw new ArgumentNullException("part5"); - } - WriteLiteral(leader.Item1); - WriteLiteral(part1.Item1.Item1); - Write(part1.Item2.Item1); - WriteLiteral(part2.Item1.Item1); - Write(part2.Item2.Item1); - WriteLiteral(part3.Item1.Item1); - Write(part3.Item2.Item1); - WriteLiteral(part4.Item1.Item1); - Write(part4.Item2.Item1); - WriteLiteral(part5.Item1.Item1); - Write(part5.Item2.Item1); - WriteLiteral(trailer.Item1); - } - - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - protected void WriteAttribute( - string name, - Tuple leader, - Tuple trailer, - Tuple, Tuple, bool> part1, - Tuple, Tuple, bool> part2, - Tuple, Tuple, bool> part3, - Tuple, Tuple, bool> part4, - Tuple, Tuple, bool> part5, - Tuple, Tuple, bool> part6) - { - if (name == null) - { - throw new ArgumentNullException("name"); - } - if (leader == null) - { - throw new ArgumentNullException("leader"); - } - if (trailer == null) - { - throw new ArgumentNullException("trailer"); - } - if (part1 == null) - { - throw new ArgumentNullException("part1"); - } - if (part2 == null) - { - throw new ArgumentNullException("part2"); - } - if (part3 == null) - { - throw new ArgumentNullException("part3"); - } - if (part4 == null) - { - throw new ArgumentNullException("part4"); - } - if (part5 == null) - { - throw new ArgumentNullException("part5"); - } - if (part6 == null) - { - throw new ArgumentNullException("part6"); - } - WriteLiteral(leader.Item1); - WriteLiteral(part1.Item1.Item1); - Write(part1.Item2.Item1); - WriteLiteral(part2.Item1.Item1); - Write(part2.Item2.Item1); - WriteLiteral(part3.Item1.Item1); - Write(part3.Item2.Item1); - WriteLiteral(part4.Item1.Item1); - Write(part4.Item2.Item1); - WriteLiteral(part5.Item1.Item1); - Write(part5.Item2.Item1); - WriteLiteral(part6.Item1.Item1); - Write(part6.Item2.Item1); - WriteLiteral(trailer.Item1); - } - - /// - /// Html encode and write + /// Write the given value directly to the output /// /// - private void WriteEncoded(string value) + protected void WriteLiteral(object value) { - Output.Write(WebUtility.HtmlEncode(value)); + WriteLiteralTo(Output, value); + } + + /// + /// Writes the given attribute to the output + /// + /// The name of the attribute to write + /// The value and position of the prefix + /// The value and position of the suffix + /// The s to write. + protected void WriteAttribute( + [NotNull] string name, + [NotNull] Tuple leader, + [NotNull] Tuple trailer, + params AttributeValue[] values) + { + WriteAttributeTo( + Output, + name, + leader, + trailer, + values); + } + + /// + /// Writes the given attribute to the given writer + /// + /// The instance to write to. + /// The name of the attribute to write + /// The value and position of the prefix + /// The value and position of the suffix + /// The s to write. + protected void WriteAttributeTo( + [NotNull] TextWriter writer, + [NotNull] string name, + [NotNull] Tuple leader, + [NotNull] Tuple trailer, + params AttributeValue[] values) + { + + WriteLiteralTo(writer, leader.Item1); + foreach (var value in values) + { + WriteLiteralTo(writer, value.Prefix.Item1); + + // The special cases here are that the value we're writing might already be a string, or that the + // value might be a bool. If the value is the bool 'true' we want to write the attribute name + // instead of the string 'true'. If the value is the bool 'false' we don't want to write anything. + // Otherwise the value is another object (perhaps an HtmlString) and we'll ask it to format itself. + string stringValue; + if (value.Value.Item1 is bool) + { + if ((bool)value.Value.Item1) + { + stringValue = name; + } + else + { + continue; + } + } + else + { + stringValue = value.Value.Item1 as string; + } + + // Call the WriteTo(string) overload when possible + if (value.Literal && stringValue != null) + { + WriteLiteralTo(writer, stringValue); + } + else if (value.Literal) + { + WriteLiteralTo(writer, value.Value.Item1); + } + else if (stringValue != null) + { + WriteTo(writer, stringValue); + } + else + { + WriteTo(writer, value.Value.Item1); + } + } + WriteLiteralTo(writer, trailer.Item1); } /// @@ -430,7 +163,7 @@ namespace Microsoft.AspNet.Diagnostics.Views /// protected void Write(object value) { - WriteEncoded(Convert.ToString(value, CultureInfo.InvariantCulture)); + WriteTo(Output, value); } /// @@ -439,7 +172,74 @@ namespace Microsoft.AspNet.Diagnostics.Views /// protected void Write(string value) { - WriteEncoded(value); + WriteTo(Output, value); + } + + /// + /// is invoked + /// + /// The to invoke + protected void Write(HelperResult result) + { + WriteTo(Output, result); + } + + /// + /// Writes the specified to . + /// + /// The instance to write to. + /// The to write. + /// + /// is invoked for types. + /// For all other types, the encoded result of is written to the + /// . + /// + protected void WriteTo(TextWriter writer, object value) + { + if (value != null) + { + var helperResult = value as HelperResult; + if (helperResult != null) + { + helperResult.WriteTo(writer); + } + else + { + WriteTo(writer, Convert.ToString(value, CultureInfo.InvariantCulture)); + } + } + } + + /// + /// Writes the specified with HTML encoding to . + /// + /// The instance to write to. + /// The to write. + protected void WriteTo(TextWriter writer, string value) + { + WriteLiteralTo(writer, WebUtility.HtmlEncode(value)); + } + + /// + /// Writes the specified without HTML encoding to the . + /// + /// The instance to write to. + /// The to write. + protected void WriteLiteralTo(TextWriter writer, object value) + { + WriteLiteralTo(writer, Convert.ToString(value, CultureInfo.InvariantCulture)); + } + + /// + /// Writes the specified without HTML encoding to . + /// + /// The to write. + protected void WriteLiteralTo(TextWriter writer, string value) + { + if (!string.IsNullOrEmpty(value)) + { + writer.Write(value); + } } } } diff --git a/src/Microsoft.AspNet.Diagnostics/Views/HelperResult.cs b/src/Microsoft.AspNet.Diagnostics/Views/HelperResult.cs new file mode 100644 index 0000000000..4c41db50eb --- /dev/null +++ b/src/Microsoft.AspNet.Diagnostics/Views/HelperResult.cs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; + +namespace Microsoft.AspNet.Diagnostics.Views +{ + /// + /// Represents a deferred write operation in a . + /// + public class HelperResult + { + /// + /// Creates a new instance of . + /// + /// The delegate to invoke when is called. + public HelperResult(Action action) + { + WriteAction = action; + } + + public Action WriteAction { get; } + + /// + /// Method invoked to produce content from the . + /// + /// The instance to write to. + public void WriteTo(TextWriter writer) + { + WriteAction(writer); + } + } +} \ No newline at end of file diff --git a/src/PageGenerator/Program.cs b/src/PageGenerator/Program.cs index a4f84c784f..10fc0db9ee 100644 --- a/src/PageGenerator/Program.cs +++ b/src/PageGenerator/Program.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.AspNet.Razor; +using Microsoft.AspNet.Razor.Generator; using Microsoft.Framework.Runtime; namespace PageGenerator @@ -76,6 +77,15 @@ namespace PageGenerator var codeLang = new CSharpRazorCodeLanguage(); var host = new RazorEngineHost(codeLang); host.DefaultBaseClass = "Microsoft.AspNet.Diagnostics.Views.BaseView"; + host.GeneratedClassContext = new GeneratedClassContext( + executeMethodName: GeneratedClassContext.DefaultExecuteMethodName, + writeMethodName: GeneratedClassContext.DefaultWriteMethodName, + writeLiteralMethodName: GeneratedClassContext.DefaultWriteLiteralMethodName, + writeToMethodName: "WriteTo", + writeLiteralToMethodName: "WriteLiteralTo", + templateTypeName: "HelperResult", + defineSectionMethodName: "DefineSection", + generatedTagHelperContext: new GeneratedTagHelperContext()); var engine = new RazorTemplateEngine(host); using (var fileStream = File.OpenText(cshtmlFilePath))