Beispiel #1
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="PocketView" /> class.
 /// </summary>
 /// <param name="nested"> A nested instance. </param>
 public PocketView(PocketView nested = null)
 {
     if (nested is not null)
     {
         _transforms = nested._transforms;
     }
     else
     {
         AddDefaultTransforms();
     }
 }
        private static ConcurrentDictionary <Type, ITypeFormatter> DefaultFormatters() =>
        new ConcurrentDictionary <Type, ITypeFormatter>
        {
            [typeof(DateTime)] = new HtmlFormatter <DateTime>((value, writer) => writer.Write(value.ToString("u"))),

            [typeof(DateTimeOffset)] = new HtmlFormatter <DateTimeOffset>((value, writer) => writer.Write(value.ToString("u"))),

            [typeof(ExpandoObject)] = new HtmlFormatter <ExpandoObject>((obj, writer) =>
            {
                var headers = new List <IHtmlContent>();
                var values  = new List <IHtmlContent>();

                foreach (var pair in obj.OrderBy(p => p.Key))
                {
                    headers.Add(th(pair.Key));
                    values.Add(td(pair.Value));
                }

                IHtmlContent view = table(
                    thead(
                        tr(
                            headers)),
                    tbody(
                        tr(
                            values)));

                view.WriteTo(writer, HtmlEncoder.Default);
            }),

            [typeof(HtmlString)] = new HtmlFormatter <HtmlString>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)),

            [typeof(JsonString)] = new HtmlFormatter <JsonString>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)),

            [typeof(PocketView)] = new HtmlFormatter <PocketView>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)),

            [typeof(ReadOnlyMemory <char>)] = new HtmlFormatter <ReadOnlyMemory <char> >((memory, writer) =>
            {
                PocketView view = span(memory.Span.ToString());

                view.WriteTo(writer, HtmlEncoder.Default);
            }),

            [typeof(string)] = new HtmlFormatter <string>((s, writer) => writer.Write(span(s))),

            [typeof(TimeSpan)] = new HtmlFormatter <TimeSpan>((timespan, writer) =>
            {
                writer.Write(timespan.ToString());
            }),

            [typeof(Type)] = _formatterForSystemType,

            [typeof(Type).GetType()] = _formatterForSystemType,
        };
Beispiel #3
0
        internal static HtmlFormatter <T> CreateForAnyObject(bool includeInternals)
        {
            var members = typeof(T).GetMembersToFormat(includeInternals)
                          .GetMemberAccessors <T>();

            return(new HtmlFormatter <T>((instance, context) =>
            {
                // Note the order of members is declaration order
                var reducedMembers =
                    members
                    .Take(Math.Max(0, HtmlFormatter.MaxProperties))
                    .ToArray();

                if (reducedMembers.Length == 0)
                {
                    // This formatter refuses to format objects without members, and
                    // refused to produce nested tables, or if no members are selected
                    return false;
                }
                else
                {
                    // Note, embeds the keys and values as arbitrary objects into the HTML content,
                    List <IHtmlContent> headers =
                        reducedMembers.Select(m => (IHtmlContent)th(m.Member.Name))
                        .ToList();

                    // Add a '..' column if we elided some members due to size limitations
                    if (reducedMembers.Length < members.Length)
                    {
                        headers.Add(th(".."));
                    }

                    IEnumerable <object> values =
                        reducedMembers.Select(m => m.GetValueOrException(instance))
                        .Select(v => td(
                                    div[@class: "dni-plaintext"](v.ToDisplayString(PlainTextFormatter.MimeType))));

                    PocketView t =
                        table(
                            thead(
                                tr(
                                    headers)),
                            tbody(
                                tr(
                                    values)));

                    t.WriteTo(context);

                    return true;
                }
            }));
        }
Beispiel #4
0
        /// <summary>
        /// Writes an element.
        /// </summary>
        public override bool TryGetMember(
            GetMemberBinder binder,
            out object result)
        {
            var returnValue = new PocketView(tagName: binder.Name, nested: this);

            if (_transforms.TryGetValue(binder.Name, out var transform))
            {
                returnValue._transform = transform;
            }

            result = returnValue;
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Writes an element.
        /// </summary>
        public override bool TryInvokeMember(
            InvokeMemberBinder binder,
            object[] args,
            out object result)
        {
            var pocketView = new PocketView(tagName: binder.Name, nested: this);

            pocketView.SetContent(args);

            if (_transforms.TryGetValue(binder.Name, out var transform))
            {
                var content = ComposeContent(binder.CallInfo.ArgumentNames, args);

                transform(pocketView._tag, content);
            }

            result = pocketView;
            return(true);
        }
Beispiel #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PocketView"/> class.
 /// </summary>
 /// <param name="tagName">Name of the tag.</param>
 /// <param name="nested">A nested instance.</param>
 public PocketView(string tagName, PocketView nested = null) : this(nested)
 {
     _tag = tagName.Tag();
 }