Beispiel #1
0
            protected override object HandleDictionaryType(object currentObj, HandleModelAttributesContext context)
            {
                Func <object, object> handler = s => Handler.Handle(s, context);

                HandleGenericItemsHelper.HandleIDictionary(currentObj, s => Handler.Handle(s, context));
                return(currentObj);
            }
Beispiel #2
0
            private string GetHrefFromRoot(string originalHref, HandleModelAttributesContext context)
            {
                if (context.FileAndType == null || string.IsNullOrEmpty(originalHref) || !RelativePath.IsRelativePath(originalHref))
                {
                    return(originalHref);
                }

                var result = originalHref;
                var ft     = context.FileAndType;
                var path   = (RelativePath)ft.File + (RelativePath)UriUtility.GetPath(originalHref);
                var file   = path.GetPathFromWorkingFolder().UrlDecode();

                if (context.Host.SourceFiles.ContainsKey(file))
                {
                    result = file.UrlEncode().ToString() + UriUtility.GetQueryStringAndFragment(originalHref);
                }

                List <LinkSourceInfo> sources;

                if (!context.FileLinkSources.TryGetValue(file, out sources))
                {
                    sources = new List <LinkSourceInfo>();
                    context.FileLinkSources[file] = sources;
                }
                sources.Add(new LinkSourceInfo
                {
                    Target     = file,
                    Anchor     = UriUtility.GetFragment(originalHref),
                    SourceFile = ft.File,
                });
                context.LinkToFiles.Add(file);

                return(result);
            }
        protected virtual void BuildArticleCore(IHostService host, FileModel model, IModelAttributeHandler handlers = null, HandleModelAttributesContext handlerContext = null, bool shouldSkipMarkup = false)
        {
            if (handlers == null)
            {
                handlers = _defaultHandler;
            }
            if (handlerContext == null)
            {
                handlerContext = new HandleModelAttributesContext
                {
                    EnableContentPlaceholder = false,
                    Host        = host,
                    FileAndType = model.OriginalFileAndType,
                    SkipMarkup  = shouldSkipMarkup,
                };
            }

            handlers.Handle(model.Content, handlerContext);

            model.LinkToUids      = model.LinkToUids.Union(handlerContext.LinkToUids);
            model.LinkToFiles     = model.LinkToFiles.Union(handlerContext.LinkToFiles);
            model.FileLinkSources = model.FileLinkSources.ToDictionary(v => v.Key, v => v.Value.ToList())
                                    .Merge(handlerContext.FileLinkSources.Select(i => new KeyValuePair <string, IEnumerable <LinkSourceInfo> >(i.Key, i.Value)))
                                    .ToImmutableDictionary(v => v.Key, v => v.Value.ToImmutableList());
            model.UidLinkSources = model.UidLinkSources.ToDictionary(v => v.Key, v => v.Value.ToList())
                                   .Merge(handlerContext.UidLinkSources.Select(i => new KeyValuePair <string, IEnumerable <LinkSourceInfo> >(i.Key, i.Value)))
                                   .ToImmutableDictionary(v => v.Key, v => v.Value.ToImmutableList());
        }
Beispiel #4
0
        public object Handle(object obj, HandleModelAttributesContext context)
        {
            if (obj == null)
            {
                return(null);
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Host == null)
            {
                throw new ArgumentNullException(nameof(context.Host));
            }

            if (context.SkipMarkup)
            {
                return(obj);
            }

            var type = obj.GetType();

            return(_cache.GetOrAdd(type, t => new MarkdownContentHandlerImpl(t, this)).Handle(obj, context));
        }
        protected IEnumerable <T> Transform <T>(FileModel model, string uid, IHostService host) where T : class, IOverwriteDocumentViewModel
        {
            var overwrites = ((List <OverwriteDocumentModel>)model.Content).Where(s => s.Uid == uid);

            return(overwrites.Select(s =>
            {
                try
                {
                    var placeholderContent = s.Conceptual;
                    s.Conceptual = null;
                    var item = s.ConvertTo <T>();
                    var context = new HandleModelAttributesContext
                    {
                        EnableContentPlaceholder = true,
                        Host = host,
                        PlaceholderContent = placeholderContent,
                        FileAndType = model.OriginalFileAndType,
                    };

                    _handler.Handle(item, context);
                    if (!context.ContainsPlaceholder)
                    {
                        item.Conceptual = placeholderContent;
                    }
                    return item;
                }
                catch (YamlException ye)
                {
                    throw new DocumentException($"Unable to deserialize YAML header from \"{s.Documentation.Path}\" Line {s.Documentation.StartLine} to TYPE {typeof(T).Name}: {ye.Message}", ye);
                }
            }));
        }
