Пример #1
0
        /// <summary>
        /// Resolve a document Id (i.e. "T:System.Object") to an URL.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="linkName">Name of the link.</param>
        /// <param name="forceLocal">if set to <c>true</c> [force local].</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="localReference">The local reference.</param>
        /// <param name="useSelf"></param>
        /// <returns></returns>
        public string ToUrl(string id, string linkName = null, bool forceLocal = false, string attributes = null, IModelReference localReference = null, bool useSelf = true)
        {
            if (string.IsNullOrWhiteSpace(id))
                return "#";

            id = id.Trim();
            if (id.StartsWith("!:"))
                id = id.Substring(2);

            if (id.Length == 0 || id == "#")
                return linkName ?? id;

            var linkDescriptor = new LinkDescriptor { Type = LinkType.None, Index = -1 };
            var typeReference = localReference as INTypeReference;
            INTypeReference genericInstance = null;
            bool isGenericInstance = typeReference != null && typeReference.IsGenericInstance;
            bool isGenericParameter = typeReference != null && typeReference.IsGenericParameter;

            if (isGenericInstance)
            {
                var elementType = typeReference.ElementType;
                id = elementType.Id;
                genericInstance = typeReference;
                linkName = elementType.Name.Substring(0, elementType.Name.IndexOf('<'));
            }

            linkDescriptor.LocalReference = FindLocalReference(id);

            if (isGenericParameter)
            {
                linkDescriptor.Name = typeReference.Name;
            } else if (linkDescriptor.LocalReference != null)
            {
                // For local references, use short name
                var method = linkDescriptor.LocalReference as NMethod;
                if (method != null)
                    linkDescriptor.Name = method.Signature;
                else
                    linkDescriptor.Name = linkDescriptor.LocalReference.Name;

                linkDescriptor.PageId = linkDescriptor.LocalReference.PageId;
                linkDescriptor.Type = LinkType.Local;
                linkDescriptor.Index = linkDescriptor.LocalReference.Index;
                if (!forceLocal && CurrentContext != null && linkDescriptor.LocalReference is NMember)
                {
                    var declaringType = ((NMember) linkDescriptor.LocalReference).DeclaringType;
                    // If link is self referencing the current context, then use a self link
                    if ((id == CurrentContext.Id || (declaringType != null && declaringType.Id == CurrentContext.Id && (!id.StartsWith("T:") && !declaringType.Id.StartsWith("T:")))) && useSelf)
                    {
                        linkDescriptor.Type = LinkType.Self;
                    }
                }

            } else 
            {
                linkDescriptor.Type = LinkType.External;
                if (id.StartsWith("http:") || id.StartsWith("https:"))
                {
                    linkDescriptor.Location = id;
                }
                else
                {
                    // Try to resolve to MSDN
                    linkDescriptor.Location = _msnRegistry.FindUrl(id);
                    var reference = TextReferenceUtility.CreateReference(id);
                    if (linkDescriptor.Location != null)
                    {
                        linkDescriptor.Name = reference.FullName;
                        // Open MSDN documentation to a new window
                        attributes = (attributes ?? "") + " target='_blank'";
                    }
                    else
                    {
                        linkDescriptor.Name = reference.Name;
                    }
                    
                }
            }

            if (LinkResolver == null)
            {
                Logger.Warning("Model.LinkResolver must be set");
                return id;
            }

            if (linkName != null)
                linkDescriptor.Name = linkName;

            linkDescriptor.Id = id;
            linkDescriptor.Attributes = attributes;

            var urlBuilder = new StringBuilder();
            urlBuilder.Append(LinkResolver(linkDescriptor));

            // Handle URL for generic instance
            if (genericInstance != null)
            {
                urlBuilder.Append("&lt;");
                for (int i = 0; i < genericInstance.GenericArguments.Count; i++)
                {
                    if (i > 0)
                        urlBuilder.Append(", ");
                    var genericArgument = genericInstance.GenericArguments[i];
                    // Recursive call here
                    urlBuilder.Append(ToUrl(genericArgument));
                }
                urlBuilder.Append("&gt;");
            }
            return urlBuilder.ToString();
        }
