public void A_transform_can_be_used_to_add_attributes() { dynamic _ = new PocketView(); _.div = PocketView.Transform( (tag, model) => tag.HtmlAttributes.Class("foo")); string output = _.div("hi").ToString(); output.Should().Be("<div class=\"foo\">hi</div>"); }
public void A_transform_merges_differently_named_attributes() { dynamic _ = new PocketView(); _.div = PocketView.Transform( (tag, model) => tag.HtmlAttributes.Class("foo")); string output = _.div[style : "color:red"]("hi").ToString(); output .Should().Be("<div class=\"foo\" style=\"color:red\">hi</div>"); }
public void A_transform_overwrites_like_named_attributes() { dynamic _ = new PocketView(); _.div = PocketView.Transform( (tag, model) => tag.HtmlAttributes["style"] = "color:yellow"); string output = _.div[style : "color:red"]("hi").ToString(); output .Should() .Be("<div style=\"color:yellow\">hi</div>"); }
public Task OnLoadAsync(Kernel kernel) { Formatter.Register <DateTime>((date, writer) => { writer.Write(date.DrawSvgClock()); }, "text/html"); Formatter.Register <DateTimeOffset>((date, writer) => { writer.Write(date.DrawSvgClock()); }, "text/html"); var clockCommand = new Command("#!clock", "Displays a clock showing the current or specified time.") { new Option <int>(new[] { "-o", "--hour" }, "The position of the hour hand"), new Option <int>(new[] { "-m", "--minute" }, "The position of the minute hand"), new Option <int>(new[] { "-s", "--second" }, "The position of the second hand") }; clockCommand.Handler = CommandHandler.Create( (int hour, int minute, int second, KernelInvocationContext invocationContext) => { invocationContext.Display(SvgClock.DrawSvgClock(hour, minute, second)); }); kernel.AddDirective(clockCommand); if (KernelInvocationContext.Current is { } context) { PocketView view = div( code(nameof(ClockExtension)), " is loaded. It adds visualizations for ", code(typeof(System.DateTime)), " and ", code(typeof(System.DateTimeOffset)), ". Try it by running: ", code("DateTime.Now"), " or ", code("#!clock -h") ); context.Display(view); } return(Task.CompletedTask); }
public Task OnLoadAsync(Kernel kernel) { Formatter.Register <Sh1106Display>((d, w) => { using (MemoryStream s = new MemoryStream()) { d.Capture().SaveAsPng(s); PocketView view = img[src: @"data:image/png;base64, " + System.Convert.ToBase64String(s.ToArray())]; w.Write(view); } }, "text/html"); return(Task.CompletedTask); }
private static void UseSolidColorBrushFormatter() { Formatter <SolidColorBrush> .Register((brush, writer) => { var color = brush.Color; string stringValue = $"#{color.R:X2}{color.G:X2}{color.B:X2}{color.A:X2}"; PocketView colorDiv = div( div[style: $"border:2px solid #FFFFFF;background-color:{stringValue};width:15px;height:15px"](), div(b(stringValue)) ); writer.Write(colorDiv); }, "text/html"); }
public void A_transform_can_be_used_to_create_an_alias() { _.foolink = PocketView.Transform( (tag, model) => { tag.Name = "a"; tag.HtmlAttributes.Add("href", "http://foo.biz"); }); string output = _.foolink("click here!").ToString(); output .Should() .Be("<a href=\"http://foo.biz\">click here!</a>"); }
public static T UseAboutMagicCommand <T>(this T kernel) where T : Kernel { var about = new Command("#!about", "Show version and build information") { Handler = CommandHandler.Create((InvocationContext ctx) => { ctx.GetService <KernelInvocationContext>().Display(VersionSensor.Version()); return(Task.CompletedTask); }) }; kernel.AddDirective(about); Formatter.Register <VersionSensor.BuildInfo>((info, writer) => { var url = "https://github.com/dotnet/interactive"; var encodedImage = string.Empty; var assembly = typeof(KernelExtensions).Assembly; using (var resourceStream = assembly.GetManifestResourceStream($"{typeof(KernelExtensions).Namespace}.resources.logo-456x456.png")) { if (resourceStream is not null) { var png = new byte[resourceStream.Length]; resourceStream.Read(png, 0, png.Length); encodedImage = $"data:image/png;base64, {Convert.ToBase64String(png)}"; } } PocketView html = table( tbody( tr( td(img[src: encodedImage, width: "125em"]), td[style: "line-height:.8em"]( p[style: "font-size:1.5em"](b(".NET Interactive")), p("© 2020 Microsoft Corporation"), p(b("Version: "), info.AssemblyInformationalVersion), p(b("Build date: "), info.BuildDate), p(a[href: url](url)) )) )); writer.Write(html); }, HtmlFormatter.MimeType); return(kernel); }
private PocketView FormatAsHtml() { var outcomeDivStyle = Outcome switch { Outcome.Success => "background:#49B83461; padding:.75em; border-color:#49B83461", Outcome.PartialSuccess => "background:#FF00008A; padding:.75em; border-color:#FF00008A", Outcome.Failure => "background:#FF00008A; padding:.75em; border-color:#FF00008A", _ => throw new NotImplementedException() }; var outcomeMessage = Outcome switch { Outcome.Success => "Success", Outcome.PartialSuccess => "Partial Success", Outcome.Failure => "Failure", _ => throw new NotImplementedException() }; var elements = new List <PocketView>(); if (string.IsNullOrWhiteSpace(Name)) { PocketView header = summary[style : outcomeDivStyle](b(outcomeMessage)); elements.Add(header); } else { PocketView header = summary[style : outcomeDivStyle]($"[ {Name} ]: ", b(outcomeMessage)); elements.Add(header); } if (!string.IsNullOrWhiteSpace(Reason)) { elements.Add(div(Reason)); } if (Hint is not null) { var hintElement = div[@class : "hint"](b("Hint: "), Hint); elements.Add(hintElement); } PocketView report = details[@class : "ruleEvaluation", style : "padding:.5em"](elements); return(report); }
public static void Javascript(string scriptContent) { PocketView value = script[type : "text/javascript"]( HTML( scriptContent)); var formatted = new FormattedValue( HtmlFormatter.MimeType, value.ToString()); var kernel = KernelInvocationContext.Current.HandlingKernel; Task.Run(() => kernel.SendAsync(new DisplayValue(formatted))) .Wait(); }
public void A_transform_can_be_used_to_create_an_expandable_tag() { _.textbox = PocketView.Transform( (tag, model) => { tag.Name = "div"; tag.Content = w => { w.Write(label[@for: model.name](model.name)); w.Write(input[value: model.value, type: "text", name: model.name]); }; }); string output = _.textbox(name: "FirstName", value: "Bob").ToString(); output .Should().Be("<div><label for=\"FirstName\">FirstName</label><input name=\"FirstName\" type=\"text\" value=\"Bob\" /></div>"); }
public static T UseAbout <T>(this T kernel) where T : KernelBase { var about = new Command("#!about") { Handler = CommandHandler.Create <KernelInvocationContext>( async context => await context.DisplayAsync(VersionSensor.Version())) }; kernel.AddDirective(about); Formatter <VersionSensor.BuildInfo> .Register((info, writer) => { // https://github.com/dotnet/swag/tree/master/netlogo var url = "https://github.com/dotnet/interactive"; PocketView html = table( tbody( tr( td( img[ src: "https://raw.githubusercontent.com/dotnet/swag/master/netlogo/small-200x198/pngs/msft.net_small_purple.png", width: "125em"]), td[style: "line-height:.8em"]( p[style: "font-size:1.5em"](b(".NET Interactive")), p("© 2020 Microsoft Corporation"), p(b("Version: "), info.AssemblyInformationalVersion), p(b("Build date: "), info.BuildDate), p(a[href: url](url)) )) )); writer.Write(html); }, HtmlFormatter.MimeType); return(kernel); }
public void Formatter_does_not_expand_properties_of_PocketView(string mimeType) { PocketView view = b(123); view.ToDisplayString(mimeType).Should().Be("<b>123</b>"); }
public void Formatter_does_not_expand_properties_of_PocketView(string mimeType) { PocketView view = b(123); view.ToDisplayString(mimeType).Should().Be($"<b>{PlainTextBegin}123{PlainTextEnd}</b>"); }
private static void SetupRecordFormatters() { // DebugRecord Formatter <DebugRecord> .Register((record, writer) => { PocketView view = pre($"DEBUG: {record.Message}"); writer.WriteLine(view.ToDisplayString(HtmlFormatter.MimeType)); }, HtmlFormatter.MimeType); Formatter <DebugRecord> .Register((record, writer) => { writer.WriteLine($"DEBUG: {record.Message}"); }, PlainTextFormatter.MimeType); // ErrorRecord Formatter <ErrorRecord> .Register((record, writer) => { string result = null; lock (_pwshLock) { var errorDetails = _pwsh.AddCommand("Microsoft.PowerShell.Utility\\Out-String") .AddParameter("InputObject", record) .InvokeAndClearCommands <string>(); result = errorDetails.Single(); } if (result != null) { PocketView view = pre(result.Trim()); writer.WriteLine(view.ToDisplayString(HtmlFormatter.MimeType)); } }, HtmlFormatter.MimeType); Formatter <ErrorRecord> .Register((record, writer) => { string result = null; lock (_pwshLock) { var errorDetails = _pwsh.AddCommand("Microsoft.PowerShell.Utility\\Out-String") .AddParameter("InputObject", record) .InvokeAndClearCommands <string>(); result = errorDetails.Single(); } if (result != null) { writer.WriteLine(result.Trim()); } }, PlainTextFormatter.MimeType); // InformationRecord Formatter <InformationRecord> .Register((record, writer) => { string prefix = (record.Tags.Count == 1 && (record.Tags[0] == "__PipelineObject__" || record.Tags[0] == "PSHOST")) ? "" : "INFORMATION: "; PocketView view = pre($"{prefix}{record.MessageData}"); writer.WriteLine(view.ToDisplayString(HtmlFormatter.MimeType)); }, HtmlFormatter.MimeType); Formatter <InformationRecord> .Register((record, writer) => { string prefix = (record.Tags.Count == 1 && (record.Tags[0] == "__PipelineObject__" || record.Tags[0] == "PSHOST")) ? "" : "INFORMATION: "; writer.WriteLine($"{prefix}{record.MessageData}"); }, PlainTextFormatter.MimeType); // ProgressRecord Formatter <ProgressRecord> .Register((record, writer) => { PocketView view = pre($"PROGRESS: {record.StatusDescription}"); writer.WriteLine(view.ToDisplayString(HtmlFormatter.MimeType)); }, HtmlFormatter.MimeType); Formatter <ProgressRecord> .Register((record, writer) => { writer.WriteLine($"PROGRESS: {record.StatusDescription}"); }, PlainTextFormatter.MimeType); // VerboseRecord Formatter <VerboseRecord> .Register((record, writer) => { PocketView view = pre($"VERBOSE: {record.Message}"); writer.WriteLine(view.ToDisplayString(HtmlFormatter.MimeType)); }, HtmlFormatter.MimeType); Formatter <VerboseRecord> .Register((record, writer) => { writer.WriteLine($"VERBOSE: {record.Message}"); }, PlainTextFormatter.MimeType); // WarningRecord Formatter <WarningRecord> .Register((record, writer) => { PocketView view = pre($"WARNING: {record.Message}"); writer.WriteLine(view.ToDisplayString(HtmlFormatter.MimeType)); }, HtmlFormatter.MimeType); Formatter <WarningRecord> .Register((record, writer) => { writer.WriteLine($"WARNING: {record.Message}"); }, PlainTextFormatter.MimeType); }