Beispiel #6
0
        public object Handle(object obj, HandleModelAttributesContext context)
        {
            var type = obj.GetType();

            if (type != _type)
            {
                throw new InvalidOperationException($"Input type {type} is not the supported type {_type}");
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (obj == null)
            {
                return(null);
            }

            object result;

            if (ShouldHandle(obj, null, null, context))
            {
                result = HandleCurrent(obj, null, null, context);
            }
            else
            {
                if (context.NestedLevel > MaximumNestedLevel)
                {
                    // If maximum nested level reached, return the object directly
                    return(obj);
                }

                context.NestedLevel++;

                if (_typeInfo.TypeOfType == TypeOfType.IDictionary)
                {
                    result = HandleDictionaryType(obj, context);
                }
                else if (_typeInfo.TypeOfType == TypeOfType.IEnumerable)
                {
                    result = HandleIEnumerableType(obj, context);
                }
                else if (_typeInfo.TypeOfType == TypeOfType.Primitive)
                {
                    result = ProcessPrimitiveType(obj, context);
                }
                else
                {
                    result = ProcessNonPrimitiveType(obj, context);
                }

                context.NestedLevel--;
            }

            return(result);
        }
        public object Handle(object obj, HandleModelAttributesContext context)
        {
            if (obj == null)
            {
                return(null);
            }
            var type = obj.GetType();

            return(_cache.GetOrAdd(type, t => new UniqueIdentityHandlerImpl(t, this)).Handle(obj, context));
        }
Beispiel #8
0
            private static string MarkupCore(string content, HandleModelAttributesContext context)
            {
                var host = context.Host;
                var mr   = host.Markup(content, context.FileAndType);

                context.LinkToUids.UnionWith(mr.LinkToUids);
                context.LinkToFiles.UnionWith(mr.LinkToFiles);
                context.FileLinkSources = context.FileLinkSources.Merge(mr.FileLinkSources.Select(s => new KeyValuePair <string, IEnumerable <LinkSourceInfo> >(s.Key, s.Value)));
                context.UidLinkSources  = context.UidLinkSources.Merge(mr.UidLinkSources.Select(s => new KeyValuePair <string, IEnumerable <LinkSourceInfo> >(s.Key, s.Value)));
                return(mr.Html);
            }
Beispiel #9
0
        /// <summary>
        /// By default enumerate Enumerable type if it does not have defined Attribute
        /// </summary>
        /// <param name="currentObj"></param>
        /// <param name="context"></param>
        protected virtual object HandleIEnumerableType(object currentObj, HandleModelAttributesContext context)
        {
            if (currentObj == null)
            {
                return(null);
            }
            Func <object, object> handler = s => Handler.Handle(s, context);

            HandleGenericItemsHelper.EnumerateIEnumerable(currentObj, s => Handler.Handle(s, context));
            return(currentObj);
        }
Beispiel #10
0
            private bool TryMarkupPlaceholderContent(string currentValue, HandleModelAttributesContext context, out string result)
            {
                result = null;
                if (context.EnableContentPlaceholder && IsPlaceholderContent(currentValue))
                {
                    context.ContainsPlaceholder = true;
                    result = context.PlaceholderContent;
                    return(true);
                }

                return(false);
            }
Beispiel #11
0
            private string Markup(string content, HandleModelAttributesContext context)
            {
                if (string.IsNullOrEmpty(content))
                {
                    return(content);
                }

                if (TryMarkupPlaceholderContent(content, context, out string result))
                {
                    return(result);
                }

                return(MarkupCore(content, context));
            }
Beispiel #12
0
        /// <summary>
        /// By default enumerate Dictionary's value if it does not have defined Attribute
        /// </summary>
        /// <param name="declaringObject"></param>
        /// <param name="currentPropertyInfo"></param>
        /// <param name="context"></param>
        protected virtual object HandleDictionaryType(object currentObj, HandleModelAttributesContext context)
        {
            if (currentObj == null)
            {
                return(null);
            }
            Func <object, object> handler = s => Handler.Handle(s, context);

            if (!HandleGenericItemsHelper.EnumerateIDictionary(currentObj, handler))
            {
                HandleGenericItemsHelper.EnumerateIReadonlyDictionary(currentObj, handler);
            }
            return(currentObj);
        }
Beispiel #13
0
 /// <summary>
 /// By default step into NonPrimitive type if it does not have defined Attribute
 /// </summary>
 /// <param name="currentObj"></param>
 /// <param name="context"></param>
 protected virtual object ProcessNonPrimitiveType(object currentObj, HandleModelAttributesContext context)
 {
     // skip string type
     if (currentObj != null && !(currentObj is string))
     {
         foreach (var prop in _typeInfo.PropInfos)
         {
             var value = ReflectionHelper.GetPropertyValue(currentObj, prop.Prop);
             if (ShouldHandle(value, currentObj, prop, context))
             {
                 HandleCurrent(value, currentObj, prop.Prop, context);
             }
             else
             {
                 Handler.Handle(value, context);
             }
         }
     }
     return(currentObj);
 }
Beispiel #14
0
            protected override object HandleCurrent(object currentObj, object declaringObject, PropertyInfo currentPropertyInfo, HandleModelAttributesContext context)
            {
                if (currentObj == null && currentPropertyInfo != null && declaringObject != null)
                {
                    currentObj = currentPropertyInfo.GetValue(declaringObject);
                }

                if (currentObj == null)
                {
                    return(null);
                }

                if (currentObj is string val)
                {
                    var updated = GetHrefFromRoot(val, context);
                    if (currentPropertyInfo != null)
                    {
                        ReflectionHelper.SetPropertyValue(declaringObject, currentPropertyInfo, updated);
                    }
                    return(updated);
                }

                if (currentObj is IList <string> list)
                {
                    for (var i = 0; i < list.Count; i++)
                    {
                        var item = list[i];
                        if (item != null)
                        {
                            list[i] = GetHrefFromRoot(item, context);
                        }
                    }
                    return(list);
                }

                throw new NotSupportedException($"Type {currentObj.GetType()} is NOT a supported type for {nameof(UrlContentAttribute)}");
            }
Beispiel #15
0
 protected virtual bool ShouldHandle(object currentObj, object declaringObject, PropInfo currentPropInfo, HandleModelAttributesContext context)
 {
     return(currentPropInfo != null && currentPropInfo.Attr != null);
 }
Beispiel #16
0
 protected override object HandleIEnumerableType(object currentObj, HandleModelAttributesContext context)
 {
     HandleGenericItemsHelper.HandleIList(currentObj, s => Handler.Handle(s, context));
     return(currentObj);
 }
Beispiel #17
0
 protected abstract object HandleCurrent(object currentObj, object declaringObject, PropertyInfo currentPropertyInfo, HandleModelAttributesContext context);
Beispiel #18
0
 /// <summary>
 /// By default skip Primitive type if it does not have defined Attribute
 /// </summary>
 /// <param name="currentObj"></param>
 /// <param name="context"></param>
 protected virtual object ProcessPrimitiveType(object currentObj, HandleModelAttributesContext context)
 {
     return(currentObj);
 }
            protected override object HandleCurrent(object currentObj, object declaringObject, PropertyInfo currentPropertyInfo, HandleModelAttributesContext context)
            {
                if (currentObj == null && currentPropertyInfo != null && declaringObject != null)
                {
                    currentObj = currentPropertyInfo.GetValue(declaringObject);
                }

                if (currentObj == null)
                {
                    return(null);
                }

                if (currentObj is string val)
                {
                    context.LinkToUids.Add(val);
                }
                else
                {
                    if (currentObj is IEnumerable list)
                    {
                        foreach (var i in list)
                        {
                            if (i != null)
                            {
                                if (i is string item)
                                {
                                    context.LinkToUids.Add(item);
                                }
                                else
                                {
                                    throw new NotSupportedException($"Type {currentObj.GetType()} inside IEnumerable is NOT a supported item type for {nameof(UniqueIdentityReferenceAttribute)}");
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new NotSupportedException($"Type {currentObj.GetType()} is NOT a supported type for {nameof(UniqueIdentityReferenceAttribute)}");
                    }
                }

                return(currentObj);
            }
Beispiel #20
0
            protected override object HandleCurrent(object currentObj, object declaringObject, PropertyInfo currentPropertyInfo, HandleModelAttributesContext context)
            {
                if (currentObj == null && currentPropertyInfo != null && declaringObject != null)
                {
                    currentObj = currentPropertyInfo.GetValue(declaringObject);
                }

                if (currentObj == null)
                {
                    return(null);
                }

                var val = currentObj as string;

                if (val != null)
                {
                    var marked = Markup(val, context);
                    if (currentPropertyInfo != null)
                    {
                        ReflectionHelper.SetPropertyValue(declaringObject, currentPropertyInfo, marked);
                    }
                    return(marked);
                }

                var list = currentObj as IList <string>;

                if (list != null)
                {
                    for (var i = 0; i < list.Count; i++)
                    {
                        var item = list[i];
                        if (item != null)
                        {
                            list[i] = Markup(item, context);
                        }
                    }
                    return(list);
                }

                throw new NotSupportedException($"Type {currentObj.GetType()} is NOT a supported type for {nameof(MarkdownContentAttribute)}");
            }
Beispiel #21
0
 protected override bool ShouldHandle(object currentObj, object declaringObject, PropInfo currentPropInfo, HandleModelAttributesContext context)
 {
     if (context.EnableContentPlaceholder)
     {
         var str = currentObj as string;
         if (IsPlaceholderContent(str))
         {
             return(true);
         }
     }
     return(base.ShouldHandle(currentObj, declaringObject, currentPropInfo, context));
 }