Пример #2
0
        /// <summary>
        /// Resolve a document Id (i.e. "T:System.Object") to an URL.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="linkName">Name of the link.</param>
        /// <param name="forceLocal">if set to <c>true</c> [force local].</param>
        /// <param name="attributes">The attributes.</param>
        /// <param name="localReference">The local reference.</param>
        /// <param name="useSelf"></param>
        /// <returns></returns>
        public string ToUrl(string id, string linkName = null, bool forceLocal = false, string attributes = null, IModelReference localReference = null, bool useSelf = true)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return("#");
            }

            id = id.Trim();
            if (id.StartsWith("!:"))
            {
                id = id.Substring(2);
            }

            if (id.Length == 0 || id == "#")
            {
                return(linkName ?? id);
            }

            var linkDescriptor = new LinkDescriptor {
                Type = LinkType.None, Index = -1
            };
            var             typeReference      = localReference as INTypeReference;
            INTypeReference genericInstance    = null;
            bool            isGenericInstance  = typeReference != null && typeReference.IsGenericInstance;
            bool            isGenericParameter = typeReference != null && typeReference.IsGenericParameter;

            if (isGenericInstance)
            {
                var elementType = typeReference.ElementType;
                id = elementType.Id;
                genericInstance = typeReference;
                linkName        = elementType.Name.Substring(0, elementType.Name.IndexOf('<'));
            }

            linkDescriptor.LocalReference = FindLocalReference(id);

            if (isGenericParameter)
            {
                linkDescriptor.Name = typeReference.Name;
            }
            else if (linkDescriptor.LocalReference != null)
            {
                // For local references, use short name
                var method = linkDescriptor.LocalReference as NMethod;
                if (method != null)
                {
                    linkDescriptor.Name = method.Signature;
                }
                else
                {
                    linkDescriptor.Name = linkDescriptor.LocalReference.Name;
                }

                linkDescriptor.PageId = linkDescriptor.LocalReference.PageId;
                linkDescriptor.Type   = LinkType.Local;
                linkDescriptor.Index  = linkDescriptor.LocalReference.Index;
                if (!forceLocal && CurrentContext != null && linkDescriptor.LocalReference is NMember)
                {
                    var declaringType = ((NMember)linkDescriptor.LocalReference).DeclaringType;
                    // If link is self referencing the current context, then use a self link
                    if ((id == CurrentContext.Id || (declaringType != null && declaringType.Id == CurrentContext.Id && (!id.StartsWith("T:") && !declaringType.Id.StartsWith("T:")))) && useSelf)
                    {
                        linkDescriptor.Type = LinkType.Self;
                    }
                }
            }
            else
            {
                linkDescriptor.Type = LinkType.External;
                if (id.StartsWith("http:") || id.StartsWith("https:"))
                {
                    linkDescriptor.Location = id;
                }
                else
                {
                    // Try to resolve to MSDN
                    linkDescriptor.Location = _msnRegistry.FindUrl(id);
                    var reference = TextReferenceUtility.CreateReference(id);
                    if (linkDescriptor.Location != null)
                    {
                        linkDescriptor.Name = reference.FullName;
                        // Open MSDN documentation to a new window
                        attributes = (attributes ?? "") + " target='_blank'";
                    }
                    else
                    {
                        linkDescriptor.Name = reference.Name;
                    }
                }
            }

            if (LinkResolver == null)
            {
                Logger.Warning("Model.LinkResolver must be set");
                return(id);
            }

            if (linkName != null)
            {
                linkDescriptor.Name = linkName;
            }

            linkDescriptor.Id         = id;
            linkDescriptor.Attributes = attributes;

            var urlBuilder = new StringBuilder();

            urlBuilder.Append(LinkResolver(linkDescriptor));

            // Handle URL for generic instance
            if (genericInstance != null)
            {
                urlBuilder.Append("&lt;");
                for (int i = 0; i < genericInstance.GenericArguments.Count; i++)
                {
                    if (i > 0)
                    {
                        urlBuilder.Append(", ");
                    }
                    var genericArgument = genericInstance.GenericArguments[i];
                    // Recursive call here
                    urlBuilder.Append(ToUrl(genericArgument));
                }
                urlBuilder.Append("&gt;");
            }
            return(urlBuilder.ToString());
        }